From 7b2842aee2d01a175607919f92d86164509650b7 Mon Sep 17 00:00:00 2001 From: Robert Izzard <r.izzard@surrey.ac.uk> Date: Mon, 29 Nov 2021 09:36:33 +0000 Subject: [PATCH] clean up of code, add option to customize the newlines at the end of log lines --- binarycpython/utils/Moe_di_Stefano_2017.py | 1 + binarycpython/utils/dataIO.py | 18 ++++++++++++++---- binarycpython/utils/functions.py | 10 ++++++++-- binarycpython/utils/grid_logging.py | 3 ++- binarycpython/utils/grid_options_defaults.py | 8 ++++++-- 5 files changed, 31 insertions(+), 9 deletions(-) diff --git a/binarycpython/utils/Moe_di_Stefano_2017.py b/binarycpython/utils/Moe_di_Stefano_2017.py index 0cfb63604..31de51a4e 100644 --- a/binarycpython/utils/Moe_di_Stefano_2017.py +++ b/binarycpython/utils/Moe_di_Stefano_2017.py @@ -813,6 +813,7 @@ eccentricity3=0 "clean load flag": False, "clean all": False, } + if not options: options = {} options = update_dicts(default_options, options) diff --git a/binarycpython/utils/dataIO.py b/binarycpython/utils/dataIO.py index f6e8990bc..9f62f748c 100644 --- a/binarycpython/utils/dataIO.py +++ b/binarycpython/utils/dataIO.py @@ -672,16 +672,19 @@ class dataIO(): else: return None - def open(self,file, mode='r', buffering=- 1, encoding=None, errors=None, newline=None, closefd=True, opener=None, compression=None, compresslevel=None): + def open(self,file, mode='r', buffering=- 1, encoding=None, errors=None, newline=None, closefd=True, opener=None, compression=None, compresslevel=None, vb=False): """ Wrapper for open() with automatic compression based on the file extension. """ if compression is None: compression = self.compression_type(file) - print("open() with mode = {mode}, compression {compression}".format( - compression=compression, - mode=mode)) + if vb: + print("open() file at \"{file}\" with mode = {mode}, compression {compression}, compresslevel {compresslevel}".format( + file=file, + compression=compression, + compresslevel=compresslevel, + mode=mode)) if compression: if compresslevel is None: compresslevel = 9 @@ -690,10 +693,14 @@ class dataIO(): # defaults to binary, which isn't compatible with JSON, # so default to text if not specified otherwise mode += 't' + if vb: + print("open() adding text mode") else: encoding = None errors = None newline = None + if vb: + print("open() setting encoding=errors=newline=None") if compression is "bzip2": file_object = bz2.open(file, mode=mode, @@ -719,4 +726,7 @@ class dataIO(): newline=newline, closefd=closefd, opener=opener) + + if vb: + print("open() return file_object =",file_object) return file_object diff --git a/binarycpython/utils/functions.py b/binarycpython/utils/functions.py index 75ff3df4d..875398fe9 100644 --- a/binarycpython/utils/functions.py +++ b/binarycpython/utils/functions.py @@ -370,7 +370,10 @@ def call_binary_c_config(argument): ######################################################## -def verbose_print(message: str, verbosity: int, minimal_verbosity: int) -> None: +def verbose_print(message: str, + verbosity: int, + minimal_verbosity: int, + newline = "\n") -> None: """ Function that decides whether to print a message based on the current verbosity and its minimum verbosity @@ -384,7 +387,10 @@ def verbose_print(message: str, verbosity: int, minimal_verbosity: int) -> None: """ if verbosity >= minimal_verbosity: - print(message) + if newline is "\n": + print(message) + else: + print(message,newline,sep="",end="") sys.stdout.flush() diff --git a/binarycpython/utils/grid_logging.py b/binarycpython/utils/grid_logging.py index b695da149..e843e0744 100644 --- a/binarycpython/utils/grid_logging.py +++ b/binarycpython/utils/grid_logging.py @@ -281,7 +281,7 @@ class grid_logging(): ) else: self.verbose_print( - "{opening_colour}{system_number}{modulo} {time_colour}{hours:02d}:{minutes:02d}:{seconds:02d} tpr={tpr:2.2e} {mem_use_colour}mem:{mem_use:.1f}MB {system_string_colour}{system_string}{closing_colour}".format( + "{opening_colour}{system_number}{modulo} {time_colour}{hours:02d}:{minutes:02d}:{seconds:02d} tpr={tpr:2.2e} {mem_use_colour}mem:{mem_use:.1f}MB {system_string_colour}{system_string}{closing_colour}{newline}".format( opening_colour=self.ANSI_colours["reset"] + self.ANSI_colours["yellow on black"], system_number=system_number, @@ -296,6 +296,7 @@ class grid_logging(): system_string_colour=self.ANSI_colours["yellow"], system_string=system_string, closing_colour=self.ANSI_colours["reset"], + newline=self.grid_options["log_newline"] ), self.grid_options["verbosity"], 1, diff --git a/binarycpython/utils/grid_options_defaults.py b/binarycpython/utils/grid_options_defaults.py index 742c2037c..1d1538ff9 100644 --- a/binarycpython/utils/grid_options_defaults.py +++ b/binarycpython/utils/grid_options_defaults.py @@ -76,11 +76,12 @@ class grid_options_defaults(): # Execution log: ########################## "verbosity": 0, # Level of verbosity of the simulation - "log_file": os.path.join( + "log_file": os.path.join( # not used (yet?) temp_dir(), "binary_c_python.log" ), # Set to None to not log to file. The directory will be created "log_dt": 5, # time between vb=1 logging outputs "n_logging_stats": 50, # number of logging stats used to calculate time remaining (etc.) default = 50 + "log_newline" : "\n", # newline character in logs ("\n" for newlines, "\x0d" for carriage return) ########################## # binary_c files ########################## @@ -200,7 +201,7 @@ class grid_options_defaults(): ######################################## "HPC_force_join" : 0, # if True, and the HPC variable ("slurm" or "condor") is 3, skip checking our own job and force the join "HPC_rebuild_joinlist": 0, # if True, ignore the joinlist we would usually use and rebuild it automatically - + ######################################## # Slurm stuff ######################################## @@ -278,6 +279,9 @@ class grid_options_defaults(): "_binary_c_executable": "Full path to the binary_c executable. This options is not used in the population object.", "_binary_c_shared_library": "Full path to the libbinary_c file. This options is not used in the population object", "verbosity": "Verbosity of the population code. Default is 0, by which only errors will be printed. Higher values will show more output, which is good for debugging.", + "log_dt" : "Time between verbose logging output.", + "log_newline" : "Newline character used at the end of verbose logging statements. This is \\n (newline) by default, but \\x0d (carriage return) might also be what you want.", + "n_logging_stats" : "Number of logging statistics used to calculate time remaining (etc.). E.g., if you set this to 10 the previous 10 calls to the verbose log will be used to construct an estimate of the time remaining.", # deprecated: "binary": "Set this to 1 if the population contains binaries. Input: int", # TODO: write what effect this has. "num_cores": "The number of cores that the population grid will use. You can set this manually by entering an integer great than 0. When 0 uses all logical cores. When -1 uses all physical cores. Input: int", "num_processes" : "Number of processes launched by multiprocessing. This should be set automatically by binary_c-python, not by the user.", -- GitLab