Comments (4)
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.
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.
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.
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)
- pandas-vet should run when flake-8 invoked by pre-commit HOT 1
- Automate releases to PyPI and conda-forge
- Challenging PD008: .at can be useful HOT 3
- False positive: dict().values()
- False positive: PD005 for regex `sub` method HOT 1
- New approach to docs with JupyterBook HOT 1
- Use generator expressions when possible HOT 2
- inplace set to a variable raises exception
- PD011 - shoud be to_numpy() instead of to_array()?
- Improve contributor experience HOT 1
- Constant column check with `nunique` HOT 2
- `PD012` is outdated HOT 1
- Improve official docs
- Check for proper `reset_index`
- torch.Tensor.sort return type values access PD011 false positive
- Add nox support to run tests, flake8, black locally HOT 1
- Add banners to Readme. HOT 1
- Add check for python functions that should be pandas methods
- Add notes about disabling checks to README
- Conda and pip install (0.2.2) not showing plugins with flake8 --version; 0.2.1 works HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pandas-vet.