|
| drop_zeros (v, replace="") |
| Drops or replaces trailing zeros and empty decimal separator, if any.
|
|
| ensure_object (obj_or_cls, attributes, populate_object=None, *args, **kwargs) |
| Ensures result is an object of specified type.
|
|
| flatten_dict (dct, sep=".") |
| Flattens a nested dictionary to a flat dictionary, with nested keys joined with separator.
|
|
| format_bytes (size, precision=2, inter=" ", strip=True) |
| Returns a formatted byte size (like 421.40 MB), trailing zeros optionally removed.
|
|
| get_arity (func, positional=True, keyword=False) |
| Returns the maximum number of arguments the function takes, -1 if variable number.
|
|
| get_nested (obj, path) |
| Returns (nested value, value parent container, key in parent container).
|
|
| get_value (obj, path, pathsep=None) |
| Returns object or dictionary or list value at (nested, path).
|
|
| make_dict (path, value) |
| Returns a nested dictionary from path, like {"nested": {"path": value}}.
|
|
| memoize (func) |
| Returns a results-caching wrapper for the function.
|
|
| merge_dicts (d1, d2) |
| Merges d2 into d1, recursively for nested dicts.
|
|
| namejoin (*args) |
| Returns arguments joined into a namespace name, starting and separated with "/".
|
|
| namesplit (name) |
| Returns argument split into (namespace, name), like "/a/b/c" as ("/a/b", "c").
|
|
| set_value (obj, path, value, pathsep=None) |
| Sets object or dictionary or list value at key or (nested, path).
|
|
| start_future (func, *args, **kwargs) |
| Returns `concurrent.futures.Future` and invokes function in a background thread.
|
|
| unique_path (pathname, empty_ok=False) |
| Returns a unique version of the path.
|
|
| wrap_arity (func) |
| Returns wrapper for invoking function with its maximum supported number of arguments.
|
|
Common utility classes and functions.
rosros.util.ensure_object |
( |
|
obj_or_cls, |
|
|
|
attributes, |
|
|
|
populate_object = None , |
|
|
* |
args, |
|
|
** |
kwargs |
|
) |
| |
Ensures result is an object of specified type.
Intended for wrapping convenience functions giving or returning object as attribute list or dictionary instead of instantiated object.
If first positional argument is an instance of specified class, it is populated from positional and keyword arguments.
If first positional argument is a dictionary, it is taken as keyword arguments.
`obj_or_cls` may be a class to instantiate, or the class instance to populate if instance not in first positional argument. Instance attributes will be given in constructor as keyword arguments.
E.g. `ensure_object(std_msgs.msg.Bool, ["data"], True)`.
- Parameters
-
obj_or_cls | object class or instance |
attributes | iterable of object attribute names to combine positional arguments from, or a callable returning iterable |
populate_object | function(dict, obj) to populate object from dictionary |
- Returns
- object of specified class, populated from positional and keyword arguments, created if not given
Definition at line 207 of file util.py.
rosros.util.wrap_arity |
( |
|
func | ) |
|
Returns wrapper for invoking function with its maximum supported number of arguments.
E.g. `wrap_arity(abs)(-1, -2)` will return result of `abs(-1)`.
Returns original function if a built-in with no arity information available.
Definition at line 450 of file util.py.