4 ms·
> Macros for metaprogramming ... No macros; write your own parsers Ooof, sounds like a lot of effort. Is anyone doing that in Python?
by fmakunbound 6y ago
> Macros for metaprogramming ... No macros; write your own parsers
Ooof, sounds like a lot of effort. Is anyone doing that in Python?
- benhoyt 6y agoPlenty of people write literal parsers in Python, for things like HTML template languages -- however, that's not really related to macros. The most crazy thing I've seen along these lines (but it leads to really nice syntax) is PonyORM (https://ponyorm.org/ https://ponyorm.org/), which allows you to write Python generator expressions like this: select(c for c in Customer if sum(c.orders.price) > 1000) And Pony will translate it to SQL like this: SELECT "c"."id" FROM "customer" "c" LEFT JOIN "order" "order-1" ON "c"."id" = "order-1"."customer" GROUP BY "c"."id" HAVING coalesce(SUM("order-1"."total_price"), 0) > 1000 But how it does this is pretty funky. Python doesn't actually provide the AST for already-compiled code, just the bytecode. So Pony decompiles the bytecode back to an AST, and then converts the Python AST into SQL. More here (from the PonyORM author): https://stackoverflow.com/a/16118756/68707 https://stackoverflow.com/a/16118756/68707
- jolux 6y agoSounds a lot like LINQ in .NET.
- benhoyt 6y agoYeah, definitely. Though note that LINQ is new syntax (at least with "query" syntax; see https://stackoverflow.com/a/823155/68707 https://stackoverflow.com/a/823155/68707), whereas this co-opts existing Python syntax. LINQ is quite a lot nicer in that it gives you the syntax tree (https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/expression-trees/ https://docs.microsoft.com/en-us/dotnet/csharp/programming-g...), whereas PonyORM has to be a huge decompile-the-bytecode-back-to-a-syntax-tree hack.
- jolux 6y agoYeah, decompiling the bytecode back into a syntax tree sounds like no fun, if extremely cool. Thankfully we don't have to use query syntax with LINQ, we can just use the extension methods on IEnumerable, which is what I typically do.
- uyt 6y agoMaybe MacroPy? https://github.com/lihaoyi/macropy https://github.com/lihaoyi/macropy