Skip to content

Commit

Permalink
fixes #124
Browse files Browse the repository at this point in the history
  • Loading branch information
jph00 committed Oct 11, 2020
1 parent 05641ae commit b57476b
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 24 deletions.
1 change: 1 addition & 0 deletions fastcore/_nbdev.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@
"parallel": "02_utils.ipynb",
"run_procs": "02_utils.ipynb",
"parallel_gen": "02_utils.ipynb",
"threaded": "02_utils.ipynb",
"type_hints": "03_dispatch.ipynb",
"anno_ret": "03_dispatch.ipynb",
"sorted_topologically": "03_dispatch.ipynb",
Expand Down
4 changes: 2 additions & 2 deletions fastcore/foundation.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ class Config:
def __init__(self, cfg_name='settings.ini'):
cfg_path = Path.cwd()
while cfg_path != cfg_path.parent and not (cfg_path/cfg_name).exists(): cfg_path = cfg_path.parent
self.config_file = cfg_path/cfg_name
self.config_path,self.config_file = cfg_path,cfg_path/cfg_name
assert self.config_file.exists(), f"Could not find {cfg_name}"
self.d = read_config_file(self.config_file)['DEFAULT']
_add_new_defaults(self.d, self.config_file,
Expand All @@ -462,4 +462,4 @@ def __getattr__(self,k): return stop(AttributeError(k)) if k=='d' or k not in
def get(self,k,default=None):
v = self.d.get(k, default)
if v is None: return v
return self.config_file.parent/v if k.endswith('_path') else v
return self.config_path/v if k.endswith('_path') else v
31 changes: 23 additions & 8 deletions fastcore/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
'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']
'parallel_gen', 'threaded']

# Cell
from .imports import *
Expand All @@ -23,6 +23,7 @@
from urllib.request import Request,urlopen
from urllib.error import HTTPError
from urllib.parse import urlencode
from threading import Thread

# Cell
def ifnone(a, b):
Expand Down Expand Up @@ -570,18 +571,22 @@ def join_path_file(file, path, ext=''):
return path/f'{file}{ext}'

# Cell
def urlread(url):
"Retrieve `url`"
with urlopen(url) as res: return res.read()
def urlread(url, data=None, **kwargs):
"Retrieve `url`, using `data` dict or `kwargs` to `POST` if present"
if kwargs and not data: data=kwargs
if data is not None:
if not isinstance(data, (str,bytes)): data = urlencode(data)
if not isinstance(data, bytes): data = data.encode('ascii')
with urlopen(url, data=data) as res: return res.read()

# Cell
def urljson(url):
def urljson(url, data=None):
"Retrieve `url` and decode json"
return json.loads(urlread(url))
return json.loads(urlread(url, data=data))

# Cell
def run(cmd, *rest, ignore_ex=False, as_bytes=False):
"Pass `cmd` (splitting with `shlex` if string) to `subprocess.run`, returning `stdout`, or raise `IOError` on failure"
"Pass `cmd` (splitting with `shlex` if string) to `subprocess.run`; return `stdout`; raise `IOError` if fails"
if rest: cmd = (cmd,)+rest
elif isinstance(cmd,str): cmd = shlex.split(cmd)
res = subprocess.run(cmd, capture_output=True)
Expand Down Expand Up @@ -777,4 +782,14 @@ def parallel_gen(cls, items, n_workers=defaults.cpus, **kwargs):
if progress_bar: items = progress_bar(items, leave=False)
f=partial(_f_pg, cls(**kwargs), queue)
done=partial(_done_pg, queue, items)
yield from run_procs(f, done, L(batches,idx).zip())
yield from run_procs(f, done, L(batches,idx).zip())

# Cell
def threaded(f):
"Run `f` in a thread, and returns the thread"
@wraps(f)
def _f(*args, **kwargs):
res = Thread(target=f, args=args, kwargs=kwargs)
res.start()
return res
return _f
4 changes: 2 additions & 2 deletions nbs/01_foundation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -3104,7 +3104,7 @@
" def __init__(self, cfg_name='settings.ini'):\n",
" cfg_path = Path.cwd()\n",
" while cfg_path != cfg_path.parent and not (cfg_path/cfg_name).exists(): cfg_path = cfg_path.parent\n",
" self.config_file = cfg_path/cfg_name\n",
" self.config_path,self.config_file = cfg_path,cfg_path/cfg_name\n",
" assert self.config_file.exists(), f\"Could not find {cfg_name}\"\n",
" self.d = read_config_file(self.config_file)['DEFAULT']\n",
" _add_new_defaults(self.d, self.config_file,\n",
Expand All @@ -3118,7 +3118,7 @@
" def get(self,k,default=None):\n",
" v = self.d.get(k, default)\n",
" if v is None: return v\n",
" return self.config_file.parent/v if k.endswith('_path') else v"
" return self.config_path/v if k.endswith('_path') else v"
]
},
{
Expand Down
44 changes: 33 additions & 11 deletions nbs/02_utils.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"from pdb import set_trace\n",
"from urllib.request import Request,urlopen\n",
"from urllib.error import HTTPError\n",
"from urllib.parse import urlencode"
"from urllib.parse import urlencode\n",
"from threading import Thread"
]
},
{
Expand Down Expand Up @@ -295,7 +296,7 @@
{
"data": {
"text/plain": [
"<__main__._t at 0x7fbd249d4100>"
"<__main__._t at 0x7ff9a9c3deb0>"
]
},
"execution_count": null,
Expand Down Expand Up @@ -2170,7 +2171,7 @@
{
"data": {
"text/plain": [
"['c', 'h', 'g', 'd', 'f', 'e', 'a', 'b']"
"['g', 'd', 'h', 'a', 'f', 'c', 'b', 'e']"
]
},
"execution_count": null,
Expand Down Expand Up @@ -2929,7 +2930,7 @@
{
"data": {
"text/plain": [
"Path('.ipynb_checkpoints')"
"Path('.gitattributes')"
]
},
"execution_count": null,
Expand Down Expand Up @@ -2963,7 +2964,7 @@
{
"data": {
"text/plain": [
"(Path('../fastcore/logargs.py'), Path('04_transform.ipynb'))"
"(Path('../fastcore/all.py'), Path('00_test.ipynb'))"
]
},
"execution_count": null,
Expand Down Expand Up @@ -3147,9 +3148,13 @@
"outputs": [],
"source": [
"#export\n",
"def urlread(url):\n",
" \"Retrieve `url`\"\n",
" with urlopen(url) as res: return res.read()"
"def urlread(url, data=None, **kwargs):\n",
" \"Retrieve `url`, using `data` dict or `kwargs` to `POST` if present\"\n",
" if kwargs and not data: data=kwargs\n",
" if data is not None:\n",
" if not isinstance(data, (str,bytes)): data = urlencode(data)\n",
" if not isinstance(data, bytes): data = data.encode('ascii')\n",
" with urlopen(url, data=data) as res: return res.read()"
]
},
{
Expand All @@ -3159,9 +3164,9 @@
"outputs": [],
"source": [
"#export\n",
"def urljson(url):\n",
"def urljson(url, data=None):\n",
" \"Retrieve `url` and decode json\"\n",
" return json.loads(urlread(url))"
" return json.loads(urlread(url, data=data))"
]
},
{
Expand All @@ -3172,7 +3177,7 @@
"source": [
"#export\n",
"def run(cmd, *rest, ignore_ex=False, as_bytes=False):\n",
" \"Pass `cmd` (splitting with `shlex` if string) to `subprocess.run`, returning `stdout`, or raise `IOError` on failure\"\n",
" \"Pass `cmd` (splitting with `shlex` if string) to `subprocess.run`; return `stdout`; raise `IOError` if fails\"\n",
" if rest: cmd = (cmd,)+rest\n",
" elif isinstance(cmd,str): cmd = shlex.split(cmd)\n",
" res = subprocess.run(cmd, capture_output=True)\n",
Expand Down Expand Up @@ -4013,6 +4018,23 @@
"test_eq(res.sorted().itemgot(1), x+1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#export\n",
"def threaded(f):\n",
" \"Run `f` in a thread, and returns the thread\"\n",
" @wraps(f)\n",
" def _f(*args, **kwargs):\n",
" res = Thread(target=f, args=args, kwargs=kwargs)\n",
" res.start()\n",
" return res\n",
" return _f"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down
5 changes: 4 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@
long_description = long_description,
long_description_content_type = 'text/markdown',
zip_safe = False,
entry_points = { 'console_scripts': cfg.get('console_scripts','').split() },
entry_points = {
'console_scripts': cfg.get('console_scripts','').split(),
'nbdev': [f'index={cfg["lib_name"]}._nbdev']
},
**setup_cfg)

0 comments on commit b57476b

Please sign in to comment.