Skip to content

Commit

Permalink
Support generators in setup/cleanup
Browse files Browse the repository at this point in the history
- Support generators such as dict.keys()
- Add comment to explain the use of list(dict.keys()) in cleanup
- Add tests for generators
- Use None instead of '' in tests, since '' results in an empty iterable and a quiet noop in cleanup/setup
  • Loading branch information
Gadgetoid committed Aug 12, 2021
1 parent 7ad2400 commit 6139351
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
13 changes: 7 additions & 6 deletions gpio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
__version__ = '1.0.0'

from threading import Lock
try:
from collections.abc import Iterable
except ImportError:
from collections import Iterable
import os


Expand Down Expand Up @@ -148,12 +152,8 @@ def write(self, value):
Args:
value (bool): use either gpio.HIGH or gpio.LOW
'''
# Convert any truthy value explicitly to HIGH and vice versa
# this is about 3x faster than int(bool(value))
value = HIGH if value else LOW
# write as bytes, about 3x faster than string IO
self.value.write(b'1' if value else b'0')
# state.value.write(str(value).encode()) # Slow alternate for Python 2

def cleanup(self):
'''Clean up pin
Expand Down Expand Up @@ -188,9 +188,10 @@ def cleanup(pin=None, assert_exists=False):
pins = pin

if pins is None:
# Must be converted to a list since _open_pins is potentially modified below
pins = list(_open_pins.keys())

if type(pins) not in (list, tuple):
if not isinstance(pins, Iterable):
pins = [pins]

for pin in pins:
Expand All @@ -213,7 +214,7 @@ def setup(pins, mode, pullup=None, initial=LOW, active_low=None):
active_low (bool, optional): Set the pin to active low. Default
is None which leaves things as configured in sysfs
'''
if type(pins) not in (list, tuple):
if not isinstance(pins, Iterable):
pins = [pins]

if pullup is not None:
Expand Down
21 changes: 17 additions & 4 deletions tests/test_gpio.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ def test_setup_class(gpio, patch_open):
patch_open().__enter__().write.assert_any_call(str(gpio.OUT))


def test_setup_rpio_list(gpio, patch_open):
gpio.setup([9, 10, 11], gpio.OUT)


def test_setup_rpio_tuple(gpio, patch_open):
gpio.setup((9, 10, 11), gpio.OUT)


def test_setup_rpio_generator(gpio, patch_open):
pins = {9: 9, 10: 10, 11: 11}
gpio.setup(pins.keys(), gpio.OUT)


def test_setup_with_pull(gpio, patch_open):
with pytest.raises(ValueError):
gpio.setup(10, gpio.OUT, pullup=1)
Expand Down Expand Up @@ -92,10 +105,10 @@ def test_cleanup_class_unexports_pin(gpio, patch_open):

def test_setup_pin_is_not_int(gpio, patch_open):
with pytest.raises(ValueError):
gpio.setup('', gpio.OUT)
gpio.setup(None, gpio.OUT)

with pytest.raises(ValueError):
pin = gpio.GPIOPin('', gpio.OUT)
pin = gpio.GPIOPin(None, gpio.OUT)


def test_cleanup_class_unregisters_self(gpio, patch_open):
Expand Down Expand Up @@ -132,7 +145,7 @@ def test_set_active_low(gpio, patch_open):
))

with pytest.raises(ValueError):
pin.set_active_low('')
pin.set_active_low(None)


def test_setup_active_low(gpio, patch_open):
Expand Down Expand Up @@ -174,7 +187,7 @@ def test_set_direction(gpio, patch_open):
))

with pytest.raises(ValueError):
pin.set_direction('')
pin.set_direction(None)


def test_unconfigured_runtimeerror(gpio, patch_open):
Expand Down

0 comments on commit 6139351

Please sign in to comment.