This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (
Help us improve Python packaging - Donate today!

astmonkey is a set of tools to play with Python AST.

Project Description

astmonkey is a set of tools to play with Python AST.


You can install astmonkey from PyPI:

$ pip install astmonkey

If you want to have latest changes you should clone this repository and use

$ hg clone
$ cd astmonkey
$ python install


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.

Example usage:

import ast
from astmonkey import transformers

node = ast.parse('x = 1')
node = transformers.ParentNodeTransformer().visit(node)

assert(node == node.body[0].parent)
assert(node.body[0].parent_field == 'body')
assert(node.body[0].parent_field_index == 0)


This visitor creates Graphviz graph from Python AST (via pydot). Before you use GraphNodeVisitor you need to add parents links to tree nodes (with ParentNodeTransformer).

Example usage:

import ast
from astmonkey import visitors, transformers

node = ast.parse('def foo(x):\n\treturn x + 1')
node = transformers.ParentNodeTransformer().visit(node)
visitor = visitors.GraphNodeVisitor()


Produced graph.png (you need to have installed graphviz binaries if you want generate images):


This visitor allow AST to Python code generation. It was originally written by Armin Ronacher (2008, license BSD) as module. astmonkey version fixes few bugs and it has good code coverage. It also works with Python 2.6, 2.7, 3.1, 3.2 and 3.3.

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)


This routine checks if target node is a docstring. Before you use is_docstring you need to add parents links to tree nodes (with ParentNodeTransformer).

Example usage:

import ast
from astmonkey import utils, transformers

node = ast.parse('def foo(x):\n\t"""doc"""')
node = transformers.ParentNodeTransformer().visit(node)

docstring_node = node.body[0].body[0].value
assert(not utils.is_docstring(node))


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

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.

Release History

Release History

This version
History Node


History Node


Download Files

Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
astmonkey-0.1.1.tar.gz (6.5 kB) Copy SHA256 Checksum SHA256 Source Dec 7, 2013

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting