From 4fe74c8014497402920a43323ec608a6b6185a16 Mon Sep 17 00:00:00 2001 From: Robert Izzard <r.izzard@surrey.ac.uk> Date: Mon, 11 Oct 2021 15:58:45 +0100 Subject: [PATCH] update logging with boxes --- binarycpython/utils/grid.py | 72 ++++++++++++++++++++++++++----------- 1 file changed, 52 insertions(+), 20 deletions(-) diff --git a/binarycpython/utils/grid.py b/binarycpython/utils/grid.py index 5eb79465c..115af951b 100644 --- a/binarycpython/utils/grid.py +++ b/binarycpython/utils/grid.py @@ -582,6 +582,7 @@ class Population: 1, ) + ################################################### # Return functions ################################################### @@ -778,6 +779,38 @@ class Population: ) return outfile + def _boxed(self,*list,colour='yellow on black',boxchar='*',separator="\n"): + """ + Function to output a list of strings in a single box. + + Args: + list = a list of strings to be output. If these contain the separator + (see below) these strings are split by it. + separator = strings are split on this, default "\n" + colour = the colour to be used, usually this is 'yellow on black' + as set in the ANSI_colours dict + boxchar = the character used to make the box, '*' by default + """ + strlen = 0 + strings = [] + if separator: + for l in list: + strings += l.split(sep=separator) + else: + strings = list + for string in strings: + strlen = max(len(string),strlen) + strlen += strlen % 2 + header = boxchar * (4 + strlen) + out = self.ANSI_colours[colour] + header + "\n" + for string in strings: + if len(string)%2 == 1: + string = ' ' + string + pad = ' ' * int((strlen - len(string))/2) + out = out + boxchar + ' ' + pad + string + pad + ' ' + boxchar +"\n" + out = out + header + "\n" + self.ANSI_colours["reset"] + return out + def _set_custom_logging(self): """ Function/routine to set all the custom logging so that the function memory pointer @@ -976,6 +1009,7 @@ class Population: return analytics_dict + def _evolve_population(self): """ Function to evolve populations. This handles the setting up, evolving @@ -1017,23 +1051,20 @@ class Population: self.grid_options["_end_time_evolution"] = time.time() # Log and print some information - verbose_print( - "Population-{} finished! The total probability was: {}. It took a total of {}s to run {} systems on {} cores".format( - self.grid_options["_population_id"], - self.grid_options["_probtot"], - self.grid_options["_end_time_evolution"] - - self.grid_options["_start_time_evolution"], - self.grid_options["_total_starcount"], - self.grid_options["num_cores"], - ), - self.grid_options["verbosity"], - 0, - ) + dtsecs = self.grid_options["_end_time_evolution"] - self.grid_options["_start_time_evolution"] + string1 = "Population-{} finished!\nThe total probability is {:g}.".format(self.grid_options["_population_id"], + self.grid_options["_probtot"],) + string2 = "It took a total of {}s to run {} systems on {} cores".format(dtsecs, + self.grid_options["_total_starcount"], + self.grid_options["num_cores"]) + verbose_print(self._boxed(string1,string2), + self.grid_options["verbosity"], + 0) if self.grid_options["_errors_found"]: # Some information afterwards verbose_print( - "During the run {} failed systems were found, with a total probability of {} and with the following unique error codes: {} ".format( + "During the run {} failed systems were found, with a total probability of {:g} and with the following unique error codes: {} ".format( self.grid_options["_failed_count"], self.grid_options["_failed_prob"], self.grid_options["_failed_systems_error_codes"], @@ -1696,7 +1727,7 @@ class Population: end_process_time = datetime.datetime.now() verbose_print( - "Process {} finished:\n\tgenerator started at {}, done at {} (total: {}s of which {}s interfacing with binary_c).\n\tRan {} systems with a total probability of {}.\n\tThis thread had {} failing systems with a total probability of {}.\n\tSkipped a total of {} systems because they had 0 probability".format( + "Process {} finished:\n\tgenerator started at {}, done at {} (total: {}s of which {}s interfacing with binary_c).\n\tRan {} systems with a total probability of {:g}.\n\tThis thread had {} failing systems with a total probability of {}\n\tSkipped a total of {} systems because they had 0 probability".format( ID, start_process_time.isoformat(), end_process_time.isoformat(), @@ -1913,12 +1944,13 @@ class Population: # Do a dry run self._dry_run() - print( - "Total starcount for this run will be: {} with a total probability of {}".format( - self.grid_options["_total_starcount"], - self.grid_options["_probtot"], - ) - ) + verbose_print( + self._boxed( + "Total starcount for this run is {starcount}".format(starcount=self.grid_options["_total_starcount"]), + "Total probability is {probtot}".format(probtot=self.grid_options["_probtot"]) + ), + self.grid_options["verbosity"], + 0) if self.grid_options["exit_after_dry_run"]: exit() -- GitLab