lkj 0.1.42__tar.gz → 0.1.43__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {lkj-0.1.42 → lkj-0.1.43}/PKG-INFO +1 -1
- {lkj-0.1.42 → lkj-0.1.43}/lkj/__init__.py +6 -1
- {lkj-0.1.42 → lkj-0.1.43}/lkj/importing.py +85 -2
- {lkj-0.1.42 → lkj-0.1.43}/lkj.egg-info/PKG-INFO +1 -1
- {lkj-0.1.42 → lkj-0.1.43}/setup.cfg +1 -1
- {lkj-0.1.42 → lkj-0.1.43}/LICENSE +0 -0
- {lkj-0.1.42 → lkj-0.1.43}/README.md +0 -0
- {lkj-0.1.42 → lkj-0.1.43}/lkj/chunking.py +0 -0
- {lkj-0.1.42 → lkj-0.1.43}/lkj/dicts.py +0 -0
- {lkj-0.1.42 → lkj-0.1.43}/lkj/filesys.py +0 -0
- {lkj-0.1.42 → lkj-0.1.43}/lkj/funcs.py +0 -0
- {lkj-0.1.42 → lkj-0.1.43}/lkj/iterables.py +0 -0
- {lkj-0.1.42 → lkj-0.1.43}/lkj/loggers.py +0 -0
- {lkj-0.1.42 → lkj-0.1.43}/lkj/misc.py +0 -0
- {lkj-0.1.42 → lkj-0.1.43}/lkj/strings.py +0 -0
- {lkj-0.1.42 → lkj-0.1.43}/lkj.egg-info/SOURCES.txt +0 -0
- {lkj-0.1.42 → lkj-0.1.43}/lkj.egg-info/dependency_links.txt +0 -0
- {lkj-0.1.42 → lkj-0.1.43}/lkj.egg-info/not-zip-safe +0 -0
- {lkj-0.1.42 → lkj-0.1.43}/lkj.egg-info/top_level.txt +0 -0
- {lkj-0.1.42 → lkj-0.1.43}/setup.py +0 -0
|
@@ -40,7 +40,12 @@ from lkj.loggers import (
|
|
|
40
40
|
wrapped_print, # Prints a string or list ensuring the total line width does not exceed `max_width`.
|
|
41
41
|
CallOnError, # Context manager that calls a function on error (subclass of suppress)
|
|
42
42
|
)
|
|
43
|
-
from lkj.importing import
|
|
43
|
+
from lkj.importing import (
|
|
44
|
+
parent_dir_of_module, # Get the parent directory of a module
|
|
45
|
+
import_from_path, # Import a module from a specified path
|
|
46
|
+
import_object, # Import an object from a module by its name
|
|
47
|
+
register_namespace_forwarding, # Register a namespace forwarding for a module
|
|
48
|
+
)
|
|
44
49
|
from lkj.chunking import chunk_iterable, chunker
|
|
45
50
|
from lkj.misc import identity, value_in_interval
|
|
46
51
|
|
|
@@ -1,6 +1,88 @@
|
|
|
1
1
|
"""Tools for importing"""
|
|
2
2
|
|
|
3
3
|
|
|
4
|
+
def parent_dir_of_module(module_obj, *, parent_levels: int = 0):
|
|
5
|
+
"""
|
|
6
|
+
Get the parent directory of a given module object.
|
|
7
|
+
:param module_obj: The module object for which to find the parent directory.
|
|
8
|
+
:return: The parent directory of the module as a string.
|
|
9
|
+
|
|
10
|
+
>>> import os # a standard library module
|
|
11
|
+
>>> parent_dir_of_module(os) # doctest: +ELLIPSIS
|
|
12
|
+
'...python...'
|
|
13
|
+
>>> import lkj # a third-party module
|
|
14
|
+
>>> parent_dir_of_module(lkj) # doctest: +ELLIPSIS
|
|
15
|
+
'.../lkj'
|
|
16
|
+
"""
|
|
17
|
+
import os
|
|
18
|
+
import inspect
|
|
19
|
+
|
|
20
|
+
# Get the file path of the module
|
|
21
|
+
module_file = inspect.getfile(module_obj)
|
|
22
|
+
|
|
23
|
+
# Get the parent directory
|
|
24
|
+
parent_dir = os.path.dirname(module_file)
|
|
25
|
+
|
|
26
|
+
if parent_levels > 0:
|
|
27
|
+
for _ in range(parent_levels):
|
|
28
|
+
parent_dir = os.path.dirname(parent_dir)
|
|
29
|
+
|
|
30
|
+
return parent_dir
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def import_from_path(
|
|
34
|
+
pkg_path: str = None, *, rootdir: str = "", insert_in_globals: bool = False
|
|
35
|
+
):
|
|
36
|
+
"""
|
|
37
|
+
Import a package from a specified path.
|
|
38
|
+
|
|
39
|
+
:param pkg_path: The path to the package to import. If None, returns a partial function.
|
|
40
|
+
:param rootdir: The root directory from which to resolve the package path.
|
|
41
|
+
:param insert_in_globals: If True, insert the imported package into the global namespace.
|
|
42
|
+
:return: The imported package or a partial function for deferred import.
|
|
43
|
+
:rtype: module or functools.partial
|
|
44
|
+
:raises ImportError: If the package cannot be found or imported.
|
|
45
|
+
|
|
46
|
+
>>> import os, inspect # standard library modules (that are surely installed)
|
|
47
|
+
>>> rootdir = parent_dir_of_module(os) # get the parent directory of os module
|
|
48
|
+
>>> os_module = import_from_path('os', rootdir=rootdir) # doctest: +ELLIPSIS
|
|
49
|
+
>>> os_module == os
|
|
50
|
+
True
|
|
51
|
+
|
|
52
|
+
>>> import lkj # a third-party module (that is surely installed)
|
|
53
|
+
>>> parent_dir_of_lkj = parent_dir_of_module(lkj, parent_levels=2) # get the great-grandma directory of lkj module
|
|
54
|
+
|
|
55
|
+
Make a partial function to import lkj from its parent directory:
|
|
56
|
+
|
|
57
|
+
>>> my_import_from_path = import_from_path(rootdir=parent_dir_of_lkj, insert_in_globals=True)
|
|
58
|
+
>>> lkj_module = my_import_from_path('lkj')
|
|
59
|
+
>>> assert 'lkj' in globals(), "Module 'lkj' should be imported into globals"
|
|
60
|
+
>>> lkj_module == lkj
|
|
61
|
+
True
|
|
62
|
+
"""
|
|
63
|
+
import sys
|
|
64
|
+
import os
|
|
65
|
+
from functools import partial
|
|
66
|
+
|
|
67
|
+
if pkg_path is None:
|
|
68
|
+
return partial(
|
|
69
|
+
import_from_path, rootdir=rootdir, insert_in_globals=insert_in_globals
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
pkg_rootdir = os.path.join(rootdir, pkg_path)
|
|
73
|
+
pkg_rootdir = os.path.abspath(pkg_rootdir)
|
|
74
|
+
|
|
75
|
+
# Add the directory to sys.path
|
|
76
|
+
if pkg_rootdir not in sys.path:
|
|
77
|
+
sys.path.insert(0, pkg_rootdir) # Use insert(0, ...) to give it high priority
|
|
78
|
+
|
|
79
|
+
pkg = __import__(pkg_path)
|
|
80
|
+
|
|
81
|
+
if insert_in_globals:
|
|
82
|
+
globals()[pkg_path] = pkg
|
|
83
|
+
return pkg
|
|
84
|
+
|
|
85
|
+
|
|
4
86
|
def import_object(dot_path: str):
|
|
5
87
|
"""Imports and returns an object from a dot string path.
|
|
6
88
|
|
|
@@ -102,10 +184,11 @@ def register_namespace_forwarding(source_base, target_base):
|
|
|
102
184
|
|
|
103
185
|
Usage:
|
|
104
186
|
|
|
105
|
-
|
|
187
|
+
If you put this code in the imbed.mdat package (say, containing a hcp module),
|
|
188
|
+
|
|
106
189
|
>>> register_namespace_forwarding('imbed.mdat', 'imbed_data_prep') # doctest: +SKIP
|
|
107
190
|
|
|
108
|
-
|
|
191
|
+
Then when you do
|
|
109
192
|
|
|
110
193
|
>>> import imbed.mdat.hcp # doctest: +SKIP
|
|
111
194
|
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|