From 72ca07566854d8d9c6b498eda94504a92d74d953 Mon Sep 17 00:00:00 2001 From: Andrew Scheller Date: Thu, 7 Apr 2016 16:12:17 +0100 Subject: [PATCH] fix various bugs found by the 'prospector' static-analysis tool --- gpiozero/boards.py | 2 +- gpiozero/devices.py | 3 +-- gpiozero/input_devices.py | 7 +++---- gpiozero/mixins.py | 13 +++++++++---- gpiozero/output_devices.py | 4 +--- gpiozero/pins/data.py | 2 +- gpiozero/pins/mock.py | 4 ++-- gpiozero/pins/pigpiod.py | 5 +++-- gpiozero/pins/rpio.py | 6 +++--- gpiozero/source_tools.py | 4 ++-- gpiozero/spi.py | 2 +- gpiozero/spi_devices.py | 6 +++--- gpiozero/threads.py | 9 +++------ 13 files changed, 33 insertions(+), 34 deletions(-) diff --git a/gpiozero/boards.py b/gpiozero/boards.py index 9bcffb3..24bd8fc 100644 --- a/gpiozero/boards.py +++ b/gpiozero/boards.py @@ -10,11 +10,11 @@ except ImportError: pass from time import sleep -from collections import namedtuple from itertools import repeat, cycle, chain from threading import Lock from .exc import ( + DeviceClosed, GPIOPinMissing, EnergenieSocketMissing, EnergenieBadSocket, diff --git a/gpiozero/devices.py b/gpiozero/devices.py index c4cbd90..90e2375 100644 --- a/gpiozero/devices.py +++ b/gpiozero/devices.py @@ -14,7 +14,7 @@ from itertools import chain from types import FunctionType from threading import RLock -from .threads import GPIOThread, _threads_shutdown +from .threads import _threads_shutdown from .mixins import ( ValuesMixin, SharedMixin, @@ -26,7 +26,6 @@ from .exc import ( GPIOPinMissing, GPIOPinInUse, GPIODeviceClosed, - GPIOBadSourceDelay, ) # Get a pin implementation to use as the default; we prefer RPi.GPIO's here diff --git a/gpiozero/input_devices.py b/gpiozero/input_devices.py index fc08de5..957af34 100644 --- a/gpiozero/input_devices.py +++ b/gpiozero/input_devices.py @@ -7,12 +7,11 @@ from __future__ import ( division, ) -import warnings from time import sleep, time from threading import Event -from .exc import InputDeviceError, GPIODeviceError, DeviceClosed -from .devices import GPIODevice, CompositeDevice +from .exc import InputDeviceError, DeviceClosed +from .devices import GPIODevice from .mixins import GPIOQueue, EventsMixin @@ -560,7 +559,7 @@ class DistanceSensor(SmoothedInputDevice): def __init__( self, echo=None, trigger=None, queue_len=30, max_distance=1, threshold_distance=0.3, partial=False): - if not (max_distance > 0): + if max_distance <= 0: raise ValueError('invalid maximum distance (must be positive)') self._trigger = None super(DistanceSensor, self).__init__( diff --git a/gpiozero/mixins.py b/gpiozero/mixins.py index 979b917..7ca534a 100644 --- a/gpiozero/mixins.py +++ b/gpiozero/mixins.py @@ -13,13 +13,18 @@ from functools import wraps from threading import Event from collections import deque try: - from statistics import median, mean + from statistics import median except ImportError: - from .compat import median, mean + from .compat import median from .threads import GPIOThread -from .exc import BadEventHandler, DeviceClosed - +from .exc import ( + BadEventHandler, + DeviceClosed, + GPIOBadSourceDelay, + GPIOBadQueueLen, + GPIOBadSampleWait, + ) class ValuesMixin(object): """ diff --git a/gpiozero/output_devices.py b/gpiozero/output_devices.py index 276e92e..8964bdf 100644 --- a/gpiozero/output_devices.py +++ b/gpiozero/output_devices.py @@ -5,8 +5,6 @@ from __future__ import ( division, ) -import warnings -from time import sleep from threading import Lock from itertools import repeat, cycle, chain @@ -194,7 +192,7 @@ class DigitalOutputDevice(OutputDevice): def _blink_device(self, on_time, off_time, n): iterable = repeat(0) if n is None else repeat(0, n) - for i in iterable: + for _ in iterable: self._write(True) if self._blink_thread.stopping.wait(on_time): break diff --git a/gpiozero/pins/data.py b/gpiozero/pins/data.py index 133b94f..d15315e 100644 --- a/gpiozero/pins/data.py +++ b/gpiozero/pins/data.py @@ -9,7 +9,7 @@ str = type('') import io from collections import namedtuple -from ..exc import PinUnknownPi, PinMultiplePins +from ..exc import PinUnknownPi, PinMultiplePins, PinNoPins # Some useful constants for describing pins diff --git a/gpiozero/pins/mock.py b/gpiozero/pins/mock.py index 53b1023..e98180a 100644 --- a/gpiozero/pins/mock.py +++ b/gpiozero/pins/mock.py @@ -15,7 +15,7 @@ try: except ImportError: from ..compat import isclose -from . import Pin, PINS_CLEANUP +from . import Pin from ..exc import PinSetInput, PinPWMUnsupported, PinFixedPull @@ -38,7 +38,7 @@ class MockPin(Pin): try: old_pin = cls._PINS[number] except KeyError: - self = super(Pin, cls).__new__(cls) + self = super(MockPin, cls).__new__(cls) cls._PINS[number] = self self._number = number self._function = 'input' diff --git a/gpiozero/pins/pigpiod.py b/gpiozero/pins/pigpiod.py index c591ba6..25b3270 100644 --- a/gpiozero/pins/pigpiod.py +++ b/gpiozero/pins/pigpiod.py @@ -16,6 +16,7 @@ from ..exc import ( PinFixedPull, PinInvalidPull, PinInvalidBounce, + PinInvalidState, ) @@ -135,7 +136,7 @@ class PiGPIOPin(Pin): if self._host == 'localhost': return "GPIO%d" % self._number else: - return "GPIO%d on %s:%d" % (self._host, self._port) + return "GPIO%d on %s:%d" % (self._number, self._host, self._port) @property def host(self): @@ -182,7 +183,7 @@ class PiGPIOPin(Pin): try: self._connection.set_PWM_dutycycle(self._number, int(value * 255)) except pigpio.error: - raise PinInvalidValue('invalid state "%s" for pin %r' % (value, self)) + raise PinInvalidState('invalid state "%s" for pin %r' % (value, self)) elif self.function == 'input': raise PinSetInput('cannot set state of pin %r' % self) else: diff --git a/gpiozero/pins/rpio.py b/gpiozero/pins/rpio.py index 8449ad6..845424c 100644 --- a/gpiozero/pins/rpio.py +++ b/gpiozero/pins/rpio.py @@ -7,8 +7,6 @@ from __future__ import ( str = type('') -from threading import Lock - import RPIO import RPIO.PWM from RPIO.Exceptions import InvalidChannelException @@ -21,6 +19,8 @@ from ..exc import ( PinFixedPull, PinInvalidPull, PinInvalidBounce, + PinInvalidState, + PinPWMError, ) @@ -126,7 +126,7 @@ class RPIOPin(Pin): def _set_state(self, value): if not 0 <= value <= 1: - raise PinInvalidValue('invalid state "%s" for pin %r' % (value, self)) + raise PinInvalidState('invalid state "%s" for pin %r' % (value, self)) if self._pwm: RPIO.PWM.clear_channel_gpio(0, self._number) if value == 0: diff --git a/gpiozero/source_tools.py b/gpiozero/source_tools.py index c7f6847..6832f32 100644 --- a/gpiozero/source_tools.py +++ b/gpiozero/source_tools.py @@ -15,8 +15,8 @@ try: from itertools import izip as zip except ImportError: pass -from itertools import count, cycle -from math import sin, cos, floor, radians +from itertools import cycle +from math import sin, cos, radians try: from statistics import mean except ImportError: diff --git a/gpiozero/spi.py b/gpiozero/spi.py index 4574204..9a4e712 100644 --- a/gpiozero/spi.py +++ b/gpiozero/spi.py @@ -184,7 +184,7 @@ class SPISoftwareBus(SharedMixin, Device): for write_word in data: mask = 1 if self.lsb_first else 1 << (self.bits_per_word - 1) read_word = 0 - for bit in range(self.bits_per_word): + for _ in range(self.bits_per_word): if self.mosi is not None: self.mosi.value = bool(write_word & mask) self.clock.on() diff --git a/gpiozero/spi_devices.py b/gpiozero/spi_devices.py index 8dad97c..0121019 100644 --- a/gpiozero/spi_devices.py +++ b/gpiozero/spi_devices.py @@ -7,9 +7,9 @@ from __future__ import ( str = type('') -from .exc import DeviceClosed +from .exc import DeviceClosed, InputDeviceError from .devices import Device -from .spi import extract_spi_args, SPI +from .spi import SPI class SPIDevice(Device): @@ -206,7 +206,7 @@ class MCP33xx(MCP3xxx): data = data[-2:] result = ((data[0] & 63) << 7) | (data[1] >> 1) # Account for the sign bit - if self.differential and value > 4095: + if self.differential and result > 4095: result = -(8192 - result) assert -4096 <= result < 4096 return result diff --git a/gpiozero/threads.py b/gpiozero/threads.py index 8a07b8a..f54ce4e 100644 --- a/gpiozero/threads.py +++ b/gpiozero/threads.py @@ -8,11 +8,6 @@ str = type('') from threading import Thread, Event -from .exc import ( - GPIOBadQueueLen, - GPIOBadSampleWait, - ) - _THREADS = set() def _threads_shutdown(): @@ -22,7 +17,9 @@ def _threads_shutdown(): class GPIOThread(Thread): - def __init__(self, group=None, target=None, name=None, args=(), kwargs={}): + def __init__(self, group=None, target=None, name=None, args=(), kwargs=None): + if kwargs is None: + kwargs = {} self.stopping = Event() super(GPIOThread, self).__init__(group, target, name, args, kwargs) self.daemon = True