|
10 | 10 | 'custom_dir', 'AttrDict', 'AttrDictDefault', 'NS', 'get_annotations_ex', 'eval_type', 'type_hints', |
11 | 11 | 'annotations', 'anno_ret', 'signature_ex', 'union2tuple', 'argnames', 'with_cast', 'store_attr', 'attrdict', |
12 | 12 | 'properties', 'camel2words', 'camel2snake', 'snake2camel', 'class2attr', 'getcallable', 'getattrs', |
13 | | - 'hasattrs', 'setattrs', 'try_attrs', 'GetAttrBase', 'GetAttr', 'delegate_attr', 'ShowPrint', 'Int', 'Str', |
14 | | - 'Float', 'partition', 'partition_dict', 'flatten', 'concat', 'strcat', 'detuplify', 'replicate', 'setify', |
15 | | - 'merge', 'range_of', 'groupby', 'last_index', 'filter_dict', 'filter_keys', 'filter_values', 'cycle', |
16 | | - 'zip_cycle', 'sorted_ex', 'not_', 'argwhere', 'filter_ex', 'renumerate', 'first', 'last', 'only', |
17 | | - 'nested_attr', 'nested_setdefault', 'nested_callable', 'nested_idx', 'set_nested_idx', 'val2idx', |
18 | | - 'uniqueify', 'loop_first_last', 'loop_first', 'loop_last', 'first_match', 'last_match', 'joins', 'fastuple', |
19 | | - 'bind', 'mapt', 'map_ex', 'compose', 'maps', 'partialler', 'instantiate', 'using_attr', 'negate', 'spread', |
20 | | - 'dspread', 'copy_func', 'patch_to', 'patch', 'compile_re', 'ImportEnum', 'StrEnum', 'str_enum', 'ValEnum', |
21 | | - 'Stateful', 'NotStr', 'PrettyString', 'even_mults', 'num_cpus', 'add_props', 'str2bool', 'str2int', |
22 | | - 'str2float', 'str2list', 'str2date', 'to_bool', 'to_int', 'to_float', 'to_list', 'to_date', 'typed', |
23 | | - 'exec_new', 'exec_import', 'sig_with_params', 'fdelegates', 'lt', 'gt', 'le', 'ge', 'eq', 'ne', 'add', 'sub', |
24 | | - 'mul', 'truediv', 'is_', 'is_not', 'mod'] |
| 13 | + 'hasattrs', 'setattrs', 'try_attrs', 'DepProp', 'GetAttrBase', 'GetAttr', 'delegate_attr', 'ShowPrint', |
| 14 | + 'Int', 'Str', 'Float', 'partition', 'partition_dict', 'flatten', 'concat', 'strcat', 'detuplify', |
| 15 | + 'replicate', 'setify', 'merge', 'range_of', 'groupby', 'last_index', 'filter_dict', 'filter_keys', |
| 16 | + 'filter_values', 'cycle', 'zip_cycle', 'sorted_ex', 'not_', 'argwhere', 'filter_ex', 'renumerate', 'first', |
| 17 | + 'last', 'only', 'nested_attr', 'nested_setdefault', 'nested_callable', 'nested_idx', 'set_nested_idx', |
| 18 | + 'val2idx', 'uniqueify', 'loop_first_last', 'loop_first', 'loop_last', 'first_match', 'last_match', 'joins', |
| 19 | + 'fastuple', 'bind', 'mapt', 'map_ex', 'compose', 'maps', 'partialler', 'instantiate', 'using_attr', 'negate', |
| 20 | + 'spread', 'dspread', 'copy_func', 'patch_to', 'patch', 'compile_re', 'ImportEnum', 'StrEnum', 'str_enum', |
| 21 | + 'ValEnum', 'Stateful', 'NotStr', 'PrettyString', 'even_mults', 'num_cpus', 'add_props', 'str2bool', |
| 22 | + 'str2int', 'str2float', 'str2list', 'str2date', 'to_bool', 'to_int', 'to_float', 'to_list', 'to_date', |
| 23 | + 'typed', 'exec_new', 'exec_import', 'sig_with_params', 'fdelegates', 'lt', 'gt', 'le', 'ge', 'eq', 'ne', |
| 24 | + 'add', 'sub', 'mul', 'truediv', 'is_', 'is_not', 'mod'] |
25 | 25 |
|
26 | 26 | # %% ../nbs/01_basics.ipynb #0e91ed82 |
27 | 27 | from .imports import * |
@@ -520,6 +520,31 @@ def try_attrs(obj, *attrs): |
520 | 520 | except: pass |
521 | 521 | raise AttributeError(attrs) |
522 | 522 |
|
| 523 | +# %% ../nbs/01_basics.ipynb #df28aacb |
| 524 | +class DepProp: |
| 525 | + "Property decorator with dependency update triggering" |
| 526 | + def __init__(self, fchange, fnorm=None): self.fchange, self.fnorm = fchange, fnorm |
| 527 | + def __set_name__(self, owner, name): self.attr = f'_{name}' |
| 528 | + |
| 529 | + def norm(self, fn): |
| 530 | + self.fnorm = fn |
| 531 | + return self |
| 532 | + |
| 533 | + def __get__(self, o, objtype=None): |
| 534 | + if o is None: return self |
| 535 | + return getattr(o, self.attr, None) |
| 536 | + |
| 537 | + def __set__(self, o, v): |
| 538 | + if self.fnorm: v = self.fnorm(o, v) |
| 539 | + change = not hasattr(o, self.attr) or v!=self.__get__(o) |
| 540 | + setattr(o, self.attr, v) |
| 541 | + if change: self.fchange(o) |
| 542 | + |
| 543 | + def __delete__(self, o): |
| 544 | + if hasattr(o, self.attr): |
| 545 | + delattr(o, self.attr) |
| 546 | + self.fchange(o) |
| 547 | + |
523 | 548 | # %% ../nbs/01_basics.ipynb #8c571e08 |
524 | 549 | class GetAttrBase: |
525 | 550 | "Basic delegation of `__getattr__` and `__dir__`" |
|
0 commit comments