Lexical ID Generator

2021.1006 · active · verified Thu Apr 16

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the basic usage of the `lexid.incr()` function. It shows how to increment a string-based ID and highlights the unique lexical ordering mechanism where the leftmost digit can be incremented 'early' to maintain ordering, which can increase the ID's length. It also includes the recommended practice of starting with a higher number to mitigate frequent rollovers.

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

view raw JSON →