Replace Event.wait(0) with Event.is_set()

The functionality is identical, and IMHO the latter is much more readable
This commit is contained in:
Andrew Scheller
2016-05-10 15:03:28 +01:00
parent c44a4bfe4b
commit 87d00f9047
4 changed files with 13 additions and 13 deletions

View File

@@ -162,7 +162,7 @@ class SmoothedInputDevice(EventsMixin, InputDevice):
except DeviceClosed: except DeviceClosed:
return super(SmoothedInputDevice, self).__repr__() return super(SmoothedInputDevice, self).__repr__()
else: else:
if self.partial or self._queue.full.wait(0): if self.partial or self._queue.full.is_set():
return super(SmoothedInputDevice, self).__repr__() return super(SmoothedInputDevice, self).__repr__()
else: else:
return "<gpiozero.%s object on pin=%r, pull_up=%s>" % ( return "<gpiozero.%s object on pin=%r, pull_up=%s>" % (

View File

@@ -234,7 +234,7 @@ class EventsMixin(object):
The length of time (in seconds) that the device has been active for. The length of time (in seconds) that the device has been active for.
When the device is inactive, this is ``None``. When the device is inactive, this is ``None``.
""" """
if self._active_event.wait(0): if self._active_event.is_set():
return time() - self._last_changed return time() - self._last_changed
else: else:
return None return None
@@ -245,7 +245,7 @@ class EventsMixin(object):
The length of time (in seconds) that the device has been inactive for. The length of time (in seconds) that the device has been inactive for.
When the device is active, this is ``None``. When the device is active, this is ``None``.
""" """
if self._inactive_event.wait(0): if self._inactive_event.is_set():
return time() - self._last_changed return time() - self._last_changed
else: else:
return None return None
@@ -434,11 +434,11 @@ class HoldThread(GPIOThread):
self.start() self.start()
def held(self, parent): def held(self, parent):
while not self.stopping.wait(0): while not self.stopping.is_set():
if self.holding.wait(0.1): if self.holding.wait(0.1):
self.holding.clear() self.holding.clear()
while not ( while not (
self.stopping.wait(0) or self.stopping.is_set() or
parent._inactive_event.wait(parent.hold_time) parent._inactive_event.wait(parent.hold_time)
): ):
if parent._held_from is None: if parent._held_from is None:

View File

@@ -64,20 +64,20 @@ def test_input_event_activated():
pin = MockPin(2) pin = MockPin(2)
with DigitalInputDevice(pin) as device: with DigitalInputDevice(pin) as device:
device.when_activated = lambda: event.set() device.when_activated = lambda: event.set()
assert not event.wait(0) assert not event.is_set()
pin.drive_high() pin.drive_high()
assert event.wait(0) assert event.is_set()
def test_input_event_deactivated(): def test_input_event_deactivated():
event = Event() event = Event()
pin = MockPin(2) pin = MockPin(2)
with DigitalInputDevice(pin) as device: with DigitalInputDevice(pin) as device:
device.when_deactivated = lambda: event.set() device.when_deactivated = lambda: event.set()
assert not event.wait(0) assert not event.is_set()
pin.drive_high() pin.drive_high()
assert not event.wait(0) assert not event.is_set()
pin.drive_low() pin.drive_low()
assert event.wait(0) assert event.is_set()
def test_input_wait_active(): def test_input_wait_active():
pin = MockPin(2) pin = MockPin(2)

View File

@@ -160,15 +160,15 @@ def test_mock_pin_edges():
pin.when_changed = changed pin.when_changed = changed
pin.drive_high() pin.drive_high()
assert pin.state assert pin.state
assert fired.wait(0) assert fired.is_set()
fired.clear() fired.clear()
pin.edges = 'falling' pin.edges = 'falling'
pin.drive_low() pin.drive_low()
assert not pin.state assert not pin.state
assert fired.wait(0) assert fired.is_set()
fired.clear() fired.clear()
pin.drive_high() pin.drive_high()
assert pin.state assert pin.state
assert not fired.wait(0) assert not fired.is_set()
assert pin.edges == 'falling' assert pin.edges == 'falling'