registry.py
Go to the documentation of this file.
1 import functools
2 import genpy
3 import collections
4 from . import numpy_msg
5 
6 _to_numpy = {}
7 _from_numpy = {}
8 
9 def converts_to_numpy(msgtype, plural=False):
10  assert issubclass(msgtype, genpy.Message)
11  def decorator(f):
12  _to_numpy[msgtype, plural] = f
13  _to_numpy[numpy_msg(msgtype), plural] = f
14  return f
15  return decorator
16 
17 def converts_from_numpy(msgtype, plural=False):
18  assert issubclass(msgtype, genpy.Message)
19  def decorator(f):
20  _from_numpy[msgtype, plural] = f
21  _from_numpy[numpy_msg(msgtype), plural] = f
22  return f
23  return decorator
24 
25 def numpify(msg, *args, **kwargs):
26  if msg is None:
27  return
28 
29  conv = _to_numpy.get((msg.__class__, False))
30  if not conv and isinstance(msg, collections.Sequence):
31  if not msg:
32  raise ValueError("Cannot determine the type of an empty Collection")
33  conv = _to_numpy.get((msg[0].__class__, True))
34 
35 
36  if not conv:
37  raise ValueError("Unable to convert message {} - only supports {}".format(
38  msg.__class__.__name__,
39  ', '.join(cls.__name__ + ("[]" if pl else '') for cls, pl in _to_numpy.keys())
40  ))
41 
42  return conv(msg, *args, **kwargs)
43 
44 def msgify(msg_type, numpy_obj, *args, **kwargs):
45  conv = _from_numpy.get((msg_type, kwargs.pop('plural', False)))
46  if not conv:
47  raise ValueError("Unable to build message {} - only supports {}".format(
48  msg_type.__name__,
49  ', '.join(cls.__name__ + ("[]" if pl else '') for cls, pl in _to_numpy.keys())
50  ))
51  return conv(numpy_obj, *args, **kwargs)
def numpify(msg, args, kwargs)
Definition: registry.py:25
def converts_from_numpy(msgtype, plural=False)
Definition: registry.py:17
def numpy_msg(cls)
Definition: numpy_msg.py:11
def converts_to_numpy(msgtype, plural=False)
Definition: registry.py:9
def msgify(msg_type, numpy_obj, args, kwargs)
Definition: registry.py:44


ros_numpy
Author(s): Eric Wieser
autogenerated on Sat Oct 3 2020 03:25:57