diff --git a/binarycpython/utils/functions.py b/binarycpython/utils/functions.py index f7e4532b73130630798e868a4e0f5643901c1c7f..dfa36e9376383fb3850ba8fb71a6a8be45368653 100644 --- a/binarycpython/utils/functions.py +++ b/binarycpython/utils/functions.py @@ -20,6 +20,7 @@ import time import types import resource import psutil +from colorama import Fore, Back, Style from io import StringIO from typing import Union, Any @@ -42,6 +43,62 @@ import py_rinterpolate # Unsorted ######################################################## +def format_number(number): + # compact number formatter + + string = "{number:.2g}".format(number=number) + string = string.replace("e+0", "e+") + string = string.replace("e-0", "e-") + + return string + +def check_if_in_shell(): + """ + Function to check whether the script is running from a shell + """ + + if sys.stdin and sys.stdin.isatty(): + in_shell = True + else: + in_shell = False + + return in_shell + +def ANSI_colours(self): + # ANSI colours dictionary + foreground_colours = { + 'red':Fore.RED, + 'yellow':Fore.YELLOW, + 'blue':Fore.BLUE, + 'cyan':Fore.CYAN, + 'green':Fore.GREEN, + 'magenta':Fore.MAGENTA, + 'white':Fore.WHITE, + 'black':Fore.BLACK, + } + + background_colours = { + 'red':Back.RED, + 'yellow':Back.YELLOW, + 'blue':Back.BLUE, + 'cyan':Back.CYAN, + 'green':Back.GREEN, + 'magenta':Back.MAGENTA, + 'white':Back.WHITE, + 'black':Back.BLACK, + } + + default_style = Style.BRIGHT + colours = {} + + for c in foreground_colours: + colours[c] = default_style + foreground_colours[c] + for d in background_colours: + colours[c + ' on ' + d] = foreground_colours[c] + background_colours[d] + colours['reset'] = Style.RESET_ALL + + return colours + def mem_use(): """ Return current process memory use in MB. (Takes no arguments) Note: this is per-thread only. diff --git a/binarycpython/utils/grid.py b/binarycpython/utils/grid.py index 7a18c516be4102223fa4293461e7005753ac3139..b3668ff1ead396e241a90296c4ecafea179ec98e 100644 --- a/binarycpython/utils/grid.py +++ b/binarycpython/utils/grid.py @@ -23,7 +23,6 @@ Tasks: import os import gc import sys -from colorama import Fore,Back,Style,init as colorama_init import copy import json @@ -42,6 +41,9 @@ from collections import ( import setproctitle import py_rinterpolate +from colorama import init as colorama_init +colorama_init() + from binarycpython.utils.grid_options_defaults import ( grid_options_defaults_dict, moe_di_stefano_default_options, @@ -55,6 +57,7 @@ from binarycpython.utils.custom_logging_functions import ( binary_c_log_code, create_and_load_logging_function, ) + from binarycpython.utils.functions import ( get_defaults, remove_file, @@ -75,7 +78,10 @@ from binarycpython.utils.functions import ( AutoVivificationDict, trem, conv_time_units, - mem_use + mem_use, + ANSI_colours, + check_if_in_shell, + format_number ) # from binarycpython.utils.hpc_functions import ( @@ -95,7 +101,6 @@ from binarycpython.utils.distribution_functions import ( raghavan2010_binary_fraction, Moe_di_Stefano_2017_multiplicity_fractions, ) -colorama_init() from binarycpython import _binary_c_bindings secs_per_day = 86400 # probably needs to go somewhere more sensible @@ -157,13 +162,10 @@ class Population: self.shared_memory = {} # variable to test if we're running in a shell - if sys.stdin and sys.stdin.isatty(): - self.in_shell = True - else: - self.in_shell = False + self.in_shell = check_if_in_shell() # ANSI colours: use them if in a shell - self.ANSI_colours = self._ANSI_colours() + self.ANSI_colours = ANSI_colours() if self.in_shell == False: for c in self.ANSI_colours: self.ANSI_colours[c] = "" @@ -908,11 +910,14 @@ class Population: # Check which type: if self.grid_options["slurm"] == 1: # Execute Slurm subroutines - self._slurm_grid() + # self._slurm_grid() + raise ValueError("Slurm evolution not available at this moment") elif self.grid_options["condor"] == 1: # Execute condor subroutines - self._condor_grid() + # self._condor_grid() + raise ValueError("Condor evolution not available at this moment") + else: # Execute population evolution subroutines self._evolve_population() @@ -1747,9 +1752,6 @@ class Population: msg = "No actual evolution options passed to the evolve call. Aborting" raise ValueError(msg) - - - def _setup(self): """ Function to set up the necessary stuff for the population evolution. @@ -1958,13 +1960,18 @@ class Population: # a variable grid ################################################### - def _add_code(self,*args,indent=0,mindent=-1): - # add code to the code_string - # - # indent (=0) is added once at the beginning - # mindent (=0) is added for every line - # - # don't use both! + def _add_code(self, *args, indent=0,mindent=-1): + """ + Function to add code to the grid code string + + add code to the code_string + + indent (=0) is added once at the beginning + mindent (=0) is added for every line + + don't use both! + """ + # if we have multiple arguments and have forgotten # to set mindent = 0, do it here @@ -1982,12 +1989,18 @@ class Population: self.code_string += self._indent_block(mindent) self.code_string += thing - def _indent_block(self,n=0): - # return an indent block, with n extra blocks in it + def _indent_block(self, n=0): + """ + return an indent block, with n extra blocks in it + """ + return (self.indent_depth + n) * self.indent_string - def _increment_indent_depth(self,delta): - # increment the indent indent_depth by delta + def _increment_indent_depth(self, delta): + """ + increment the indent indent_depth by delta + """ + self.indent_depth += delta def _generate_grid_code(self, dry_run=False): @@ -3329,10 +3342,8 @@ class Population: print("Error. No grid function found!") raise ValueError - return binary_c_calls_full_filename - def _cleanup_defaults(self): """ Function to clean up the default values: @@ -4348,23 +4359,16 @@ eccentricity3=0 # add up memory use from each thread total_mem_use = sum(self.shared_memory["memory_use_per_thread"]) - def _format_number(number): - # compact number formatter - string = "{number:.2g}".format(number=number) - string = string.replace("e+0","e+") - string = string.replace("e-0","e-") - return string - # make a string to describe the system e.g. M1, M2, etc. system_string = "" if 'multiplicity' in system_dict: for i in range(system_dict['multiplicity']): i1 = str(i+1) - system_string += "M{}=".format(i1) + _format_number(system_dict['M_'+i1]) + " " + system_string += "M{}=".format(i1) + format_number(system_dict['M_'+i1]) + " " if 'separation' in system_dict: - system_string += "a=" + _format_number(system_dict['separation']) + system_string += "a=" + format_number(system_dict['separation']) if 'orbital_period' in system_dict: - system_string += "P=" + _format_number(system_dict['orbital_period']) + system_string += "P=" + format_number(system_dict['orbital_period']) # do the print verbose_print( @@ -4394,34 +4398,3 @@ eccentricity3=0 self.grid_options["verbosity"], 1 ) - - def _ANSI_colours(self): - # ANSI colours dictionary - foreground_colours = { - 'red':Fore.RED, - 'yellow':Fore.YELLOW, - 'blue':Fore.BLUE, - 'cyan':Fore.CYAN, - 'green':Fore.GREEN, - 'magenta':Fore.MAGENTA, - 'white':Fore.WHITE, - 'black':Fore.BLACK, - } - background_colours = { - 'red':Back.RED, - 'yellow':Back.YELLOW, - 'blue':Back.BLUE, - 'cyan':Back.CYAN, - 'green':Back.GREEN, - 'magenta':Back.MAGENTA, - 'white':Back.WHITE, - 'black':Back.BLACK, - } - default_style = Style.BRIGHT - colours = {} - for c in foreground_colours: - colours[c] = default_style + foreground_colours[c] - for d in background_colours: - colours[c + ' on ' + d] = foreground_colours[c] + background_colours[d] - colours['reset'] = Style.RESET_ALL - return colours