See also
Purpose¶
The purpose of token-utils is to simplify manipulations of tokens normally obtained 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))
Installation¶
pip install token-utils
Example¶
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, where we changed the name of the function for greater clarity:
# decimal_py.py
#
# Example taken from Python's tokenize module documentation
from tokenize import tokenize, untokenize, NUMBER, STRING, NAME, OP
from io import BytesIO
def float_to_decimal(s): # changed name from Python's example
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")
About the examples
The files with a name as a top comment
are found in the /docs_examples
directory of the repository.
Here’s how you could achieve the same result with token-utils:
# decimal_tok.py
from token_utils import tokenize, untokenize
def float_to_decimal(source):
tokens = tokenize(source)
for token in tokens:
if token.is_float():
token.string = f"Decimal('{token.string}')"
return untokenize(tokens)
Important
token_utils’s tokenizer is based on Python’s version 3.11. As such, it has a limitation when it comes to parsing f-strings.
This was done because, starting with Python 3.12, the tokenizer can raise an exception when it encounters expressions that are not valid Python syntax.
As token_utils is partly intended to experiments with alternative to Python’s syntax, we had to resort to using an older version, at the cost of not supporting fancy f-strings.
Quick links to topics¶
API
- Token class
- API extracted by Sphinx
- token_utils.token_class.Token
- __bool__
- __contains__
- __eq__
- __hash__
- __init__
- __len__
- __repr__
- __str__
- __weakref__
- clone
- indentation
- is_NULL
- is_assignment
- is_bitwise
- is_bracket
- is_close_bracket
- is_comment
- is_comparison
- is_complex
- is_f_string
- is_float
- is_identical
- is_identifier
- is_immediately_after
- is_immediately_before
- is_in
- is_indentation
- is_integer
- is_keyword
- is_matching_bracket
- is_math_operator
- is_name
- is_newline
- is_number
- is_open_bracket
- is_operator
- is_other_operator
- is_space
- is_string
- is_unclosed_string
- token_utils.token_class.add_operator
- token_utils.token_class.make_fake_token
- token_utils.token_class.Token
- API extracted by Sphinx
- Tokenizing related methods
- API extracted by Sphinx
- tokenizing.py
- token_utils.tokenizing.dedent
- token_utils.tokenizing.generate_tokens
- token_utils.tokenizing.get_logical_lines
- token_utils.tokenizing.get_physical_lines
- token_utils.tokenizing.print_tokens
- token_utils.tokenizing.stringify
- token_utils.tokenizing.tokenize
- token_utils.tokenizing.untokenize
- API extracted by Sphinx
- Utility functions and classes
- Custom warnings
Appendix