Commit 88bc3001 authored by David Hendriks's avatar David Hendriks
Browse files

executed black formatting on the code

parent 50e1612d
Loading
Loading
Loading
Loading
+10 −7
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@ import ctypes
import random
import uuid


def autogen_C_logging_code(logging_dict):
    """
    Function that autogenerates PRINTF statements for binaryc. intput is a dictionary where the key is the header of that logging line and items which are lists of parameters\
@@ -234,7 +235,7 @@ def compile_shared_lib(code, sourcefile_name, outfile_name, verbose=False):
    # Remove the library if present:
    if os.path.exists(outfile_name):
        try:
            print('removing' ,outfile_name)
            print("removing", outfile_name)
            os.remove(outfile_name)
        except:
            print("Error while deleting file {}".format(outfile_name))
@@ -242,7 +243,6 @@ def compile_shared_lib(code, sourcefile_name, outfile_name, verbose=False):
    # create compilation command
    compilation_dict = return_compilation_dict()


    # Construct full command
    command = "{cc} {ccflags} {libs} -o {outfile_name} {sourcefile_name} {inc}".format(
        cc=compilation_dict["cc"],
@@ -292,8 +292,9 @@ def create_and_load_logging_function(custom_logging_code):

    #


    library_name = os.path.join(temp_custom_logging_dir(), "libcustom_logging_{}.so".format(uuid.uuid4().hex))
    library_name = os.path.join(
        temp_custom_logging_dir(), "libcustom_logging_{}.so".format(uuid.uuid4().hex)
    )

    compile_shared_lib(
        custom_logging_code,
@@ -308,11 +309,13 @@ def create_and_load_logging_function(custom_logging_code):
    dll1 = ctypes.CDLL("libgslcblas.so", mode=ctypes.RTLD_GLOBAL)
    dll2 = ctypes.CDLL("libgsl.so", mode=ctypes.RTLD_GLOBAL)
    dll3 = ctypes.CDLL("libbinary_c.so", mode=ctypes.RTLD_GLOBAL)
    libcustom_logging = ctypes.CDLL(library_name,
        mode=ctypes.RTLD_GLOBAL,
    libcustom_logging = ctypes.CDLL(
        library_name, mode=ctypes.RTLD_GLOBAL,
    )  # loads the shared library

    # Get memory adress of function. mimicking a pointer
    func_memaddr = ctypes.cast(libcustom_logging.custom_output_function, ctypes.c_void_p).value
    func_memaddr = ctypes.cast(
        libcustom_logging.custom_output_function, ctypes.c_void_p
    ).value

    return func_memaddr
+13 −14
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@ from binarycpython.utils.custom_logging_functions import (
    create_and_load_logging_function,
)


def get_help_super(print_help=False, return_dict=True):
    """
    Function that first runs get_help_all, and then per argument also run the help function to get as much information as possible.
@@ -14,35 +15,34 @@ def get_help_super(print_help=False, return_dict=True):

    help_all_dict = get_help_all(print_help=False, return_dict=True)


    # print(help_all_dict)

    # print(json.dumps(help_all_dict, indent=4))

    help_all_super_dict = help_all_dict.copy()


    for section_name in help_all_dict.keys():
        section = help_all_dict[section_name]

        # print(section)
        for parameter_name in section['parameters'].keys():
            parameter = section['parameters'][parameter_name]
        for parameter_name in section["parameters"].keys():
            parameter = section["parameters"][parameter_name]

            detailed_help = get_help(parameter_name, print_help=False, return_dict=True, fail_silently=True)
            detailed_help = get_help(
                parameter_name, print_help=False, return_dict=True, fail_silently=True
            )

            parameter['detailed_help'] = detailed_help
            parameter["detailed_help"] = detailed_help

            if parameter['detailed_help']:
                if not parameter['description']==parameter['detailed_help']['description']:
            if parameter["detailed_help"]:
                if (
                    not parameter["description"]
                    == parameter["detailed_help"]["description"]
                ):
                    print(json.dumps(parameter, indent=4))


            # print(parameter['description']==parameter['detailed_help']['description'])




            # print(parameter.keys())

    if print_help:
@@ -54,7 +54,6 @@ def get_help_super(print_help=False, return_dict=True):
        return



def get_help_all(print_help=True, return_dict=False):
    """
    Function that reads out the output of the help_all api call to binary_c
+14 −14
Original line number Diff line number Diff line
@@ -92,10 +92,13 @@ class Population(object):
                self.grid_options[key] = kwargs[key]
            # The of the keys go into a custom_options dict
            else:
                print("!! Key doesnt match previously known parameter: adding: {}={} to custom_options".format(key, kwargs[key]))
                print(
                    "!! Key doesnt match previously known parameter: adding: {}={} to custom_options".format(
                        key, kwargs[key]
                    )
                )
                self.custom_options[key] = kwargs[key]


    def return_argline(self, parameter_dict=None):
        """
        Function to create the string for the arg line from a parameter dict
@@ -281,7 +284,6 @@ class Population(object):
                "custom_logging_func_memaddr"
            ] = create_and_load_logging_function(custom_logging_code)


    ###################################################
    # Evolution functions
    ###################################################
@@ -298,7 +300,11 @@ class Population(object):
        argline = self.return_argline(self.bse_options)

        # Run system
        out = binary_c_python_api.run_system(argline, self.grid_options['custom_logging_func_memaddr'], self.grid_options['store_memaddr']) # Todo: change this to run_binary again but then build in checks in binary
        out = binary_c_python_api.run_system(
            argline,
            self.grid_options["custom_logging_func_memaddr"],
            self.grid_options["store_memaddr"],
        )  # Todo: change this to run_binary again but then build in checks in binary

        # TODO: add call to function that cleans up the temp customlogging dir, and unloads the loaded libraries.

@@ -333,18 +339,17 @@ class Population(object):
            ] = create_and_load_logging_function(custom_logging_code)

        ### Load store
        self.grid_options['store_memaddr'] = binary_c_python_api.return_store("")
        self.grid_options["store_memaddr"] = binary_c_python_api.return_store("")

        # Execute.
        out = binary_c_python_api.run_population(
            self.return_argline(),
            self.grid_options['custom_logging_func_memaddr'], 
            self.grid_options['store_memaddr']
            self.grid_options["custom_logging_func_memaddr"],
            self.grid_options["store_memaddr"],
        )

        print(out)


        ### Arguments
        # If user inputs a file containing arg lines then use that
        if custom_arg_file:
@@ -365,10 +370,8 @@ class Population(object):

            pass


        quit()


        #######
        # Do stuff
        for line in population_arglines:
@@ -378,9 +381,6 @@ class Population(object):

        # TODO: add call to function that cleans up the temp customlogging dir, and unloads the loaded libraries.




    ###################################################
    # Testing functions
    ###################################################
+1 −1
Original line number Diff line number Diff line
@@ -9,7 +9,7 @@ grid_options_defaults_dict = {
    "C_logging_code": None,  # Should contain a string which holds the logging code.
    "custom_logging_func_memaddr": -1,  # Contains the custom_logging functions memory address
    # Store pre-loading:
    'store_memaddr': -1, # Contains the store object memory adress, useful for preloading. defaults to -1 and isnt used if thats the default then.
    "store_memaddr": -1,  # Contains the store object memory adress, useful for preloading. defaults to -1 and isnt used if thats the default then.
    # Log args: logging of arguments
    "log_args": 0,  #
    "log_args_dir": "/tmp/",
+9 −2
Original line number Diff line number Diff line
@@ -103,6 +103,7 @@ def run_example_binary_with_run_system():

run_example_binary_with_run_system()


def run_example_custom_logging_autogenerated():
    """
    This is an example function for the autogeneration of logging codes that binary_c uses.
@@ -116,7 +117,6 @@ def run_example_custom_logging_autogenerated():
        }
    )


    # Generate code around logging lines
    custom_logging_code = binary_c_log_code(logging_line)

@@ -132,11 +132,18 @@ def run_example_custom_logging_autogenerated():
    metallicity = 0.02
    max_evolution_time = 15000
    argstring = "binary_c M_1 {0:g} M_2 {1:g} separation {2:g} orbital_period {3:g} eccentricity {4:g} metallicity {5:g} max_evolution_time {6:g}".format(
        m1, m2, separation, orbital_period, eccentricity, metallicity, max_evolution_time
        m1,
        m2,
        separation,
        orbital_period,
        eccentricity,
        metallicity,
        max_evolution_time,
    )
    output = binary_c_python_api.run_binary_custom_logging(argstring, func_memaddr)
    print(output)


run_example_custom_logging_autogenerated()


Loading