function normalize_uid
Normalizes a UID value by converting it to a string, or generates a new UUID v4 if the input is empty or None.
/tf/active/vicechatdev/CDocs/utils/uid_helper.py
10 - 26
simple
Purpose
This function ensures consistent UID formatting in database operations by standardizing UID values to strings. When no valid UID is provided (None, empty string, or falsy value), it automatically generates a new UUID v4 to guarantee uniqueness. This is useful for maintaining data integrity and ensuring all records have valid unique identifiers.
Source Code
def normalize_uid(uid_value):
"""
Normalize a UID to ensure consistent format in the database.
Parameters
----------
uid_value : str or None
The UID value to normalize
Returns
-------
str
Normalized UID
"""
if not uid_value:
return str(uuid.uuid4())
return str(uid_value)
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
uid_value |
- | - | positional_or_keyword |
Parameter Details
uid_value: The UID value to normalize. Can be a string containing an existing UID, None, or any falsy value (empty string, 0, False, etc.). If a truthy value is provided, it will be converted to a string. If a falsy value is provided, a new UUID v4 will be generated.
Return Value
Returns a string representation of the normalized UID. If uid_value was truthy, returns str(uid_value). If uid_value was falsy (None, empty string, etc.), returns a newly generated UUID v4 as a string in the format 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx' where x is any hexadecimal digit and y is one of 8, 9, A, or B.
Dependencies
uuid
Required Imports
import uuid
Usage Example
import uuid
def normalize_uid(uid_value):
if not uid_value:
return str(uuid.uuid4())
return str(uid_value)
# Example 1: Normalize an existing UID
existing_uid = "12345-abcde"
normalized = normalize_uid(existing_uid)
print(normalized) # Output: '12345-abcde'
# Example 2: Generate new UID when None is passed
new_uid = normalize_uid(None)
print(new_uid) # Output: 'a1b2c3d4-e5f6-4789-a012-b3c4d5e6f7a8' (example UUID)
# Example 3: Generate new UID when empty string is passed
new_uid_empty = normalize_uid('')
print(new_uid_empty) # Output: 'f9e8d7c6-b5a4-4321-9876-543210fedcba' (example UUID)
# Example 4: Normalize integer UID
int_uid = normalize_uid(12345)
print(int_uid) # Output: '12345'
Best Practices
- This function treats all falsy values (None, empty string, 0, False, empty list, etc.) the same way by generating a new UUID. Be aware that passing 0 or False will result in a new UUID being generated.
- The function does not validate whether the input is a valid UUID format - it simply converts any truthy value to a string. Consider adding UUID format validation if strict UUID compliance is required.
- Each call with a falsy value generates a unique UUID v4, so calling normalize_uid(None) multiple times will produce different results each time.
- The function always returns a string type, ensuring consistent data types for database storage.
- Consider using this function as a default value generator in database models or ORM definitions to ensure all records have valid UIDs.
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function extract_uid 53.1% similar
-
function get_node_by_uid 48.4% similar
-
function create_user 44.7% similar
-
function create_node_with_uid 44.6% similar
-
function update_node 44.3% similar