Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
python-ev3dev2 (2.0.0~beta3) stable; urgency=medium

[Daniel Walton]
* cache attributes that never change
* Display: correct xy variable names passed to Display.rectangle()
* GyroSensor wait_until_angle_changed_by added direction_sensitive option
* Added MoveDifferential
Expand Down
37 changes: 35 additions & 2 deletions ev3dev2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def get_current_platform():
"""
board_info_dir = '/sys/class/board-info/'

if not os.path.exists(board_info_dir):
if not os.path.exists(board_info_dir) or os.environ.get("FAKE_SYS"):
return 'fake'

for board in os.listdir(board_info_dir):
Expand Down Expand Up @@ -145,7 +145,12 @@ class DeviceNotFound(Exception):
class Device(object):
"""The ev3dev device base class"""

__slots__ = ['_path', '_device_index', 'kwargs']
__slots__ = [
'_path',
'_device_index',
'_attr_cache',
'kwargs',
]

DEVICE_ROOT_PATH = '/sys/class'

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

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

def get_index(file):
match = Device._DEVICE_INDEX.match(file)
Expand Down Expand Up @@ -281,6 +287,15 @@ def get_attr_int(self, attribute, name):
attribute, value = self._get_attribute(attribute, name)
return attribute, int(value)

def get_cached_attr_int(self, filehandle, keyword):
value = self._attr_cache.get(keyword)

if value is None:
(filehandle, value) = self.get_attr_int(filehandle, keyword)
self._attr_cache[keyword] = value

return (filehandle, value)

def set_attr_int(self, attribute, name, value):
return self._set_attribute(attribute, name, str(int(value)))

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

def get_cached_attr_string(self, filehandle, keyword):
value = self._attr_cache.get(keyword)

if value is None:
(filehandle, value) = self.get_attr_string(filehandle, keyword)
self._attr_cache[keyword] = value

return (filehandle, value)

def set_attr_string(self, attribute, name, value):
return self._set_attribute(attribute, name, value)

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

def get_cached_attr_set(self, filehandle, keyword):
value = self._attr_cache.get(keyword)

if value is None:
(filehandle, value) = self.get_attr_set(filehandle, keyword)
self._attr_cache[keyword] = value

return (filehandle, value)

def get_attr_from_set(self, attribute, name):
attribute, value = self.get_attr_line(attribute, name)
for a in value.split():
Expand Down
14 changes: 7 additions & 7 deletions ev3dev2/motor.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ def commands(self):
- `reset` will reset all of the motor parameter attributes to their default value.
This will also have the effect of stopping the motor.
"""
self._commands, value = self.get_attr_set(self._commands, 'commands')
(self._commands, value) = self.get_cached_attr_set(self._commands, 'commands')
return value

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

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

@property
def driver_name(self):
"""
Returns the name of the driver that provides this tacho motor device.
"""
self._driver_name, value = self.get_attr_string(self._driver_name, 'driver_name')
(self._driver_name, value) = self.get_cached_attr_string(self._driver_name, 'driver_name')
return value

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

@property
Expand Down Expand Up @@ -590,7 +590,7 @@ def max_speed(self):
may be slightly different than the maximum speed that a particular motor can
reach - it's the maximum theoretical speed.
"""
self._max_speed, value = self.get_attr_int(self._max_speed, 'max_speed')
(self._max_speed, value) = self.get_cached_attr_int(self._max_speed, 'max_speed')
return value

@property
Expand Down Expand Up @@ -726,7 +726,7 @@ def stop_actions(self):
position. If an external force tries to turn the motor, the motor will 'push
back' to maintain its position.
"""
self._stop_actions, value = self.get_attr_set(self._stop_actions, 'stop_actions')
(self._stop_actions, value) = self.get_cached_attr_set(self._stop_actions, 'stop_actions')
return value

@property
Expand Down
4 changes: 2 additions & 2 deletions ev3dev2/port.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,15 @@ def driver_name(self):
Returns the name of the driver that loaded this device. You can find the
complete list of drivers in the [list of port drivers].
"""
self._driver_name, value = self.get_attr_string(self._driver_name, 'driver_name')
(self._driver_name, value) = self.get_cached_attr_string(self._driver_name, 'driver_name')
Comment thread
WasabiFan marked this conversation as resolved.
Resolved
return value

@property
def modes(self):
"""
Returns a list of the available modes of the port.
"""
self._modes, value = self.get_attr_set(self._modes, 'modes')
(self._modes, value) = self.get_cached_attr_set(self._modes, 'modes')
Comment thread
WasabiFan marked this conversation as resolved.
Resolved
return value

@property
Expand Down
8 changes: 4 additions & 4 deletions ev3dev2/sensor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def commands(self):
Returns a list of the valid commands for the sensor.
Returns -EOPNOTSUPP if no commands are supported.
"""
self._commands, value = self.get_attr_set(self._commands, 'commands')
(self._commands, value) = self.get_cached_attr_set(self._commands, 'commands')
return value

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

@property
Expand All @@ -183,7 +183,7 @@ def modes(self):
"""
Returns a list of the valid modes for the sensor.
"""
self._modes, value = self.get_attr_set(self._modes, 'modes')
(self._modes, value) = self.get_cached_attr_set(self._modes, 'modes')
return value

@property
Expand Down Expand Up @@ -313,7 +313,7 @@ def fw_version(self):
Returns the firmware version of the sensor if available. Currently only
I2C/NXT sensors support this.
"""
self._fw_version, value = self.get_attr_string(self._fw_version, 'fw_version')
(self._fw_version, value) = self.get_cached_attr_string(self._fw_version, 'fw_version')
return value

@property
Expand Down
8 changes: 4 additions & 4 deletions tests/api_tests.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#!/usr/bin/env python3
import unittest, sys, os.path
import os

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

sys.path.append(FAKE_SYS)
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
Expand All @@ -12,6 +14,7 @@
import ev3dev2
from ev3dev2.sensor.lego import InfraredSensor
from ev3dev2.motor import \
OUTPUT_A, OUTPUT_B, \
Motor, MediumMotor, LargeMotor, \
MoveTank, MoveSteering, MoveJoystick, \
SpeedPercent, SpeedDPM, SpeedDPS, SpeedRPM, SpeedRPS, SpeedNativeUnits
Expand All @@ -27,10 +30,6 @@
DistanceStuds
)

# We force OUTPUT_A and OUTPUT_B to be imported from platform "fake" so that
# we can run these test cases on an EV3, brickpi3, etc.
from ev3dev2._platform.fake import OUTPUT_A, OUTPUT_B

ev3dev2.Device.DEVICE_ROOT_PATH = os.path.join(FAKE_SYS, 'arena')

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

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