Skip to content

Commit 8598aa0

Browse files
authored
Merge pull request #417 from muellerzr/getcallable
Add a `nested_callable` and `getcallable` util
2 parents d66eadf + e753579 commit 8598aa0

File tree

5 files changed

+323
-232
lines changed

5 files changed

+323
-232
lines changed

fastcore/_nbdev.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"camel2snake": "01_basics.ipynb",
6060
"snake2camel": "01_basics.ipynb",
6161
"class2attr": "01_basics.ipynb",
62+
"getcallable": "01_basics.ipynb",
6263
"getattrs": "01_basics.ipynb",
6364
"hasattrs": "01_basics.ipynb",
6465
"setattrs": "01_basics.ipynb",
@@ -92,6 +93,7 @@
9293
"renumerate": "01_basics.ipynb",
9394
"first": "01_basics.ipynb",
9495
"nested_attr": "01_basics.ipynb",
96+
"nested_callable": "01_basics.ipynb",
9597
"nested_idx": "01_basics.ipynb",
9698
"set_nested_idx": "01_basics.ipynb",
9799
"val2idx": "01_basics.ipynb",

fastcore/basics.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55
'Inf', 'in_', 'lt', 'gt', 'le', 'ge', 'eq', 'ne', 'add', 'sub', 'mul', 'truediv', 'is_', 'is_not', 'in_',
66
'true', 'stop', 'gen', 'chunked', 'otherwise', 'custom_dir', 'AttrDict', 'get_annotations_ex', 'eval_type',
77
'type_hints', 'annotations', 'anno_ret', 'argnames', 'with_cast', 'store_attr', 'attrdict', 'properties',
8-
'camel2words', 'camel2snake', 'snake2camel', 'class2attr', 'getattrs', 'hasattrs', 'setattrs', 'try_attrs',
9-
'GetAttrBase', 'GetAttr', 'delegate_attr', 'ShowPrint', 'Int', 'Str', 'Float', 'flatten', 'concat', 'strcat',
10-
'detuplify', 'replicate', 'setify', 'merge', 'range_of', 'groupby', 'last_index', 'filter_dict',
11-
'filter_keys', 'filter_values', 'cycle', 'zip_cycle', 'sorted_ex', 'not_', 'argwhere', 'filter_ex',
12-
'range_of', 'renumerate', 'first', 'nested_attr', 'nested_idx', 'set_nested_idx', 'val2idx', 'uniqueify',
13-
'loop_first_last', 'loop_first', 'loop_last', 'num_methods', 'rnum_methods', 'inum_methods', 'fastuple',
14-
'arg0', 'arg1', 'arg2', 'arg3', 'arg4', 'bind', 'mapt', 'map_ex', 'compose', 'maps', 'partialler',
15-
'instantiate', 'using_attr', 'Self', 'Self', 'copy_func', 'patch_to', 'patch', 'patch_property',
16-
'compile_re', 'ImportEnum', 'StrEnum', 'str_enum', 'Stateful', 'PrettyString', 'even_mults', 'num_cpus',
17-
'add_props', 'typed', 'exec_new', 'exec_import']
8+
'camel2words', 'camel2snake', 'snake2camel', 'class2attr', 'getcallable', 'getattrs', 'hasattrs', 'setattrs',
9+
'try_attrs', 'GetAttrBase', 'GetAttr', 'delegate_attr', 'ShowPrint', 'Int', 'Str', 'Float', 'flatten',
10+
'concat', 'strcat', 'detuplify', 'replicate', 'setify', 'merge', 'range_of', 'groupby', 'last_index',
11+
'filter_dict', 'filter_keys', 'filter_values', 'cycle', 'zip_cycle', 'sorted_ex', 'not_', 'argwhere',
12+
'filter_ex', 'range_of', 'renumerate', 'first', 'nested_attr', 'nested_callable', 'nested_idx',
13+
'set_nested_idx', 'val2idx', 'uniqueify', 'loop_first_last', 'loop_first', 'loop_last', 'num_methods',
14+
'rnum_methods', 'inum_methods', 'fastuple', 'arg0', 'arg1', 'arg2', 'arg3', 'arg4', 'bind', 'mapt', 'map_ex',
15+
'compose', 'maps', 'partialler', 'instantiate', 'using_attr', 'Self', 'Self', 'copy_func', 'patch_to',
16+
'patch', 'patch_property', 'compile_re', 'ImportEnum', 'StrEnum', 'str_enum', 'Stateful', 'PrettyString',
17+
'even_mults', 'num_cpus', 'add_props', 'typed', 'exec_new', 'exec_import']
1818

1919
# Cell
2020
from .imports import *
@@ -406,6 +406,11 @@ def class2attr(self, cls_name):
406406
"Return the snake-cased name of the class; strip ending `cls_name` if it exists."
407407
return camel2snake(re.sub(rf'{cls_name}$', '', self.__class__.__name__) or cls_name.lower())
408408

409+
# Cell
410+
def getcallable(o, attr):
411+
"Calls `getattr` with a default of `noop`"
412+
return getattr(o, attr, noop)
413+
409414
# Cell
410415
def getattrs(o, *attrs, default=None):
411416
"List of all `attrs` in `o`"
@@ -628,6 +633,11 @@ def nested_attr(o, attr, default=None):
628633
except AttributeError: return default
629634
return o
630635

636+
# Cell
637+
def nested_callable(o, attr):
638+
"Same as `nested_attr` but if not found will return `noop`"
639+
return nested_attr(o, attr, noop)
640+
631641
# Cell
632642
def _access(coll, idx): return coll.get(idx, None) if hasattr(coll, 'get') else coll[idx] if idx<len(coll) else None
633643

fastcore/transform.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def _call1(self, x, name, **kwargs):
122122
# Cell
123123
def get_func(t, name, *args, **kwargs):
124124
"Get the `t.name` (potentially partial-ized with `args` and `kwargs`) or `noop` if not defined"
125-
f = getattr(t, name, noop)
125+
f = nested_callable(t, name)
126126
return f if not (args or kwargs) else partial(f, *args, **kwargs)
127127

128128
# Cell

0 commit comments

Comments
 (0)