Today I finally wrote a readme for my little python tool cutter.

This is something that I use daily in conjuction of wdb for debugging python code.

Here’s the readme, I hope you will find it as useful as I am:

Readme

Cutter is a small python module that add some sugar on top of list like objects to ease list traversal.

Usage

from cutter import cut

numbers = [
  ['one', 'two', 'three'],
  ['un', 'deux', 'trois'],
  ['uno', 'due', 'tre']
]

>>> cut(numbers)[1]  # Take the second element of every sublist
['two', 'deux', 'due']*

>>> cut(numbers)[1, 2]  # Take the third letter of every second element of every sublist
['o', 'u', 'e'].

>>> cut(numbers)[1][2]  # Idem
['o', 'u', 'e'].

>>> cut(numbers)[1, 2].upper()  # Upper case the third letter of every second element of every sublist
['O', 'U', 'E']

>>> cut(numbers)[...]  # Flatten the list for one level
['one', 'two', 'three', 'un', 'deux', 'trois', 'uno', 'due', 'tre']

>>> cut(numbers)[..., 0]  # First letter of every elements
['o', 't', 't', 'u', 'd', 't', 'u', 'd', 't'].

Syntaxic sugars

There are two syntaxic sugars for an even easier use:

The |_ syntax

from cutter import _

>>> numbers |_ [1]
['two', 'deux', 'due']*

>>> (numbers |_ [1] |_ [2] |_ .upper)()
['O', 'U', 'E']

>>> numbers |_ [...]
['one', 'two', 'three', 'un', 'deux', 'trois', 'uno', 'due', 'tre']

>>> numbers |_ [..., 0]
['o', 't', 't', 'u', 'd', 't', 'u', 'd', 't'].

The ! syntax

This syntax is meant for use in shells.

It is for example used in wdb and the bundled cut.py interpreter.

Cutter provide a bang_compile function which is a wrapper of the python builtin compile function.

# This code muste be compiled with cutter.utils.bang_compile

>>> numbers!1
['two', 'deux', 'due']*

>>> numbers!1!2!upper()
['O', 'U', 'E']

>>> numbers!*
['one', 'two', 'three', 'un', 'deux', 'trois', 'uno', 'due', 'tre']

>>> numbers!*!0
['o', 't', 't', 'u', 'd', 't', 'u', 'd', 't'].

This syntax use the python tokenizer and ast to make it work. This is really useful when debugging to inspect list content.

More

Cutter works with dictionaries too:

cut(dict)['key']

slices:

cut(list)[:5]

For more examples see the test files : test

Cutter is compatible with at least: python 2.6, 2.7, 3.2, 3.3, 3.4 and pypy and is licensed under lgpl v3