Skip to content

Commit d06d540

Browse files
committed
fixes #218
1 parent 569a67e commit d06d540

File tree

5 files changed

+81
-20
lines changed

5 files changed

+81
-20
lines changed

fastcore/_nbdev.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@
162162
"run": "03_xtras.ipynb",
163163
"do_request": "03_xtras.ipynb",
164164
"threaded": "03_xtras.ipynb",
165+
"startthread": "03_xtras.ipynb",
165166
"start_server": "03_xtras.ipynb",
166167
"start_client": "03_xtras.ipynb",
167168
"sort_by_run": "03_xtras.ipynb",

fastcore/script.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ def _f(*args, **kwargs):
9595
mod = inspect.getmodule(inspect.currentframe().f_back)
9696
if not mod: return func(*args, **kwargs)
9797
if not SCRIPT_INFO.func and mod.__name__=="__main__": SCRIPT_INFO.func = func.__name__
98+
if len(sys.argv)>1 and sys.argv[1]=='': sys.argv.pop(1)
9899
p = anno_parser(func)
99100
args = p.parse_args().__dict__
100101
xtra = otherwise(args.pop('xtra', ''), eq(1), p.prog)

fastcore/xtras.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
__all__ = ['dict2obj', 'repr_dict', 'is_listy', 'shufflish', 'mapped', 'IterLen', 'ReindexCollection', 'open_file',
44
'save_pickle', 'load_pickle', 'maybe_open', 'image_size', 'bunzip', 'join_path_file', 'urlquote', 'urlwrap',
55
'urlopen', 'urlread', 'urljson', 'urlcheck', 'urlclean', 'urlsave', 'urlvalid', 'untar_dir', 'repo_details',
6-
'run', 'do_request', 'threaded', 'start_server', 'start_client', 'sort_by_run', 'trace', 'round_multiple',
7-
'modified_env', 'ContextManagers', 'str2bool', 'set_num_threads', 'ProcessPoolExecutor',
6+
'run', 'do_request', 'threaded', 'startthread', 'start_server', 'start_client', 'sort_by_run', 'trace',
7+
'round_multiple', 'modified_env', 'ContextManagers', 'str2bool', 'set_num_threads', 'ProcessPoolExecutor',
88
'ThreadPoolExecutor', 'parallel', 'run_procs', 'parallel_gen']
99

1010
# Cell
@@ -193,25 +193,25 @@ def urlquote(url):
193193
return urlunparse(p)
194194

195195
# Cell
196-
def urlwrap(url):
196+
def urlwrap(url, data=None, headers=None):
197197
"Wrap `url` in a urllib `Request` with a user-agent header"
198-
if not isinstance(url,Request): url = Request(urlquote(url))
198+
if not isinstance(url,Request): url = Request(urlquote(url), data=data, headers=headers or {})
199199
url.headers['User-Agent'] = _ua
200200
return url
201201

202202
# Cell
203-
def urlopen(url, data=None, **kwargs):
203+
def urlopen(url, data=None, headers=None, **kwargs):
204204
"Like `urllib.request.urlopen`, but first `urlwrap` the `url`, and encode `data`"
205205
if kwargs and not data: data=kwargs
206206
if data is not None:
207207
if not isinstance(data, (str,bytes)): data = urlencode(data)
208208
if not isinstance(data, bytes): data = data.encode('ascii')
209-
return urllib.request.urlopen(urlwrap(url))
209+
return urllib.request.urlopen(urlwrap(url, data=data, headers=headers))
210210

211211
# Cell
212-
def urlread(url, data=None, **kwargs):
212+
def urlread(url, data=None, headers=None, **kwargs):
213213
"Retrieve `url`, using `data` dict or `kwargs` to `POST` if present"
214-
with urlopen(url, data=data) as res: return res.read()
214+
with urlopen(url, data=data, headers=headers, **kwargs) as res: return res.read()
215215

216216
# Cell
217217
def urljson(url, data=None):
@@ -292,6 +292,11 @@ def _f(*args, **kwargs):
292292
return res
293293
return _f
294294

295+
# Cell
296+
def startthread(f):
297+
"Like `threaded`, but start thread immediately"
298+
threaded(f)()
299+
295300
# Cell
296301
def _socket_det(port,host,dgram):
297302
if isinstance(port,int): family,addr = socket.AF_INET,(host or socket.gethostname(),port)

nbs/03_xtras.ipynb

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@
588588
{
589589
"data": {
590590
"text/plain": [
591-
"['h', 'e', 'f', 'b', 'c', 'a', 'g', 'd']"
591+
"['c', 'h', 'f', 'e', 'g', 'b', 'd', 'a']"
592592
]
593593
},
594594
"execution_count": null,
@@ -1222,9 +1222,9 @@
12221222
"outputs": [],
12231223
"source": [
12241224
"#export\n",
1225-
"def urlwrap(url):\n",
1225+
"def urlwrap(url, data=None, headers=None):\n",
12261226
" \"Wrap `url` in a urllib `Request` with a user-agent header\"\n",
1227-
" if not isinstance(url,Request): url = Request(urlquote(url))\n",
1227+
" if not isinstance(url,Request): url = Request(urlquote(url), data=data, headers=headers or {})\n",
12281228
" url.headers['User-Agent'] = _ua\n",
12291229
" return url"
12301230
]
@@ -1236,13 +1236,44 @@
12361236
"outputs": [],
12371237
"source": [
12381238
"#export\n",
1239-
"def urlopen(url, data=None, **kwargs):\n",
1239+
"def urlopen(url, data=None, headers=None, **kwargs):\n",
12401240
" \"Like `urllib.request.urlopen`, but first `urlwrap` the `url`, and encode `data`\"\n",
12411241
" if kwargs and not data: data=kwargs\n",
12421242
" if data is not None:\n",
12431243
" if not isinstance(data, (str,bytes)): data = urlencode(data)\n",
12441244
" if not isinstance(data, bytes): data = data.encode('ascii')\n",
1245-
" return urllib.request.urlopen(urlwrap(url))"
1245+
" return urllib.request.urlopen(urlwrap(url, data=data, headers=headers))"
1246+
]
1247+
},
1248+
{
1249+
"cell_type": "code",
1250+
"execution_count": null,
1251+
"metadata": {},
1252+
"outputs": [],
1253+
"source": [
1254+
"u = urlwrap('http://localhost:6065/setup.py?a=1', data=urlencode({'foo':'bar'}), headers={'Content-Type':'application/json'})"
1255+
]
1256+
},
1257+
{
1258+
"cell_type": "code",
1259+
"execution_count": null,
1260+
"metadata": {},
1261+
"outputs": [
1262+
{
1263+
"data": {
1264+
"text/plain": [
1265+
"[('Content-type', 'application/json'),\n",
1266+
" ('User-Agent',\n",
1267+
" 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36')]"
1268+
]
1269+
},
1270+
"execution_count": null,
1271+
"metadata": {},
1272+
"output_type": "execute_result"
1273+
}
1274+
],
1275+
"source": [
1276+
"u.header_items()"
12461277
]
12471278
},
12481279
{
@@ -1252,9 +1283,9 @@
12521283
"outputs": [],
12531284
"source": [
12541285
"#export\n",
1255-
"def urlread(url, data=None, **kwargs):\n",
1286+
"def urlread(url, data=None, headers=None, **kwargs):\n",
12561287
" \"Retrieve `url`, using `data` dict or `kwargs` to `POST` if present\"\n",
1257-
" with urlopen(url, data=data) as res: return res.read()"
1288+
" with urlopen(url, data=data, headers=headers, **kwargs) as res: return res.read()"
12581289
]
12591290
},
12601291
{
@@ -1486,6 +1517,18 @@
14861517
" return _f"
14871518
]
14881519
},
1520+
{
1521+
"cell_type": "code",
1522+
"execution_count": null,
1523+
"metadata": {},
1524+
"outputs": [],
1525+
"source": [
1526+
"#export\n",
1527+
"def startthread(f):\n",
1528+
" \"Like `threaded`, but start thread immediately\"\n",
1529+
" threaded(f)()"
1530+
]
1531+
},
14891532
{
14901533
"cell_type": "markdown",
14911534
"metadata": {},
@@ -1563,13 +1606,12 @@
15631606
}
15641607
],
15651608
"source": [
1566-
"@threaded\n",
1609+
"@startthread\n",
15671610
"def _f():\n",
15681611
" with start_server(47354, 'localhost') as s:\n",
15691612
" conn,addr = s.accept()\n",
15701613
" print(conn.recv(1024))\n",
15711614
"\n",
1572-
"_f()\n",
15731615
"time.sleep(0.2) # Wait for server to start\n",
15741616
"with start_client(47354, 'localhost') as c: c.send(b\"hello\")"
15751617
]

nbs/08_script.ipynb

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,9 +291,21 @@
291291
"cell_type": "code",
292292
"execution_count": null,
293293
"metadata": {},
294-
"outputs": [],
294+
"outputs": [
295+
{
296+
"name": "stdout",
297+
"output_type": "stream",
298+
"text": [
299+
"Help on function f in module __main__:\n",
300+
"\n",
301+
"f(required: int <Required param>, a: bool_arg <param 1>, b: str <param 2> = 'test')\n",
302+
" my docs\n",
303+
"\n"
304+
]
305+
}
306+
],
295307
"source": [
296-
"f?"
308+
"help(f)"
297309
]
298310
},
299311
{
@@ -433,6 +445,7 @@
433445
" mod = inspect.getmodule(inspect.currentframe().f_back)\n",
434446
" if not mod: return func(*args, **kwargs)\n",
435447
" if not SCRIPT_INFO.func and mod.__name__==\"__main__\": SCRIPT_INFO.func = func.__name__\n",
448+
" if len(sys.argv)>1 and sys.argv[1]=='': sys.argv.pop(1)\n",
436449
" p = anno_parser(func)\n",
437450
" args = p.parse_args().__dict__\n",
438451
" xtra = otherwise(args.pop('xtra', ''), eq(1), p.prog)\n",
@@ -501,7 +514,6 @@
501514
"Converted 03_xtras.ipynb.\n",
502515
"Converted 04_dispatch.ipynb.\n",
503516
"Converted 05_transform.ipynb.\n",
504-
"Converted 06_logargs.ipynb.\n",
505517
"Converted 07_meta.ipynb.\n",
506518
"Converted 08_script.ipynb.\n",
507519
"Converted index.ipynb.\n"

0 commit comments

Comments
 (0)