|
1 | 1 | # AUTOGENERATED! DO NOT EDIT! File to edit: nbs/01_basics.ipynb (unless otherwise specified). |
2 | 2 |
|
3 | | -__all__ = ['defaults', 'ifnone', 'maybe_attr', 'basic_repr', 'is_array', 'listify', 'true', 'NullType', 'null', |
4 | | - 'tonull', 'get_class', 'mk_class', 'wrap_class', 'ignore_exceptions', 'exec_local', 'risinstance', 'Inf', |
5 | | - 'in_', 'lt', 'gt', 'le', 'ge', 'eq', 'ne', 'add', 'sub', 'mul', 'truediv', 'is_', 'is_not', 'in_', 'true', |
6 | | - 'stop', 'gen', 'chunked', 'otherwise', 'custom_dir', 'AttrDict', 'type_hints', 'annotations', 'anno_ret', |
7 | | - 'argnames', 'with_cast', 'store_attr', 'attrdict', 'properties', 'camel2snake', 'snake2camel', 'class2attr', |
8 | | - 'getattrs', 'hasattrs', 'setattrs', 'try_attrs', 'ShowPrint', 'Int', 'Str', 'Float', 'detuplify', |
9 | | - 'replicate', 'setify', 'merge', 'range_of', 'groupby', 'last_index', 'filter_dict', 'filter_keys', |
10 | | - 'filter_values', 'cycle', 'zip_cycle', 'sorted_ex', 'negate_func', 'argwhere', 'filter_ex', 'range_of', |
11 | | - 'renumerate', 'first', 'nested_attr', 'nested_idx', 'num_methods', 'rnum_methods', 'inum_methods', |
12 | | - 'fastuple', 'arg0', 'arg1', 'arg2', 'arg3', 'arg4', 'bind', 'map_ex', 'compose', 'maps', 'partialler', |
13 | | - 'instantiate', 'using_attr', 'Self', 'Self', 'Stateful', 'PrettyString', 'even_mults', 'num_cpus', |
14 | | - 'add_props', 'typed'] |
| 3 | +__all__ = ['defaults', 'ifnone', 'maybe_attr', 'basic_repr', 'is_array', 'listify', 'tuplify', 'true', 'NullType', |
| 4 | + 'null', 'tonull', 'get_class', 'mk_class', 'wrap_class', 'ignore_exceptions', 'exec_local', 'risinstance', |
| 5 | + 'Inf', 'in_', 'lt', 'gt', 'le', 'ge', 'eq', 'ne', 'add', 'sub', 'mul', 'truediv', 'is_', 'is_not', 'in_', |
| 6 | + 'true', 'stop', 'gen', 'chunked', 'otherwise', 'custom_dir', 'AttrDict', 'type_hints', 'annotations', |
| 7 | + 'anno_ret', 'argnames', 'with_cast', 'store_attr', 'attrdict', 'properties', 'camel2snake', 'snake2camel', |
| 8 | + 'class2attr', 'getattrs', 'hasattrs', 'setattrs', 'try_attrs', 'ShowPrint', 'Int', 'Str', 'Float', |
| 9 | + 'detuplify', 'replicate', 'setify', 'merge', 'range_of', 'groupby', 'last_index', 'filter_dict', |
| 10 | + 'filter_keys', 'filter_values', 'cycle', 'zip_cycle', 'sorted_ex', 'negate_func', 'argwhere', 'filter_ex', |
| 11 | + 'range_of', 'renumerate', 'first', 'nested_attr', 'nested_idx', 'val2idx', 'uniqueify', 'num_methods', |
| 12 | + 'rnum_methods', 'inum_methods', 'fastuple', 'arg0', 'arg1', 'arg2', 'arg3', 'arg4', 'bind', 'map_ex', |
| 13 | + 'compose', 'maps', 'partialler', 'instantiate', 'using_attr', 'Self', 'Self', 'Stateful', 'PrettyString', |
| 14 | + 'even_mults', 'num_cpus', 'add_props', 'typed'] |
15 | 15 |
|
16 | 16 | # Cell |
17 | 17 | from .imports import * |
@@ -44,13 +44,25 @@ def is_array(x): |
44 | 44 | return hasattr(x,'__array__') or hasattr(x,'iloc') |
45 | 45 |
|
46 | 46 | # Cell |
47 | | -def listify(o): |
| 47 | +def listify(o=None, *rest, use_list=False, match=None): |
48 | 48 | "Convert `o` to a `list`" |
49 | | - if o is None: return [] |
50 | | - if isinstance(o, list): return o |
51 | | - if isinstance(o, str) or is_array(o): return [o] |
52 | | - if is_iter(o): return list(o) |
53 | | - return [o] |
| 49 | + if rest: o = (o,)+rest |
| 50 | + if use_list: res = list(o) |
| 51 | + elif o is None: res = [] |
| 52 | + elif isinstance(o, list): res = o |
| 53 | + elif isinstance(o, str) or is_array(o): res = [o] |
| 54 | + elif is_iter(o): res = list(o) |
| 55 | + else: res = [o] |
| 56 | + if match is not None: |
| 57 | + if is_coll(match): match = len(match) |
| 58 | + if len(res)==1: res = res*match |
| 59 | + else: assert len(res)==match, 'Match length mismatch' |
| 60 | + return res |
| 61 | + |
| 62 | +# Cell |
| 63 | +def tuplify(o, use_list=False, match=None): |
| 64 | + "Make `o` a tuple" |
| 65 | + return tuple(listify(o, use_list=use_list, match=match)) |
54 | 66 |
|
55 | 67 | # Cell |
56 | 68 | def true(x): |
@@ -262,23 +274,25 @@ def _inner(*args, **kwargs): |
262 | 274 |
|
263 | 275 | # Cell |
264 | 276 | def _store_attr(self, anno, **attrs): |
265 | | - stored = self.__stored_args__ |
| 277 | + stored = getattr(self, '__stored_args__', None) |
266 | 278 | for n,v in attrs.items(): |
267 | 279 | if n in anno: v = anno[n](v) |
268 | 280 | setattr(self, n, v) |
269 | | - stored[n] = v |
| 281 | + if stored is not None: stored[n] = v |
270 | 282 |
|
271 | 283 | # Cell |
272 | | -def store_attr(names=None, self=None, but='', cast=False, **attrs): |
| 284 | +def store_attr(names=None, self=None, but='', cast=False, store_args=None, **attrs): |
273 | 285 | "Store params named in comma-separated `names` from calling context into attrs in `self`" |
274 | 286 | fr = sys._getframe(1) |
275 | 287 | args = argnames(fr, True) |
276 | 288 | if self: args = ('self', *args) |
277 | 289 | else: self = fr.f_locals[args[0]] |
278 | | - if not hasattr(self, '__stored_args__'): self.__stored_args__ = {} |
| 290 | + if store_args is None: store_args = not hasattr(self,'__slots__') |
| 291 | + if store_args and not hasattr(self, '__stored_args__'): self.__stored_args__ = {} |
279 | 292 | anno = annotations(self) if cast else {} |
280 | 293 | if not attrs: |
281 | | - ns = re.split(', *', names) if names else args[1:] |
| 294 | + if names and isinstance(names,str): names = re.split(', *', names) |
| 295 | + ns = names if names else args[1:] |
282 | 296 | attrs = {n:fr.f_locals[n] for n in ns} |
283 | 297 | if isinstance(but,str): but = re.split(', *', but) |
284 | 298 | attrs = {k:v for k,v in attrs.items() if k not in but} |
@@ -492,6 +506,19 @@ def nested_idx(coll, *idxs): |
492 | 506 | res = coll.get(idxs[0], None) if hasattr(coll, 'get') else coll[idxs[0]] if idxs[0]<len(coll) else None |
493 | 507 | return nested_idx(res, *idxs[1:]) |
494 | 508 |
|
| 509 | +# Cell |
| 510 | +def val2idx(x): |
| 511 | + "Dict from value to index" |
| 512 | + return {v:k for k,v in enumerate(x)} |
| 513 | + |
| 514 | +# Cell |
| 515 | +def uniqueify(x, sort=False, bidir=False, start=None): |
| 516 | + "Unique elements in `x`, optionally `sort`-ed, optionally return reverse correspondence, optionally prepend with elements." |
| 517 | + res = list(dict.fromkeys(x)) |
| 518 | + if start is not None: res = listify(start)+res |
| 519 | + if sort: res.sort() |
| 520 | + return (res,val2idx(res)) if bidir else res |
| 521 | + |
495 | 522 | # Cell |
496 | 523 | num_methods = """ |
497 | 524 | __add__ __sub__ __mul__ __matmul__ __truediv__ __floordiv__ __mod__ __divmod__ __pow__ |
|
0 commit comments