astmonkey is a set of tools to play with Python AST.
Project description
=========
astmonkey
=========
|Python Versions| |Build Status| |Coverage Status| |Code Climate|
``astmonkey`` is a set of tools to play with Python AST.
Installation
------------
You can install ``astmonkey`` from PyPI:
::
$ pip install astmonkey
If you want to have latest changes you should clone this repository and use ``setup.py``:
::
$ git clone https://github.com/mutpy/astmonkey.git
$ cd astmonkey
$ python setup.py install
visitors.SourceGeneratorNodeVisitor
-----------------------------------
This visitor allow AST to Python code generation. It was originally written by
Armin Ronacher (2008, license BSD) as ``codegen.py`` module. ``astmonkey`` version
fixes few bugs and it has good code coverage.
Example usage:
::
import ast
from astmonkey import visitors
code = 'x = y + 1'
node = ast.parse(code)
generated_code = visitors.to_source(node)
assert(code == generated_code)
transformers.ParentChildNodeTransformer
----------------------------------
This transformer adds few fields to every node in AST:
* ``parent`` - link to parent node,
* ``parents`` - list of all parents (only ``ast.expr_context`` nodes have more than one parent node, in other causes this is one-element list),
* ``parent_field`` - name of field in parent node including child node,
* ``parent_field_index`` - parent node field index, if it is a list.
* ``children`` - link to children nodes.
Example usage:
::
import ast
from astmonkey import transformers
node = ast.parse('x = 1')
node = transformers.ParentChildNodeTransformer().visit(node)
assert(node == node.body[0].parent)
assert(node.body[0].parent_field == 'body')
assert(node.body[0].parent_field_index == 0)
assert(node.body[0] in node.children)
visitors.GraphNodeVisitor
-------------------------
This visitor creates Graphviz graph from Python AST (via ``pydot``). Before you use
``GraphNodeVisitor`` you need to add parents links to tree nodes (with
``ParentChildNodeTransformer``).
Example usage:
::
import ast
from astmonkey import visitors, transformers
node = ast.parse('def foo(x):\n\treturn x + 1')
node = transformers.ParentChildNodeTransformer().visit(node)
visitor = visitors.GraphNodeVisitor()
visitor.visit(node)
visitor.graph.write_png('graph.png')
Produced ``graph.png`` (you need to have installed ``graphviz`` binaries if you want generate
images):
.. image:: examples/graph.png
utils.is_docstring
------------------
This routine checks if target node is a docstring. Before you use
``is_docstring`` you need to add parents links to tree nodes (with
``ParentChildNodeTransformer``).
Example usage:
::
import ast
from astmonkey import utils, transformers
node = ast.parse('def foo(x):\n\t"""doc"""')
node = transformers.ParentChildNodeTransformer().visit(node)
docstring_node = node.body[0].body[0].value
assert(not utils.is_docstring(node))
assert(utils.is_docstring(docstring_node))
License
-------
Copyright [2013] [Konrad Hałas]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
.. |Python Versions| image:: https://img.shields.io/pypi/pyversions/astmonkey.svg
:target: https://github.com/mutpy/astmonkey
.. |Build Status| image:: https://travis-ci.org/mutpy/astmonkey.png
:target: https://travis-ci.org/mutpy/astmonkey
.. |Coverage Status| image:: https://coveralls.io/repos/github/mutpy/astmonkey/badge.svg?branch=master
:target: https://coveralls.io/github/mutpy/astmonkey?branch=master
.. |Code Climate| image:: https://codeclimate.com/github/mutpy/astmonkey/badges/gpa.svg
:target: https://codeclimate.com/github/mutpy/astmonkey
astmonkey
=========
|Python Versions| |Build Status| |Coverage Status| |Code Climate|
``astmonkey`` is a set of tools to play with Python AST.
Installation
------------
You can install ``astmonkey`` from PyPI:
::
$ pip install astmonkey
If you want to have latest changes you should clone this repository and use ``setup.py``:
::
$ git clone https://github.com/mutpy/astmonkey.git
$ cd astmonkey
$ python setup.py install
visitors.SourceGeneratorNodeVisitor
-----------------------------------
This visitor allow AST to Python code generation. It was originally written by
Armin Ronacher (2008, license BSD) as ``codegen.py`` module. ``astmonkey`` version
fixes few bugs and it has good code coverage.
Example usage:
::
import ast
from astmonkey import visitors
code = 'x = y + 1'
node = ast.parse(code)
generated_code = visitors.to_source(node)
assert(code == generated_code)
transformers.ParentChildNodeTransformer
----------------------------------
This transformer adds few fields to every node in AST:
* ``parent`` - link to parent node,
* ``parents`` - list of all parents (only ``ast.expr_context`` nodes have more than one parent node, in other causes this is one-element list),
* ``parent_field`` - name of field in parent node including child node,
* ``parent_field_index`` - parent node field index, if it is a list.
* ``children`` - link to children nodes.
Example usage:
::
import ast
from astmonkey import transformers
node = ast.parse('x = 1')
node = transformers.ParentChildNodeTransformer().visit(node)
assert(node == node.body[0].parent)
assert(node.body[0].parent_field == 'body')
assert(node.body[0].parent_field_index == 0)
assert(node.body[0] in node.children)
visitors.GraphNodeVisitor
-------------------------
This visitor creates Graphviz graph from Python AST (via ``pydot``). Before you use
``GraphNodeVisitor`` you need to add parents links to tree nodes (with
``ParentChildNodeTransformer``).
Example usage:
::
import ast
from astmonkey import visitors, transformers
node = ast.parse('def foo(x):\n\treturn x + 1')
node = transformers.ParentChildNodeTransformer().visit(node)
visitor = visitors.GraphNodeVisitor()
visitor.visit(node)
visitor.graph.write_png('graph.png')
Produced ``graph.png`` (you need to have installed ``graphviz`` binaries if you want generate
images):
.. image:: examples/graph.png
utils.is_docstring
------------------
This routine checks if target node is a docstring. Before you use
``is_docstring`` you need to add parents links to tree nodes (with
``ParentChildNodeTransformer``).
Example usage:
::
import ast
from astmonkey import utils, transformers
node = ast.parse('def foo(x):\n\t"""doc"""')
node = transformers.ParentChildNodeTransformer().visit(node)
docstring_node = node.body[0].body[0].value
assert(not utils.is_docstring(node))
assert(utils.is_docstring(docstring_node))
License
-------
Copyright [2013] [Konrad Hałas]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
.. |Python Versions| image:: https://img.shields.io/pypi/pyversions/astmonkey.svg
:target: https://github.com/mutpy/astmonkey
.. |Build Status| image:: https://travis-ci.org/mutpy/astmonkey.png
:target: https://travis-ci.org/mutpy/astmonkey
.. |Coverage Status| image:: https://coveralls.io/repos/github/mutpy/astmonkey/badge.svg?branch=master
:target: https://coveralls.io/github/mutpy/astmonkey?branch=master
.. |Code Climate| image:: https://codeclimate.com/github/mutpy/astmonkey/badges/gpa.svg
:target: https://codeclimate.com/github/mutpy/astmonkey
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
astmonkey-0.2.3.tar.gz
(9.2 kB
view details)
File details
Details for the file astmonkey-0.2.3.tar.gz
.
File metadata
- Download URL: astmonkey-0.2.3.tar.gz
- Upload date:
- Size: 9.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/2.7.14
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 30cbaa2f3fa8ddcd57311ceb99aa11a7477cca45fe21708ca23b7fca12ae53ce |
|
MD5 | 20732e3596e29d1a94325e15b895c7e9 |
|
BLAKE2b-256 | 47e509cc250e542505804ed22a8e55cde0f0ec76e0ba9625f9ab94fdcd57569c |