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.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| token_utils-0.3.6.tar.gz | 7.9 MB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| token_utils-0.3.6-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 7.9 MB
Release files / token_utils-0.3.6.tar.gz
| Download URL | token_utils-0.3.6.tar.gz |
|---|---|
| Size | 7.9 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
de5198f3a2ee2c846ad49628d28f0cd97aec9e6d9b2d9345ccd8d83e211fd6a9
|
|
BLAKE2b-256 checksum How to use checksums |
9a8c9104b230892068c17dcac5345177a569e48b72c92c27588c3c1107dc0059
|
| 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.6-py3-none-any.whl
| Download URL | token_utils-0.3.6-py3-none-any.whl |
|---|---|
| Size | 25.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
18187f228b7088892a210833416fde386e26a9f27317675d1513583e8caafe4e
|
|
BLAKE2b-256 checksum How to use checksums |
22c10337c3c93e8dd148bfd73f011c416a4b1671acf9ede70a2917b7b42c1a52
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.10.11
|