Skip to content

robot.utils.misc

printable_name

printable_name(string, code_style=False)

Generates and returns printable name from the given string.

Examples: 'simple' -> 'Simple' 'name with spaces' -> 'Name With Spaces' 'more spaces' -> 'More Spaces' 'Cases AND spaces' -> 'Cases AND Spaces' '' -> ''

If 'code_style' is True:

'mixedCAPSCamel' -> 'Mixed CAPS Camel' 'camelCaseName' -> 'Camel Case Name' 'under_score_name' -> 'Under Score Name' 'under_and space' -> 'Under And Space' 'miXed_CAPS_nAMe' -> 'MiXed CAPS NAMe' '' -> ''

Source code in src/robot/utils/misc.py
def printable_name(string, code_style=False):
    """Generates and returns printable name from the given string.

    Examples:
    'simple'           -> 'Simple'
    'name with spaces' -> 'Name With Spaces'
    'more   spaces'    -> 'More Spaces'
    'Cases AND spaces' -> 'Cases AND Spaces'
    ''                 -> ''

    If 'code_style' is True:

    'mixedCAPSCamel'   -> 'Mixed CAPS Camel'
    'camelCaseName'    -> 'Camel Case Name'
    'under_score_name' -> 'Under Score Name'
    'under_and space'  -> 'Under And Space'
    'miXed_CAPS_nAMe'  -> 'MiXed CAPS NAMe'
    ''                 -> ''
    """
    if code_style and '_' in string:
        string = string.replace('_', ' ')
    parts = string.split()
    if code_style and len(parts) == 1 \
            and not (string.isalpha() and string.islower()):
        parts = _split_camel_case(parts[0])
    return ' '.join(part[0].upper() + part[1:] for part in parts)

seq2str

seq2str(sequence, quote="'", sep=', ', lastsep=' and ')

Returns sequence in format 'item 1', 'item 2' and 'item 3'.

Source code in src/robot/utils/misc.py
def seq2str(sequence, quote="'", sep=', ', lastsep=' and '):
    """Returns sequence in format `'item 1', 'item 2' and 'item 3'`."""
    sequence = [f'{quote}{safe_str(item)}{quote}' for item in sequence]
    if not sequence:
        return ''
    if len(sequence) == 1:
        return sequence[0]
    last_two = lastsep.join(sequence[-2:])
    return sep.join(sequence[:-2] + [last_two])

seq2str2

seq2str2(sequence)

Returns sequence in format [ item 1 | item 2 | ... ].

Source code in src/robot/utils/misc.py
def seq2str2(sequence):
    """Returns sequence in format `[ item 1 | item 2 | ... ]`."""
    if not sequence:
        return '[ ]'
    return '[ %s ]' % ' | '.join(safe_str(item) for item in sequence)

test_or_task

test_or_task(text: str, rpa: bool)

Replace 'test' with 'task' in the given text depending on rpa.

If given text is test, test or task is returned directly. Otherwise, pattern {test} is searched from the text and occurrences replaced with test or task.

In both cases matching the word test is case-insensitive and the returned test or task has exactly same case as the original.

Source code in src/robot/utils/misc.py
def test_or_task(text: str, rpa: bool):
    """Replace 'test' with 'task' in the given `text` depending on `rpa`.

     If given text is `test`, `test` or `task` is returned directly. Otherwise,
     pattern `{test}` is searched from the text and occurrences replaced with
     `test` or `task`.

     In both cases matching the word `test` is case-insensitive and the returned
     `test` or `task` has exactly same case as the original.
     """
    def replace(test):
        if not rpa:
            return test
        upper = [c.isupper() for c in test]
        return ''.join(c.upper() if up else c for c, up in zip('task', upper))
    if text.upper() == 'TEST':
        return replace(text)
    return re.sub('{(test)}', lambda m: replace(m.group(1)), text, flags=re.IGNORECASE)

classproperty

classproperty(fget, fset=None, fdel=None, doc=None)

Bases: property

Property that works with classes in addition to instances.

Only supports getters. Setters and deleters cannot work with classes due to how the descriptor protocol works, and they are thus explicitly disabled. Metaclasses must be used if they are needed.

Source code in src/robot/utils/misc.py
def __init__(self, fget, fset=None, fdel=None, doc=None):
    if fset:
        self.setter(fset)
    if fdel:
        self.deleter(fset)
    super().__init__(fget)
    if doc:
        self.__doc__ = doc