I didn’t know how much I didn’t know about the internals of python until reading about
Pony ORM. This ORM allows one to query SQL databases using python-like syntax. For example:
select(p for p in Product)
select((p1, p2) for p1 in Product for p2 in Product if p1.name == p2.name and p1 != p2)
select((p.name, count(p.orders)) for p in Product)
There is a good explanation of how on StackOverFlow. Basically, PonyORM compiles the parameter to select (which is python code) using the python compiler and converts the python AST into SQL.
This blows my mind.