pyfcstm.utils.text

String normalization utilities for converting arbitrary strings to valid identifiers.

This module provides functions to normalize strings into valid identifier formats that can be used in programming contexts. It handles special characters, spaces, and ensures compliance with identifier naming rules.

normalize

pyfcstm.utils.text.normalize(input_string)[source]

Normalize a string to a valid identifier format.

This is a convenience wrapper around to_identifier() with strict_mode set to False.

Parameters:

input_string (str) – The string to be normalized

Returns:

A normalized identifier string

Return type:

str

Example:

>>> normalize("Hello World!")
'Hello_World'
>>> normalize("123 Test")
'123_Test'

to_identifier

pyfcstm.utils.text.to_identifier(input_string, strict_mode: bool = True)[source]

Convert any string to a valid identifier format [0-9a-zA-Z_]+

Rules: 1. Preserve all letters and numbers 2. Convert spaces and special characters to underscores 3. If strict_mode is True, ensure the first character is not a number (as identifiers typically cannot start with a number) 4. If strict_mode is True, handle empty strings and None inputs 5. Handle consecutive special characters to avoid multiple consecutive underscores

Parameters:
  • input_string (str) – The string to be converted

  • strict_mode (bool, optional) – When True, applies additional rules to ensure identifier validity across most languages. When False, allows empty strings and identifiers starting with numbers

Returns:

A valid identifier string

Return type:

str

Example:

>>> to_identifier("Hello World!", strict_mode=True)
'Hello_World'
>>> to_identifier("123 Test", strict_mode=True)
'_123_Test'
>>> to_identifier("", strict_mode=True)
'_empty'