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.4
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.4.tar.gz | 27.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| token_utils-0.3.4-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 63.7 kB
Release files / token_utils-0.3.4.tar.gz
| Download URL | token_utils-0.3.4.tar.gz |
|---|---|
| Size | 27.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a453483fd952a5e09430d48435a866a93af3c6c242a9ab4768964661fb328e0e
|
|
BLAKE2b-256 checksum How to use checksums |
a8f5ea72c40c2f4500c5e5282c127d1c46c87cb8b31a423c8e23d8457e824f81
|
| 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.4-py3-none-any.whl
| Download URL | token_utils-0.3.4-py3-none-any.whl |
|---|---|
| Size | 36.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e4ba5c0e72a638d024629c53542331f9952abecdef0b5685b9a2d5d2062753e1
|
|
BLAKE2b-256 checksum How to use checksums |
1765eab1db018df5599a2c8659d0ae705d063695a1761e313930af5388c67939
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.10.11
|