From 6b7a3c8851d731ef4ee4a3e54b4072a0886ed72b Mon Sep 17 00:00:00 2001 From: Dave Jones Date: Wed, 23 Sep 2015 13:52:41 +0100 Subject: [PATCH] Fix #25 Add a nice __repr__ to the GPIODevice base class. This isn't much but generally I think `__repr__` implementations should be deliberately simple: firstly, they're frequently used for debugging so if they're at all complex you risk making a debugging tool buggy (very annoying!). Secondly, if you pour too much info into them you risk making the debugging output cluttered, so I tend to prefer keeping it to straight-forward simple to retrieve/calculate info without excessive detail (if the user wants more, they can always query it directly). There is one refinement here: in SmoothedInputDevice, `__repr__` is tweaked to ensure that when partial is False (the default), and the queue isn't filled, `__repr__` doesn't block (because it should *never* block). --- gpiozero/devices.py | 4 ++++ gpiozero/input_devices.py | 11 +++++++++++ 2 files changed, 15 insertions(+) diff --git a/gpiozero/devices.py b/gpiozero/devices.py index 88f9fe5..592ecc3 100644 --- a/gpiozero/devices.py +++ b/gpiozero/devices.py @@ -31,6 +31,10 @@ class GPIODevice(object): def is_active(self): return self._read() + def __repr__(self): + return "" % ( + self.__class__.__name__, self.pin, self.is_active) + _GPIO_THREADS = set() diff --git a/gpiozero/input_devices.py b/gpiozero/input_devices.py index bef371d..5fc18a3 100644 --- a/gpiozero/input_devices.py +++ b/gpiozero/input_devices.py @@ -36,6 +36,10 @@ class InputDevice(GPIODevice): def pull_up(self): return self._pull_up + def __repr__(self): + return "" % ( + self.__class__.__name__, self.pin, self.pull_up, self.is_active) + class WaitableInputDevice(InputDevice): def __init__(self, pin=None, pull_up=False): @@ -143,6 +147,13 @@ class SmoothedInputDevice(WaitableInputDevice): self._queue = GPIOQueue(self, queue_len, sample_wait, partial) self.threshold = float(threshold) + def __repr__(self): + if self.partial or self._queue.full.wait(0): + return super(SmoothedInputDevice, self).__repr__() + else: + return "" % ( + self.__class__.__name__, self.pin, self.pull_up) + @property def queue_len(self): return self._queue.queue.maxlen