Skip to content

Commit 4a85fd2

Browse files
pepijndevosddemidov
authored andcommitted
Error on non-existing arguments/parameters (#278)
This patch adds __slots__ to all API classes that use setattr magic and kwargs for function parameters. Besides reducing memory and speeding up access, the main point of this change is that a typo when calling a function will result in an error, rather than silently setting non-existing properties.
1 parent 488b89f commit 4a85fd2

File tree

3 files changed

+184
-1
lines changed

3 files changed

+184
-1
lines changed

ev3dev/core.py

Lines changed: 178 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ def matches(attribute, pattern):
108108
class Device(object):
109109
"""The ev3dev device base class"""
110110

111+
__slots__ = ['_path', 'connected', '_device_index', 'kwargs']
112+
111113
DEVICE_ROOT_PATH = '/sys/class'
112114

113115
_DEVICE_INDEX = re.compile(r'^.*(?P<idx>\d+)$')
@@ -318,6 +320,41 @@ def __init__(self, address=None, name_pattern=SYSTEM_DEVICE_NAME_CONVENTION, nam
318320

319321
self._poll = None
320322

323+
__slots__ = [
324+
# ~autogen generic-class-slots classes.motor>currentClass
325+
326+
'_address',
327+
'_command',
328+
'_commands',
329+
'_count_per_rot',
330+
'_count_per_m',
331+
'_driver_name',
332+
'_duty_cycle',
333+
'_duty_cycle_sp',
334+
'_full_travel_count',
335+
'_polarity',
336+
'_position',
337+
'_position_p',
338+
'_position_i',
339+
'_position_d',
340+
'_position_sp',
341+
'_max_speed',
342+
'_speed',
343+
'_speed_sp',
344+
'_ramp_up_sp',
345+
'_ramp_down_sp',
346+
'_speed_p',
347+
'_speed_i',
348+
'_speed_d',
349+
'_state',
350+
'_stop_action',
351+
'_stop_actions',
352+
'_time_sp',
353+
354+
# ~autogen
355+
'_poll',
356+
]
357+
321358
# ~autogen generic-get-set classes.motor>currentClass
322359

323360
@property
@@ -923,6 +960,12 @@ def __init__(self, address=None, name_pattern=SYSTEM_DEVICE_NAME_CONVENTION, nam
923960

924961

925962
# ~autogen
963+
__slots__ = [
964+
# ~autogen generic-class-slots classes.largeMotor>currentClass
965+
966+
967+
# ~autogen
968+
]
926969
# ~autogen generic-class classes.mediumMotor>currentClass
927970

928971
class MediumMotor(Motor):
@@ -940,6 +983,12 @@ def __init__(self, address=None, name_pattern=SYSTEM_DEVICE_NAME_CONVENTION, nam
940983

941984

942985
# ~autogen
986+
__slots__ = [
987+
# ~autogen generic-class-slots classes.mediumMotor>currentClass
988+
989+
990+
# ~autogen
991+
]
943992
# ~autogen generic-class classes.actuonix50Motor>currentClass
944993

945994
class ActuonixL1250Motor(Motor):
@@ -957,6 +1006,12 @@ def __init__(self, address=None, name_pattern=SYSTEM_DEVICE_NAME_CONVENTION, nam
9571006

9581007

9591008
# ~autogen
1009+
__slots__ = [
1010+
# ~autogen generic-class-slots classes.actuonix50Motor>currentClass
1011+
1012+
1013+
# ~autogen
1014+
]
9601015
# ~autogen generic-class classes.actuonix100Motor>currentClass
9611016

9621017
class ActuonixL12100Motor(Motor):
@@ -974,6 +1029,12 @@ def __init__(self, address=None, name_pattern=SYSTEM_DEVICE_NAME_CONVENTION, nam
9741029

9751030

9761031
# ~autogen
1032+
__slots__ = [
1033+
# ~autogen generic-class-slots classes.actuonix100Motor>currentClass
1034+
1035+
1036+
# ~autogen
1037+
]
9771038
# ~autogen generic-class classes.dcMotor>currentClass
9781039

9791040
class DcMotor(Device):
@@ -1009,6 +1070,26 @@ def __init__(self, address=None, name_pattern=SYSTEM_DEVICE_NAME_CONVENTION, nam
10091070

10101071
# ~autogen
10111072

1073+
__slots__ = [
1074+
# ~autogen generic-class-slots classes.dcMotor>currentClass
1075+
1076+
'_address',
1077+
'_command',
1078+
'_commands',
1079+
'_driver_name',
1080+
'_duty_cycle',
1081+
'_duty_cycle_sp',
1082+
'_polarity',
1083+
'_ramp_down_sp',
1084+
'_ramp_up_sp',
1085+
'_state',
1086+
'_stop_action',
1087+
'_stop_actions',
1088+
'_time_sp',
1089+
1090+
# ~autogen
1091+
]
1092+
10121093
# ~autogen generic-get-set classes.dcMotor>currentClass
10131094

10141095
@property
@@ -1263,6 +1344,23 @@ def __init__(self, address=None, name_pattern=SYSTEM_DEVICE_NAME_CONVENTION, nam
12631344

12641345
# ~autogen
12651346

1347+
__slots__ = [
1348+
# ~autogen generic-class-slots classes.servoMotor>currentClass
1349+
1350+
'_address',
1351+
'_command',
1352+
'_driver_name',
1353+
'_max_pulse_sp',
1354+
'_mid_pulse_sp',
1355+
'_min_pulse_sp',
1356+
'_polarity',
1357+
'_position_sp',
1358+
'_rate_sp',
1359+
'_state',
1360+
1361+
# ~autogen
1362+
]
1363+
12661364
# ~autogen generic-get-set classes.servoMotor>currentClass
12671365

12681366
@property
@@ -1482,8 +1580,29 @@ def __init__(self, address=None, name_pattern=SYSTEM_DEVICE_NAME_CONVENTION, nam
14821580
self._value = [None,None,None,None,None,None,None,None]
14831581

14841582
self._bin_data_format = None
1583+
self._bin_data_size = None
14851584
self._bin_data = None
14861585

1586+
__slots__ = [
1587+
# ~autogen generic-class-slots classes.sensor>currentClass
1588+
1589+
'_address',
1590+
'_command',
1591+
'_commands',
1592+
'_decimals',
1593+
'_driver_name',
1594+
'_mode',
1595+
'_modes',
1596+
'_num_values',
1597+
'_units',
1598+
1599+
# ~autogen
1600+
'_value',
1601+
'_bin_data_format',
1602+
'_bin_data_size',
1603+
'_bin_data',
1604+
]
1605+
14871606
# ~autogen generic-get-set classes.sensor>currentClass
14881607

14891608
@property
@@ -1629,7 +1748,7 @@ def bin_data(self, fmt=None):
16291748
(28,)
16301749
"""
16311750

1632-
if '_bin_data_size' not in self.__dict__:
1751+
if self._bin_data_size == None:
16331752
self._bin_data_size = {
16341753
"u8": 1,
16351754
"s8": 1,
@@ -1705,6 +1824,8 @@ class TouchSensor(Sensor):
17051824
Touch Sensor
17061825
"""
17071826

1827+
__slots__ = ['auto_mode']
1828+
17081829
SYSTEM_CLASS_NAME = Sensor.SYSTEM_CLASS_NAME
17091830
SYSTEM_DEVICE_NAME_CONVENTION = Sensor.SYSTEM_DEVICE_NAME_CONVENTION
17101831

@@ -1735,6 +1856,8 @@ class ColorSensor(Sensor):
17351856
LEGO EV3 color sensor.
17361857
"""
17371858

1859+
__slots__ = ['auto_mode']
1860+
17381861
SYSTEM_CLASS_NAME = Sensor.SYSTEM_CLASS_NAME
17391862
SYSTEM_DEVICE_NAME_CONVENTION = Sensor.SYSTEM_DEVICE_NAME_CONVENTION
17401863

@@ -1850,6 +1973,8 @@ class UltrasonicSensor(Sensor):
18501973
LEGO EV3 ultrasonic sensor.
18511974
"""
18521975

1976+
__slots__ = ['auto_mode']
1977+
18531978
SYSTEM_CLASS_NAME = Sensor.SYSTEM_CLASS_NAME
18541979
SYSTEM_DEVICE_NAME_CONVENTION = Sensor.SYSTEM_DEVICE_NAME_CONVENTION
18551980

@@ -1916,6 +2041,8 @@ class GyroSensor(Sensor):
19162041
LEGO EV3 gyro sensor.
19172042
"""
19182043

2044+
__slots__ = ['auto_mode']
2045+
19192046
SYSTEM_CLASS_NAME = Sensor.SYSTEM_CLASS_NAME
19202047
SYSTEM_DEVICE_NAME_CONVENTION = Sensor.SYSTEM_DEVICE_NAME_CONVENTION
19212048

@@ -1980,6 +2107,8 @@ class InfraredSensor(Sensor):
19802107
LEGO EV3 infrared sensor.
19812108
"""
19822109

2110+
__slots__ = ['auto_mode']
2111+
19832112
SYSTEM_CLASS_NAME = Sensor.SYSTEM_CLASS_NAME
19842113
SYSTEM_DEVICE_NAME_CONVENTION = Sensor.SYSTEM_DEVICE_NAME_CONVENTION
19852114

@@ -2022,6 +2151,8 @@ class SoundSensor(Sensor):
20222151
LEGO NXT Sound Sensor
20232152
"""
20242153

2154+
__slots__ = ['auto_mode']
2155+
20252156
SYSTEM_CLASS_NAME = Sensor.SYSTEM_CLASS_NAME
20262157
SYSTEM_DEVICE_NAME_CONVENTION = Sensor.SYSTEM_DEVICE_NAME_CONVENTION
20272158

@@ -2067,6 +2198,8 @@ class LightSensor(Sensor):
20672198
LEGO NXT Light Sensor
20682199
"""
20692200

2201+
__slots__ = ['auto_mode']
2202+
20702203
SYSTEM_CLASS_NAME = Sensor.SYSTEM_CLASS_NAME
20712204
SYSTEM_DEVICE_NAME_CONVENTION = Sensor.SYSTEM_DEVICE_NAME_CONVENTION
20722205

@@ -2134,6 +2267,20 @@ def __init__(self, address=None, name_pattern=SYSTEM_DEVICE_NAME_CONVENTION, nam
21342267
self._delay_off = None
21352268

21362269
# ~autogen
2270+
2271+
__slots__ = [
2272+
# ~autogen generic-class-slots classes.led>currentClass
2273+
2274+
'_max_brightness',
2275+
'_brightness',
2276+
'_triggers',
2277+
'_trigger',
2278+
'_delay_on',
2279+
'_delay_off',
2280+
2281+
# ~autogen
2282+
]
2283+
21372284
# ~autogen generic-get-set classes.led>currentClass
21382285

21392286
@property
@@ -2164,6 +2311,8 @@ def triggers(self):
21642311
self._triggers, value = self.get_attr_set(self._triggers, 'trigger')
21652312
return value
21662313

2314+
2315+
21672316
# ~autogen
21682317

21692318
@property
@@ -2526,6 +2675,20 @@ def __init__(self, address=None, name_pattern=SYSTEM_DEVICE_NAME_CONVENTION, nam
25262675
self._type = None
25272676

25282677
# ~autogen
2678+
2679+
__slots__ = [
2680+
# ~autogen generic-class-slots classes.powerSupply>currentClass
2681+
2682+
'_measured_current',
2683+
'_measured_voltage',
2684+
'_max_voltage',
2685+
'_min_voltage',
2686+
'_technology',
2687+
'_type',
2688+
2689+
# ~autogen
2690+
]
2691+
25292692
# ~autogen generic-get-set classes.powerSupply>currentClass
25302693

25312694
@property
@@ -2639,6 +2802,20 @@ def __init__(self, address=None, name_pattern=SYSTEM_DEVICE_NAME_CONVENTION, nam
26392802
self._status = None
26402803

26412804
# ~autogen
2805+
2806+
__slots__ = [
2807+
# ~autogen generic-class-slots classes.legoPort>currentClass
2808+
2809+
'_address',
2810+
'_driver_name',
2811+
'_modes',
2812+
'_mode',
2813+
'_set_device',
2814+
'_status',
2815+
2816+
# ~autogen
2817+
]
2818+
26422819
# ~autogen generic-get-set classes.legoPort>currentClass
26432820

26442821
@property
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{% for prop in currentClass.systemProperties %}{%
2+
assign prop_name = prop.name | downcase | underscore_spaces %}
3+
'_{{ prop_name }}',{%
4+
endfor %}

templates/special-sensors.liquid

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ for line in currentClass.description %}{%
2828
{% endif %}{%
2929
endfor %}
3030
"""
31+
32+
__slots__ = ['auto_mode']
3133
{% if currentClass.inheritance %}
3234
SYSTEM_CLASS_NAME = {{ base_class }}.SYSTEM_CLASS_NAME
3335
SYSTEM_DEVICE_NAME_CONVENTION = {{ base_class }}.SYSTEM_DEVICE_NAME_CONVENTION

0 commit comments

Comments
 (0)