From 54415f934558fcd26d198ace01073ec76a38c859 Mon Sep 17 00:00:00 2001 From: David Hendriks <davidhendriks93@gmail.com> Date: Thu, 21 Nov 2019 23:40:56 +0000 Subject: [PATCH] Tried to fully package the core api but that seems like a very difficult endeavour, as passing the shared library via package_data is not enough. it complains about missing symbols, meaning that the other shared/linked stuff is not working anymore. it only works good if things are now loaded by adding this package to the PYTHONPATH atm. will do it later with pip, but thats gonna be some more work --- Makefile | 2 +- binarycpython/core/__init__.py | 0 binarycpython/utils/functions.py | 14 +++++++------- examples/examples.py | 32 ++++++++------------------------ lib/__init__.py | 0 setup.py | 20 +++++++++++++------- src/binary_c_python.c | 8 ++++---- 7 files changed, 33 insertions(+), 43 deletions(-) create mode 100644 binarycpython/core/__init__.py create mode 100644 lib/__init__.py diff --git a/Makefile b/Makefile index 3bf93bfcb..36703ebb1 100644 --- a/Makefile +++ b/Makefile @@ -45,7 +45,7 @@ SO_FLAGS := -shared PY_EXEC := python3 PY_SETUP := setup.py #PY_OPTIONS := build_ext --build-lib $(TARGET_LIB_DIR) -PY_OPTIONS := build_ext --inplace -- +PY_OPTIONS := build_ext --inplace # maybe pass argument from here to the setup.py? all: diff --git a/binarycpython/core/__init__.py b/binarycpython/core/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/binarycpython/utils/functions.py b/binarycpython/utils/functions.py index cc7e3b954..539bcf831 100644 --- a/binarycpython/utils/functions.py +++ b/binarycpython/utils/functions.py @@ -1,7 +1,7 @@ from collections import defaultdict -import binary_c -from binaryc_python_utils.custom_logging_functions import ( +import binary_c_python_api +from binarycpython.utils.custom_logging_functions import ( create_and_load_logging_function, ) @@ -22,7 +22,7 @@ def get_defaults(): Function that calls the binaryc get args function and cast it into a dictionary All the values are strings """ - default_output = binary_c.return_arglines() + default_output = binary_c_python_api.return_arglines() default_dict = {} for default in default_output.split("\n"): @@ -71,7 +71,7 @@ def run_system(**kwargs): arg_string = "binary_c {}".format(arg_string) # Run it and get output - output = binary_c.run_binary_custom_logging(arg_string, func_memaddr) + output = binary_c_python_api.run_binary_custom_logging(arg_string, func_memaddr) return output elif "log_filename" in kwargs: @@ -84,7 +84,7 @@ def run_system(**kwargs): arg_string = "binary_c {}".format(arg_string) # Run it and get output - output = binary_c.run_binary_with_logfile(arg_string) + output = binary_c_python_api.run_binary_with_logfile(arg_string) return output else: # run the plain basic type @@ -98,7 +98,7 @@ def run_system(**kwargs): arg_string = "binary_c {}".format(arg_string) # Run it and get output - output = binary_c.run_binary(arg_string) + output = binary_c_python_api.run_binary(arg_string) return output @@ -129,7 +129,7 @@ def run_system_with_log(**kwargs): # Run it and get output buffer = "" - output = binary_c.run_binary_with_log(arg_string) + output = binary_c_python_api.run_binary_with_log(arg_string) return output diff --git a/examples/examples.py b/examples/examples.py index 7df0e0287..ab728b6e4 100644 --- a/examples/examples.py +++ b/examples/examples.py @@ -2,10 +2,10 @@ import os import sys -import binary_c +import binary_c_python_api -from binaryc_python_utils.functions import run_system, parse_output -from binaryc_python_utils.custom_logging_functions import ( +from binarycpython.utils.functions import run_system, parse_output +from binarycpython.utils.custom_logging_functions import ( autogen_C_logging_code, binary_c_log_code, ) @@ -42,10 +42,10 @@ def run_example_binary(): metallicity=metallicity, max_evolution_time=max_evolution_time, ) - output = binary_c.run_binary(argstring) + output = binary_c_python_api.run_binary(argstring) print(output) -# run_example_binary() +run_example_binary() def run_example_binary_with_run_system(): @@ -143,7 +143,7 @@ def run_example_binary_with_custom_logging(): # Do whatever you like with the dataframe. print(df) -# run_example_binary_with_custom_logging() +run_example_binary_with_custom_logging() def run_example_binary_with_writing_logfile(): """ @@ -161,23 +161,7 @@ def run_example_binary_with_writing_logfile(): separation=0, orbital_period=100000000000, log_filename=tempfile.gettempdir() + "/test_log.txt", - ) - - # Catch results that start with a given header. (Mind that binary_c has to be configured to print them if your not using a custom logging function) - result_example_header = parse_output(output, "example_header") - #### Now do whatever you want with it: - # Put it in numpy arrays - # t_res = np.asarray(result_example_header['t'], dtype=np.float64, order='C') - # m_res = np.asarray(result_example_header['mass'], dtype=np.float64, order='C') - - # Cast the data into a dataframe. - df = pd.DataFrame.from_dict(result_example_header, dtype=np.float64) - - # print(df) - # sliced_df = df[df.t < 1000] # Cut off late parts of evolution - # print(sliced_df[["t","m1"]]) - - # Some routine to plot. + ) -# run_example_binary_with_writing_logfile() \ No newline at end of file +run_example_binary_with_writing_logfile() \ No newline at end of file diff --git a/lib/__init__.py b/lib/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/setup.py b/setup.py index 2abee2e38..9e97e0f49 100644 --- a/setup.py +++ b/setup.py @@ -58,8 +58,9 @@ binary_c_define_macros.extend([("BINARY_C_API_H", API_h)]) binary_c_python_api_module = Extension( - "binary_c", - ["src/binary_c_python.c"], + # name="binarycpython.core.binary_c", + name="binary_c_python_api", + sources=["src/binary_c_python.c"], include_dirs=[ os.environ["BINARY_C"] + "/src", os.environ["BINARY_C"] + "/src/API", @@ -71,17 +72,20 @@ binary_c_python_api_module = Extension( os.environ["BINARY_C"] + "/src", "./", os.path.join(CWD, "lib/"), + # os.path.join(CWD, "binarycpython/core/"), ] + binary_c_libdirs, runtime_library_dirs=[ os.environ["BINARY_C"] + "/src", "./", os.path.join(CWD, "lib/"), + # os.path.join(CWD, "binarycpython/core/"), ] + binary_c_libdirs, define_macros=[] + binary_c_define_macros, extra_objects=[], extra_compile_args=[], + language="C", ) def readme(): @@ -100,15 +104,17 @@ setup( package_dir = { 'binarycpython': 'binarycpython', 'binarycpython.utils': 'binarycpython/utils', + # 'binarycpython.core': 'lib', }, - packages=['binarycpython', + packages=[ + 'binarycpython', 'binarycpython.utils', + # 'binarycpython.core', ], - - - - + # package_data={ + # 'binarycpython.core': ['libbinary_c_api.so'], + # }, ext_modules=[ binary_c_python_api_module diff --git a/src/binary_c_python.c b/src/binary_c_python.c index 7a96ccc4d..9504c50fe 100644 --- a/src/binary_c_python.c +++ b/src/binary_c_python.c @@ -82,7 +82,7 @@ static PyMethodDef module_methods[] = { #if PY_MAJOR_VERSION >= 3 /* Python 3+ */ -static struct PyModuleDef Py_binary_c = +static struct PyModuleDef Py_binary_c_python_api = { PyModuleDef_HEAD_INIT, "binary_c", /* name of module */ @@ -91,9 +91,9 @@ static struct PyModuleDef Py_binary_c = module_methods }; -PyMODINIT_FUNC PyInit_binary_c(void) +PyMODINIT_FUNC PyInit_binary_c_python_api(void) { - return PyModule_Create(&Py_binary_c); + return PyModule_Create(&Py_binary_c_python_api); } #else @@ -106,7 +106,7 @@ PyMODINIT_FUNC PyInit_binary_c(void) PyMODINIT_FUNC initbinary_c(void) { - PyObject *m = Py_InitModule3("binary_c", module_methods, module_docstring); + PyObject *m = Py_InitModule3("binary_c_python_api", module_methods, module_docstring); if(m == NULL) return; } -- GitLab