190 points · by ssize_t · 6y ago · 48 comments
By using a custom parser, people are able to add custom syntax in python (which is... undoubtedly unpythonic) with just a decorator(or a magic comment) on front. I can't wait to start abusing this... :-)
Edit: Looks like a pattern matching syntax that hooks in pampy would be my first hacking project :-)
# coding: patmatch
import patmatch
input = [1, 2, 3]
patmatch input:
case [1, 2, x]:
print(f"{x} is matched!") #=> 3 is matched!
def fibonacci(n):
patmatch n:
case 1: return 1
case 2: return 1
case _: return fibonacci(n - 1) + fibonacci(n - 2)The reason you don't see it in python much is more a matter of culture: we don't like too much magic nor DSL.
With the new generation of coders incomming, it may change though.
Hope not, as most devs are terrible at designing magic or languages and equally terrible at admitting they are.
It took a decade for the trend of monkey patching things in ruby and js to disapear and I don't regret that time one bit. Bad habits die hard.
I bet if either of those two weren't true, you'd see a lot more of it.
Would you have any pointers (heh) given the discovery you made?
As far as parsing goes, if you want to stick with python I've had good success with pyparsing [1], otherwise I have a strong preference to do language-related things in OCaml with menhir [2]. I've toyed with wrapping the Python parser in an OCaml library with decent success [3]. But, of course, unless you're optimizing for fun, it's probably a good idea to stick with Python.
Another, weirder, approach is creating a language that happens to be able to be parsed by the same grammar as python, then using a decorator (or similar) to get the source code or the ast to reparse and transpile. As an example you could have something like `a <- b` be the syntax for an actor model message receive dsl (which is valid python).
[1] https://github.com/pyparsing/pyparsing [2] http://gallium.inria.fr/~fpottier/menhir/ [3] https://github.com/georgek42/pyparser
Though I have missed switch statements a few times in Python, when implementing state machines - for parsing or sensor/actuator logic. Though these tended to be quite simple and not performance-critical, so a if/elif sequence was just fine.
Hissp, my Lisp-on-Python project, uses multiline lambdas to simplify its compilation target to a functional subset of Python: https://github.com/gilch/hissp
Hebigo is a skin built on Hissp with a more Python-like syntax and macros based on Drython. It's what Python would be like if its statements were composable like its expressions are: https://github.com/gilch/hebigo
relies on https://www.python.org/dev/peps/pep-0263/ it seems (this is from 2001, maybe there's more recent)
encoding is a huge problem in py2
Yes...exactly! Good thing that it's dead now, every meaningful library has either been updated or replaced and nobody is using it anymore, right? Like Windows 7! /s
Seriously tho, what's keeping you from updating? Genuinely curious at this point...
My boss doesn't pay the upgrade bills. Only adding new features are billed. Sad.
And there are thousands of real-time users on the system and their task can not be interrupted, also if there is any py2 and py3 mismatch, the outcomes would be quite costy.
It's a cool technique, but quite a lot of effort and can break a bunch of tooling (getting my codec to work with black for example was... fun)
is a framework for creating custom Python syntax with the codec trick.