Skip to main content

token-utils

Installation: pip install token-utils.

The purpose of token-utils is to simplify manipulations of tokens normally obtaind from Python's tokenize module. One of token-utils' features is that, unlike Python's version, the following is always guaranteed:

from token_utils import tokenize, untokenize

source = "Arbitrary Python code here"

assert source == untokenize(tokenize(source))

To get an idea of the simplicity of using token-utils, consider this example from Python's standard library which substitute Decimals for floats in a string of statements:

from tokenize import tokenize, untokenize, NUMBER, STRING, NAME, OP
from io import BytesIO

def decistmt(s):
    result = []
    g = tokenize(BytesIO(s.encode('utf-8')).readline)  # tokenize the string
    for toknum, tokval, _, _, _ in g:
        if toknum == NUMBER and '.' in tokval:  # replace NUMBER tokens
            result.extend([
                (NAME, 'Decimal'),
                (OP, '('),
                (STRING, repr(tokval)),
                (OP, ')')
            ])
        else:
            result.append((toknum, tokval))
    return untokenize(result).decode('utf-8')

Here's how you could achieve the same result with token-utils:

from token_utils import tokenize, untokenize

def decistmt(source):
    tokens = tokenize(source)
    for token in tokens:
        if token.is_float():
            token.string = f"Decimal('{token.string}')"
    return untokenize(tokens)

See the documentation for more information.

Release files for token-utils 0.2.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for token-utils 0.2.3
File Size Uploaded
token_utils-0.2.3.tar.gz 9.4 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for token-utils 0.2.3
File Interpreter ABI Platform
token_utils-0.2.3-py3-none-any.whl Python 3 none any Details

Total release size: 9.4 MB

Release files / token_utils-0.2.3.tar.gz

Download URL token_utils-0.2.3.tar.gz
Size 9.4 MB
Tags Source
SHA-256 checksum
How to use checksums
7118358f629a4292adf58cdaa9bbd99a885099e772b64580f154274b0fa67bf4
BLAKE2b-256 checksum
How to use checksums
bd68d0b01c11c7d0eca663b14507a3a6a5781c424b7ac7c7bcc2359bbcd64aa5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.13

Release files / token_utils-0.2.3-py3-none-any.whl

Download URL token_utils-0.2.3-py3-none-any.whl
Size 16.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
14c0625fed2a2d8974f2b38f0513ea6e2315329bfc20cee15d6b4f14aebffa1b
BLAKE2b-256 checksum
How to use checksums
58a38f1bdc0cf986375ce799b11adf16e3e0ff45756ede577291c59be6e6a835
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.13

Release history Release notifications | RSS feed

This release

0.2.3 This release

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page