Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a nested_callable and getcallable util #417

Merged
merged 2 commits into from
Jun 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions fastcore/_nbdev.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"camel2snake": "01_basics.ipynb",
"snake2camel": "01_basics.ipynb",
"class2attr": "01_basics.ipynb",
"getcallable": "01_basics.ipynb",
"getattrs": "01_basics.ipynb",
"hasattrs": "01_basics.ipynb",
"setattrs": "01_basics.ipynb",
Expand Down Expand Up @@ -92,6 +93,7 @@
"renumerate": "01_basics.ipynb",
"first": "01_basics.ipynb",
"nested_attr": "01_basics.ipynb",
"nested_callable": "01_basics.ipynb",
"nested_idx": "01_basics.ipynb",
"set_nested_idx": "01_basics.ipynb",
"val2idx": "01_basics.ipynb",
Expand Down
30 changes: 20 additions & 10 deletions fastcore/basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
'Inf', 'in_', 'lt', 'gt', 'le', 'ge', 'eq', 'ne', 'add', 'sub', 'mul', 'truediv', 'is_', 'is_not', 'in_',
'true', 'stop', 'gen', 'chunked', 'otherwise', 'custom_dir', 'AttrDict', 'get_annotations_ex', 'eval_type',
'type_hints', 'annotations', 'anno_ret', 'argnames', 'with_cast', 'store_attr', 'attrdict', 'properties',
'camel2words', 'camel2snake', 'snake2camel', 'class2attr', 'getattrs', 'hasattrs', 'setattrs', 'try_attrs',
'GetAttrBase', 'GetAttr', 'delegate_attr', 'ShowPrint', 'Int', 'Str', 'Float', 'flatten', 'concat', 'strcat',
'detuplify', 'replicate', 'setify', 'merge', 'range_of', 'groupby', 'last_index', 'filter_dict',
'filter_keys', 'filter_values', 'cycle', 'zip_cycle', 'sorted_ex', 'not_', 'argwhere', 'filter_ex',
'range_of', 'renumerate', 'first', 'nested_attr', 'nested_idx', 'set_nested_idx', 'val2idx', 'uniqueify',
'loop_first_last', 'loop_first', 'loop_last', 'num_methods', 'rnum_methods', 'inum_methods', 'fastuple',
'arg0', 'arg1', 'arg2', 'arg3', 'arg4', 'bind', 'mapt', 'map_ex', 'compose', 'maps', 'partialler',
'instantiate', 'using_attr', 'Self', 'Self', 'copy_func', 'patch_to', 'patch', 'patch_property',
'compile_re', 'ImportEnum', 'StrEnum', 'str_enum', 'Stateful', 'PrettyString', 'even_mults', 'num_cpus',
'add_props', 'typed', 'exec_new', 'exec_import']
'camel2words', 'camel2snake', 'snake2camel', 'class2attr', 'getcallable', 'getattrs', 'hasattrs', 'setattrs',
'try_attrs', 'GetAttrBase', 'GetAttr', 'delegate_attr', 'ShowPrint', 'Int', 'Str', 'Float', 'flatten',
'concat', 'strcat', 'detuplify', 'replicate', 'setify', 'merge', 'range_of', 'groupby', 'last_index',
'filter_dict', 'filter_keys', 'filter_values', 'cycle', 'zip_cycle', 'sorted_ex', 'not_', 'argwhere',
'filter_ex', 'range_of', 'renumerate', 'first', 'nested_attr', 'nested_callable', 'nested_idx',
'set_nested_idx', 'val2idx', 'uniqueify', 'loop_first_last', 'loop_first', 'loop_last', 'num_methods',
'rnum_methods', 'inum_methods', 'fastuple', 'arg0', 'arg1', 'arg2', 'arg3', 'arg4', 'bind', 'mapt', 'map_ex',
'compose', 'maps', 'partialler', 'instantiate', 'using_attr', 'Self', 'Self', 'copy_func', 'patch_to',
'patch', 'patch_property', 'compile_re', 'ImportEnum', 'StrEnum', 'str_enum', 'Stateful', 'PrettyString',
'even_mults', 'num_cpus', 'add_props', 'typed', 'exec_new', 'exec_import']

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

# Cell
def getcallable(o, attr):
"Calls `getattr` with a default of `noop`"
return getattr(o, attr, noop)

# Cell
def getattrs(o, *attrs, default=None):
"List of all `attrs` in `o`"
Expand Down Expand Up @@ -628,6 +633,11 @@ def nested_attr(o, attr, default=None):
except AttributeError: return default
return o

# Cell
def nested_callable(o, attr):
"Same as `nested_attr` but if not found will return `noop`"
return nested_attr(o, attr, noop)

# Cell
def _access(coll, idx): return coll.get(idx, None) if hasattr(coll, 'get') else coll[idx] if idx<len(coll) else None

Expand Down
2 changes: 1 addition & 1 deletion fastcore/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def _call1(self, x, name, **kwargs):
# Cell
def get_func(t, name, *args, **kwargs):
"Get the `t.name` (potentially partial-ized with `args` and `kwargs`) or `noop` if not defined"
f = getattr(t, name, noop)
f = nested_callable(t, name)
return f if not (args or kwargs) else partial(f, *args, **kwargs)

# Cell
Expand Down
Loading