|
1 | 1 | # AUTOGENERATED! DO NOT EDIT! File to edit: nbs/02_utils.ipynb (unless otherwise specified). |
2 | 2 |
|
3 | 3 | __all__ = ['ifnone', 'maybe_attr', 'basic_repr', 'get_class', 'mk_class', 'wrap_class', 'ignore_exceptions', 'dict2obj', |
4 | | - 'store_attr', 'attrdict', 'properties', 'camel2snake', 'snake2camel', 'class2attr', 'hasattrs', 'ShowPrint', |
5 | | - 'Int', 'Str', 'Float', 'tuplify', 'detuplify', 'replicate', 'uniqueify', 'setify', 'merge', 'is_listy', |
6 | | - 'range_of', 'groupby', 'last_index', 'shufflish', 'IterLen', 'ReindexCollection', 'num_methods', |
| 4 | + 'store_attr', 'attrdict', 'properties', 'camel2snake', 'snake2camel', 'class2attr', 'hasattrs', 'setattrs', |
| 5 | + 'ShowPrint', 'Int', 'Str', 'Float', 'tuplify', 'detuplify', 'replicate', 'uniqueify', 'setify', 'merge', |
| 6 | + 'is_listy', 'range_of', 'groupby', 'last_index', 'shufflish', 'IterLen', 'ReindexCollection', 'num_methods', |
7 | 7 | 'rnum_methods', 'inum_methods', 'fastuple', 'Inf', 'in_', 'lt', 'gt', 'le', 'ge', 'eq', 'ne', 'add', 'sub', |
8 | 8 | 'mul', 'truediv', 'is_', 'is_not', 'in_', 'true', 'stop', 'gen', 'chunked', 'trace', 'compose', 'maps', |
9 | 9 | 'partialler', 'mapped', 'instantiate', 'using_attr', 'Self', 'Self', 'remove_patches_path', 'bunzip', |
10 | | - 'join_path_file', 'urlread', 'urljson', 'run_proc', 'do_request', 'sort_by_run', 'PrettyString', |
11 | | - 'round_multiple', 'even_mults', 'num_cpus', 'add_props', 'ContextManagers', 'set_num_threads', |
12 | | - 'ProcessPoolExecutor', 'ThreadPoolExecutor', 'parallel', 'run_procs', 'parallel_gen'] |
| 10 | + 'join_path_file', 'urlread', 'urljson', 'run', 'do_request', 'sort_by_run', 'PrettyString', 'round_multiple', |
| 11 | + 'even_mults', 'num_cpus', 'add_props', 'ContextManagers', 'set_num_threads', 'ProcessPoolExecutor', |
| 12 | + 'ThreadPoolExecutor', 'parallel', 'run_procs', 'parallel_gen'] |
13 | 13 |
|
14 | 14 | # Cell |
15 | 15 | from .imports import * |
16 | 16 | from .foundation import * |
17 | 17 | from functools import wraps |
18 | 18 |
|
19 | | -import mimetypes,bz2,pickle,random,json,urllib,subprocess |
| 19 | +import mimetypes,bz2,pickle,random,json,urllib,subprocess,shlex |
20 | 20 | from contextlib import contextmanager |
21 | 21 | from urllib.request import Request,urlopen |
22 | 22 | from urllib.error import HTTPError |
@@ -148,6 +148,12 @@ def hasattrs(o,attrs): |
148 | 148 | "Test whether `o` contains all `attrs`" |
149 | 149 | return all(hasattr(o,attr) for attr in attrs) |
150 | 150 |
|
| 151 | +# Cell |
| 152 | +def setattrs(dest, flds, src): |
| 153 | + f = dict.get if isinstance(src, dict) else getattr |
| 154 | + flds = re.split(r",\s*", flds) |
| 155 | + for fld in flds: setattr(dest, fld, f(src, fld)) |
| 156 | + |
151 | 157 | # Cell |
152 | 158 | #hide |
153 | 159 | class ShowPrint: |
@@ -563,9 +569,11 @@ def urljson(url): |
563 | 569 | return json.loads(urlread(url)) |
564 | 570 |
|
565 | 571 | # Cell |
566 | | -def run_proc(*args): |
567 | | - "Pass `args` to `subprocess.run`, returning `stdout`, or raise `IOError` on failure" |
568 | | - res = subprocess.run(args, capture_output=True) |
| 572 | +def run(cmd, *rest): |
| 573 | + "Pass `cmd` (splitting with `shlex` if string) to `subprocess.run`, returning `stdout`, or raise `IOError` on failure" |
| 574 | + if rest: cmd = (cmd,)+rest |
| 575 | + elif isinstance(cmd,str): cmd = shlex.split(cmd) |
| 576 | + res = subprocess.run(cmd, capture_output=True) |
569 | 577 | if res.returncode: raise IOError("{} ;; {}".format(res.stdout, res.stderr)) |
570 | 578 | return res.stdout |
571 | 579 |
|
|
0 commit comments