|
6 | 6 | __all__ = ['spark_chars', 'UNSET', 'walk_join', 'walk', 'exttypes', 'globtastic', 'pglob', 'maybe_open', 'mkdir', 'image_size', |
7 | 7 | 'img_bytes', 'detect_mime', 'bunzip', 'loads', 'loads_multi', 'dumps', 'untar_dir', 'repo_details', 'shell', |
8 | 8 | 'ssh', 'rsync_multi', 'run', 'open_file', 'save_pickle', 'load_pickle', 'parse_env', 'expand_wildcards', |
9 | | - 'atomic_save', 'import_no_init', 'dict2obj', 'obj2dict', 'repr_dict', 'is_listy', 'mapped', 'IterLen', |
10 | | - 'ReindexCollection', 'SaveReturn', 'trim_wraps', 'save_iter', 'asave_iter', 'frontmatter', |
| 9 | + 'atomic_save', 'load_mod', 'import_no_init', 'dict2obj', 'obj2dict', 'repr_dict', 'is_listy', 'mapped', |
| 10 | + 'IterLen', 'ReindexCollection', 'SaveReturn', 'trim_wraps', 'save_iter', 'asave_iter', 'frontmatter', |
11 | 11 | 'clean_cli_output', 'unqid', 'rtoken_hex', 'friendly_name', 'n_friendly_names', 'exec_eval', |
12 | 12 | 'get_source_link', 'sparkline', 'modify_exception', 'round_multiple', 'set_num_threads', 'join_path_file', |
13 | 13 | 'autostart', 'EventTimer', 'stringfmt_names', 'PartialFormatter', 'partial_format', 'truncstr', 'utc2local', |
@@ -429,19 +429,23 @@ def atomic_save(fn, mode='wb', uid=-1, gid=-1, **kwargs): |
429 | 429 | Path(f.name).rename(fn) |
430 | 430 |
|
431 | 431 |
|
| 432 | +# %% ../nbs/03_xtras.ipynb #52746e0e |
| 433 | +def load_mod(name, path): |
| 434 | + "Load module `name` from file `path`" |
| 435 | + import importlib.util as iu |
| 436 | + spec = iu.spec_from_file_location(name, Path(path)) |
| 437 | + mod = iu.module_from_spec(spec) |
| 438 | + spec.loader.exec_module(mod) |
| 439 | + return mod |
| 440 | + |
432 | 441 | # %% ../nbs/03_xtras.ipynb #68d60335 |
433 | 442 | def import_no_init(name): |
434 | 443 | "Import dotted `name` without running any `__init__.py`" |
435 | | - from importlib.machinery import PathFinder |
436 | 444 | import importlib.util as iu |
437 | 445 | parts = name.split('.') |
438 | | - spec = PathFinder.find_spec(parts[0]) or iu.find_spec(parts[0]) |
439 | | - path = Path(spec.origin) |
| 446 | + path = Path(iu.find_spec(parts[0]).origin) |
440 | 447 | if len(parts)>1: path = path.parent.joinpath(*parts[1:]).with_suffix('.py') |
441 | | - spec = iu.spec_from_file_location(name, path) |
442 | | - mod = iu.module_from_spec(spec) |
443 | | - spec.loader.exec_module(mod) |
444 | | - return mod |
| 448 | + return load_mod(name, path) |
445 | 449 |
|
446 | 450 | # %% ../nbs/03_xtras.ipynb #9579358d |
447 | 451 | def dict2obj(d=None, list_func=L, dict_func=AttrDict, **kwargs): |
|
0 commit comments