From fdac078c04313ba8fa43b6d3d87f996f8e0f19f5 Mon Sep 17 00:00:00 2001 From: Ximenes Rocha Resende Date: Thu, 30 Oct 2025 10:42:53 -0300 Subject: [PATCH 1/7] Add flatten function --- mathphys/functions.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/mathphys/functions.py b/mathphys/functions.py index 903a8db..38119ca 100644 --- a/mathphys/functions.py +++ b/mathphys/functions.py @@ -2,7 +2,7 @@ import os as _os import builtins as _builtins import importlib as _importlib -from collections import namedtuple as _namedtuple +from collections import namedtuple as _namedtuple, ItemsView as _Iterable from functools import partial as _partial import pickle as _pickle import subprocess as _subprocess @@ -333,6 +333,17 @@ def get_package_string(package): return repo_str +def flatten(x): + """Flatten recursive lists.""" + if isinstance(x, _Iterable) and not isinstance(x, (str, bytes)): + r = [] + for e in x: + r.extend(flatten(e)) + return r + else: + return [x] + + # ------------------------- HELPER METHODS ------------------------------------ _BUILTINTYPES = (int, float, complex, str, bytes, bool) _BUILTINNAMES = {typ.__name__ for typ in _BUILTINTYPES} From df2ddd5f11598aa76389b9c25cd96eab0e79eac5 Mon Sep 17 00:00:00 2001 From: Ximenes Rocha Resende Date: Thu, 30 Oct 2025 11:01:43 -0300 Subject: [PATCH 2/7] Abandon soon-deprecated pkg_resources package --- mathphys/functions.py | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/mathphys/functions.py b/mathphys/functions.py index 38119ca..f265a63 100644 --- a/mathphys/functions.py +++ b/mathphys/functions.py @@ -2,6 +2,8 @@ import os as _os import builtins as _builtins import importlib as _importlib +import importlib.util as _implib_util +import importlib.metadata as _implib_metadata from collections import namedtuple as _namedtuple, ItemsView as _Iterable from functools import partial as _partial import pickle as _pickle @@ -291,17 +293,35 @@ def get_path_from_package(package): Returns: location (str): Package installation directory - version (str) : Package installation version - + version (str): Package installation version """ if isinstance(package, str): - pkg = package + pkg_name = package elif isinstance(package, _ModuleType): - pkg = package.__package__ + pkg_name = package.__package__ or package.__name__ else: raise ValueError('Invalid package type, must be str or module') - dist = _pkg_resources.get_distribution(pkg) - return dist.location, dist.version + + # Try to get version using importlib.metadata + try: + version = _implib_metadata.version(pkg_name) + except _implib_metadata.PackageNotFoundError: + version = None + + # Try to resolve the package’s filesystem location + spec = _implib_util.find_spec(pkg_name) + if spec is None or spec.origin is None: + location = None + else: + # Usually .../site-packages/pkg_name/__init__.py + # So we take the parent directory of the package itself + if spec.submodule_search_locations: + location = spec.submodule_search_locations[0] + else: + import os + location = os.path.dirname(spec.origin) + + return location, version def is_git_repo(path): From 2ecee62c858cd40c1b4d880a81db79e02059f1ab Mon Sep 17 00:00:00 2001 From: Ximenes Rocha Resende Date: Thu, 30 Oct 2025 11:49:03 -0300 Subject: [PATCH 3/7] Remoce pkg_resources import --- mathphys/functions.py | 1 - 1 file changed, 1 deletion(-) diff --git a/mathphys/functions.py b/mathphys/functions.py index f265a63..063e62b 100644 --- a/mathphys/functions.py +++ b/mathphys/functions.py @@ -8,7 +8,6 @@ from functools import partial as _partial import pickle as _pickle import subprocess as _subprocess -import pkg_resources as _pkg_resources from types import ModuleType as _ModuleType import gzip as _gzip From b85884cd3adfc1be557ba104995dcfdcbe53f167 Mon Sep 17 00:00:00 2001 From: Ximenes Rocha Resende Date: Fri, 31 Oct 2025 21:07:06 -0300 Subject: [PATCH 4/7] Refactor get_path_from_package --- mathphys/functions.py | 47 ++++++++++++++----------------------------- 1 file changed, 15 insertions(+), 32 deletions(-) diff --git a/mathphys/functions.py b/mathphys/functions.py index 063e62b..5ff8769 100644 --- a/mathphys/functions.py +++ b/mathphys/functions.py @@ -1,20 +1,25 @@ """Useful functions.""" import os as _os import builtins as _builtins -import importlib as _importlib -import importlib.util as _implib_util -import importlib.metadata as _implib_metadata -from collections import namedtuple as _namedtuple, ItemsView as _Iterable +from collections import namedtuple as _namedtuple, Iterable as _Iterable from functools import partial as _partial import pickle as _pickle import subprocess as _subprocess +# NOTE: Change to importlib.metadata once python3.6 is not supported anymore: +import importlib_metadata as _implib_meta from types import ModuleType as _ModuleType import gzip as _gzip -import h5py as _h5py +try: + import h5py as _h5py +except ModuleNotFoundError: + _h5py = None + + import numpy as _np + def generate_random_numbers(n_part, dist_type='exp', cutoff=3): """Generate random numbers with a cutted off dist_type distribution. @@ -283,44 +288,22 @@ def repo_info(repo_path): def get_path_from_package(package): """Return the directory where package is installed. - Args: package (str or module): Package name or module - Raises: ValueError: If package argument type is different from str or module - Returns: location (str): Package installation directory - version (str): Package installation version + version (str) : Package installation version """ if isinstance(package, str): - pkg_name = package + pkg = package elif isinstance(package, _ModuleType): - pkg_name = package.__package__ or package.__name__ + pkg = package.__package__ else: raise ValueError('Invalid package type, must be str or module') - - # Try to get version using importlib.metadata - try: - version = _implib_metadata.version(pkg_name) - except _implib_metadata.PackageNotFoundError: - version = None - - # Try to resolve the package’s filesystem location - spec = _implib_util.find_spec(pkg_name) - if spec is None or spec.origin is None: - location = None - else: - # Usually .../site-packages/pkg_name/__init__.py - # So we take the parent directory of the package itself - if spec.submodule_search_locations: - location = spec.submodule_search_locations[0] - else: - import os - location = os.path.dirname(spec.origin) - - return location, version + dist = _implib_meta.distribution(pkg) + return str(dist.locate_file("")), dist.version def is_git_repo(path): From 68c7eff5691930d14a88ba8632d2004fb0da8f5d Mon Sep 17 00:00:00 2001 From: Ximenes Rocha Resende Date: Fri, 31 Oct 2025 21:09:58 -0300 Subject: [PATCH 5/7] Adapt for mersging with PR#37 --- mathphys/functions.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mathphys/functions.py b/mathphys/functions.py index 5ff8769..ed0d439 100644 --- a/mathphys/functions.py +++ b/mathphys/functions.py @@ -1,7 +1,8 @@ """Useful functions.""" import os as _os import builtins as _builtins -from collections import namedtuple as _namedtuple, Iterable as _Iterable +from collections import Iterable as _Iterable +from collections import namedtuple as _namedtuple from functools import partial as _partial import pickle as _pickle import subprocess as _subprocess From dcc359665b5c2a9b1f222eb6756297de72b7f414 Mon Sep 17 00:00:00 2001 From: Ximenes Rocha Resende Date: Mon, 3 Nov 2025 13:34:32 -0300 Subject: [PATCH 6/7] Import Iterable from collections.abc --- mathphys/functions.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/mathphys/functions.py b/mathphys/functions.py index ed0d439..29f9daa 100644 --- a/mathphys/functions.py +++ b/mathphys/functions.py @@ -1,15 +1,16 @@ """Useful functions.""" -import os as _os import builtins as _builtins -from collections import Iterable as _Iterable -from collections import namedtuple as _namedtuple -from functools import partial as _partial +import gzip as _gzip +import os as _os import pickle as _pickle import subprocess as _subprocess +from collections import namedtuple as _namedtuple +from collections.abc import Iterable as _Iterable +from functools import partial as _partial +from types import ModuleType as _ModuleType + # NOTE: Change to importlib.metadata once python3.6 is not supported anymore: import importlib_metadata as _implib_meta -from types import ModuleType as _ModuleType -import gzip as _gzip try: import h5py as _h5py @@ -20,7 +21,6 @@ import numpy as _np - def generate_random_numbers(n_part, dist_type='exp', cutoff=3): """Generate random numbers with a cutted off dist_type distribution. From 29d25c14644d274ca382bb29e4ee6a1b5310a712 Mon Sep 17 00:00:00 2001 From: Ximenes Rocha Resende Date: Mon, 3 Nov 2025 13:37:47 -0300 Subject: [PATCH 7/7] Fix bug in import --- mathphys/functions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mathphys/functions.py b/mathphys/functions.py index 29f9daa..14eadcd 100644 --- a/mathphys/functions.py +++ b/mathphys/functions.py @@ -10,7 +10,7 @@ from types import ModuleType as _ModuleType # NOTE: Change to importlib.metadata once python3.6 is not supported anymore: -import importlib_metadata as _implib_meta +import importlib.metadata as _implib_meta try: import h5py as _h5py