cutter
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.
Use the ! syntax in interpreter
This is at your own risk but you can add:
try:
from cutter.utils import bang_compile, cut_as_builtin
from code import InteractiveConsole
import codeop
except ImportError:
pass
else:
sys.ps1 = "\001\033[1;36m\002!\001\033[1;32m\002>> \001\033[1;37m\002"
codeop.compile = bang_compile
cut_as_builtin()
try:
InteractiveConsole().interact('')
except Exception:
from traceback import print_exc
print_exc()
sys.exit(0)
in your ~/.pythonrc
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
Release files for cutter 0.5.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| cutter-0.5.1.tar.gz | 64.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| cutter-0.5.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 69.3 kB
Release files / cutter-0.5.1.tar.gz
| Download URL | cutter-0.5.1.tar.gz |
|---|---|
| Size | 64.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
33ec1f1d1a2317057c5219e91b2b51430bc2c6ac98d0b69d63232e1947b5f808
|
|
BLAKE2b-256 checksum How to use checksums |
089209aa1b8de9fe4dfe3b905bf0aae061839aba1b1458898655b8439577c35b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
Hatch/1.16.5 cpython/3.14.3 HTTPX/0.28.1
|
Release files / cutter-0.5.1-py3-none-any.whl
| Download URL | cutter-0.5.1-py3-none-any.whl |
|---|---|
| Size | 4.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c3fc9ca732a82b2b0c0a70fb62d65fc9bb915506a651593c071eef9c6f6735f2
|
|
BLAKE2b-256 checksum How to use checksums |
bb5bd2bc9c9ddde7f6a126e746c4b5644f6f0db83482f02e1b719d50c688d94e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
Hatch/1.16.5 cpython/3.14.3 HTTPX/0.28.1
|