|
25 | 25 | "from pdb import set_trace\n", |
26 | 26 | "from urllib.request import Request,urlopen\n", |
27 | 27 | "from urllib.error import HTTPError\n", |
28 | | - "from urllib.parse import urlencode" |
| 28 | + "from urllib.parse import urlencode\n", |
| 29 | + "from threading import Thread" |
29 | 30 | ] |
30 | 31 | }, |
31 | 32 | { |
|
295 | 296 | { |
296 | 297 | "data": { |
297 | 298 | "text/plain": [ |
298 | | - "<__main__._t at 0x7fbd249d4100>" |
| 299 | + "<__main__._t at 0x7ff9a9c3deb0>" |
299 | 300 | ] |
300 | 301 | }, |
301 | 302 | "execution_count": null, |
|
2170 | 2171 | { |
2171 | 2172 | "data": { |
2172 | 2173 | "text/plain": [ |
2173 | | - "['c', 'h', 'g', 'd', 'f', 'e', 'a', 'b']" |
| 2174 | + "['g', 'd', 'h', 'a', 'f', 'c', 'b', 'e']" |
2174 | 2175 | ] |
2175 | 2176 | }, |
2176 | 2177 | "execution_count": null, |
|
2929 | 2930 | { |
2930 | 2931 | "data": { |
2931 | 2932 | "text/plain": [ |
2932 | | - "Path('.ipynb_checkpoints')" |
| 2933 | + "Path('.gitattributes')" |
2933 | 2934 | ] |
2934 | 2935 | }, |
2935 | 2936 | "execution_count": null, |
|
2963 | 2964 | { |
2964 | 2965 | "data": { |
2965 | 2966 | "text/plain": [ |
2966 | | - "(Path('../fastcore/logargs.py'), Path('04_transform.ipynb'))" |
| 2967 | + "(Path('../fastcore/all.py'), Path('00_test.ipynb'))" |
2967 | 2968 | ] |
2968 | 2969 | }, |
2969 | 2970 | "execution_count": null, |
|
3147 | 3148 | "outputs": [], |
3148 | 3149 | "source": [ |
3149 | 3150 | "#export\n", |
3150 | | - "def urlread(url):\n", |
3151 | | - " \"Retrieve `url`\"\n", |
3152 | | - " with urlopen(url) as res: return res.read()" |
| 3151 | + "def urlread(url, data=None, **kwargs):\n", |
| 3152 | + " \"Retrieve `url`, using `data` dict or `kwargs` to `POST` if present\"\n", |
| 3153 | + " if kwargs and not data: data=kwargs\n", |
| 3154 | + " if data is not None:\n", |
| 3155 | + " if not isinstance(data, (str,bytes)): data = urlencode(data)\n", |
| 3156 | + " if not isinstance(data, bytes): data = data.encode('ascii')\n", |
| 3157 | + " with urlopen(url, data=data) as res: return res.read()" |
3153 | 3158 | ] |
3154 | 3159 | }, |
3155 | 3160 | { |
|
3159 | 3164 | "outputs": [], |
3160 | 3165 | "source": [ |
3161 | 3166 | "#export\n", |
3162 | | - "def urljson(url):\n", |
| 3167 | + "def urljson(url, data=None):\n", |
3163 | 3168 | " \"Retrieve `url` and decode json\"\n", |
3164 | | - " return json.loads(urlread(url))" |
| 3169 | + " return json.loads(urlread(url, data=data))" |
3165 | 3170 | ] |
3166 | 3171 | }, |
3167 | 3172 | { |
|
3172 | 3177 | "source": [ |
3173 | 3178 | "#export\n", |
3174 | 3179 | "def run(cmd, *rest, ignore_ex=False, as_bytes=False):\n", |
3175 | | - " \"Pass `cmd` (splitting with `shlex` if string) to `subprocess.run`, returning `stdout`, or raise `IOError` on failure\"\n", |
| 3180 | + " \"Pass `cmd` (splitting with `shlex` if string) to `subprocess.run`; return `stdout`; raise `IOError` if fails\"\n", |
3176 | 3181 | " if rest: cmd = (cmd,)+rest\n", |
3177 | 3182 | " elif isinstance(cmd,str): cmd = shlex.split(cmd)\n", |
3178 | 3183 | " res = subprocess.run(cmd, capture_output=True)\n", |
|
4013 | 4018 | "test_eq(res.sorted().itemgot(1), x+1)" |
4014 | 4019 | ] |
4015 | 4020 | }, |
| 4021 | + { |
| 4022 | + "cell_type": "code", |
| 4023 | + "execution_count": null, |
| 4024 | + "metadata": {}, |
| 4025 | + "outputs": [], |
| 4026 | + "source": [ |
| 4027 | + "#export\n", |
| 4028 | + "def threaded(f):\n", |
| 4029 | + " \"Run `f` in a thread, and returns the thread\"\n", |
| 4030 | + " @wraps(f)\n", |
| 4031 | + " def _f(*args, **kwargs):\n", |
| 4032 | + " res = Thread(target=f, args=args, kwargs=kwargs)\n", |
| 4033 | + " res.start()\n", |
| 4034 | + " return res\n", |
| 4035 | + " return _f" |
| 4036 | + ] |
| 4037 | + }, |
4016 | 4038 | { |
4017 | 4039 | "cell_type": "markdown", |
4018 | 4040 | "metadata": {}, |
|
0 commit comments