Skip to main content

Build Status Coverage Status

tokenize-rt

The stdlib tokenize module does not properly roundtrip. This wrapper around the stdlib provides two additional tokens ESCAPED_NL and UNIMPORTANT_WS, and a Token data type. Use src_to_tokens and tokens_to_src to roundtrip.

This library is useful if you're writing a refactoring tool based on the python tokenization.

Installation

pip install tokenize-rt

Usage

datastructures

tokenize_rt.Offset(line=None, utf8_byte_offset=None)

A token offset, useful as a key when cross referencing the ast and the tokenized source.

tokenize_rt.Token(name, src, line=None, utf8_byte_offset=None)

Construct a token

  • name: one of the token names listed in token.tok_name or ESCAPED_NL or UNIMPORTANT_WS
  • src: token's source as text
  • line: the line number that this token appears on. This will be None for ESCAPED_NL and UNIMPORTANT_WS tokens.
  • utf8_byte_offset: the utf8 byte offset that this token appears on in the line. This will be None for ESCAPED_NL and UNIMPORTANT_WS tokens.

tokenize_rt.Token.offset

Retrieves an Offset for this token.

converting to and from Token representations

tokenize_rt.src_to_tokens(text) -> List[Token]

tokenize_rt.tokens_to_src(Sequence[Token]) -> text

additional tokens added by tokenize-rt

tokenize_rt.ESCAPED_NL

tokenize_rt.UNIMPORTANT_WS

helpers

tokenize_rt.NON_CODING_TOKENS

A frozenset containing tokens which may appear between others while not affecting control flow or code:

  • COMMENT
  • ESCAPED_NL
  • NL
  • UNIMPORTANT_WS

tokenize_rt.parse_string_literal(text) -> Tuple[str, str]

parse a string literal into its prefix and string content

>>> parse_string_literal('f"foo"')
('f', '"foo"')

tokenize_rt.reversed_enumerate(Sequence[Token]) -> Iterator[Tuple[int, Token]]

yields (index, token) pairs. Useful for rewriting source.

Differences from tokenize

  • tokenize-rt adds ESCAPED_NL for a backslash-escaped newline "token"
  • tokenzie-rt adds UNIMPORTANT_WS for whitespace (discarded in tokenize)
  • tokenize-rt normalizes string prefixes, even if they are not parsed -- for instance, this means you'll see Token('STRING', "f'foo'", ...) even in python 2.
  • tokenize-rt normalizes DEDENT tokens so they appear before the indentation instead of after

Sample usage

Metadata

Release files for tokenize-rt 3.0.0

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

Source distribution (sdist)

Source distribution for tokenize-rt 3.0.0
File Size Uploaded Provenance Actions
tokenize_rt-3.0.0.tar.gz 4.7 kB Without provenance Details for tokenize_rt-3.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for tokenize-rt 3.0.0
File Interpreter ABI Platform Actions
tokenize_rt-3.0.0-py2.py3-none-any.whl Python 2, Python 3 none any Details for tokenize_rt-3.0.0-py2.py3-none-any.whl

Total release size: 9.9 kB

Release files / tokenize_rt-3.0.0.tar.gz

Download URL tokenize_rt-3.0.0.tar.gz
Size 4.7 kB
Tags Source
SHA-256 checksum
How to use checksums
a599c322fd7096707e4419e280eccd80a878ee12a8365aa86cade7d153078982
BLAKE2b-256 checksum
How to use checksums
5566e8b9dd283a2aaf6bd4e869675f88837b65381e76135e19f0dd9aa1b4e5dd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.7

Release files / tokenize_rt-3.0.0-py2.py3-none-any.whl

Download URL tokenize_rt-3.0.0-py2.py3-none-any.whl
Size 5.1 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
0ad773efe3325971ebb06231879166d63d0f64acbc224a78c91e3f5b5ec34c0a
BLAKE2b-256 checksum
How to use checksums
95e63d487dc522a442f88e5730538a3b9a380d04a331c305251e44f9e5ddabd5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.7

Security for tokenize-rt 3.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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