Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Foxtrot ALL over those nested iterables!

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 pywalk
Octocat-spinner-32 test
Octocat-spinner-32 .gitignore
Octocat-spinner-32 MANIFEST.in
Octocat-spinner-32 README.rst
Octocat-spinner-32 publish.sh
Octocat-spinner-32 setup.py
README.rst

Pywalk

Pywalk is a python module written for traversing trees built of dictionaries, lists and tuples in a breadth-first manner.

This module is still under development. If you're used to mature, polished python libraries you may want to avoid pywalk. Additionally, it has only been tested against Python 2.7.1+ (what happens to be what's on my computer) so it may not work as expected in Python 3.x. However, if you're a more adventurous developer with an urge (or need) for manipulating nested data structures, this just might be the library for you!

Pywalk is a spiritual port of js-traverse, in that it's meant to solve similar problems.

How It Works:

walk(tree) returns a decorator that applies a callback to each node in the tree, in a breadth-first manner. For example:

tree = {'a': 1, 'b': [9, 9, 6, 7, 6]}

print("Tree before: "+repr(tree))

@walk(tree)
def that_aint_my_zip_code(node):
    if node.value == [9, 9, 6, 7, 6] :
        node.set([9, 9, 7, 7, 5])

print("Tree after: "+repr(tree))

Installation:

sudo pip install pywalk

Methods for node in the callback:

The object passed to the callback has a bunch of attributes which are hopefully useful:

  • node.value is the value of the node itself.
  • node.path is a list of indices used to get to the given node. Ex: The node.path for the 7 in the previous example is, ['b', 3].
  • node.key is basically node.path[-1].
  • node.level is basically len(node.path).
  • node.is_root is True if the node is the root node, and otherwise False.
  • node.is_leaf is True if the node has no children (ie, is not a dict, list or tuple), and False otherwise.
  • node.set(v) changes the value of the current node to value (v).
  • [unimplemented] node.del should probably delete the node and the associated branch from the tree.
  • [unimplemented] node.parent should return the parent node object.

Tests:

Not much for tests right now, but if you want to run them (it) anyway, try:

nosetests test/*

The one test right now parallels an example used in js-traverse.

Developers! Developers! Developers!

If you're a python fan and like what you see (or don't quite like what you see), I heartily invite you to dig in, fork it up and git push it good.

License:

MIT.

Something went wrong with that request. Please try again.