7 ms·
Python's ast module is fantastic, we use it both to compile Python to Michelson[0], an assembly language for the Tezos blockchain, and Python to JavaScript[1].
by 1337shadow 5y ago
Python's ast module is fantastic, we use it both to compile Python to Michelson[0], an assembly language for the Tezos blockchain, and Python to JavaScript[1]. However, for refactoring one might want to consider redbaron[2].
[0] https://yourlabs.io/pyratzlabs/pymich https://yourlabs.io/pyratzlabs/pymich
[1] https://yourlabs.io/oss/ryzom/#javascript https://yourlabs.io/oss/ryzom/#javascript
[2] https://redbaron.readthedocs.io/en/latest/ https://redbaron.readthedocs.io/en/latest/
- heinrichhartman 5y ago> However, for refactoring one might want to consider redbaron[2]. > [2] https://redbaron.readthedocs.io/en/latest/ https://redbaron.readthedocs.io/en/latest/ Thanks for the link. This looks really cool!
- codethief 5y agoIt does! But it also looks really… dead? > For now, RedBaron can be considered in alpha, the core is quite stable but it is not battle tested yet and is still a bit rough. > RedBaron [supports] up to python 3.7 grammar > Last commit: 11 Aug 2019 Does anyone know what's going on with the project?
- anitil 5y agoI love it as well. I'm working on a Python AST viewer tool at the moment [0] so I've been spending a bit of time playing with it lately. I'm thinking of calling it Pasta (Python AST view....A? I don't have a word that starts with A). [0] I only really have a gif - https://imgur.com/a/4YYdfmZ https://imgur.com/a/4YYdfmZ
- joshuamorton 5y agohttps://github.com/google/pasta https://github.com/google/pasta has you beat :p
- anitil 5y agoNooooo I guess it was pretty low hanging food.
- austinjp 5y agoOh, nice. Based on the diagrams and the sibbling comment about Google's pasta, perhaps name it spaghetti? :)
- anitil 5y agoGood idea, thanks!
- erezsh 5y ago> Python to JavaScript That's cool. I'm working on something very similar, with the goal of generating idiomatic Javascript, which seems to be a goal for your code as well. I did make a few different choices. - Instead of worrying about formatting and indent, I just output whatever I feel like, and run Prettier afterwards. That way I don't have to worry about whitespace, indents, or superfluous parenthesis. - I integrated it with type analysis, so that operators like `+=` will produce different code for each type (ints vs lists, for example). - I also include the comments from the python source in the target javascript But these things aside, I can see a lot of similarities!
- 1337shadow 5y agoInteresting! can you link your repo please? Life without webpack has been only pure joy TBH ;)
- erezsh 5y agoSorry, no repo yet.
- acka 5y agoPlease do realize that if your project ever ends up under source code management, reading diffs and merging changes may cause headaches for anyone who wishes to contribute to your code. SCM and liberal use of code reformatting tools usually don't mix well, unless you're very careful while configuring them.
- erezsh 5y ago> SCM and liberal use of code reformatting tools usually don't mix well Care to elaborate about that? It seems to me like the resulting code will be very consistent, and only change when I either improve the transpiler, or when Prettier issue an update to their formatting. But that's unlikely, and anyway I can just use a fixed version.
- stevekemp 5y agoAgreed. I've not used it much, but I've done some fun things with it - mostly relating to adding linting checks for pipelines. For example we use troposphere to generate cloudformation templates, so I wrote a linter which would ensure that every time we tried to create an elastic container registry on AWS we explicitly setup "security scanning" to be "on". That kind of thing is trivial to write when you have the AST to manipulate: * Find all calls to function "foo(...)". * Error out if there is is no parameter "scan_on_push = True".