utils package

Submodules

utils.ApiBase module

class utils.ApiBase.ApiBase(base_dm: Type[BaseModel] | None = None)[source]

Bases: object

Base class to implement an API for the toolbox projects.

Attributes to override:
  • VERSION (str): The version of the API.

  • TITLE (str): The title of the API.

host

The bind host.

Type:

str

port

The bind port.

Type:

int

allowed_origins

List of origins that should be permitted to make cross-origin requests.

Type:

List[str]

local_image_storage

If the images are stored locally and can be accessed by their path.

Type:

bool

context_cli

The ContextCli.

Type:

ContextCli

config

The config of the API.

Type:

dict

base_dm

Data model class that will be sent.

Type:

Optional[Type[BaseModel]]

Methods to be implemented:
  • _predict_entity(data_model, post_to_broker) -> List[Type[BaseModel]]

Initialize the API.

Parameters:

base_dm (Optional[Type, optional) – Data model class that will be sent. Defaults to None.

TITLE = 'BASE API'
VERSION = '0.2.2'
run()[source]

Parse the command-line arguments and run the API server.

utils.DemoBase module

class utils.DemoBase.DemoBase[source]

Bases: object

Base class to implement a demo application for the toolbox projects.

Methods to be implemented:
  • _load_model(config, task)

  • _process_image(image)

Initialize the demo.

run()[source]

Run the demo.

utils.config_utils module

utils.config_utils.parse_config(path: str | Path, parse_env: bool = True, parse_base: bool = True) dict[source]

Parse a config yaml file.

Parameters:
  • parse_env (bool, optional) – Parse environment variables (e.g. !ENV ${PORT}). Defaults to True.

  • parse_base (bool, optional) – Parse the base config specified with ‘__BASE__: base/config/path.yaml’. Defaults to True.

Returns:

dict

utils.config_utils.read_yaml(path: str | Path) dict[source]

Parse a yaml file.

Parameters:

path (Union[str, Path]) – Path to a yaml file.

Returns:

The parsed yaml

Return type:

dict

utils.config_utils.read_yaml_env(path: str | Path, tag: str = '!ENV') dict[source]

Load a yaml file and resolve the environment variables. The environment variables must have !ENV before them and be in the format: ${VAR_NAME}. (e.g. port: !ENV ${PORT})

Parameters:
  • path (Union[str, Path]) – Path to the yaml file.

  • tag (str, optional) – Tag used to mark where to start searching for the environment variables. Defaults to ‘!ENV’.

Returns:

The parsed yaml

Return type:

dict

utils.config_utils.update_dict(base: dict, new: dict) dict[source]

Update a dict recursively.

Parameters:
  • base (dict) – The base dict to update.

  • new (dict) – Dict with the values to update.

Returns:

base dict with the values of new dict updated.

Return type:

dict

utils.simple_http_server module

utils.simple_http_server.create_http_server(port: int, get_callback: callable | None = None, post_callback: callable | None = None)[source]

Run a basic https server that calls a callback every time a get or post request is received. Runs forever and blocks the execution until a KeyboardInterrupt exception occurs.

Parameters:
  • port (int) – The server port.

  • get_callback (Union[callable, None]) – Function to call when a GET petition is received. It must accept one argument: the path. And return a string with the response.

  • post_callback (Union[callable, None]) – Function to call when a POST petition is received. It must accept three parameters: The path, the content-type and the content data. And return a string with the response.

utils.utils module

utils.utils.float_or_none(value: any)[source]

Tries to convert a value to float. If fails return None.

utils.utils.get_logger(name: str | None = None) Logger[source]

Get the default logger.

Parameters:

name (Optional[str], optional) – Name of the logger. Defaults to None.

Returns:

logging.Logger

utils.utils.get_version()[source]
utils.utils.hash_str(value: str, algorithm='sha1') str[source]

Return the hash of a string.

Parameters:
  • value (str) – The string to hash.

  • algorithm (str, optional) – Name of the hash algorithm. One of [“md5”,”sha1”,”sha224”,”sha256”,”sha384”,”sha512”] Defaults to “sha1”.

Raises:

ValueError – If algorithm is not recognized.

Returns:

The hash string.

Return type:

str

utils.utils.is_url(url: str) bool[source]

Check if a string is a valid URL.

utils.utils.random_string(length: int = 8) str[source]

Create a random strings with digits and upper and lower case letters.

Parameters:

length (int, optional) – Length of the generated string.. Defaults to 8.

Returns:

A random string.

Return type:

str

utils.utils.str_separator(length: int = 80, title: str = '', char: str = '-', new_line: bool = True) str[source]

Create a separator string with an optional title. e.g: ——–title——–

Parameters:
  • length (int, optional) – Total character-length of the string. Defaults to 80.

  • title (str, optional) – Optional separator title. Defaults to “”.

  • char (str, optional) – Character used for the separator. Defaults to “-“.

  • new_line (bool, optional) – Add a new line at the end. Defaults to True.

Returns:

The separator string.

Return type:

str

utils.utils.urljoin(*args: str) str[source]

Join the given arguments into a url. Trailing but not leading slashes are stripped for each argument.

Module contents