Utilities
In this file implemented the helper function
timing(f)
Decorator to measure and print execution time of a function. If it's a class method, logs as 'ClassName.method'. Else just 'function'.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f |
callable
|
The function to be measured. |
required |
Returns:
| Type | Description |
|---|---|
callable
|
The wrapped function. |
Source code in src/utils.py
generate_unit_vector(length)
json_save(path, obj)
Save supported files to json format
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path |
Union[str, Path]
|
path to save object |
required |
obj: Dict object
Source code in src/utils.py
json_load(path, encoding='utf-8')
Load json files from path
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path |
Union[str, Path]
|
path to save object |
required |
encoding |
str
|
encoding type |
'utf-8'
|
Returns:
| Name | Type | Description |
|---|---|---|
obj |
object
|
Contents of the file |
Source code in src/utils.py
generate_guid(k=6)
Generate random id
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
k |
int
|
length of guid |
6
|
Returns:
| Name | Type | Description |
|---|---|---|
guid |
str
|
random id |
Source code in src/utils.py
add_ids_to_dicts(ids, dicts)
Add ids to corresponding dictionaries
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ids |
List[int]
|
ids to add to the dictionaries in the same order as the dicts themselves |
required |
dicts |
List[dict]
|
|
required |
Returns:
| Name | Type | Description |
|---|---|---|
out |
List[dict]
|
updated dictionaries |
Source code in src/utils.py
argmax_dict(dict_)
Return dict item with maximal value
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dict_ |
dict
|
any dictionary with numeric values |
required |
Returns:
| Name | Type | Description |
|---|---|---|
out |
dict
|
element(s) with maximal value |
Source code in src/utils.py
pil_to_b64(image, format_='PNG', encoding='ascii')
Encode PIL.Image to base64 string (with given encoding)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image |
Image
|
input image |
required |
format_ |
str
|
format to save image in buffer |
'PNG'
|
encoding |
str
|
bytes to string encoding method |
'ascii'
|
Returns:
| Name | Type | Description |
|---|---|---|
image_encoded |
str
|
output string |
Source code in src/utils.py
b64_to_pil(image_b64)
Decode base64 converted string into image
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image_b64 |
str
|
base64 image |
required |
Returns:
| Name | Type | Description |
|---|---|---|
image |
Image
|
out image |
Source code in src/utils.py
generate_openapi_json(openapi_version='3.1.0')
Generate and save openapi specification of the application
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
openapi_version |
str
|
openapi version specifier |
'3.1.0'
|
Returns:
| Type | Description |
|---|---|
None
|
|
Source code in src/utils.py
get_subclasses(cls)
Get subclasses of the given class recursively. Function will work even for nested inheritances.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls |
Object
|
given class |
required |
Returns:
| Name | Type | Description |
|---|---|---|
subclasses |
List
|
all subclasses |
Source code in src/utils.py
convert_to_supported_format(base64_img, conversion_format='PNG')
Checks whether the base64 has is of a supported format. If not, converts it to conversion format
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base64_img |
str
|
Image represented as a base64 string |
required |
conversion_format |
str = PNG
|
The format to convert the image to in case the format is not supported |
'PNG'
|
Returns:
| Name | Type | Description |
|---|---|---|
base64_img |
str
|
Image represented as a base64 string in a supported format |
is_valid |
bool
|
Whether the resulting b64 os a valid supported image or not. False if the conversion failed |
Source code in src/utils.py
is_supported_format(base64_str)
Function that checks weather or not the string starts with specified format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base64_str |
str
|
Checks weather str starts with the specified format |
required |
Returns:
| Name | Type | Description |
|---|---|---|
out |
bool
|
|
Source code in src/utils.py
process_image_formats(ids, instances)
Process image formats to separate valid and invalid images.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ids |
List
|
ids of inputs |
required |
instances |
List
|
inputs to the network |
required |
Returns:
| Type | Description |
|---|---|
Tuple containing lists of valid ids, valid images (base64 encoded), and invalid ids.
|
|