Skip to content

Value Converters

convert_to_bool

Turns a value into a bool string, suitable for an XML. It is case-insensitive, meaning that the words can also be capitalised.

Accepted values
  • false, 0, 0.0, no, non, nein -> False
  • true, 1, 1.0, yes, oui, ja -> True

Parameters:

Name Type Description Default
value Any

value to transform

required

Returns:

Type Description
bool

True or False if it is an accepted value.

Examples:

result = xmllib.convert_to_bool_string(1)
# result == True
result = xmllib.convert_to_bool_string("nein")
# result == False
result = xmllib.convert_to_bool_string(None)
# raises InputError
Source code in dsp/dsp-tools/src/dsp_tools/xmllib/value_converters.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
def convert_to_bool(value: Any) -> bool:
    """
    Turns a value into a bool string, suitable for an XML.
    It is case-insensitive, meaning that the words can also be capitalised.

    Accepted values:
         - `false`, `0`, `0.0`, `no`, `non`, `nein` -> `False`
         - `true`, `1`, `1.0`, `yes`, `oui`, `ja` -> `True`

    Args:
        value: value to transform

    Returns:
        `True` or `False` if it is an accepted value.

    Raises:
        InputError: If the value is not convertable to a boolean

    Examples:
        ```python
        result = xmllib.convert_to_bool_string(1)
        # result == True
        ```

        ```python
        result = xmllib.convert_to_bool_string("nein")
        # result == False
        ```

        ```python
        result = xmllib.convert_to_bool_string(None)
        # raises InputError
        ```
    """
    str_val = str(value).lower().strip()
    if str_val in ("false", "0", "0.0", "no", "non", "nein"):
        return False
    elif str_val in ("true", "1", "1.0", "yes", "oui", "ja"):
        return True
    raise InputError(f"The entered value '{value}' cannot be converted to a bool.")

replace_newlines_with_tags

Converts the newlines in a string to XML tags.

Parameters:

Name Type Description Default
text str

string to convert

required
converter_option NewlineReplacement

specifies what tag to use instead of the newline

required

Returns:

Type Description
str

String with replaced values

Examples:

result = xmllib.replace_newlines_with_tags(
    "Start\nEnd", xmllib.NewlineReplacement.NONE
)
# result == "Start\nEnd"
result = xmllib.replace_newlines_with_tags(
    "Start\nEnd", xmllib.NewlineReplacement.LINEBREAK
)
# result == "Start<br/>End"
result = xmllib.replace_newlines_with_tags(
    "Start\n\nEnd", xmllib.NewlineReplacement.PARAGRAPH
)
# result == "<p>Start</p><p>End</p>"
Source code in dsp/dsp-tools/src/dsp_tools/xmllib/value_converters.py
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
def replace_newlines_with_tags(text: str, converter_option: NewlineReplacement) -> str:
    """
    Converts the newlines in a string to XML tags.

    Args:
        text: string to convert
        converter_option: specifies what tag to use instead of the newline

    Returns:
        String with replaced values

    Raises:
        InputError: If an invalid conversion option is given

    Examples:
        ```python
        result = xmllib.replace_newlines_with_tags(
            "Start\\nEnd", xmllib.NewlineReplacement.NONE
        )
        # result == "Start\\nEnd"
        ```

        ```python
        result = xmllib.replace_newlines_with_tags(
            "Start\\nEnd", xmllib.NewlineReplacement.LINEBREAK
        )
        # result == "Start<br/>End"
        ```

        ```python
        result = xmllib.replace_newlines_with_tags(
            "Start\\n\\nEnd", xmllib.NewlineReplacement.PARAGRAPH
        )
        # result == "<p>Start</p><p>End</p>"
        ```
    """
    match converter_option:
        case NewlineReplacement.NONE:
            return text
        case NewlineReplacement.LINEBREAK:
            return replace_newlines_with_br_tags(text)
        case NewlineReplacement.PARAGRAPH:
            return replace_newlines_with_paragraph_tags(text)

replace_newlines_with_paragraph_tags

Replace Start\nEnd with <p>Start</p><p>End</p>

Parameters:

Name Type Description Default
text str

string to be formatted

required

Returns:

Type Description
str

Formatted string with paragraph tags

Examples:

result = xmllib.replace_newlines_with_paragraph_tags("Start\nEnd")
# result == "<p>Start</p><p>End</p>"
# multiple consecutive newlines will be treated as one newline

result = xmllib.replace_newlines_with_paragraph_tags("Start\n\nEnd")
# result == "<p>Start</p><p>End</p>"
Source code in dsp/dsp-tools/src/dsp_tools/xmllib/value_converters.py
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
def replace_newlines_with_paragraph_tags(text: str) -> str:
    """
    Replace `Start\\nEnd` with `<p>Start</p><p>End</p>`

    Args:
        text: string to be formatted

    Returns:
        Formatted string with paragraph tags

    Examples:
        ```python
        result = xmllib.replace_newlines_with_paragraph_tags("Start\\nEnd")
        # result == "<p>Start</p><p>End</p>"
        ```

        ```python
        # multiple consecutive newlines will be treated as one newline

        result = xmllib.replace_newlines_with_paragraph_tags("Start\\n\\nEnd")
        # result == "<p>Start</p><p>End</p>"
        ```
    """
    splt = [x for x in text.split("\n") if x != ""]
    formatted = [f"<p>{x}</p>" for x in splt]
    return "".join(formatted)

replace_newlines_with_br_tags

Replaces \n with <br/>

Parameters:

Name Type Description Default
text str

string to be formatted

required

Returns:

Type Description
str

Formatted string with break-line tags

Examples:

result = xmllib.replace_newlines_with_br_tags("Start\nEnd")
# result == "Start<br/>End"
# multiple consecutive newlines will be converted into multiple break-lines

result = xmllib.replace_newlines_with_br_tags("Start\n\nEnd")
# result == "Start<br/><br/>End"
Source code in dsp/dsp-tools/src/dsp_tools/xmllib/value_converters.py
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
def replace_newlines_with_br_tags(text: str) -> str:
    """
    Replaces `\\n` with `<br/>`

    Args:
        text: string to be formatted

    Returns:
        Formatted string with break-line tags

    Examples:
        ```python
        result = xmllib.replace_newlines_with_br_tags("Start\\nEnd")
        # result == "Start<br/>End"
        ```

        ```python
        # multiple consecutive newlines will be converted into multiple break-lines

        result = xmllib.replace_newlines_with_br_tags("Start\\n\\nEnd")
        # result == "Start<br/><br/>End"
        ```
    """
    return text.replace("\n", "<br/>")