From f9d92042ed51024e8308e344befd0b87448ba773 Mon Sep 17 00:00:00 2001 From: David Hendriks <davidhendriks93@gmail.com> Date: Sun, 29 Nov 2020 19:53:22 +0000 Subject: [PATCH] updated binarycpython to handle the c_bindings as part of the module --- binarycpython/utils/functions.py | 38 ++++++++++++++++++----- binarycpython/utils/grid.py | 29 ++++++----------- binarycpython/utils/run_system_wrapper.py | 5 +-- 3 files changed, 43 insertions(+), 29 deletions(-) diff --git a/binarycpython/utils/functions.py b/binarycpython/utils/functions.py index 85cfc27e9..27a325312 100644 --- a/binarycpython/utils/functions.py +++ b/binarycpython/utils/functions.py @@ -10,13 +10,14 @@ import os import tempfile import copy import inspect +import ast from collections import defaultdict import h5py import numpy as np -import binary_c_python_api +from binarycpython import _binary_c_bindings ######################################################## # utility functions @@ -140,7 +141,7 @@ def return_binary_c_version_info(parsed=False): Function that returns the version information of binary_c """ - version_info = binary_c_python_api.return_version_info().strip() + version_info = _binary_c_bindings.return_version_info().strip() if parsed: version_info = parse_binary_c_version_info(version_info) @@ -407,7 +408,7 @@ def get_defaults(filter_values=False): filter_values: whether to filter out NULL and Function defaults. """ - default_output = binary_c_python_api.return_arglines() + default_output = _binary_c_bindings.return_arglines() default_dict = {} for default in default_output.split("\n"): @@ -500,7 +501,7 @@ def get_help(param_name="", print_help=True, fail_silently=False): return None if param_name in available_arg_keys: - help_info = binary_c_python_api.return_help(param_name) + help_info = _binary_c_bindings.return_help(param_name) cleaned = [el for el in help_info.split("\n") if not el == ""] # Get line numbers @@ -580,7 +581,7 @@ def get_help_all(print_help=True): """ # Call function - help_all = binary_c_python_api.return_help_all() + help_all = _binary_c_bindings.return_help_all() # String manipulation split = help_all.split( @@ -633,12 +634,16 @@ def get_help_all(print_help=True): # Put the information in a dict param_name = split_param_info[0] param_description = split_param_info[1] - rest = split_param_info[2] + + if len(split_param_info) > 2: + rest = split_param_info[2:] + else: + rest = None params_dict[param_name] = { "param_name": param_name, "description": param_description, - "rest": rest, + "rest": ''.join(rest) if rest else '', } # make section_dict @@ -937,3 +942,22 @@ def binaryc_json_serializer(obj): if inspect.isfunction(obj): return str(obj) return obj + +def handle_ensemble_string_to_json(raw_output): + """ + Function that deals with the raw output of the ensemble and + creates a working JSON dictionary out of it. + + It seems that JSON does not like strings that contain dictionaries which have a number as a key, but more specifically it is the fact that binary-c outputs as 5e2 instead of + + e.g. with python 3.6.4: json.loads("{5e2: 10}") leads to a JSONDecodeError. To fix this, I do the following: + + with ensemble_output being the literal string of output, and json_output the correct jsonified version of that. + + eval_dict = ast.literal_eval(ensemble_output) + json_output = json.loads(json.dumps(eval_dict), cls=binarycDecoder) + + Note: this does pose somewhat of a risk, since eval'ing something might lead to unexpected behaviour. + """ + + return json.loads(json.dumps(ast.literal_eval(raw_output)), cls=binarycDecoder) diff --git a/binarycpython/utils/grid.py b/binarycpython/utils/grid.py index 1d8bae3d4..14036a907 100644 --- a/binarycpython/utils/grid.py +++ b/binarycpython/utils/grid.py @@ -49,8 +49,7 @@ from binarycpython.utils.hpc_functions import ( get_python_details, ) - -import binary_c_python_api +from binarycpython import _binary_c_bindings # Tasks # TODO: add functionality to 'on-init' set arguments @@ -349,9 +348,7 @@ class Population: def _return_binary_c_version_info(self, parsed=False): """ Function that returns the version information of binary_c - """ - - version_info = binary_c_python_api.return_version_info().strip() + """.return_version_info().strip() if parsed: version_info = parse_binary_c_version_info(version_info) @@ -534,8 +531,7 @@ class Population: """ for thread_nr in self.grid_options["amt_cores"]: - persistent_data_memaddr = ( - binary_c_python_api.binary_c_return_persistent_data_memaddr() + persistent_data_memad.binary_c_return_persistent_data_memaddr() ) self.persistent_data_memory_dict[thread_nr] = persistent_data_memaddr verbose_print( @@ -567,8 +563,7 @@ class Population: 1, ) - # Get the output and decode it correctly to get the numbers correct - ensemble_json_output = binary_c_python_api.binary_c_free_persistent_data_memaddr_and_return_json_output( + # Get the output and decode it correctly.binary_c_free_persistent_data_memaddr_and_return_json_output( persistent_data_memaddr ) parsed_json = json.loads( @@ -700,8 +695,7 @@ class Population: full_system_dict = self.bse_options.copy() full_system_dict.update(system) - binary_cmdline_string = self._return_argline(full_system_dict) - out = binary_c_python_api.run_system( + binary_cmdline_string =.run_system( argstring=binary_cmdline_string, custom_logging_func_memaddr=self.grid_options[ "custom_logging_func_memaddr" @@ -719,9 +713,7 @@ class Population: def evolve_system_mp(self, binary_cmdline_string): """ Function that the multiprocessing evolution method calls to evolve a system - """ - - out = binary_c_python_api.run_system( + """.run_system( argstring=binary_cmdline_string, custom_logging_func_memaddr=self.grid_options[ "custom_logging_func_memaddr" @@ -766,8 +758,7 @@ class Population: argline = self._return_argline(self.bse_options) verbose_print("Running {}".format(argline), self.grid_options["verbosity"], 1) - # Run system - out = binary_c_python_api.run_system( + # Run syste.run_system( argstring=argline, custom_logging_func_memaddr=self.grid_options[ "custom_logging_func_memaddr" @@ -811,8 +802,7 @@ class Population: ### Custom logging code: self._set_custom_logging() - ### Load store - self.grid_options["store_memaddr"] = binary_c_python_api.return_store_memaddr() + ### Load stor.return_store_memaddr() ### ensemble: ## check the settings: @@ -930,8 +920,7 @@ class Population: # Unload functions - # Unload store - binary_c_python_api.free_store_memaddr(self.grid_options["store_memaddr"]) + # Unload stor.free_store_memaddr(self.grid_options["store_memaddr"]) ################################################### # Gridcode functions diff --git a/binarycpython/utils/run_system_wrapper.py b/binarycpython/utils/run_system_wrapper.py index 7b726892e..e13535cd4 100644 --- a/binarycpython/utils/run_system_wrapper.py +++ b/binarycpython/utils/run_system_wrapper.py @@ -13,7 +13,8 @@ from binarycpython.utils.functions import ( from binarycpython.utils.custom_logging_functions import ( create_and_load_logging_function, ) -import binary_c_python_api + +from binarycpython import _binary_c_bindings def run_system(**kwargs): @@ -87,7 +88,7 @@ def run_system(**kwargs): binary_c_command = "binary_c {}".format(arg_string) # Call binary_c - output = binary_c_python_api.run_system( + output = _binary_c_bindings.run_system( binary_c_command, custom_logging_func_memaddr=func_memaddr, write_logfile=write_logfile, -- GitLab