21
21
def feedback (key = None ):
22
22
"""
23
23
If this decorator is applied to a function,
24
- it's return value will automatically be sent
25
- to NetworkTables at key ``/robot/component /component/key``
24
+ its return value will automatically be sent
25
+ to NetworkTables at key ``/robot/components /component/key``.
26
26
27
27
``key`` is an optional parameter, and if it is not supplied,
28
28
the key will default to the method name with 'get_' removed.
29
29
If the method does not start with 'get_', the key will be the full
30
- name of the method
30
+ name of the method.
31
31
32
32
.. note:: The function will automatically be called in disabled,
33
- autonomous, and teleop.
33
+ autonomous, and teleop.
34
34
35
35
.. warning:: The function should only act as a getter, and accept
36
- no arguments.
36
+ no arguments.
37
37
38
- Example
38
+ Example::
39
39
40
- class Component1:
41
-
42
- @feedback()
43
- def get_angle(self):
44
- return self.navx.getYaw()
40
+ class Component1:
45
41
42
+ @feedback()
43
+ def get_angle(self):
44
+ return self.navx.getYaw()
46
45
47
46
In this example, the NetworkTable key is stored at
48
- ``/robot/components/component1/angle``
47
+ ``/robot/components/component1/angle``.
49
48
"""
50
49
def decorator (func ):
51
- if not hasattr (func , '__call__' ):
52
- raise ValueError ('Illegal use of feedback decorator on { }' .format (func . __name__ ))
50
+ if not callable (func ):
51
+ raise ValueError ('Illegal use of feedback decorator on non-callable {!r }' .format (func ))
53
52
sig = inspect .signature (func )
54
53
name = func .__name__
55
54
56
- for i , arg in enumerate (sig .parameters .values ()):
57
- if i == 0 and arg .name != 'self' :
58
- raise ValueError ("First argument to %s must be 'self'" % name )
59
- elif i != 0 :
60
- raise ValueError ('Only \' self\' is allowed for {}' .format (name ))
55
+ if len (sig .parameters ) != 1 :
56
+ raise ValueError ("{} may not take arguments other than 'self' (must be a simple getter method)" .format (name ))
61
57
62
58
nt_key = key
63
59
if nt_key is None :
@@ -70,9 +66,9 @@ def decorator(func):
70
66
else :
71
67
nt_key = name
72
68
# Set '__feedback__ attribute to be checked during injection
73
- setattr ( func , ' __feedback__' , True )
69
+ func . __feedback__ = True
74
70
# Store key within the function to avoid using class dictionary
75
- setattr ( func , ' __key__' , nt_key )
71
+ func . __key__ = nt_key
76
72
return func
77
73
return decorator
78
74
0 commit comments