by pcr910303 · 6y ago · 0 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
Even before that, I had a "let" in Drython: https://github.com/gilch/drython/blob/master/drython/stateme...
In Haskell let and lambda have a subtle distinction due to the type system, but in Lisp and dynamically typed languages like Python, "let" can simply be lambda definition that is called immediately.
def parent_function():
output_function_a = lambda x,y: (x + 23) * y
def output_function_b(x, y):
return (x + 23) * y
return output_function_a, output_function_b