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.

To get an idea of the simplicity of using token-utils, consider this example from Python's own documentation 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.

Metadata

Release files for token-utils 0.3.7

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.3.7
File Size Uploaded
token_utils-0.3.7.tar.gz 7.8 MB Details

Built distribution (wheel)

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

Total release size: 7.9 MB

Release files / token_utils-0.3.7.tar.gz

Download URL token_utils-0.3.7.tar.gz
Size 7.8 MB
Tags Source
SHA-256 checksum
How to use checksums
a432ee30fbc16eddc9f291b9e15cf6766e9db1395bd297e8542200639016850c
BLAKE2b-256 checksum
How to use checksums
f9db8b69e0125d0832969275c8387ab45680f9846167fbae01413408c275cac8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.11

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

Download URL token_utils-0.3.7-py3-none-any.whl
Size 25.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2fdda492a6ac156eacd3ef8dbd5976bc75fc1c33137b4187cb77a8f48a0f5fd0
BLAKE2b-256 checksum
How to use checksums
901da7e413fb24a5f1597ca383d8a796158e645fd83f36bb0a4b62dd52508968
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.11

Release history Release notifications | RSS feed

0.3.10

2 release files

0.3.9

2 release files

0.3.8

2 release files

This release

0.3.7 This release

2 release files

0.3.6

2 release files

0.3.5

2 release files

0.3.4

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.4

2 release files

0.2.3

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