🔍 Code Extractor

function normalize_uid

Maturity: 43

Normalizes a UID value by converting it to a string, or generates a new UUID v4 if the input is empty or None.

File:
/tf/active/vicechatdev/CDocs/utils/uid_helper.py
Lines:
10 - 26
Complexity:
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.

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function extract_uid 53.1% similar

    Extracts a UID (Unique Identifier) from an object or dictionary by checking both 'uid' and 'UID' properties, returning a string representation or a default value if not found.

    From: /tf/active/vicechatdev/CDocs/utils/uid_helper.py
  • function get_node_by_uid 48.4% similar

    Retrieves a node from a Neo4j graph database by its unique identifier (UID) and returns it as a dictionary.

    From: /tf/active/vicechatdev/CDocs/db/db_operations.py
  • function create_user 44.7% similar

    Creates a new user in the system with validation, password hashing, and role assignment, including CDocs user extension setup.

    From: /tf/active/vicechatdev/CDocs/controllers/admin_controller.py
  • function create_node_with_uid 44.6% similar

    Creates a new node in a Neo4j graph database with a specified UID, label, and properties, automatically adding a creation timestamp if not provided.

    From: /tf/active/vicechatdev/CDocs/db/db_operations.py
  • function update_node 44.3% similar

    Updates properties of a Neo4j graph database node identified by its unique UID, automatically adding a modification timestamp.

    From: /tf/active/vicechatdev/CDocs/db/db_operations.py
← Back to Browse