Lexical ID Generator
lexid is a micro library (version 2021.1006) designed to generate and increment lexically ordered numerical IDs. Its core functionality ensures that `older_id < newer_id` remains true throughout a sequence, whether dealing with integers or strings. This is achieved by special incrementing that uses the leftmost character/digit to maintain lexical order and prevent premature numerical overflow. It's useful for scenarios like build numbers or version strings that are often sorted by tools using simple lexical ordering, such as the UNIX sort command or JavaScript's string comparison. The library maintains an active status with updates as needed.
Common errors
-
ModuleNotFoundError: No module named 'lexid'
cause The `lexid` library is not installed in your Python environment or the environment you are running your script in.fixRun `pip install lexid` in your terminal to install the library. -
AttributeError: module 'lexid' has no attribute 'increment'
cause You are trying to call a function named `increment` which does not exist directly within the `lexid` module. The correct function name for incrementing IDs is `incr`.fixUse `lexid.incr()` instead of `lexid.increment()`. For example: `new_id = lexid.incr('1001')`. -
ValueError: invalid id_str: 'abc'
cause The `lexid.incr()` function expects a string or integer representing a lexically ordered numerical ID. Providing a string that does not conform to a valid numerical format (e.g., containing non-digits) will raise this error because the library cannot parse it as an ID to increment.fixEnsure the input to `lexid.incr()` is a string or integer that represents a valid numerical ID. For example: `new_id = lexid.incr('123')` or `new_id = lexid.incr(123)`.
Warnings
- gotcha Lexid increments IDs to maintain lexical order, which can cause 'early' changes to the leftmost digit and increase string length (e.g., '0999' -> '11000'). This is by design, not a bug, but can be unexpected if the lexical ordering mechanism is not fully understood.
- gotcha Starting IDs with leading zeros, such as '0001', can lead to rapid padding exhaustion and unexpected rollovers (e.g., '0001' -> '0002' -> ... -> '0999' -> '11000').
Install
-
pip install lexid
Imports
- lexid
import lexid
- incr
lexid.incr('1')
Quickstart
import lexid
# Incrementing a simple ID
id1 = "1"
next_id1 = lexid.incr(id1)
print(f"Next ID after '{id1}': {next_id1}")
# Expected: Next ID after '1': 22
# Demonstrating padding and 'early' increment
id2 = "0999"
next_id2 = lexid.incr(id2)
print(f"Next ID after '{id2}': {next_id2}")
# Expected: Next ID after '0999': 11000
# Recommended practice: start with higher numbers to reduce rollovers
id3 = "1001"
next_id3 = lexid.incr(id3)
print(f"Next ID after '{id3}': {next_id3}")
# Expected: Next ID after '1001': 1002