PyCon 2016 in Portland, Or
hills next to breadcrumb illustration

Tuesday 5:10 p.m.–5:40 p.m.

Oneliner-izer: An Exercise in Constrained Coding

Chelsea Voss

Audience level:
Intermediate
Category:
Python Core (language, stdlib, etc.)

Description

We'll describe the ideas and implementation behind Oneliner-izer, a "compiler" which can convert most Python 2 programs into one line of code. As we discuss how to construct each language feature within this unorthodox constraint, we'll explore the boundaries of what Python permits and encounter some gems of functional programming – lambda calculus, continuations, and the Y combinator.

Abstract

Have you ever been tempted to write a giant list comprehension that, though unwieldy and unreadable, accomplishes exactly what you need it to do? [Oneliner-izer](https://github.com/csvoss/onelinerizer) is a project that pushes that giant list comprehension to its natural extreme: it's a "compiler" that can convert most Python programs into one big line of code with the same behavior. When I say "most", here's what I mean: `with` and `finally` are the two major open problems, but as for everything else – `def`, `while`, `for`, `if`/`elif`/`else`, `class`, `try`/`except`/`else`, `import`, `break`, `continue`, `raise`, `assert`, `del`, `exec` – these language features can all be implemented within one line of code! In fact, this is enough for us to oneliner-ize the code of Oneliner-izer itself, as seen [here](https://github.com/csvoss/onelinerizer/blob/master/main_ol.py). In this talk, I'll explain how Oneliner-izer oneliner-izes. We'll describe how to oneliner-ize simple blocks of code, then move up through control flow, loops, recursion, and beyond. Along the way, we'll also learn about a bunch of quirks and oft-unexplored corners of Python, as I share how we can use those corners to work around each challenge. You'll learn about how we used continuation-passing and the Y combinator, and you'll come away from this talk with a new appreciation for the wily list comprehension and the humble lambda expression.