Utils

typus.utils.re_compile(pattern, flags=58)

A shortcut to compile regex with predefined flags: re.I, re.U, re.M, re.S.

Parameters:
  • pattern (str) – A string to compile pattern from.
  • flags (int) – Python re module flags.
>>> foo = re_compile('[a-z]')  # matches with 'test' and 'TEST'
>>> bool(foo.match('TEST'))
True
>>> bar = re_compile('[a-z]', flags=0)  # doesn't match with 'TEST'
>>> bool(bar.match('TEST'))
False
class typus.utils.idict(obj=None, **kwargs)

Case-insensitive dictionary.

Parameters:
  • obj (mapping/iterable) – An object to initialize new dictionary from
  • **kwargskey=value pairs to put in the new dictionary
>>> foo = idict({'A': 0, 'b': 1}, bar=2)
>>> foo['a'], foo['B'], foo['bAr']
(0, 1, 2)

Caution

idict is not a full-featured case-insensitive dictionary. As it’s made for map_choices() and has limited functionality.

typus.utils.map_choices(data, group=u'({0})', dict_class=<class 'typus.utils.idict'>)

typus.processors.Expressions helper. Builds regex pattern from the dictionary keys and maps them to values via replace function.

Parameters:
  • data (mapping/iterable) – A pairs of (find, replace with) strings
  • group (str) – A string to format in choices.
  • dict_class (class) – A dictionary class to convert source data. By default idict is used which is case-insensitive. In instance, to map (c) and (C) to different values pass regular python dict. Or if the order matters use collections.OrderedDict
Returns:

A regex non-compiled pattern and replace function

Return type:

tuple

>>> import re
>>> pattern, replace = map_choices({'a': 0, 'b': 1})
>>> re.sub(pattern, replace, 'abc')
'01c'
typus.utils.splinter(delimiter)

typus.processors.EscapePhrases helper. Almost like str.split() but handles delimiter escaping and strips spaces.

Parameters:delimiter (str) – String delimiter
Raises:ValueError – If delimiter is a slash or an empty space
Returns:A list of stripped phrases splitted by the delimiter
Return type:list
>>> split = splinter(',  ')  # strips this spaces
>>> split('a, b,c ,  d\,e')  # and this ones too
['a', 'b', 'c', 'd,e']