Skip to content

Utilities

TWA_BASE_URL module-attribute

TWA_BASE_URL = 'https://www.theworldavatar.com/kg/'

To be used by attaching specific namespace and class name to it e.g. https://www.theworldavatar.com/kg/ontolab/LabEquipment

check_valid_url

check_valid_url(url: str) -> str

This function checks if the provided URL for namespace starts with "http://" or "https://". If so, it returns the URL and add "/" if it's not already ending with a "/" or "#".

Parameters:

Name Type Description Default
url str

The URL to be checked

required

Raises:

Type Description
Exception

The URL is not provided with either "http://" or "https://" as its start

Returns:

Name Type Description
str str

The original URL or the processed URL with a "/" added at its end

Source code in JPS_BASE_LIB/python_wrapper/twa/data_model/utils.py
def check_valid_url(url: str) -> str:
    """
    This function checks if the provided URL for namespace starts with "http://" or "https://".
    If so, it returns the URL and add "/" if it's not already ending with a "/" or "#".

    Args:
        url (str): The URL to be checked

    Raises:
        Exception: The URL is not provided with either "http://" or "https://" as its start

    Returns:
        str: The original URL or the processed URL with a "/" added at its end
    """
    if url.startswith('http://') or url.startswith('https://'):
        return url if url[-1] in ['/', '#'] else url + '/'
    else:
        raise Exception("The provide url for namespace should start with either 'http://' or 'https://'.")

construct_namespace_iri

construct_namespace_iri(base_url: str, namespace: str) -> str

This function constructs the namespace IRI from the base URL and namespace. For example, if the base URL is "https://www.theworldavatar.com/kg" and the namespace is "ontolab", The function will return "https://www.theworldavatar.com/kg/ontolab".

Parameters:

Name Type Description Default
base_url str

The base URL of the namespace IRI, e.g. "https://www.theworldavatar.com/kg"

required
namespace str

The namespace, e.g. "ontolab", will be ignored if None

required

Returns:

Name Type Description
str str

The namespace IRI, e.g. "https://www.theworldavatar.com/kg/ontolab"

Source code in JPS_BASE_LIB/python_wrapper/twa/data_model/utils.py
def construct_namespace_iri(base_url: str, namespace: str) -> str:
    """
    This function constructs the namespace IRI from the base URL and namespace.
    For example, if the base URL is "https://www.theworldavatar.com/kg" and the namespace is "ontolab",
    The function will return "https://www.theworldavatar.com/kg/ontolab".

    Args:
        base_url (str): The base URL of the namespace IRI, e.g. "https://www.theworldavatar.com/kg"
        namespace (str): The namespace, e.g. "ontolab", will be ignored if None

    Returns:
        str: The namespace IRI, e.g. "https://www.theworldavatar.com/kg/ontolab"
    """
    return f'{check_valid_url(base_url)}{namespace}' if namespace is not None else base_url

construct_rdf_type

construct_rdf_type(namespace_iri: str, class_name: str) -> str

This function constructs the RDF type IRI from the namespace IRI and class name.

Parameters:

Name Type Description Default
namespace_iri str

The namespace IRI, e.g. "https://www.theworldavatar.com/kg/ontolab"

required
class_name str

The class name, e.g. "LabEquipment"

required

Returns:

Name Type Description
str str

The RDF type IRI, e.g. "https://www.theworldavatar.com/kg/ontolab/LabEquipment"

Source code in JPS_BASE_LIB/python_wrapper/twa/data_model/utils.py
def construct_rdf_type(namespace_iri: str, class_name: str) -> str:
    """
    This function constructs the RDF type IRI from the namespace IRI and class name.

    Args:
        namespace_iri (str): The namespace IRI, e.g. "https://www.theworldavatar.com/kg/ontolab"
        class_name (str): The class name, e.g. "LabEquipment"

    Returns:
        str: The RDF type IRI, e.g. "https://www.theworldavatar.com/kg/ontolab/LabEquipment"
    """
    return f'{check_valid_url(namespace_iri)}{class_name}'

init_instance_iri

init_instance_iri(namespace_iri: str, class_name: str) -> str

The function constructs a unique IRI for an instance of a class in a namespace.

Parameters:

Name Type Description Default
namespace_iri str

The namespace IRI, e.g. "https://www.theworldavatar.com/kg/ontolab"

required
class_name str

The class name, e.g. "LabEquipment"

required

Returns:

Name Type Description
str str

The unique IRI for the instance, e.g. "https://www.theworldavatar.com/kg/ontolab/LabEquipment_12345678"

Source code in JPS_BASE_LIB/python_wrapper/twa/data_model/utils.py
def init_instance_iri(namespace_iri: str, class_name: str) -> str:
    """
    The function constructs a unique IRI for an instance of a class in a namespace.

    Args:
        namespace_iri (str): The namespace IRI, e.g. "https://www.theworldavatar.com/kg/ontolab"
        class_name (str): The class name, e.g. "LabEquipment"

    Returns:
        str: The unique IRI for the instance, e.g. "https://www.theworldavatar.com/kg/ontolab/LabEquipment_12345678"
    """
    return f'{construct_rdf_type(namespace_iri, class_name)}_{str(uuid4())}'

trim_iri

trim_iri(iri: Union[str, List[str]]) -> Union[str, List[str]]

This function trims the "<" and ">" characters from the left and right side of the given IRI (or lists of IRIs).

Parameters:

Name Type Description Default
iri str or list

The IRI(s) to be trimmed

required

Returns:

Name Type Description
str Union[str, List[str]]

The trimmed IRI

Source code in JPS_BASE_LIB/python_wrapper/twa/data_model/utils.py
def trim_iri(iri: Union[str, List[str]]) -> Union[str, List[str]]:
    """
    This function trims the "<" and ">" characters from the left and right side of the given IRI (or lists of IRIs).

    Args:
        iri (str or list): The IRI(s) to be trimmed

    Returns:
        str: The trimmed IRI
    """
    if isinstance(iri, list):
        for i in range(len(iri)):
            iri[i] = trim_iri(iri[i])
    else:
        iri = iri.strip().lstrip("<").rstrip(">")
    return iri