Skip to content

Commit

Permalink
fixes #125
Browse files Browse the repository at this point in the history
  • Loading branch information
jph00 committed Oct 12, 2020
1 parent c0c419a commit 1b96df6
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 17 deletions.
2 changes: 1 addition & 1 deletion fastcore/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.1.1"
__version__ = "1.1.2"
1 change: 1 addition & 0 deletions fastcore/_nbdev.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@
"defaults.cpus": "02_utils.ipynb",
"add_props": "02_utils.ipynb",
"ContextManagers": "02_utils.ipynb",
"typed": "02_utils.ipynb",
"set_num_threads": "02_utils.ipynb",
"ProcessPoolExecutor": "02_utils.ipynb",
"ThreadPoolExecutor": "02_utils.ipynb",
Expand Down
8 changes: 4 additions & 4 deletions fastcore/foundation.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,15 +422,15 @@ def setattrs(self, attr, val): [setattr(o,attr,val) for o in self]
Sequence.register(L);

# Cell
def save_config_file(file, d):
def save_config_file(file, d, **kwargs):
"Write settings dict to a new config file, or overwrite the existing one."
config = ConfigParser()
config = ConfigParser(**kwargs)
config['DEFAULT'] = d
config.write(open(file, 'w'))

# Cell
def read_config_file(file):
config = ConfigParser()
def read_config_file(file, **kwargs):
config = ConfigParser(**kwargs)
config.read(file)
return config

Expand Down
21 changes: 19 additions & 2 deletions fastcore/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
'inum_methods', 'fastuple', 'trace', 'compose', 'maps', 'partialler', 'mapped', 'instantiate', 'using_attr',
'Self', 'Self', 'save_pickle', 'load_pickle', 'bunzip', 'join_path_file', 'urlread', 'urljson', 'run',
'do_request', 'sort_by_run', 'PrettyString', 'round_multiple', 'even_mults', 'num_cpus', 'add_props',
'ContextManagers', 'set_num_threads', 'ProcessPoolExecutor', 'ThreadPoolExecutor', 'parallel', 'run_procs',
'parallel_gen', 'threaded']
'ContextManagers', 'typed', 'set_num_threads', 'ProcessPoolExecutor', 'ThreadPoolExecutor', 'parallel',
'run_procs', 'parallel_gen', 'threaded']

# Cell
from .imports import *
Expand Down Expand Up @@ -675,6 +675,23 @@ def __init__(self, mgrs): self.default,self.stack = L(mgrs),ExitStack()
def __enter__(self): self.default.map(self.stack.enter_context)
def __exit__(self, *args, **kwargs): self.stack.__exit__(*args, **kwargs)

# Cell
def typed(f):
"Decorator to check param and return types at runtime"
names = f.__code__.co_varnames
anno = f.__annotations__
ret = anno.pop('return',None)
def _f(*args,**kwargs):
kw = {**kwargs}
if len(anno) > 0:
for i,arg in enumerate(args): kw[names[i]] = arg
for k,v in kw.items():
if not isinstance(v,anno[k]): raise TypeError(f"{k}=={v} not {anno[k]}")
res = f(*args,**kwargs)
if ret is not None and not isinstance(res,ret): raise TypeError(f"return=={res} not {ret}")
return res
return functools.update_wrapper(_f, f)

# Cell
from multiprocessing import Process, Queue
import concurrent.futures
Expand Down
12 changes: 6 additions & 6 deletions nbs/01_foundation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1964,7 +1964,7 @@
{
"data": {
"text/plain": [
"['k', 4, 'j']"
"[0, 9, 1]"
]
},
"execution_count": null,
Expand Down Expand Up @@ -2143,7 +2143,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### Class L Methods"
"### `L` Methods"
]
},
{
Expand Down Expand Up @@ -3036,9 +3036,9 @@
"outputs": [],
"source": [
"#export\n",
"def save_config_file(file, d):\n",
"def save_config_file(file, d, **kwargs):\n",
" \"Write settings dict to a new config file, or overwrite the existing one.\"\n",
" config = ConfigParser()\n",
" config = ConfigParser(**kwargs)\n",
" config['DEFAULT'] = d\n",
" config.write(open(file, 'w'))"
]
Expand All @@ -3050,8 +3050,8 @@
"outputs": [],
"source": [
"#export\n",
"def read_config_file(file):\n",
" config = ConfigParser()\n",
"def read_config_file(file, **kwargs):\n",
" config = ConfigParser(**kwargs)\n",
" config.read(file)\n",
" return config"
]
Expand Down
50 changes: 47 additions & 3 deletions nbs/02_utils.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@
{
"data": {
"text/plain": [
"<__main__._t at 0x7ff9a9c3deb0>"
"<__main__._t at 0x7f945b39e070>"
]
},
"execution_count": null,
Expand Down Expand Up @@ -2171,7 +2171,7 @@
{
"data": {
"text/plain": [
"['g', 'd', 'h', 'a', 'f', 'c', 'b', 'e']"
"['h', 'g', 'f', 'b', 'd', 'e', 'a', 'c']"
]
},
"execution_count": null,
Expand Down Expand Up @@ -3513,7 +3513,7 @@
{
"data": {
"text/plain": [
"64"
"8"
]
},
"execution_count": null,
Expand Down Expand Up @@ -3621,6 +3621,50 @@
"show_doc(ContextManagers, title_level=4)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#export\n",
"def typed(f):\n",
" \"Decorator to check param and return types at runtime\"\n",
" names = f.__code__.co_varnames\n",
" anno = f.__annotations__\n",
" ret = anno.pop('return',None)\n",
" def _f(*args,**kwargs):\n",
" kw = {**kwargs}\n",
" if len(anno) > 0:\n",
" for i,arg in enumerate(args): kw[names[i]] = arg\n",
" for k,v in kw.items():\n",
" if not isinstance(v,anno[k]): raise TypeError(f\"{k}=={v} not {anno[k]}\")\n",
" res = f(*args,**kwargs)\n",
" if ret is not None and not isinstance(res,ret): raise TypeError(f\"return=={res} not {ret}\")\n",
" return res\n",
" return functools.update_wrapper(_f, f)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"@typed\n",
"def foo(a:int, b:str='a'): return a\n",
"test_eq(foo(1, '2'), 1)\n",
"test_fail(partial(foo, 1, 2))\n",
"\n",
"@typed\n",
"def foo()->str: return 1\n",
"test_fail(partial(foo))\n",
"\n",
"@typed\n",
"def foo()->str: return '1'\n",
"assert foo()"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down
2 changes: 1 addition & 1 deletion settings.ini
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ author = Jeremy Howard and Sylvain Gugger
author_email = [email protected]
copyright = fast.ai
branch = master
version = 1.1.1
version = 1.1.2
min_python = 3.6
audience = Developers
language = English
Expand Down

0 comments on commit 1b96df6

Please sign in to comment.