Skip to content

Commit 598242e

Browse files
committed
fixes #818
1 parent 5172e76 commit 598242e

3 files changed

Lines changed: 93 additions & 16 deletions

File tree

fastcore/_modidx.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,7 @@
829829
'fastcore.xtras.is_namedtuple': ('xtras.html#is_namedtuple', 'fastcore/xtras.py'),
830830
'fastcore.xtras.is_typeddict': ('xtras.html#is_typeddict', 'fastcore/xtras.py'),
831831
'fastcore.xtras.join_path_file': ('xtras.html#join_path_file', 'fastcore/xtras.py'),
832+
'fastcore.xtras.load_mod': ('xtras.html#load_mod', 'fastcore/xtras.py'),
832833
'fastcore.xtras.load_pickle': ('xtras.html#load_pickle', 'fastcore/xtras.py'),
833834
'fastcore.xtras.loads': ('xtras.html#loads', 'fastcore/xtras.py'),
834835
'fastcore.xtras.loads_multi': ('xtras.html#loads_multi', 'fastcore/xtras.py'),

fastcore/xtras.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
__all__ = ['spark_chars', 'UNSET', 'walk_join', 'walk', 'exttypes', 'globtastic', 'pglob', 'maybe_open', 'mkdir', 'image_size',
77
'img_bytes', 'detect_mime', 'bunzip', 'loads', 'loads_multi', 'dumps', 'untar_dir', 'repo_details', 'shell',
88
'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',
1111
'clean_cli_output', 'unqid', 'rtoken_hex', 'friendly_name', 'n_friendly_names', 'exec_eval',
1212
'get_source_link', 'sparkline', 'modify_exception', 'round_multiple', 'set_num_threads', 'join_path_file',
1313
'autostart', 'EventTimer', 'stringfmt_names', 'PartialFormatter', 'partial_format', 'truncstr', 'utc2local',
@@ -429,19 +429,23 @@ def atomic_save(fn, mode='wb', uid=-1, gid=-1, **kwargs):
429429
Path(f.name).rename(fn)
430430

431431

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+
432441
# %% ../nbs/03_xtras.ipynb #68d60335
433442
def import_no_init(name):
434443
"Import dotted `name` without running any `__init__.py`"
435-
from importlib.machinery import PathFinder
436444
import importlib.util as iu
437445
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)
440447
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)
445449

446450
# %% ../nbs/03_xtras.ipynb #9579358d
447451
def dict2obj(d=None, list_func=L, dict_func=AttrDict, **kwargs):

nbs/03_xtras.ipynb

Lines changed: 79 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,6 +1402,83 @@
14021402
" test_eq(fn.read_text(), 'new')"
14031403
]
14041404
},
1405+
{
1406+
"cell_type": "code",
1407+
"execution_count": null,
1408+
"id": "52746e0e",
1409+
"metadata": {},
1410+
"outputs": [],
1411+
"source": [
1412+
"#| export\n",
1413+
"def load_mod(name, path):\n",
1414+
" \"Load module `name` from file `path`\"\n",
1415+
" import importlib.util as iu\n",
1416+
" spec = iu.spec_from_file_location(name, Path(path))\n",
1417+
" mod = iu.module_from_spec(spec)\n",
1418+
" spec.loader.exec_module(mod)\n",
1419+
" return mod"
1420+
]
1421+
},
1422+
{
1423+
"cell_type": "markdown",
1424+
"id": "758e316b",
1425+
"metadata": {},
1426+
"source": [
1427+
"We can get a module spec from a module name:"
1428+
]
1429+
},
1430+
{
1431+
"cell_type": "code",
1432+
"execution_count": null,
1433+
"id": "3ebd34ef",
1434+
"metadata": {},
1435+
"outputs": [],
1436+
"source": [
1437+
"import importlib.util"
1438+
]
1439+
},
1440+
{
1441+
"cell_type": "code",
1442+
"execution_count": null,
1443+
"id": "55b97916",
1444+
"metadata": {},
1445+
"outputs": [
1446+
{
1447+
"data": {
1448+
"text/plain": [
1449+
"ModuleSpec(name='fastcore.basics', loader=<_frozen_importlib_external.SourceFileLoader object>, origin='/Users/jhoward/aai-ws/fastcore/fastcore/basics.py')"
1450+
]
1451+
},
1452+
"execution_count": null,
1453+
"metadata": {},
1454+
"output_type": "execute_result"
1455+
}
1456+
],
1457+
"source": [
1458+
"spec = importlib.util.find_spec('fastcore.basics')\n",
1459+
"spec"
1460+
]
1461+
},
1462+
{
1463+
"cell_type": "markdown",
1464+
"id": "c3878e64",
1465+
"metadata": {},
1466+
"source": [
1467+
"...then we can load it, using the origin path:"
1468+
]
1469+
},
1470+
{
1471+
"cell_type": "code",
1472+
"execution_count": null,
1473+
"id": "71e4473f",
1474+
"metadata": {},
1475+
"outputs": [],
1476+
"source": [
1477+
"m = load_mod('fastcore.basics', spec.origin)\n",
1478+
"test_eq(m.__name__, 'fastcore.basics')\n",
1479+
"assert hasattr(m, 'store_attr')"
1480+
]
1481+
},
14051482
{
14061483
"cell_type": "code",
14071484
"execution_count": null,
@@ -1412,16 +1489,11 @@
14121489
"#| export\n",
14131490
"def import_no_init(name):\n",
14141491
" \"Import dotted `name` without running any `__init__.py`\"\n",
1415-
" from importlib.machinery import PathFinder\n",
14161492
" import importlib.util as iu\n",
14171493
" parts = name.split('.')\n",
1418-
" spec = PathFinder.find_spec(parts[0]) or iu.find_spec(parts[0])\n",
1419-
" path = Path(spec.origin)\n",
1494+
" path = Path(iu.find_spec(parts[0]).origin)\n",
14201495
" if len(parts)>1: path = path.parent.joinpath(*parts[1:]).with_suffix('.py')\n",
1421-
" spec = iu.spec_from_file_location(name, path)\n",
1422-
" mod = iu.module_from_spec(spec)\n",
1423-
" spec.loader.exec_module(mod)\n",
1424-
" return mod"
1496+
" return load_mod(name, path)"
14251497
]
14261498
},
14271499
{

0 commit comments

Comments
 (0)