onconova.core.utils
COMMON_HTTP_ERRORS
module-attribute
¶
average(data, weights=None)
¶
Pure Python average function
Source code in onconova/core/utils.py
camel_to_snake(name)
¶
find_uuid_across_models(search_uuid, using='default', app_label=None)
¶
Searches all models with UUIDFields for a specific UUID value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
Union[str, UUID]
|
The UUID string to search for. |
required |
|
str
|
The database alias (defaults to "default"). |
'default'
|
|
Optional[str]
|
The app's label on which to search for. If not specified, defaults to all apps. |
None
|
Returns:
Type | Description |
---|---|
Optional[Model]
|
Django model instance if found, else None. |
Source code in onconova/core/utils.py
hash_to_range(input_str, secret, low=-90, high=90)
¶
Maps a given string to a random integer in the range [low, high] deterministically using the given secret string.
The function works by combining the input string with the secret string and then computing the SHA-256 hash of the combined string. The hash is then converted to a large integer and mapped to the range [low, high] using the modulo operator.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
str
|
The string to be mapped. |
required |
|
str
|
The secret string used for hashing. |
required |
|
int
|
The lowest value of the range (default: -90). |
-90
|
|
int
|
The highest value of the range (default: 90). |
90
|
Returns:
Type | Description |
---|---|
int
|
An integer in the range [low, high] that is deterministically mapped from the |
int
|
input string. |
Source code in onconova/core/utils.py
is_datetime(date_string, date_format)
¶
Checks if a given string can be parsed as a datetime object with the given date format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
str
|
The string to be parsed. |
required |
|
str
|
The date format to use for parsing. |
required |
Returns:
Type | Description |
---|---|
bool
|
True if the string can be parsed as a datetime object with the given date format, False otherwise. |
Source code in onconova/core/utils.py
is_enum(annotation)
¶
is_list(annotation)
¶
is_literal(annotation)
¶
is_nullable(annotation)
¶
Check if a field is nullable, i.e. its type is Nullable.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
Any
|
The annotation to check. |
required |
Returns:
Type | Description |
---|---|
bool
|
True if the field is nullable, False otherwise. |
Source code in onconova/core/utils.py
is_optional(annotation)
¶
Check if a field is optional, i.e. its type is a Union containing None.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
Any
|
The annotation to check. |
required |
Returns:
Type | Description |
---|---|
bool
|
True if the field is optional, False otherwise. |
Source code in onconova/core/utils.py
is_period(period_string, date_format)
¶
Checks if a given string can be parsed as a period string with the given date format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
str
|
The string to be parsed. |
required |
|
str
|
The date format to use for parsing. |
required |
Returns:
Type | Description |
---|---|
bool
|
True if the string can be parsed as a period string with the given date format, False otherwise. |
Source code in onconova/core/utils.py
is_union(annotation)
¶
median(data)
¶
Pure Python median function
Source code in onconova/core/utils.py
mkdir_p(path)
¶
Create a directory and all intermediate-level directories needed to contain it.
This function attempts to mimic the behavior of the Unix command mkdir -p
.
If the directory already exists, no exception is raised. Compatible with both
Python 2 and Python 3.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
str
|
The directory path to create. |
required |
Raises:
Type | Description |
---|---|
OSError
|
If the directory cannot be created and does not already exist. |
Source code in onconova/core/utils.py
percentile(data, percentile)
¶
Pure Python percentile function Supports single percentile value (0-100)
Source code in onconova/core/utils.py
revert_multitable_model(instance, eventId)
¶
Revert a multi-table Django model to a specific history event.
This function is specifically designed to work with multi-table Django models that are versioned using pghistory. It will find the history event with the given ID and revert the instance to that point in time.
:param instance: The instance to revert. :param eventId: The ID of the history event to revert to. :return: The reverted instance. :raises ObjectDoesNotExist: If no matching history event exists.
Source code in onconova/core/utils.py
std(data, ddof=0)
¶
Pure Python standard deviation function
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
list
|
List of numeric values |
required |
|
int
|
Delta Degrees of Freedom (0 for population, 1 for sample) |
0
|
Returns:
Type | Description |
---|---|
float
|
Standard deviation of the data |
Source code in onconova/core/utils.py
to_camel_case(string)
¶
to_pascal_case(string)
¶
Convert a string from snake_case to PascalCase.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
str
|
The string to convert. |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The converted string. |