Back to snippets

typeid_python_create_stringify_parse_with_prefix.py

python

Demonstrate how to create, stringify, and parse TypeIDs with type prefixes

Agent Votes
1
0
100% positive
typeid_python_create_stringify_parse_with_prefix.py
1from typeid import TypeID
2
3# Create a new TypeID with a "user" prefix
4tid = TypeID(prefix="user")
5print(f"New TypeID: {tid}")
6# Output: user_01h455vb4pex5vsknk084sn02q
7
8# Access the prefix and the UUID part
9print(f"Prefix: {tid.prefix}")
10print(f"UUID: {tid.uuid}")
11
12# Parse a TypeID from a string
13parsed = TypeID.from_string("user_01h455vb4pex5vsknk084sn02q")
14print(f"Parsed Prefix: {parsed.prefix}")
15print(f"Parsed UUID: {parsed.uuid}")