Skip to content

Commit

Permalink
fixes #350
Browse files Browse the repository at this point in the history
  • Loading branch information
jph00 committed Aug 11, 2021
1 parent 0cbe37e commit 05a7549
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
1 change: 1 addition & 0 deletions fastcore/_nbdev.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
"defaults.cpus": "01_basics.ipynb",
"add_props": "01_basics.ipynb",
"typed": "01_basics.ipynb",
"exec_new": "01_basics.ipynb",
"working_directory": "02_foundation.ipynb",
"add_docs": "02_foundation.ipynb",
"docs": "02_foundation.ipynb",
Expand Down
11 changes: 9 additions & 2 deletions fastcore/basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
'nested_idx', 'val2idx', 'uniqueify', 'num_methods', 'rnum_methods', 'inum_methods', 'fastuple', 'arg0',
'arg1', 'arg2', 'arg3', 'arg4', 'bind', 'mapt', 'map_ex', 'compose', 'maps', 'partialler', 'instantiate',
'using_attr', 'Self', 'Self', 'copy_func', 'patch_to', 'patch', 'patch_property', 'ImportEnum', 'StrEnum',
'str_enum', 'Stateful', 'PrettyString', 'even_mults', 'num_cpus', 'add_props', 'typed']
'str_enum', 'Stateful', 'PrettyString', 'even_mults', 'num_cpus', 'add_props', 'typed', 'exec_new']

# Cell
from .imports import *
Expand Down Expand Up @@ -875,4 +875,11 @@ def _f(*args,**kwargs):
res = f(*args,**kwargs)
if ret is not None and not isinstance(res,ret): raise _typeerr("return", res, ret)
return res
return functools.update_wrapper(_f, f)
return functools.update_wrapper(_f, f)

# Cell
def exec_new(code):
"Execute `code` in a new environment and return it"
g = {}
exec(code, g)
return g
35 changes: 35 additions & 0 deletions nbs/01_basics.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -5137,6 +5137,41 @@
"with ExceptionExpected(TypeError): Foo(1,2, 'a string').test(10)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#export\n",
"def exec_new(code):\n",
" \"Execute `code` in a new environment and return it\"\n",
" g = {}\n",
" exec(code, g)\n",
" return g"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"g = exec_new('a=1')\n",
"g['a']"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down

0 comments on commit 05a7549

Please # to comment.