https://gist.github.com/andybak/45e3155420d18420a34a
Here's another approach based on Macropy (basically, macros for Python): https://github.com/lihaoyi/macropy#pattern-matching.
https://gist.github.com/andybak/45e3155420d18420a34a