Skip to content

Instantly share code, notes, and snippets.

@waveform80
Last active November 5, 2015 06:53
Show Gist options
  • Save waveform80/af91a6f5f198bff3a3c9 to your computer and use it in GitHub Desktop.
Save waveform80/af91a6f5f198bff3a3c9 to your computer and use it in GitHub Desktop.
from gpiozero import RyanteckRobot, Button
from signal import pause
robot = RyanteckRobot()
button_actions = {
Button(26): robot.forward,
Button(16): robot.left,
Button(21): robot.right,
Button(20): robot.backward,
}
for btn, action in button_actions.items():
btn.when_pressed = action
btn.when_released = robot.stop
pause()
from gpiozero import RyanteckRobot as R, Button as B
from signal import pause
r = R()
a = {
B(26): r.forward,
B(16): r.left,
B(21): r.right,
B(20): r.backward,
}
for b, m in button_actions.items():
b.when_pressed = m
b.when_released = r.stop
pause()
# When the going gets weird, the weird turn pro...
from gpiozero import RyanteckRobot as R, Button as B
from signal import pause
r = R()
s = setattr
x = lambda b, m: (b, s(b, 'when_pressed', m), s(b, 'when_released', r.stop))[0]
l = [x(B(p), m) for p, m in {
26: r.forward,
16: r.left,
21: r.right,
20: r.backward,
}.items()]
pause()
# For the truly bloody minded...
from gpiozero import RyanteckRobot as R, Button as B
from signal import pause
l = [
(lambda b, m: (
b,
setattr(b, 'when_pressed', m),
setattr(b, 'when_released', r.stop)
)[0])
(B(p), m)
for r in (R(),)
for p, m in (
(26, r.forward),
(16, r.left),
(21, r.right),
(20, r.backward),
)]
pause()
# Help
from gpiozero import RyanteckRobot as R, Button as B
from signal import pause
l = [
(b, s(b, 'when_pressed', m), s(b, 'when_released', r.stop))[0]
for r, s in (R(), setattr)
for p, m in (
(26, r.forward),
(16, r.left),
(21, r.right),
(20, r.backward),
)
for b in (B(p),)]
pause()
@waveform80
Copy link
Author

I don't seriously claim that 3 or 4 is in any way better, I was more intrigued whether I could come up with a pure functional method of assigning everything and keeping all the references alive at the end. And yes ... it's truly horrid.

@c0d3st0rm
Copy link

need to make it smaller ;). a valid option would be to modify the class names and function names to 1 character ones, but that might be a bit extreme

@waveform80
Copy link
Author

I could also remove all the indentations, but there's some things I just can't bring my self to do, not even for golf! ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment