Skip to content

Commit 0d60fca

Browse files
authored
cache attributes that never change (ev3dev#564)
For issue ev3dev#365
1 parent cfda62c commit 0d60fca

6 files changed

Lines changed: 53 additions & 19 deletions

File tree

‎debian/changelog‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
python-ev3dev2 (2.0.0~beta3) stable; urgency=medium
22

33
[Daniel Walton]
4+
* cache attributes that never change
45
* Display: correct xy variable names passed to Display.rectangle()
56
* GyroSensor wait_until_angle_changed_by added direction_sensitive option
67
* Added MoveDifferential

‎ev3dev2/__init__.py‎

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def get_current_platform():
5757
"""
5858
board_info_dir = '/sys/class/board-info/'
5959

60-
if not os.path.exists(board_info_dir):
60+
if not os.path.exists(board_info_dir) or os.environ.get("FAKE_SYS"):
6161
return 'fake'
6262

6363
for board in os.listdir(board_info_dir):
@@ -145,7 +145,12 @@ class DeviceNotFound(Exception):
145145
class Device(object):
146146
"""The ev3dev device base class"""
147147

148-
__slots__ = ['_path', '_device_index', 'kwargs']
148+
__slots__ = [
149+
'_path',
150+
'_device_index',
151+
'_attr_cache',
152+
'kwargs',
153+
]
149154

150155
DEVICE_ROOT_PATH = '/sys/class'
151156

@@ -178,6 +183,7 @@ def __init__(self, class_name, name_pattern='*', name_exact=False, **kwargs):
178183

179184
classpath = abspath(Device.DEVICE_ROOT_PATH + '/' + class_name)
180185
self.kwargs = kwargs
186+
self._attr_cache = {}
181187

182188
def get_index(file):
183189
match = Device._DEVICE_INDEX.match(file)
@@ -281,6 +287,15 @@ def get_attr_int(self, attribute, name):
281287
attribute, value = self._get_attribute(attribute, name)
282288
return attribute, int(value)
283289

290+
def get_cached_attr_int(self, filehandle, keyword):
291+
value = self._attr_cache.get(keyword)
292+
293+
if value is None:
294+
(filehandle, value) = self.get_attr_int(filehandle, keyword)
295+
self._attr_cache[keyword] = value
296+
297+
return (filehandle, value)
298+
284299
def set_attr_int(self, attribute, name, value):
285300
return self._set_attribute(attribute, name, str(int(value)))
286301

@@ -290,6 +305,15 @@ def set_attr_raw(self, attribute, name, value):
290305
def get_attr_string(self, attribute, name):
291306
return self._get_attribute(attribute, name)
292307

308+
def get_cached_attr_string(self, filehandle, keyword):
309+
value = self._attr_cache.get(keyword)
310+
311+
if value is None:
312+
(filehandle, value) = self.get_attr_string(filehandle, keyword)
313+
self._attr_cache[keyword] = value
314+
315+
return (filehandle, value)
316+
293317
def set_attr_string(self, attribute, name, value):
294318
return self._set_attribute(attribute, name, value)
295319

@@ -300,6 +324,15 @@ def get_attr_set(self, attribute, name):
300324
attribute, value = self.get_attr_line(attribute, name)
301325
return attribute, [v.strip('[]') for v in value.split()]
302326

327+
def get_cached_attr_set(self, filehandle, keyword):
328+
value = self._attr_cache.get(keyword)
329+
330+
if value is None:
331+
(filehandle, value) = self.get_attr_set(filehandle, keyword)
332+
self._attr_cache[keyword] = value
333+
334+
return (filehandle, value)
335+
303336
def get_attr_from_set(self, attribute, name):
304337
attribute, value = self.get_attr_line(attribute, name)
305338
for a in value.split():

‎ev3dev2/motor.py‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ def commands(self):
438438
- `reset` will reset all of the motor parameter attributes to their default value.
439439
This will also have the effect of stopping the motor.
440440
"""
441-
self._commands, value = self.get_attr_set(self._commands, 'commands')
441+
(self._commands, value) = self.get_cached_attr_set(self._commands, 'commands')
442442
return value
443443

444444
@property
@@ -448,7 +448,7 @@ def count_per_rot(self):
448448
are used by the position and speed attributes, so you can use this value
449449
to convert rotations or degrees to tacho counts. (rotation motors only)
450450
"""
451-
self._count_per_rot, value = self.get_attr_int(self._count_per_rot, 'count_per_rot')
451+
(self._count_per_rot, value) = self.get_cached_attr_int(self._count_per_rot, 'count_per_rot')
452452
return value
453453

454454
@property
@@ -458,15 +458,15 @@ def count_per_m(self):
458458
counts are used by the position and speed attributes, so you can use this
459459
value to convert from distance to tacho counts. (linear motors only)
460460
"""
461-
self._count_per_m, value = self.get_attr_int(self._count_per_m, 'count_per_m')
461+
(self._count_per_m, value) = self.get_cached_attr_int(self._count_per_m, 'count_per_m')
462462
return value
463463

464464
@property
465465
def driver_name(self):
466466
"""
467467
Returns the name of the driver that provides this tacho motor device.
468468
"""
469-
self._driver_name, value = self.get_attr_string(self._driver_name, 'driver_name')
469+
(self._driver_name, value) = self.get_cached_attr_string(self._driver_name, 'driver_name')
470470
return value
471471

472472
@property
@@ -499,7 +499,7 @@ def full_travel_count(self):
499499
combined with the `count_per_m` atribute, you can use this value to
500500
calculate the maximum travel distance of the motor. (linear motors only)
501501
"""
502-
self._full_travel_count, value = self.get_attr_int(self._full_travel_count, 'full_travel_count')
502+
(self._full_travel_count, value) = self.get_cached_attr_int(self._full_travel_count, 'full_travel_count')
503503
return value
504504

505505
@property
@@ -590,7 +590,7 @@ def max_speed(self):
590590
may be slightly different than the maximum speed that a particular motor can
591591
reach - it's the maximum theoretical speed.
592592
"""
593-
self._max_speed, value = self.get_attr_int(self._max_speed, 'max_speed')
593+
(self._max_speed, value) = self.get_cached_attr_int(self._max_speed, 'max_speed')
594594
return value
595595

596596
@property
@@ -726,7 +726,7 @@ def stop_actions(self):
726726
position. If an external force tries to turn the motor, the motor will 'push
727727
back' to maintain its position.
728728
"""
729-
self._stop_actions, value = self.get_attr_set(self._stop_actions, 'stop_actions')
729+
(self._stop_actions, value) = self.get_cached_attr_set(self._stop_actions, 'stop_actions')
730730
return value
731731

732732
@property

‎ev3dev2/port.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,15 @@ def driver_name(self):
9898
Returns the name of the driver that loaded this device. You can find the
9999
complete list of drivers in the [list of port drivers].
100100
"""
101-
self._driver_name, value = self.get_attr_string(self._driver_name, 'driver_name')
101+
(self._driver_name, value) = self.get_cached_attr_string(self._driver_name, 'driver_name')
102102
return value
103103

104104
@property
105105
def modes(self):
106106
"""
107107
Returns a list of the available modes of the port.
108108
"""
109-
self._modes, value = self.get_attr_set(self._modes, 'modes')
109+
(self._modes, value) = self.get_cached_attr_set(self._modes, 'modes')
110110
return value
111111

112112
@property

‎ev3dev2/sensor/__init__.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def commands(self):
144144
Returns a list of the valid commands for the sensor.
145145
Returns -EOPNOTSUPP if no commands are supported.
146146
"""
147-
self._commands, value = self.get_attr_set(self._commands, 'commands')
147+
(self._commands, value) = self.get_cached_attr_set(self._commands, 'commands')
148148
return value
149149

150150
@property
@@ -162,7 +162,7 @@ def driver_name(self):
162162
Returns the name of the sensor device/driver. See the list of [supported
163163
sensors] for a complete list of drivers.
164164
"""
165-
self._driver_name, value = self.get_attr_string(self._driver_name, 'driver_name')
165+
(self._driver_name, value) = self.get_cached_attr_string(self._driver_name, 'driver_name')
166166
return value
167167

168168
@property
@@ -183,7 +183,7 @@ def modes(self):
183183
"""
184184
Returns a list of the valid modes for the sensor.
185185
"""
186-
self._modes, value = self.get_attr_set(self._modes, 'modes')
186+
(self._modes, value) = self.get_cached_attr_set(self._modes, 'modes')
187187
return value
188188

189189
@property
@@ -313,7 +313,7 @@ def fw_version(self):
313313
Returns the firmware version of the sensor if available. Currently only
314314
I2C/NXT sensors support this.
315315
"""
316-
self._fw_version, value = self.get_attr_string(self._fw_version, 'fw_version')
316+
(self._fw_version, value) = self.get_cached_attr_string(self._fw_version, 'fw_version')
317317
return value
318318

319319
@property

‎tests/api_tests.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#!/usr/bin/env python3
22
import unittest, sys, os.path
3+
import os
34

45
FAKE_SYS = os.path.join(os.path.dirname(__file__), 'fake-sys')
6+
os.environ["FAKE_SYS"] = "1"
57

68
sys.path.append(FAKE_SYS)
79
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
@@ -12,6 +14,7 @@
1214
import ev3dev2
1315
from ev3dev2.sensor.lego import InfraredSensor
1416
from ev3dev2.motor import \
17+
OUTPUT_A, OUTPUT_B, \
1518
Motor, MediumMotor, LargeMotor, \
1619
MoveTank, MoveSteering, MoveJoystick, \
1720
SpeedPercent, SpeedDPM, SpeedDPS, SpeedRPM, SpeedRPS, SpeedNativeUnits
@@ -27,10 +30,6 @@
2730
DistanceStuds
2831
)
2932

30-
# We force OUTPUT_A and OUTPUT_B to be imported from platform "fake" so that
31-
# we can run these test cases on an EV3, brickpi3, etc.
32-
from ev3dev2._platform.fake import OUTPUT_A, OUTPUT_B
33-
3433
ev3dev2.Device.DEVICE_ROOT_PATH = os.path.join(FAKE_SYS, 'arena')
3534

3635
_internal_set_attribute = ev3dev2.Device._set_attribute
@@ -356,3 +355,4 @@ def test_units(self):
356355

357356
if __name__ == "__main__":
358357
unittest.main()
358+
del os.environ["FAKE_SYS"]

0 commit comments

Comments
 (0)