Skip to content

Commit 44fe982

Browse files
committed
exec_import
1 parent a180dd6 commit 44fe982

File tree

3 files changed

+49
-18
lines changed

3 files changed

+49
-18
lines changed

fastcore/_nbdev.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@
133133
"add_props": "01_basics.ipynb",
134134
"typed": "01_basics.ipynb",
135135
"exec_new": "01_basics.ipynb",
136+
"exec_import": "01_basics.ipynb",
136137
"working_directory": "02_foundation.ipynb",
137138
"add_docs": "02_foundation.ipynb",
138139
"docs": "02_foundation.ipynb",

fastcore/basics.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
'arg0', 'arg1', 'arg2', 'arg3', 'arg4', 'bind', 'mapt', 'map_ex', 'compose', 'maps', 'partialler',
1515
'instantiate', 'using_attr', 'Self', 'Self', 'copy_func', 'patch_to', 'patch', 'patch_property',
1616
'compile_re', 'ImportEnum', 'StrEnum', 'str_enum', 'Stateful', 'PrettyString', 'even_mults', 'num_cpus',
17-
'add_props', 'typed', 'exec_new']
17+
'add_props', 'typed', 'exec_new', 'exec_import']
1818

1919
# Cell
2020
from .imports import *
@@ -998,6 +998,13 @@ def _f(*args,**kwargs):
998998
# Cell
999999
def exec_new(code):
10001000
"Execute `code` in a new environment and return it"
1001-
g = {}
1001+
pkg = None if __name__=='__main__' else Path().cwd().name
1002+
g = {'__name__': __name__, '__package__': pkg}
10021003
exec(code, g)
1003-
return g
1004+
return g
1005+
1006+
# Cell
1007+
def exec_import(mod, sym):
1008+
"Import `sym` from `mod` in a new environment"
1009+
pref = '' if __name__=='__main__' or mod[0]=='.' else '.'
1010+
return exec_new(f'from {pref}{mod} import {sym}')

nbs/01_basics.ipynb

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5492,7 +5492,8 @@
54925492
"#export\n",
54935493
"def exec_new(code):\n",
54945494
" \"Execute `code` in a new environment and return it\"\n",
5495-
" g = {}\n",
5495+
" pkg = None if __name__=='__main__' else Path().cwd().name\n",
5496+
" g = {'__name__': __name__, '__package__': pkg}\n",
54965497
" exec(code, g)\n",
54975498
" return g"
54985499
]
@@ -5501,21 +5502,23 @@
55015502
"cell_type": "code",
55025503
"execution_count": null,
55035504
"metadata": {},
5504-
"outputs": [
5505-
{
5506-
"data": {
5507-
"text/plain": [
5508-
"1"
5509-
]
5510-
},
5511-
"execution_count": null,
5512-
"metadata": {},
5513-
"output_type": "execute_result"
5514-
}
5515-
],
5505+
"outputs": [],
55165506
"source": [
55175507
"g = exec_new('a=1')\n",
5518-
"g['a']"
5508+
"test_eq(g['a'], 1)"
5509+
]
5510+
},
5511+
{
5512+
"cell_type": "code",
5513+
"execution_count": null,
5514+
"metadata": {},
5515+
"outputs": [],
5516+
"source": [
5517+
"#export\n",
5518+
"def exec_import(mod, sym):\n",
5519+
" \"Import `sym` from `mod` in a new environment\"\n",
5520+
" pref = '' if __name__=='__main__' or mod[0]=='.' else '.'\n",
5521+
" return exec_new(f'from {pref}{mod} import {sym}')"
55195522
]
55205523
},
55215524
{
@@ -5693,7 +5696,27 @@
56935696
"cell_type": "code",
56945697
"execution_count": null,
56955698
"metadata": {},
5696-
"outputs": [],
5699+
"outputs": [
5700+
{
5701+
"name": "stdout",
5702+
"output_type": "stream",
5703+
"text": [
5704+
"Converted 00_test.ipynb.\n",
5705+
"Converted 01_basics.ipynb.\n",
5706+
"Converted 02_foundation.ipynb.\n",
5707+
"Converted 03_xtras.ipynb.\n",
5708+
"Converted 03a_parallel.ipynb.\n",
5709+
"Converted 03b_net.ipynb.\n",
5710+
"Converted 04_dispatch.ipynb.\n",
5711+
"Converted 05_transform.ipynb.\n",
5712+
"Converted 06_docments.ipynb.\n",
5713+
"Converted 07_meta.ipynb.\n",
5714+
"Converted 08_script.ipynb.\n",
5715+
"Converted index.ipynb.\n",
5716+
"Converted parallel_win.ipynb.\n"
5717+
]
5718+
}
5719+
],
56975720
"source": [
56985721
"#hide\n",
56995722
"from nbdev.export import notebook2script\n",

0 commit comments

Comments
 (0)