rosros 0.2.5
Simple unified interface to ROS1 / ROS2 Python API
Loading...
Searching...
No Matches
rosros.util Namespace Reference

Common utility classes and functions. More...

Classes

class  ThrottledLogger
 Logger wrapper with support for throttling logged messages per call site. More...
 

Functions

 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.
 

Detailed Description

Common utility classes and functions.

Function Documentation

◆ drop_zeros()

rosros.util.drop_zeros (   v,
  replace = "" 
)

Drops or replaces trailing zeros and empty decimal separator, if any.

Definition at line 177 of file util.py.

◆ ensure_object()

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_clsobject class or instance
attributesiterable of object attribute names to combine positional arguments from, or a callable returning iterable
populate_objectfunction(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.

◆ flatten_dict()

rosros.util.flatten_dict (   dct,
  sep = "." 
)

Flattens a nested dictionary to a flat dictionary, with nested keys joined with separator.

Parameters
dctthe dictionary to flatten
sepseparator between nested keys
Returns
flat dictionary like {"my.nested.key": value}

Definition at line 231 of file util.py.

◆ format_bytes()

rosros.util.format_bytes (   size,
  precision = 2,
  inter = " ",
  strip = True 
)

Returns a formatted byte size (like 421.40 MB), trailing zeros optionally removed.

Definition at line 242 of file util.py.

◆ get_arity()

rosros.util.get_arity (   func,
  positional = True,
  keyword = False 
)

Returns the maximum number of arguments the function takes, -1 if variable number.

Parameters
positionalcount positional-only and positional/keyword arguments
keywordcount keyword-only and positional/keyword arguments

Definition at line 260 of file util.py.

◆ get_nested()

rosros.util.get_nested (   obj,
  path 
)

Returns (nested value, value parent container, key in parent container).

Raises if path not found.

Parameters
objobject or dictionary or list, with arbitrary nesting
path"name" or ("nested", "path") or "nested.path", element can be list index
Returns
(value, value parent container, key in parent container)

Definition at line 288 of file util.py.

◆ get_value()

rosros.util.get_value (   obj,
  path,
  pathsep = None 
)

Returns object or dictionary or list value at (nested, path).

Raises if path not found.

Parameters
objobject or dictionary or list, with arbitrary nesting
path("nested", "path") or "nested.path", element can be list index
pathsepstring to split scalar key by, like "." for "nested.path"

Definition at line 311 of file util.py.

◆ make_dict()

rosros.util.make_dict (   path,
  value 
)

Returns a nested dictionary from path, like {"nested": {"path": value}}.

Definition at line 338 of file util.py.

◆ memoize()

rosros.util.memoize (   func)

Returns a results-caching wrapper for the function.

All arguments to function must be hashable.

Definition at line 328 of file util.py.

◆ merge_dicts()

rosros.util.merge_dicts (   d1,
  d2 
)

Merges d2 into d1, recursively for nested dicts.

Returns
updated d1

Definition at line 352 of file util.py.

◆ namejoin()

rosros.util.namejoin ( args)

Returns arguments joined into a namespace name, starting and separated with "/".

Definition at line 361 of file util.py.

◆ namesplit()

rosros.util.namesplit (   name)

Returns argument split into (namespace, name), like "/a/b/c" as ("/a/b", "c").

Definition at line 366 of file util.py.

◆ set_value()

rosros.util.set_value (   obj,
  path,
  value,
  pathsep = None 
)

Sets object or dictionary or list value at key or (nested, path).

Lists are appended to if specified index does not exist.

Parameters
objobject or dictionary or list, with arbitrary nesting
pathscalar key or ("nested", "path"), element can be list index
valuevalue to set
pathsepstring to split scalar key by, like "." for "nested.path"

Definition at line 382 of file util.py.

◆ start_future()

rosros.util.start_future (   func,
args,
**  kwargs 
)

Returns `concurrent.futures.Future` and invokes function in a background thread.

Future will be done when function returns or raises. Background thread is not daemonic.

Definition at line 406 of file util.py.

◆ unique_path()

rosros.util.unique_path (   pathname,
  empty_ok = False 
)

Returns a unique version of the path.

If a file or directory with the same name already exists, returns a unique version (e.g. "/tmp/my.2.file" if ""/tmp/my.file" already exists).

Parameters
empty_okwhether to ignore existence if file is empty

Definition at line 423 of file util.py.

◆ wrap_arity()

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.