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).
This commit is contained in:
Dave Jones
2015-09-23 13:52:41 +01:00
parent d869d1e092
commit 6b7a3c8851
2 changed files with 15 additions and 0 deletions

View File

@@ -36,6 +36,10 @@ class InputDevice(GPIODevice):
def pull_up(self):
return self._pull_up
def __repr__(self):
return "<gpiozero.%s object on pin=%d, pull_up=%s, is_active=%s>" % (
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 "<gpiozero.%s object on pin=%d, pull_up=%s>" % (
self.__class__.__name__, self.pin, self.pull_up)
@property
def queue_len(self):
return self._queue.queue.maxlen