Sanitize context label values

Python code example of how to ensure invalid characters are not part of a context label value being sent to the context labels API.

1

Check that replacement character is valid

If a replacement other than the default _ is specified, ensure that is valid itself

def sanitize_label(self, label, replacement_char="_", max_label_len=80):
        if not label:
            raise ValueError("Empty label string cannot be sanitized")

        if replacement_char != "_":
            valid_char_pattern = r"^[a-zA-Z0-9 ._/\\\-#~:()]$"
            if not re.match(valid_char_pattern, replacement_char):
                raise ValueError("Invalid replacement character")
2

Strip leading/trailing spaces

Labels can not start or end with blank space

        label = label.strip()
3

Replace invalid characters

If any invalid characters are found, replace with the replacement character specified

        # Include the backslash as an invalid character in the pattern
        invalid_label_chars = re.compile(r"[^a-zA-Z0-9 ._/\\\-#~:()]|\\")
        label = invalid_label_chars.sub(replacement_char, label)
4

Truncate a label at maximum length

If the label is longer than the limit, truncate it

        if len(label) > max_label_len:
            label = label[:max_label_len]

        return label

Last updated