Skip to content

text

xsdata.utils.text

CharType

Character types.

Source code in xsdata/utils/text.py
168
169
170
171
172
173
174
class CharType:
    """Character types."""

    UPPER = 1
    LOWER = 2
    NUMERIC = 3
    OTHER = 4

prefix(value, sep=':')

Return the first part of the string before the separator.

Source code in xsdata/utils/text.py
69
70
71
def prefix(value: str, sep: str = ":") -> str:
    """Return the first part of the string before the separator."""
    return split(value, sep)[0]

suffix(value, sep=':')

Return the last part of the string after the separator.

Source code in xsdata/utils/text.py
74
75
76
def suffix(value: str, sep: str = ":") -> str:
    """Return the last part of the string after the separator."""
    return split(value, sep)[1]

split(value, sep=':')

Split the given value with the given separator once.

Source code in xsdata/utils/text.py
79
80
81
82
def split(value: str, sep: str = ":") -> Tuple:
    """Split the given value with the given separator once."""
    left, _, right = value.partition(sep)
    return (left, right) if right else (None, left)

capitalize(value, **kwargs)

Capitalize the given string.

Source code in xsdata/utils/text.py
85
86
87
def capitalize(value: str, **kwargs: Any) -> str:
    """Capitalize the given string."""
    return value[0].upper() + value[1:]

original_case(value, **kwargs)

Return the input string but ensure it's a valid Python variable.

Source code in xsdata/utils/text.py
90
91
92
93
94
95
def original_case(value: str, **kwargs: Any) -> str:
    """Return the input string but ensure it's a valid Python variable."""
    # Strip out all characters that are not alphanumeric or underscores
    value = re.sub(r"\W", "", value)
    # Then strip out leading digit and underscore characters
    return re.sub(r"^[^a-zA-Z_]+", "", value)

pascal_case(value, **kwargs)

Convert the given string to pascal case.

Source code in xsdata/utils/text.py
 98
 99
100
def pascal_case(value: str, **kwargs: Any) -> str:
    """Convert the given string to pascal case."""
    return "".join(map(str.title, split_words(value)))

camel_case(value, **kwargs)

Convert the given string to camel case.

Source code in xsdata/utils/text.py
103
104
105
106
def camel_case(value: str, **kwargs: Any) -> str:
    """Convert the given string to camel case."""
    result = "".join(map(str.title, split_words(value)))
    return result[0].lower() + result[1:]

mixed_case(value, **kwargs)

Convert the given string to mixed case.

Source code in xsdata/utils/text.py
109
110
111
def mixed_case(value: str, **kwargs: Any) -> str:
    """Convert the given string to mixed case."""
    return "".join(split_words(value))

mixed_pascal_case(value, **kwargs)

Convert the given string to mixed pascal case.

Source code in xsdata/utils/text.py
114
115
116
def mixed_pascal_case(value: str, **kwargs: Any) -> str:
    """Convert the given string to mixed pascal case."""
    return capitalize(mixed_case(value))

mixed_snake_case(value, **kwargs)

Convert the given string to mixed snake case.

Source code in xsdata/utils/text.py
119
120
121
def mixed_snake_case(value: str, **kwargs: Any) -> str:
    """Convert the given string to mixed snake case."""
    return "_".join(split_words(value))

snake_case(value, **kwargs)

Convert the given string to snake case.

Source code in xsdata/utils/text.py
124
125
126
def snake_case(value: str, **kwargs: Any) -> str:
    """Convert the given string to snake case."""
    return "_".join(map(str.lower, split_words(value)))

screaming_snake_case(value, **kwargs)

Convert the given string to screaming snake case.

Source code in xsdata/utils/text.py
129
130
131
def screaming_snake_case(value: str, **kwargs: Any) -> str:
    """Convert the given string to screaming snake case."""
    return snake_case(value, **kwargs).upper()

kebab_case(value, **kwargs)

Convert the given string to kebab case.

Source code in xsdata/utils/text.py
134
135
136
def kebab_case(value: str, **kwargs: Any) -> str:
    """Convert the given string to kebab case."""
    return "-".join(split_words(value))

split_words(value)

Split a string on capital letters and not alphanumeric characters.

Source code in xsdata/utils/text.py
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
def split_words(value: str) -> List[str]:
    """Split a string on capital letters and not alphanumeric characters."""
    words: List[str] = []
    buffer: List[str] = []
    previous = None

    def flush():
        if buffer:
            words.append("".join(buffer))
            buffer.clear()

    for char in value:
        tp = classify(char)
        if tp == CharType.OTHER:
            flush()
        elif not previous or tp == previous:
            buffer.append(char)
        elif tp == CharType.UPPER and previous != CharType.UPPER:
            flush()
            buffer.append(char)
        else:
            buffer.append(char)

        previous = tp

    flush()
    return words

classify(character)

String classifier.

Source code in xsdata/utils/text.py
177
178
179
180
181
182
183
184
185
186
187
188
189
def classify(character: str) -> int:
    """String classifier."""
    code_point = ord(character)
    if 64 < code_point < 91:
        return CharType.UPPER

    if 96 < code_point < 123:
        return CharType.LOWER

    if 47 < code_point < 58:
        return CharType.NUMERIC

    return CharType.OTHER

escape_string(value)

Escape a string for code generation.

Source code in xsdata/utils/text.py
206
207
208
209
210
211
212
def escape_string(value: str) -> str:
    """Escape a string for code generation."""

    def replace(match: Match) -> str:
        return ESCAPE_DCT[match.group(0)]

    return ESCAPE.sub(replace, value)

alnum(value)

Return the ascii alphanumerical characters in lower case.

Source code in xsdata/utils/text.py
218
219
220
def alnum(value: str) -> str:
    """Return the ascii alphanumerical characters in lower case."""
    return "".join(filter(__alnum_ascii__.__contains__, value)).lower()