Skip to main content

A tool that converts arbitrary text (like user input or file names) into valid Python identifiers while preserving as much of the original meaning as possible.

Project description

A tool that converts arbitrary text (like user input or file names) into valid Python identifiers while preserving as much of the original meaning as possible.

Installation

pip install text-to-identifier

Usage

# coding=utf-8
from text_to_identifier import text_to_identifier

assert text_to_identifier(u"") == u"_"
assert text_to_identifier(u" \r\n\t") == u"_"
assert text_to_identifier(u"123abc") == u"_123abc"
assert text_to_identifier(u"&abc 123") == u"_abc_123"
assert text_to_identifier(u"  hello  world  $") == u"_hello_world__"
assert text_to_identifier(u"测试@unicode") == u"测试_unicode"

How It Works

First, get_unicode_character_type() categorizes each Unicode character into one of four types:

  • LETTER_OR_UNDERSCORE (Unicode category starting with 'L' or underscore _)
  • DECIMAL_DIGIT (Unicode category 'Nd')
  • SPACE_OR_CONTROL (Unicode categories starting with 'Z' or 'Cc')
  • OTHER (all other characters)

Then, it implements the following conversion rules using a state machine:

  • The first character must be a letter/underscore. If the first character is a digit, prepend _ to make it valid.
  • Subsequent valid characters (letters/underscores/digits) are kept as-is.
  • Other characters are replaced with underscores, but whitespace/control character sequences are collapsed into single underscores.
  • Ensures the output is non-empty (appends _ if empty).

Contributing

Contributions are welcome! Please submit pull requests or open issues on the GitHub repository.

License

This project is licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

text_to_identifier-0.1.0a1.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

text_to_identifier-0.1.0a1-py2.py3-none-any.whl (4.3 kB view details)

Uploaded Python 2Python 3

File details

Details for the file text_to_identifier-0.1.0a1.tar.gz.

File metadata

  • Download URL: text_to_identifier-0.1.0a1.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.2

File hashes

Hashes for text_to_identifier-0.1.0a1.tar.gz
Algorithm Hash digest
SHA256 4125733b3e6c2888fbf7be0f38ec5f2929cbf35c29f030c1d2b3aba179070e4d
MD5 fffaf27b0fc46844ff21f1823023981b
BLAKE2b-256 6ccc6de95a878faa5d62c8e93aa6b443eb742b14c037e446e8efbb30c3d277ed

See more details on using hashes here.

File details

Details for the file text_to_identifier-0.1.0a1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for text_to_identifier-0.1.0a1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 3b30e9cfa2b8c8b3f97dfd679927623ce8d9194964265d7d7d147c4cbf52d9ad
MD5 cbaea124e3fd587032958b30ca3a4869
BLAKE2b-256 836943ef835a21c206b52ac891ade144e9935169c14b5a2e21844367d76fa502

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page