Utility functions¶
aana.utils.asyncio
¶
run_async
¶
Run a coroutine in a thread if the current thread is running an event loop.
Otherwise, run the coroutine in the current asyncio loop.
Useful when you want to run an async function in a non-async context.
From: https://stackoverflow.com/a/75094151
PARAMETER | DESCRIPTION |
---|---|
coro |
The coroutine to run.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
T
|
The result of the coroutine.
TYPE:
|
Source code in aana/utils/asyncio.py
aana.utils.json
¶
json_serializer_default
¶
Default function for json serializer to handle custom objects.
If json serializer does not know how to serialize an object, it calls the default function.
For example, if we see that the object is a pydantic model, we call the dict method to get the dictionary representation of the model that json serializer can deal with.
If the object is not supported, we raise a TypeError.
PARAMETER | DESCRIPTION |
---|---|
obj |
The object to serialize.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
object
|
The serializable object.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
TypeError
|
If the object is not a pydantic model, Path, or Media object. |
Source code in aana/utils/json.py
jsonify
¶
Serialize content using orjson.
PARAMETER | DESCRIPTION |
---|---|
data |
The content to serialize.
TYPE:
|
option |
The option for orjson.dumps.
TYPE:
|
as_bytes |
Return output as bytes instead of string
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
str | bytes
|
bytes | str: The serialized data as desired format. |
Source code in aana/utils/json.py
aana.utils.download
¶
download_model
¶
Download a model from a URL.
PARAMETER | DESCRIPTION |
---|---|
url |
the URL of the file to download
TYPE:
|
model_hash |
hash of the model file for checking sha256 hash if checksum is True
TYPE:
|
model_path |
optional model path where it needs to be downloaded
TYPE:
|
check_sum |
boolean to mention whether to check SHA-256 sum or not
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Path
|
the downloaded file path
TYPE:
|
RAISES | DESCRIPTION |
---|---|
DownloadException
|
Request does not succeed. |
Source code in aana/utils/download.py
download_file
¶
Download a file from a URL.
PARAMETER | DESCRIPTION |
---|---|
url |
the URL of the file to download
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
bytes
|
the file content
TYPE:
|
RAISES | DESCRIPTION |
---|---|
DownloadException
|
Request does not succeed. |