Skip to content

robot.utils.normalizing

normalize

1
2
3
4
5
6
normalize(
    string: str,
    ignore: Sequence[str] = (),
    caseless: bool = True,
    spaceless: bool = True,
) -> str

Normalize the string according to the given spec.

By default, string is turned to lower case (actually case-folded) and all whitespace is removed. Additional characters can be removed by giving them in ignore list.

Source code in src/robot/utils/normalizing.py
def normalize(string: str, ignore: 'Sequence[str]' = (), caseless: bool = True,
              spaceless: bool = True) -> str:
    """Normalize the ``string`` according to the given spec.

    By default, string is turned to lower case (actually case-folded) and all
    whitespace is removed. Additional characters can be removed by giving them
    in ``ignore`` list.
    """
    if spaceless:
        string = ''.join(string.split())
    if caseless:
        string = string.casefold()
        ignore = [i.casefold() for i in ignore]
    # both if statements below enhance performance a little
    if ignore:
        for ign in ignore:
            if ign in string:
                string = string.replace(ign, '')
    return string

NormalizedDict

1
2
3
4
5
6
7
8
NormalizedDict(
    initial: (
        Mapping[str, V] | Iterable[tuple[str, V]] | None
    ) = None,
    ignore: Sequence[str] = (),
    caseless: bool = True,
    spaceless: bool = True,
)

Bases: MutableMapping[str, V]

Custom dictionary implementation automatically normalizing keys.

Initialized with possible initial value and normalizing spec.

Initial values can be either a dictionary or an iterable of name/value pairs.

Normalizing spec has exact same semantics as with the :func:normalize function.

Source code in src/robot/utils/normalizing.py
def __init__(self, initial: 'Mapping[str, V]|Iterable[tuple[str, V]]|None' = None,
             ignore: 'Sequence[str]' = (), caseless: bool = True,
             spaceless: bool = True):
    """Initialized with possible initial value and normalizing spec.

    Initial values can be either a dictionary or an iterable of name/value
    pairs.

    Normalizing spec has exact same semantics as with the :func:`normalize`
    function.
    """
    self._data: 'dict[str, V]' = {}
    self._keys: 'dict[str, str]' = {}
    self._normalize = lambda s: normalize(s, ignore, caseless, spaceless)
    if initial:
        self.update(initial)