Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is json2ast possible? #7

Open
KevinRodrigues05 opened this issue Sep 23, 2020 · 1 comment
Open

Is json2ast possible? #7

KevinRodrigues05 opened this issue Sep 23, 2020 · 1 comment

Comments

@KevinRodrigues05
Copy link

KevinRodrigues05 commented Sep 23, 2020

Hi @Psycojoker ,
Im looking for a solution if we could convert the stored ast (in json) back to the same ast format/instance and unparse it.

Right now I can only unparse the ast (using astunparse) with the parsed ast instance.

Could somebody please suggest a solution for this?

@Psycojoker
Copy link
Member

Hello,

I've took a quick look at ast.py source code and that looks doable: https://github.com/python/cpython/blob/3.8/Lib/ast.py

The _ast module (which seems to be a C module) contains all nodes classes that you can instantiate at will and set attributes on them like you want as you can see:

image

So you probably just need to write a small recursive function that takes ast2json json export and recreate the objects who's classes are stored in "_type" and set the corresponding attributes.

As always, the devil might be in the details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants