Skip to content

Value Converters

convert_to_bool_string

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
str

'true' or 'false' if it is an accepted value,

str

else it returns the original value as a string.

Source code in dsp/dsp-tools/src/dsp_tools/xmllib/value_converters.py
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
def convert_to_bool_string(value: Any) -> str:
    """
    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,
        else it returns the original value as a string.
    """
    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"
    return str(value)

replace_newlines_with_tags

Converts the newlines in a string to XML tags. The type of tags is specified through the converter_option enum.

Parameters:

Name Type Description Default
text str

string to convert

required
converter_option NewlineReplacement

tag options

required

Returns:

Type Description
str

String with replaced values

Source code in dsp/dsp-tools/src/dsp_tools/xmllib/value_converters.py
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
def replace_newlines_with_tags(text: str, converter_option: NewlineReplacement) -> str:
    """
    Converts the newlines in a string to XML tags.
    The type of tags is specified through the converter_option enum.

    Args:
        text: string to convert
        converter_option: tag options

    Returns:
        String with replaced values

    Raises:
        InputError: If an invalid conversion option is given
    """
    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>

Multiple consecutive newlines will be treated as one newline: Start\nMiddle\n\nEnd becomes <p>Start</p><p>Middle</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

Source code in dsp/dsp-tools/src/dsp_tools/xmllib/value_converters.py
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
def replace_newlines_with_paragraph_tags(text: str) -> str:
    """
    Replace `Start\\nEnd` with `<p>Start</p><p>End</p>`

    Multiple consecutive newlines will be treated as one newline:
    `Start\\nMiddle\\n\\nEnd` becomes `<p>Start</p><p>Middle</p><p>End</p>`

    Args:
        text: string to be formatted

    Returns:
        Formatted string with paragraph tags
    """
    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 Start\nEnd with Start<br/>End

Multiple consecutive newlines will be converted into multiple break-lines: Start\n\nEnd with Start<br/><br/>End

Parameters:

Name Type Description Default
text str

string to be formatted

required

Returns:

Type Description
str

Formatted string with break-line tags

Source code in dsp/dsp-tools/src/dsp_tools/xmllib/value_converters.py
72
73
74
75
76
77
78
79
80
81
82
83
84
85
def replace_newlines_with_br_tags(text: str) -> str:
    """
    Replaces `Start\\nEnd` with `Start<br/>End`

    Multiple consecutive newlines will be converted into multiple break-lines:
    `Start\\n\\nEnd` with `Start<br/><br/>End`

    Args:
        text: string to be formatted

    Returns:
        Formatted string with break-line tags
    """
    return text.replace("\n", "<br/>")