5 ms·
Replacing `import` with `accio`: A Dive into Bootstrapping and Python's Grammar
Summary of Blog Post (I'm not the author):
At Hacker School, I've been building an alternate universe Python by overwriting builtin functions and statements with Harry Potter spells. This is a thing you can do at Hacker School!
- deleted 13y ago[deleted]
- varenc 13y agoits probably easier to add custom syntax to python by using custom encodings. The dropbox pyxl library does this. Details: https://github.com/dropbox/pyxl#implementation-details https://github.com/dropbox/pyxl#implementation-details
- stcredzero 13y agoWhen I search for "Harry Potter Programming Language" I keep getting references to an article where JK Rowling's writing process is used as an analogy for the creation of Python.
- Schiphol 13y agoIs there an interesting story behind that search? You got me wondering.
- jodrellblank 13y agoI was expecting the magic to be changing import math x = math.ceil(y) into x = accio ceil(y) and having it Just Magically Work (tm). But it's a nice write up of recompiling Python with a changed grammar.
- lazzlazzlazz 13y agoI don't know how you could possibly have expected that.
- spoiler 13y agoHence the "magically" :-)
- NoodleIncident 13y agoThis kind of stuff is why I read Hacker News. Completely pointless but indisputably awesome technical hacks that teach you something about how stuff works anyway.
- sadfnjksdf 13y agoAwesome, but accio is a bit Potterish. How about 'have' or something shorter.
- pekk 13y agoFeel free to make your own project which is based on the theme of using words like 'have'.
- jacalata 13y agoYou know its deliberately potter-ish, right? Did you miss the wingardium leviosa reference at the beginning of the post?
- sadfnjksdf 13y agoAnd avra kadabra to you also.
- MBlume 13y agoHuh, now I'm wondering how much you'd have to do to make the import keyword a build-time command-line argument
- sitkack 13y agoI am wondering how to add keywords at runtime, or remove all the keywords and make them functions...
- dalek2point3 13y agothis is such a great assignment for a principles of programming languages class. there is no better way to learn something than to break it up, change it and make it do your wish. i'm going to be teaching my next batch of muggles this.
- j1z0 13y agoWow. Such an awesome article. I really enjoyed reading it.
- groovy2shoes 13y agoReminds me in some ways of LUA: https://github.com/mniip/LUA https://github.com/mniip/LUA
- lawl 13y agoI'm not a python programmer. But shouldn't this be possible at runtime by modifying the AST to change it back to import in the AST? That way you could have a module you can just include to run this without a custom interpreter. Edit: Looks like this is doable by messing with the tokenizer: http://stackoverflow.com/questions/214881/can-you-add-new-statements-to-pythons-syntax http://stackoverflow.com/questions/214881/can-you-add-new-st... You could create a loader.py where you tokenize the script yourself, and replace accio with a custom import that again loadds imports by tokenizing them yourself first. And then after you've modified the tokens you should be able to build a valid AST and run it :) I'm pretty sure you can just throw a SyntaxError there yourself too if you encounter a real "import". Nice hack nevertheless.
- ThePhysicist 13y agoI think you wouldn't be able to generate a valid AST to begin with since the parser would probably complain that it doesn't know the "accio" keyword, but I'm note 100 % sure if there isn't a workaround for this.
- lvh 13y agoYes and no. The regular python parser will barf on that unknown keyword, so you don't get an ast because the parser never gets there. Using import hooks, this can be done in a regular interpreter. For example, that's how hy, a lisp in python, works!
- anon4 13y agoOh yes, you can. But once you're doing that, why stop at a single statement? Why not macros https://github.com/lihaoyi/macropy https://github.com/lihaoyi/macropy
- sitkack 13y agoDon't forget http://www.pocoo.org/projects/karnickel/#karnickel http://www.pocoo.org/projects/karnickel/#karnickel the magnificent
- lvh 13y agoAwesome hack! Did you consider import hooks to accomplish this? Of course that wouldn't make your bootstrap language any different; that'd still be vanilla python. In the imported file you can do whatever you want, for example the inimitable paultag implemented hy, a python lisp, using those.
- deleted 13y ago[deleted]
- girvo 13y agoReally neat post, but a missed opportunity in naming it: "Basilisk" would have been quite a nice fit with both worlds ;)
- jebus989 13y agoDid you see she named it Nagini? Arguably even better
- daGrevis 13y agoIf you just want to accio act as import, you can always do this: accio = __import__ Anyway, interesting article!
- jodrellblank 13y agoRead the article, that was discussed and dismissed. It doesn't make accio 'act as import' because import is a keyword not a function call.
- daGrevis 13y agoimport is a statement. __import__ is a function hence will work. In [1]: accio = __import__ In [2]: accio("os") Out[2]: <module 'os' from '/usr/lib/python2.7/os.pyc'>
- raimondious 13y agoFrom the article... >>> accio = __builtins__.__import__ >>> accio sys File "<stdin>", line 1 accio sys ^ SyntaxError: invalid syntax # :( The author wanted accio to replace import not __import__.