Skip to main content

Python list cutter tool

Project description

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

Project details


Download files

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

Source Distribution

cutter-0.5.1.tar.gz (64.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cutter-0.5.1-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file cutter-0.5.1.tar.gz.

File metadata

  • Download URL: cutter-0.5.1.tar.gz
  • Upload date:
  • Size: 64.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Hatch/1.16.5 cpython/3.14.3 HTTPX/0.28.1

File hashes

Hashes for cutter-0.5.1.tar.gz
Algorithm Hash digest
SHA256 33ec1f1d1a2317057c5219e91b2b51430bc2c6ac98d0b69d63232e1947b5f808
MD5 640b25ce57336b53707d5c4f3afa91e3
BLAKE2b-256 089209aa1b8de9fe4dfe3b905bf0aae061839aba1b1458898655b8439577c35b

See more details on using hashes here.

File details

Details for the file cutter-0.5.1-py3-none-any.whl.

File metadata

  • Download URL: cutter-0.5.1-py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Hatch/1.16.5 cpython/3.14.3 HTTPX/0.28.1

File hashes

Hashes for cutter-0.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c3fc9ca732a82b2b0c0a70fb62d65fc9bb915506a651593c071eef9c6f6735f2
MD5 24149580a10a029e8616b08753ff3ae7
BLAKE2b-256 bb5bd2bc9c9ddde7f6a126e746c4b5644f6f0db83482f02e1b719d50c688d94e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page