7 ms·
Exploit custom codecs to write inline C in Python
- tyingq 7y agoInteresting feature abuse. Perl's had various Inline modules for some time, but via a deliberate feature. https://www.perl.com/pub/2001/02/inline.html/ https://www.perl.com/pub/2001/02/inline.html/
- baryphonic 7y agoIndeed it is. Guido himself maintained a project called Pyxl3 [1] for a while (forked from one at Dropbox during his tenure there) that abused this codec feature to implement something similar to JSX within Python source. [1] https://github.com/gvanrossum/pyxl3 https://github.com/gvanrossum/pyxl3
- smabie 7y agoKinda sad when you think that CL could implement this feature in a pretty simple macro.
- agumonkey 7y agoOh never heard of the # codec declaration relies on https://www.python.org/dev/peps/pep-0263/ https://www.python.org/dev/peps/pep-0263/ it seems (this is from 2001, maybe there's more recent)
- est 7y agoI envy you py3 kids... encoding is a huge problem in py2
- franga2000 7y ago> 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...
- est 7y ago> 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.
- smitty1e 7y agoThis has got to be the least-pythonic thing I've ever seen.
- bigiain 7y agoI read it and immediately thought "Hey, I remember doing this in Perl in the late '90s or early 2000s!" - very nostalgic...
- pcr910303 7y agoWait... looking at this, doesn't this mean that by deliberately misusing the `codec` module, meta programming at compile-time (somewhat like Lisp macros) is possible in Python? 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)
- loeg 7y agoSomeone can finally implement multiline lambdas!
- nrclark 7y agoYou can actually just use a regular function declaration in the middle of arbitrary Python code if you want. Does that count as a multi-line lambda?
- loeg 7y agoNo.
- nrclark 7y agonot trying to shitpost - I'm legit curious. What's the difference between the two? Like in this snippet, are the two resulting functions different from each other in some way that I haven't considered? 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
- 7y ago
- tjwei 7y agoI wrote a similar piece of code called MagicCodec back in 2008, including a zhpy codec(Chinese Python) and a Lua codec, which translates Lua source code to Python. (Blog content in Mandarin Chinese, with sample code http://weijr-note.blogspot.com/2008/02/python-magiccodec-01.html http://weijr-note.blogspot.com/2008/02/python-magiccodec-01....) There was a time that I am slightly interested in Chinese Python for education purpose. Another interesting approach is using ctypes to replace keywords in CPython implementation, http://weijr-note.blogspot.com/2011/06/python-32-keyword.html http://weijr-note.blogspot.com/2011/06/python-32-keyword.htm... (Also a blog post in Mandarin Chinese with sample code, which is also in Mandarin Chinese)
- tony1234 7y agonice job
- ssize_t 7y agothanks!
- 0xff00ffee 7y agoIf Python is so awesome why do we need this? Flagged for asking a question.
- yellow_lead 7y agoGuess I'm adding Python to my resume. Nice work
- deleted 7y ago[deleted]
- deleted 7y ago[deleted]
- j88439h84 7y agohttps://github.com/metatooling/syntactic https://github.com/metatooling/syntactic is a framework for creating custom Python syntax with the codec trick.
- jokoon 7y agoI guess one could also use a lightweight compiler like TCC and write a python module with it. That's a good idea for a side project.
- kilon 7y agoPython syntax can be heavily and easily manipulated via the ast module. An example of this is hylang https://github.com/hylang/hy https://github.com/hylang/hy
- delfick 7y agoI have also used this ability to make a dsl with https://noseofyeti.readthedocs.io https://noseofyeti.readthedocs.io (rspec style "describe", "it" for writing python tests). 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)