operations
Utility functions for generating a UUID.
Can generate a UUID as a uuid.UUID, str, or hex (UUID without '-' characters).
Allows trimming a number of characters from the end of a UUID string, as well as returning the first n number of characters.
Note
A UUID string is 36 characters (32 characters as hex).
first_n_chars(first_n=36, in_uuid=uuid.uuid4(), as_hex=False)
Return first n characters of UUID string (where n is first_n).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
first_n |
int
|
trim (int): Number of characters to remove from beginning of UUID string. |
36
|
in_uuid |
str
|
in_uuid (str): An existing UUID |
uuid4()
|
as_hex |
bool
|
as_hex (bool): If |
False
|
Returns
(str): A 36 character UUID string
(str): A 32 character UUID hex string (a UUID minus the `-` characters)
Raises
ValueError: If input `first_n` is an invalid number of characters to return, less than 0 or greater than predefined max value (32 for hex, 36 for standard).
Source code in src/red_utils/std/uuid_utils/operations.py
gen_uuid(as_hex=False)
Return a UUID.
Nested function to simply return a UUID object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
as_hex |
bool
|
If True, returns a UUID hex (a UUID without the '-' characters, which is 32 characters instead of 36). |
False
|
Returns
(str): A 36 character UUID string
(str): A 32 character UUID hex string (a UUID minus the `-` characters)
Source code in src/red_utils/std/uuid_utils/operations.py
get_rand_uuid(trim=0, characters=0, as_str=True, as_hex=False)
Return a UUID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trim |
int
|
Remove |
0
|
characters(int) |
Return first |
required | |
as_str |
bool
|
|
True
|
as_hex |
bool
|
Return UUID as a hexadecimal (32 chars, UUID without |
False
|
Returns
(str): A 36 character UUID string
(str): A 32 character UUID hex string (a UUID minus the `-` characters)
Raises
ValueError: If inputs `trim` or `characters` are invalid. TODO: Add `TypeErrors` too
Source code in src/red_utils/std/uuid_utils/operations.py
trim_uuid(trim=0, in_uuid=uuid.uuid4(), as_hex=False)
Trim UUID string, removing n characters from end of string (where n is value of trim).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trim |
int
|
Number of characters to remove from end of UUID string. |
0
|
in_uuid |
str
|
An existing UUID |
uuid4()
|
as_hex |
bool
|
If |
False
|
Returns
(str): A 36 character UUID string
(str): A 32 character UUID hex string (a UUID minus the `-` characters)