Skip to content

Commit e753579

Browse files
committed
getcallable
1 parent 89386f8 commit e753579

File tree

3 files changed

+42
-9
lines changed

3 files changed

+42
-9
lines changed

fastcore/_nbdev.py

Lines changed: 1 addition & 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",

fastcore/basics.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
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_callable', 'nested_idx', 'set_nested_idx',
13-
'val2idx', 'uniqueify', 'loop_first_last', 'loop_first', 'loop_last', 'num_methods', 'rnum_methods',
14-
'inum_methods', 'fastuple', 'arg0', 'arg1', 'arg2', 'arg3', 'arg4', 'bind', 'mapt', 'map_ex', 'compose',
15-
'maps', 'partialler', 'instantiate', 'using_attr', 'Self', 'Self', 'copy_func', 'patch_to', 'patch',
16-
'patch_property', 'compile_re', 'ImportEnum', 'StrEnum', 'str_enum', 'Stateful', 'PrettyString',
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',
1717
'even_mults', 'num_cpus', 'add_props', 'typed', 'exec_new', 'exec_import']
1818

1919
# Cell
@@ -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`"

nbs/01_basics.ipynb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2285,6 +2285,33 @@
22852285
"test_eq(cp2.name, 'parent_child_of')"
22862286
]
22872287
},
2288+
{
2289+
"cell_type": "code",
2290+
"execution_count": 373,
2291+
"metadata": {},
2292+
"outputs": [],
2293+
"source": [
2294+
"#export\n",
2295+
"def getcallable(o, attr):\n",
2296+
" \"Calls `getattr` with a default of `noop`\"\n",
2297+
" return getattr(o, attr, noop)"
2298+
]
2299+
},
2300+
{
2301+
"cell_type": "code",
2302+
"execution_count": 378,
2303+
"metadata": {},
2304+
"outputs": [],
2305+
"source": [
2306+
"class Math:\n",
2307+
" def addition(self,a,b): return a+b\n",
2308+
"\n",
2309+
"m = Math()\n",
2310+
"\n",
2311+
"test_eq(getcallable(m, \"addition\")(a=1,b=2), 3)\n",
2312+
"test_eq(getcallable(m, \"subtraction\")(a=1,b=2), None)"
2313+
]
2314+
},
22882315
{
22892316
"cell_type": "code",
22902317
"execution_count": 296,

0 commit comments

Comments
 (0)