Common¶
Environmental Intelligence: Geospatial APIs SDK (ibmpairs): A Python module to wrap the core functionality of the Geospatial APIs component.
Copyright 2019-2024 IBM Software: Sustainability, IBM Corp. All Rights Reserved.
SPDX-License-Identifier: BSD-3-Clause
- class common.AsyncThreadWrapper(func, args, kwargs)¶
- run()¶
The method runs an arbitrarily declared asyncio function from information held in the AsyncThreadWrapper object.
- exception common.PAWException(message)¶
- common.check_bool(b: Any) bool ¶
The method checks if a boolean value, or a string or int that can validly be inferred to be boolean is found. If ‘true’ or ‘false’ (to lowercase) or ‘0’ or ‘1’ are found, they are transformed appropriately to a bool.
- Parameters:
b (Any) – The input value to check.
- Returns:
A boolean of appropriate value ‘True’ or ‘False’
- Return type:
bool
- Raises:
Exception – If the type of ‘b’ is not in [‘bool’, ‘string’ or ‘int’], if the value of ‘b’ is a string but not (when converted to lowercase) ‘true’ or ‘false’, if the value of ‘b’ is an integer but not ‘1’ or ‘0’.
- common.check_class(instance: Any, class_type: Type[T]) Any ¶
The method checks if a provided ‘instance’ is of type ‘class_type’ with the intention of usage to check classes.
- Parameters:
instance – The input value to check.
class_type (Type[T]) – The input type to check the input value against.
- Returns:
The input value.
- Return type:
Any
- Raises:
Exception – If the value of ‘instance’ is not type ‘class_type’.
- common.check_dict(d: Any) dict ¶
The method checks if the input value is of type dict.
- Parameters:
d (dict) – The input value to check.
- Returns:
The input value.
- Return type:
dict
- Raises:
Exception – If the type of ‘d’ is not ‘dict’.
- common.check_float(f: Any)¶
The method checks if the input value is of type float, if the input type is an ‘int’ or ‘str’ a cast is attempted.
- Parameters:
f (float) – The input value to check.
- Returns:
The input value.
- Return type:
float
- Raises:
Exception – If the type of ‘f’ is not ‘float’ or ‘int’ or ‘str’, if the input value cannot be cast to ‘float’.
- common.check_int(i: Any)¶
The method checks if the input value is of type int, if the input type is str a cast is attempted.
- Parameters:
s (int) – The input value to check.
- Returns:
The input value.
- Return type:
int
- Raises:
Exception – If the type of ‘i’ is not ‘int’ or ‘str’, if the input value cannot be cast to ‘int’.
- common.check_list(l: Any) list ¶
The method checks if the input value is of type list.
- Parameters:
l (list) – The input value to check.
- Returns:
The input value.
- Return type:
list
- Raises:
Exception – If the type of ‘l’ is not ‘list’.
- common.check_str(s: Any) str ¶
The method checks if the input value is of type str, if the input type is int a cast is attempted.
- Parameters:
s (str) – The input value to check.
- Returns:
The input value.
- Return type:
str
- Raises:
Exception – If the type of ‘s’ is not ‘str’ or ‘int’, if the input value cannot be cast to ‘str’.
- common.class_to_dict(instance: Any, class_type: Type[T]) dict ¶
The method casts a classes attribute structure to a dict.
- Parameters:
instance (Any) – The input instance of a class.
class_type (Type[T]) – The input class type.
- Returns:
A dictionary of the input classes attributes.
- Return type:
dict
- Raises:
Exception – If the class cannot be cast to a dict.
- common.ensure_api_path(host: str, version: int = None)¶
Adds /core/v4, /core/v3 or /v2 to host
- Parameters:
host (int) – The host string.
host – The api version.
- Returns:
The host string with path added.
- Return type:
str
- common.ensure_protocol(host: str)¶
Ensures https:// or https:// is prepended to a string.
- Parameters:
host (str) – The host string.
- Returns:
The host string with the protocol added.
- Return type:
str
- common.ensure_slash(directory: str, position: int)¶
The method ensures a slash is present.
- Parameters:
directory (str) – The directory string to check.
position (int) – The position to check for slash.
- Returns:
A directory path with slash.
- Return type:
str
- common.get_tenant_id(client_id: str)¶
Gets tenant id from client_id with ‘saascore-’, ‘geospatial-’ prefixes or without.
- Parameters:
client_id (str) – A client id for the authentication system.
- Returns:
The tenant id.
- Return type:
str
- common.run_async_in_thread(func, *args, **kwargs)¶
The method runs an arbitrarily declared asyncio function in a new thread.
- Parameters:
func (str) – The name of the function to run.
args (args) – The arguments of the function.
kwargs (kwargs) – The arguments of the function.
- Returns:
the result of the execution of the thread
- Return type:
AsyncThreadWrapper.result
- common.set_client(input_client, global_client, self_client=None)¶
The method selects a ibmpairs.client.Client from a few choices.
- Parameters:
input_client (ibmpairs.client.Client) – A client input into the method that calls set_client().
global_client (ibmpairs.client.Client) – The global client in the environment.
self_client (ibmpairs.client.Client) – The client held in the object that calls the method that calls set_client().
- Returns:
the selected client from the hierarchy.
- Return type:
ibmpairs.client.Client
- Raises:
Exception – If no client is present.
- common.strip_api_path(host: str)¶
Removes core/v4, core/v3 and /v2 from a string.
- Parameters:
host (str) – The host string.
- Returns:
The host string with the path.
- Return type:
str
- common.strip_protocol(host: str)¶
Removes https:// or http:// from a string.
- Parameters:
host (str) – The host string.
- Returns:
The host string with the protocol removed.
- Return type:
str
- common.strip_slash(input: str)¶
The method ensures a slash is not at the end of a string. :param intput: The input string to check. :type input: str :returns: A string without a trailing “/”. :rtype: str