Utilities
The Utils module provides essential utility functions for workflow operations including ID generation, datetime handling, data transformation, and cross-product operations.
ID Generation
gen_id
Generate running ID for tracking workflow executions. Uses MD5 algorithm or simple mode based on configuration.
ID Generation
from ddeutil.workflow.utils import gen_id
# Simple hash-based ID
id1 = gen_id("workflow-name")
# Output: "a1b2c3d4e5"
# Case-insensitive ID
id2 = gen_id("WORKFLOW-NAME", sensitive=False)
# Output: "a1b2c3d4e5" (same as lowercase)
# Unique ID with timestamp
id3 = gen_id("workflow-name", unique=True)
# Output: "20240115103000123456Ta1b2c3d4e5"
# Simple mode (configurable)
id4 = gen_id("workflow-name", simple_mode=True)
# Output: "20240115103000123456Ta1b2c3d4e5"
Parameters
Parameter | Type | Default | Description |
---|---|---|---|
value |
Any | Required | Value to generate ID from |
sensitive |
bool | True |
Case-sensitive ID generation |
unique |
bool | False |
Add timestamp for uniqueness |
simple_mode |
bool | None | None |
Use simple mode (from config) |
extras |
dict | None | None |
Override config values |
default_gen_id
Generate a default running ID for manual executions.
Default ID
cut_id
Cut running ID to specified length for display purposes.
ID Cutting
DateTime Utilities
get_dt_now
Get current datetime with timezone and offset support.
Current DateTime
get_d_now
Get current date with timezone and offset support.
Current Date
replace_sec
Replace seconds and microseconds in datetime to zero.
Time Replacement
clear_tz
Remove timezone information from datetime object.
Timezone Removal
get_diff_sec
Get difference in seconds between datetime and current time.
Time Difference
Date/Time Checking
reach_next_minute
Check if datetime is in the next minute relative to current time.
Minute Check
wait_until_next_minute
Wait with sleep until the next minute with optional offset.
Wait Function
delay
Delay execution with random offset for load distribution.
Random Delay
Data Transformation
to_train
Convert camelCase to train-case (kebab-case).
Case Conversion
prepare_newline
Prepare newline characters in strings for consistent formatting.
Newline Preparation
filter_func
Filter out function objects from data structures, replacing with function names.
Function Filtering
dump_all
Recursively dump all nested Pydantic models to dictionaries.
Model Dumping
from ddeutil.workflow.utils import dump_all
from pydantic import BaseModel
class User(BaseModel):
name: str
age: int
class Team(BaseModel):
name: str
members: list[User]
team = Team(
name="Dev Team",
members=[User(name="Alice", age=30), User(name="Bob", age=25)]
)
# Dump all nested models
result = dump_all(team)
# Output: Plain dict with all nested models converted
Matrix Operations
cross_product
Generate cross product of matrix values for parameter combinations.
Cross Product
from ddeutil.workflow.utils import cross_product
matrix = {
"env": ["dev", "prod"],
"version": ["1.0", "2.0"],
"region": ["us", "eu"]
}
# Generate all combinations
for combination in cross_product(matrix):
print(combination)
# Output:
# {"env": "dev", "version": "1.0", "region": "us"}
# {"env": "dev", "version": "1.0", "region": "eu"}
# {"env": "dev", "version": "2.0", "region": "us"}
# {"env": "dev", "version": "2.0", "region": "eu"}
# {"env": "prod", "version": "1.0", "region": "us"}
# {"env": "prod", "version": "1.0", "region": "eu"}
# {"env": "prod", "version": "2.0", "region": "us"}
# {"env": "prod", "version": "2.0", "region": "eu"}
File Operations
make_exec
Make a file executable by changing its permissions.
File Permissions
Object Utilities
obj_name
Get the name of an object, class, or string.
Object Names
from ddeutil.workflow.utils import obj_name
class MyClass:
pass
instance = MyClass()
# Get class name from instance
name1 = obj_name(instance) # "MyClass"
# Get class name from class
name2 = obj_name(MyClass) # "MyClass"
# Return string as-is
name3 = obj_name("CustomName") # "CustomName"
# Handle None
name4 = obj_name(None) # None
Configuration
Utility functions can be configured through environment variables:
Variable | Default | Description |
---|---|---|
WORKFLOW_CORE_WORKFLOW_ID_SIMPLE_MODE |
false |
Enable simple ID generation mode |
WORKFLOW_CORE_TZ |
UTC |
Default timezone for datetime operations |
Performance Notes
- ID generation functions are optimized for high-frequency use
- DateTime utilities handle timezone conversions efficiently
- Cross product operations use generators for memory efficiency
- Function filtering preserves object structure while removing callables