Skip to content

string_utils

camel_to_snake(text)

Converts a camel case string to snake case.

Parameters:

Name Type Description Default
text str

The camel case string to be converted.

required

Returns:

Name Type Description
str str

The snake case version of the input string.

Source code in exe_kg_lib/utils/string_utils.py
17
18
19
20
21
22
23
24
25
26
27
28
def camel_to_snake(text: str) -> str:
    """
    Converts a camel case string to snake case.

    Args:
        text (str): The camel case string to be converted.

    Returns:
        str: The snake case version of the input string.
    """
    text = re.sub("(.)([A-Z][a-z]+)", r"\1_\2", text)
    return re.sub("([a-z0-9])([A-Z])", r"\1_\2", text).lower()

class_name_to_method_name(class_name)

Converts a class name to a method name by removing the word "Method" from the end of the class name.

Parameters:

Name Type Description Default
class_name str

The class name to convert.

required

Returns:

Name Type Description
str str

The converted method name.

Source code in exe_kg_lib/utils/string_utils.py
61
62
63
64
65
66
67
68
69
70
71
72
def class_name_to_method_name(class_name: str) -> str:
    """
    Converts a class name to a method name by removing the word "Method" from the end of the class name.

    Args:
        class_name (str): The class name to convert.

    Returns:
        str: The converted method name.
    """
    name = re.sub("Method$", "", class_name)
    return name

class_name_to_module_name(class_name)

Converts a class name to a module name by removing the "Module" suffix and converting it to snake case.

Parameters:

Name Type Description Default
class_name str

The class name to convert.

required

Returns:

Name Type Description
str str

The converted module name.

Source code in exe_kg_lib/utils/string_utils.py
47
48
49
50
51
52
53
54
55
56
57
58
def class_name_to_module_name(class_name: str) -> str:
    """
    Converts a class name to a module name by removing the "Module" suffix and converting it to snake case.

    Args:
        class_name (str): The class name to convert.

    Returns:
        str: The converted module name.
    """
    name = re.sub("Module$", "", class_name)
    return camel_to_snake(name)

concat_paths(*paths)

Concatenates multiple paths into a single path.

Parameters:

Name Type Description Default
*paths Union[str, Path]

Variable number of paths to be concatenated.

()

Returns:

Name Type Description
str str

The concatenated path.

Example

concat_paths('path1', 'path2', 'path3') 'path1/path2/path3'

Source code in exe_kg_lib/utils/string_utils.py
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
def concat_paths(*paths: Union[str, Path]) -> str:
    """
    Concatenates multiple paths into a single path.

    Args:
        *paths: Variable number of paths to be concatenated.

    Returns:
        str: The concatenated path.

    Example:
        >>> concat_paths('path1', 'path2', 'path3')
        'path1/path2/path3'
    """
    output_path = ""
    for path in paths:
        if not output_path:
            output_path = path
        else:
            output_path = (
                output_path / path
                if isinstance(output_path, Path) or isinstance(path, Path)
                else f"{output_path}/{path}"
            )

    return str(output_path)

get_instance_name(instance_type, instance_number, pipeline_name)

Generates a unique instance name based on the instance type and number.

Parameters:

Name Type Description Default
instance_type str

The type of the instance.

required
instance_number int

The number of the instance for this type.

required
pipeline_name str

The name of the pipeline.

required

Returns:

Name Type Description
str str

The generated instance name.

Source code in exe_kg_lib/utils/string_utils.py
103
104
105
106
107
108
109
110
111
112
113
114
115
def get_instance_name(instance_type: str, instance_number: int, pipeline_name: str) -> str:
    """
    Generates a unique instance name based on the instance type and number.

    Args:
        instance_type (str): The type of the instance.
        instance_number (int): The number of the instance for this type.
        pipeline_name (str): The name of the pipeline.

    Returns:
        str: The generated instance name.
    """
    return f"{instance_type}{str(instance_number)}_{pipeline_name}"

get_task_output_name(output_type, task_instance_name, method_type)

Generates the name for a task's output based on the output type, task instance name, and method type.

Parameters:

Name Type Description Default
output_type str

The type of the output.

required
task_instance_name str

The name of the task instance.

required
method_type str

The type of the method.

required

Returns:

Name Type Description
str str

The generated task output name that follows the format "DataOutNameEx_TaskTypeEx1_PipelineName_MethodEx" (see TASK_OUTPUT_NAME_REGEX above for details).

Source code in exe_kg_lib/utils/string_utils.py
118
119
120
121
122
123
124
125
126
127
128
129
130
def get_task_output_name(output_type: str, task_instance_name: str, method_type: str) -> str:
    """
    Generates the name for a task's output based on the output type, task instance name, and method type.

    Args:
        output_type (str): The type of the output.
        task_instance_name (str): The name of the task instance.
        method_type (str): The type of the method.

    Returns:
        str: The generated task output name that follows the format "DataOutNameEx_TaskTypeEx1_PipelineName_MethodEx" (see TASK_OUTPUT_NAME_REGEX above for details).
    """
    return f"{output_type}_{task_instance_name}_{method_type}"

prettify_data_entity_name(data_entity_name)

Prettifies the given data entity name by removing unnecessary prefixes and components.

Parameters:

Name Type Description Default
data_entity_name str

The name of the data entity.

required

Returns:

Name Type Description
str str

The prettified data entity name.

Source code in exe_kg_lib/utils/string_utils.py
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
def prettify_data_entity_name(data_entity_name: str) -> str:
    """
    Prettifies the given data entity name by removing unnecessary prefixes and components.

    Args:
        data_entity_name (str): The name of the data entity.

    Returns:
        str: The prettified data entity name.
    """
    match = re.match(TASK_OUTPUT_NAME_REGEX, data_entity_name)
    if match:
        # data_entity_name refers to a data entity that is an output of a previous task
        task_output_name = match.group(1)
        task_output_name = re.sub(r"^DataOut", "", task_output_name)
        task_name = match.group(2)
        task_instance_number = match.group(3)
        method_type = match.group(5)

        return f"{task_name}{task_instance_number} {method_type} {task_output_name}"

    return data_entity_name

property_iri_to_field_name(property_iri)

Converts a property IRI to a Python field name.

Parameters:

Name Type Description Default
property_iri str

The property IRI.

required

Returns:

Name Type Description
str str

The converted field name.

Source code in exe_kg_lib/utils/string_utils.py
31
32
33
34
35
36
37
38
39
40
41
42
43
44
def property_iri_to_field_name(property_iri: str) -> str:
    """
    Converts a property IRI to a Python field name.

    Args:
        property_iri (str): The property IRI.

    Returns:
        str: The converted field name.
    """
    snake_case = camel_to_snake(property_iri.split("#")[1])
    snake_case = re.sub("^has_", "", snake_case)
    snake_case = re.sub("^param_", "", snake_case)
    return snake_case