Giter VIP home page Giter VIP logo

Comments (4)

simchuck avatar simchuck commented on May 26, 2024

For reference, current manual method is as follows:

import ast

statement = "...test statement here..."
tree = ast.parse(statement)
node = tree.body[0].value

With above code in the REPL, manually descend into the node (e.g., node.__dict__) until the desired pattern is identified. Then use the node chain in a new check_ function with __init__.py as appropriate, and add a call to this function from an appropriate method in the Visitor class.

from pandas-vet.

simchuck avatar simchuck commented on May 26, 2024

I am working on developing something here. Hopefully it will make the process easier so that others can get up to speed quickly and contribute.

from pandas-vet.

simchuck avatar simchuck commented on May 26, 2024

Good docs for AST at https://greentreesnakes.readthedocs.io/en/latest/index.html

Also links to a pretty-print module to dump the contents of the AST which might be sufficient for original proposed feature.

from pandas-vet.

simchuck avatar simchuck commented on May 26, 2024

There are a couple of existing options that should be sufficient:

Option 1: Using the standard library

The ast module has a .dump() function which returns a formatted string dump of the AST tree node. Default is to show annotations, which helps in identifying the attribute chain.

ast.dump(node, annotate_fields=True, include_attributes=False)

Example:

import ast

code = """
print("Hello World!")
s = "I'm a string!"
print(s)
"""
tree = ast.parse(code)
print(ast.dump(tree))

Output:

'Module(body=[Expr(value=Call(func=Name(id=\'print\', ctx=Load()), args=[Str(s=\'Hello World!\')], keywords=[])), Assign(targets=[Name(id=\'s\', ctx=Store())], value=Str(s="I\'m a string!")), Expr(value=Call(func=Name(id=\'print\', ctx=Load()), args=[Name(id=\'s\', ctx=Load())], keywords=[]))])'

Option 2: Using the astpp module from greentreesnakes

The pretty-print module mentioned in the above post (available at https://bitbucket.org/takluyver/greentreesnakes/src/default/astpp.py) includes a astpp.dump() function which produces essentially the same output, but presented in a tree format that provides more visual clarity.

astpp.dump(node, annotate_fields=True, include_attributes=False, indent=' ')

import ast
import astpp
 
code = """
print("Hello World!")
s = "I'm a string!"
print(s)
"""
tree = ast.parse(code)
print(astpp.dump(tree))

Output:

Module(body=[
    Print(dest=None, values=[
        Str(s='Hello World!'),
      ], nl=True),
    Assign(targets=[
        Name(id='s', ctx=Store()),
      ], value=Str(s="I'm a string!")),
    Print(dest=None, values=[
        Name(id='s', ctx=Load()),
      ], nl=True),
  ])

The astpp module also includes a parseprint() function to produce this formatted dump directly from the code string (no need to manually parse), along with an alias pdp() which references the same function:

astpp.parseprint(code, filename='<string>', mode='exec', **kwargs)
astpp.pdp(code)

from pandas-vet.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.