ubelt.util_func module¶
Helpers for functional programming.
The identity()
function simply returns its own inputs. This is useful for
bypassing print statements and many other cases. I also think it looks a little
nicer than lambda x: x
.
The inject_method()
function “injects” another function into a class
instance as a method. This is useful for monkey patching.
- ubelt.util_func.identity(arg=None, *args, **kwargs)[source]¶
The identity function. Simply returns the value of its first input.
All other inputs are ignored. Defaults to None if called without args.
- Parameters
arg (object, default=None) – some value
*args – ignored
**kwargs – ignored
- Returns
arg - the same value
- Return type
Example
>>> import ubelt as ub >>> ub.identity(42) 42 >>> ub.identity(42, 42) 42 >>> ub.identity() None
- ubelt.util_func.inject_method(self, func, name=None)[source]¶
Injects a function into an object instance as a bound method
The main use case of this function is for monkey patching. While monkey patching is sometimes necessary it should generally be avoided. Thus, we simply remind the developer that there might be a better way.
- Parameters
self (T) – Instance to inject a function into.
func (Callable[…, Any]) – The function to inject (must contain an arg for self).
name (str, default=None) – Name of the method. optional. If not specified the name of the function is used.
Example
>>> import ubelt as ub >>> class Foo(object): >>> def bar(self): >>> return 'bar' >>> def baz(self): >>> return 'baz' >>> self = Foo() >>> assert self.bar() == 'bar' >>> assert not hasattr(self, 'baz') >>> ub.inject_method(self, baz) >>> assert not hasattr(Foo, 'baz'), 'should only change one instance' >>> assert self.baz() == 'baz' >>> ub.inject_method(self, baz, 'bar') >>> assert self.bar() == 'baz'
- ubelt.util_func.compatible(config, func, start=0)[source]¶
Take the subset of dict items that can be passed to function as kwargs
- Parameters
config (Dict[str, Any]) – a flat configuration dictionary
func (Callable) – a function or method
start (int, default=0) – Only take args after this position. Set to 1 if calling with an unbound method to avoid the
self
argument.
- Returns
a subset of
config
that only contains items compatible with the signature offunc
.- Return type
Dict[str, Any]
Example
>>> # An example use case is to select a subset of of a config >>> # that can be passed to some function as kwargs >>> import ubelt as ub >>> # Define a function with args that match some keys in a config. >>> def func(a, e, f): >>> return a * e * f >>> # Define a config that has a superset of items needed by the func >>> config = { ... 'a': 2, 'b': 3, 'c': 7, ... 'd': 11, 'e': 13, 'f': 17, ... } >>> # Call the function only with keys that are compatible >>> func(**ub.compatible(config, func)) 442
Example
>>> # Test case with kwargs >>> import ubelt as ub >>> def func(a, e, f, *args, **kwargs): >>> return a * e * f >>> config = { ... 'a': 2, 'b': 3, 'c': 7, ... 'd': 11, 'e': 13, 'f': 17, ... } >>> func(**ub.compatible(config, func))