pydrake.common.containers
Provides extensions for containers of Drake-related objects.
- class pydrake.common.containers.EqualToDict(*args, **kwargs)
Bases:
pydrake.common.containers._DictKeyWrap
Implements a dictionary where keys are compared using type and lhs.EqualTo(rhs).
- __init__(*args, **kwargs)
- pydrake.common.containers.namedview(name, fields, *, sanitize_field_names=True)
Creates a class that is a named view with given
fields
. When the class is instantiated, it must be given the object that it will be a proxy for. Similar tonamedtuple
.If
sanitize_field_names
is True (the default), then any characters infields
which are not valid in Python identifiers will be automatically replaced with _. Leading numbers will have _ inserted, and duplicate _ will be replaced by a single _.Example
MyView = namedview("MyView", ('a', 'b')) value = np.array([1, 2]) view = MyView(value) view.a = 10 # `value` is now [10, 2] value[1] = 100 # `view` is now [10, 100] view[:] = 3 # `value` is now [3, 3] # Get an array from the view *aliasing* the original vector. value_view = view[:] # Another way to get an aliased array. value_view_2 = np.asarray(view) # Get an array from the view that is a *copy* of the original # vector. value_copy = np.array(view)
Warning
As illustrated above, if you use
np.array(view)
, then it will provide a copied array from the view. If you want an aliased array from the view, then use operations likeview[:]
,np.asarray(view)
, ornp.array(view, copy=False)
.For more details, see
NamedViewBase
.
- class pydrake.common.containers.NamedViewBase(value)
Bases:
object
Base for classes generated by
namedview
.Inspired by: https://gitlab.com/ericvsmith/namedlist
- __init__(value)
Creates a view on
value
. Any mutations on this instance will be reflected invalue
, and any mutations onvalue
will be reflected in this instance.
- classmethod get_fields()
Returns all fields for this class or object.
- classmethod Zero()
Constructs a view onto values set to all zeros.