The course
Iterators and functional Python is an intermediate Coding course in seven lessons. The first six run on a Raspberry Pi 4 with two LEDs (red on GPIO17, yellow on GPIO27); the finale uses the gallery's MCP3008 knob circuit, which is built on a Pi 5 (the same Python runs on both). Each step is checked against what the Pi prints or what its pins do.
- Comprehensions: list comprehensions over sonar pings, the if-else
expression inside one, dict and set comprehensions over the planets,
nested comprehensions that flatten and transpose a grid, and generator
expressions with
sumandanylighting a deep water alarm. - Iterators:
iterandnext, aforloop written out withStopIteration, whyzipandmapwork only once, a countdown class with__iter__and__next__, iterables that start over, and the countdown flashing an LED. - Generators in depth: lazy start, endless generators,
sendfor a running average,yield fromwith a return value,closeandfinally, and a pipeline that shows when each value moves. - map and filter: functions as values,
lambda,mapover two lists, a dict of converters, key functions forsortedandmax, and a lazy filter that blinks once per hot reading. - itertools:
countandislice,cyclefor a watch rota,accumulateandpairwise,groupbyand why it needs sorted data,combinationsandproduct, andchain,takewhileanddropwhile. - functools:
reducewithoperator,partialfor an ADC conversion,lru_cachetaking 21,891 calls down to 21, decorators, and acomposeof your own. - Project: a streaming sensor pipeline: the finale.
The finale
A knob on channel 0 of an MCP3008 (a 10-bit ADC on SPI) becomes an endless
generator of readings. Stages are functions that take a stream and return
one: volts (map with partial), a moving average over a deque of four,
and a filter that passes only moves of 0.2 V or more. The sink sets a
PWMLED on GPIO18 to the voltage, so the green LED follows the knob. The
last step turns the nested calls into a list of stages folded together with
reduce, and stops after five changes. Nothing in the pipeline ever holds
more than four readings.
What the Pi's engine does differently
The Pi runs MicroPython 1.29 with a CPython-style standard library. Checked while writing the course:
itertoolshas every function CPython's has, written as Python generators, so it is slower. Itsislicereads one value more from its source than it hands out before it stops (CPython's stops at the last one), so the finale stops its live stream withbreak, which works the same everywhere.functoolshasreduce,partial,lru_cache,cache,cached_property,total_ordering,cmp_to_keyandwraps. There is nosingledispatch; a dict from type to function does the job.wrapsis there but copies nothing: MicroPython functions cannot hold attributes, so a wrapped function's__name__stayswrapper. A class-based decorator keeps the name, and the course shows one.iter(function, stop_value)is missing; awhile Trueloop withnextandStopIterationdoes the same.- A
map,filteror generator prints differently from CPython (<map>rather than<map object at ...>), and a dict does not keep the order keys went in, so the lessons sort keys or read them by name.