tocolib

build status Documentation Status

A multipurpose utility library for Python 2 and 3.

The library namespace is tocoli. For information on available packages, modules and functionality see Structure.

Highlights

Sorting dictionaries

Sort a ‘list’ of ‘dict’ by simply defining the keys you like to sort by in order from last to first.

Example:

>>> dicts = [{'firstname': 'Bob',   'lastname': 'Abel'},
             {'firstname': 'Alice', 'lastname': 'Bond'},
             {'firstname': 'Carol', 'lastname': 'Bond'},
             {'firstname': 'Bob',   'lastname': 'Bond'},
             {'firstname': 'Carol', 'lastname': 'Abel'},
             {'firstname': 'Alice', 'lastname': 'Abel'}]
>>> from tocoli.sort import sort_dicts_by_value
>>> sort_dicts_by_value(dicts, ['lastname', 'firstname'])
[{'firstname': 'Alice', 'lastname': 'Abel'},
 {'firstname': 'Bob',   'lastname': 'Abel'},
 {'firstname': 'Carol', 'lastname': 'Abel'},
 {'firstname': 'Alice', 'lastname': 'Bond'},
 {'firstname': 'Bob',   'lastname': 'Bond'},
 {'firstname': 'Carol', 'lastname': 'Bond'}]

A Domain Specific Language for intuitive function calls

The dsl package provides a coherent style to access the tocolib modules and functions as module or static class functions.

Example:

>>> from tocoli.dsl import sort
>>> sort.dicts.by.value(dicts, ['lastname', 'firstname'])
[{'firstname': 'Alice', 'lastname': 'Abel'},
 {'firstname': 'Bob',   'lastname': 'Abel'},
 {'firstname': 'Carol', 'lastname': 'Abel'},
 {'firstname': 'Alice', 'lastname': 'Bond'},
 {'firstname': 'Bob',   'lastname': 'Bond'},
 {'firstname': 'Carol', 'lastname': 'Bond'}]
>>> sort.dicts.by.similarity(dicts, 'Karol', ['firstname'])
[{'firstname': 'Carol', 'lastname': 'Bond'},
 {'firstname': 'Carol', 'lastname': 'Abel'},
 {'firstname': 'Alice', 'lastname': 'Bond'},
 {'firstname': 'Alice', 'lastname': 'Abel'},
 {'firstname': 'Bob',   'lastname': 'Abel'},
 {'firstname': 'Bob',   'lastname': 'Bond'}]

Powerful mapping

Use recursive mapping to apply functions to nested data structures.

Example:

>>> from tocoli.dsl import map
>>> def upper(item, parent):
        return item.upper()
>>> map.recursive(dicts, upper)
[{'firstname': 'BOB', 'lastname': 'ABEL'},
 {'firstname': 'ALICE', 'lastname': 'BOND'},
 {'firstname': 'CAROL', 'lastname': 'BOND'},
 {'firstname': 'BOB', 'lastname': 'BOND'},
 {'firstname': 'CAROL', 'lastname': 'ABEL'},
 {'firstname': 'ALICE', 'lastname': 'ABEL'}]
>>> map_keys = (map.DEFAULT | map.DICT_KEY) ^ map.DICT_VALUE
>>> map.recursive(dicts, upper, map_keys)
[{'FIRSTNAME': 'Bob', 'LASTNAME': 'Abel'},
 {'FIRSTNAME': 'Alice', 'LASTNAME': 'Bond'},
 {'FIRSTNAME': 'Carol', 'LASTNAME': 'Bond'},
 {'FIRSTNAME': 'Bob', 'LASTNAME': 'Bond'},
 {'FIRSTNAME': 'Carol', 'LASTNAME': 'Abel'},
 {'FIRSTNAME': 'Alice', 'LASTNAME': 'Abel'}]

What’s New

  • Changed minimum requirement: passlib>=1.7.0
  • The former dsl module is now an own subpackage.
  • The keys parameter notation for sorting functions changed.
  • There are new flags paramter options for mapping functions.

For more detailed information on current changes check the change log.

Structure

Namespace

tocoli - root
The tocolib wraps the six library (Python 2 and 3 compatibility utilities) at the root. Thus all six packages and modules are also available under the root namespace.

Subpackages

dsl - a domain specific language for tocolib
Python, like it should be. The module contains a domain specific language for common functions like filtering, sorting, mapping and more. All functions have a consistent API and results.

Modules

auth - common authetication helpers
Its dangerous out there. This module is all about passwords, hashes, salts, tokens and api keys.
cmp - compare utilities
For those who like to compare apples with pears. Make different data types comparable.
enc - encoding functions
Encoding without pain. Provides universal encoding functions.
filter - filter functions
The good ones go into the pot, the bad ones go into your crop. Advanced functions to filter dictionaries or lists of strings.
fn - common lambda functions
To Be or not to Be: That is the question! Short value extractor functions and more.
join - join/reduce/folding functions
Bring together what belongs together.
map - mapping functions
It’s still magic even if you know how it’s done. Map data by applying any higher-order function to it.
ratio - ratio functions
Comparisons make unhappy, but can be quite useful. Provides ratio functions for varios purposes.
regex - regular expression utilities
Find what you are searching for. Generate common regular expressions.
sort - sort functions
Chuck Norris is able to sort black pens by color. Sort data by value or keys.
spell - spelling utilities
Life doesn’t come with spell-check, but tocolib does.
test - testing and benchmarking
Tests cant prove the absence of bugs. Thus test as good as you can.
type - type conversion utilities
What doesn’t fit is made to fit. Universal type transformations.

Contents

tocoli package

Subpackages
tocoli.dsl package
Submodules
tocoli.dsl.filter module
class tocoli.dsl.filter.dict[source]
class by[source]
static key(d, keys, all_keys=False)

Returns this ‘dict’ when it contains at least one of the specified keys else None.

static value(d, keywords, keys=None, all_keys=False)

Returns this ‘dict’ when it contains at least one of the keywords as value in the specified keys else None.

class tocoli.dsl.filter.dicts[source]
class by[source]
static key(iterable, keys, all_keys=False)

Returns a ‘list’ of ‘dict’ whose dictionaries contain at least one of the specified keys.

static value(iterable, keywords, keys=None, all_keys=False)

Returns a ‘list’ of ‘dict’ whose dictionaries contain at least one of the keywords as value in the specified keys.

class tocoli.dsl.filter.strings[source]
class by[source]
static keywords(iterable, keywords, case_sensitive=False)

Filter strings by exact keywords. Returns a new list.

static similarity(iterable, keywords, ratio=0.8, weights=(1, 4), case_sensitive=False)

Filter strings by similar keywords. Returns a new list of strings.

tocoli.dsl.join module
class tocoli.dsl.join.strings[source]
class by[source]
static keywords(list, keywords, join=' ')

Join strings by keywords. Returns a new list with joined strings.

tocoli.dsl.map module
class tocoli.dsl.map.string[source]
static non_accented(str)

Returns a non accented string.

tocoli.dsl.sort module
class tocoli.dsl.sort.dict[source]
class by[source]
static key(d, reverse=False)

Returns a ‘list’ of tuple(key, value).

static value(d, reverse=False)

Returns a ‘list’ of tuple(key, value).

class tocoli.dsl.sort.dicts[source]
class by[source]
static similarity(iterable, keyword, keys, values=<function first_kwarg>, default=None, sequentially=True, reverse=False, weights=(1, 1), case_sensitive=False)

Returns a sorted ‘list’ of ‘dict’.

static value(iterable, keys, values=<function first_kwarg>, default=None, sequentially=True, reverse=False)

Sort a list of dictionaries as you like!

Sort a ‘list’ of ‘dict’ by value. For simple sorting define keys as ‘list’ of ‘str’, where the strings are keynames. The functional values parameter behaves like the functional key parameter from the built-in sorted function.

Examples

Simple sort by a key:

>>> dicts = [{'name': 'Eve'},
             {'name': 'Alice', 'email': 'alice@example.com'},
             {'email': 'bob@example.com', 'name': 'Bob'}]
>>> sort_dicts_by_value(dicts, ['name'])
[{'name': 'Alice', 'email': 'alice@example.com'},
 {'email': 'bob@example.com', 'name': 'Bob'},
 {'name': 'Eve'}]

Advanced sort for multiple keys and custom values function:

>>> dicts = [{'price': 100},
             {'price': 50, 'shipping': 40},
             {'shipping': 5, 'price': 55}]
>>> def total(price, shipping):
        return price + shipping
>>> sort_dicts_by_value(dicts,
                        ['price', 'shipping'],
                        values=total,
                        default=0,
                        sequentially=False)
[{'price': 55, 'shipping': 5},
 {'price': 50, 'shipping': 40},
 {'price': 100}]

Sort dictionaries with non string keys:

>>> dicts = [{1: False},
             {'right': True, 1: False},
             {1: True, 'right': True}]
>>> def both(left, right):
        return left and right
>>> sort_dicts_by_value(dicts,
                        [{'key': 1, 'alias': 'left'}, 'right'],
                        values=both,
                        default=False,
                        sequentially=False,
                        reverse=True)
[{1: True, 'right': True},
 {1: False},
 {1: False, 'right': True}]
Parameters:
  • iterable (list(dict)) – The input dictonaries.
  • keys (list(string or dict)) –

    Sort by defined keys. If you have non string keys then keys should be a ‘dict’ instead of a ‘list’ with plain keys, which defines an alternative keyname for the non string key (e.g. keys={'key': 1: alias: 'one'} instead of keys=[1]). Note: Those dictionaries which do not contain any key of keys will be removed from the result.

    Options:
    • 'key': defines the key
    • 'alias': alternative keyname for non string key names
    • 'sort': sort order (ASCENDING or DESCENDING)
  • values (function(**kwargs) -> value) – Returns the value to sort by. Defaults to the first value of given keywords (unordered). The values function receives the defined keys as **kwargs. Thus it is possible to process the values easily by their name (e.g. lambda firstname, lastname: firstname + ' ' + lastname). Note: Take the default and the sequentially parameter into account, when you specify a custom values function. In sequential mode you have to expect different keywords (e.g. lambda **kwargs: ).
  • default (value) – Default value. Defaults to None. Defines the default value which is passed to the values function if a specified key from keys is not present or None.
  • sequentially (bool) – Run in sequence. Defaults to True. If True each key in keys gets sorted one after the other else runs in batch mode and passes all keys to the values function. Note: The keys get sorted in reverse order. Thus the first key gets sorted as last and vice versa.
  • reverse (bool) – Reverse sorting. Defaults to False. Reverses sotring order for each key of keys idependently if the sequentially parameter is True else reverses the sorted ‘list’ as whole.
Returns:

A ‘list’ of ‘dict’.

Note: Those dictionaries which do not contain any key of keys will be removed from the result.

Return type:

list

class tocoli.dsl.sort.strings[source]
class by[source]
static similarity(iterable, keyword, reverse=False, weights=(1, 1), case_sensitive=False)

Returns a sorted ‘list’.

tocoli.dsl.string module
class tocoli.dsl.string.strip[source]
static accents(str)

Returns a non accented string.

Module contents
tocoli.py2 package
Submodules
tocoli.py2.enc module
tocoli.py2.enc.decode(input, encoding='utf-8', errors='strict', detect='utf-8')[source]

Decode any string. Returns a decoded ‘unicode’ otherwise the input.

tocoli.py2.enc.encode(input, output_encoding='utf-8', errors='strict', input_encoding='utf-8')[source]

Encode any string. Returns an encoded ‘str’ otherwise the input.

Module contents
tocoli.py3 package
Submodules
tocoli.py3.enc module
tocoli.py3.enc.decode(input, encoding='utf-8', errors='strict', detect='utf-8')[source]

Decode any string. Returns a decoded ‘str’ otherwise the input.

tocoli.py3.enc.encode(input, output_encoding='utf-8', errors='strict', input_encoding='utf-8')[source]

Encode any string. Returns encoded ‘bytes’ otherwise the input.

Module contents
Submodules
tocoli.auth module
tocoli.auth.encrypt_api_key(user_id, secret_key)[source]

Encrypts a user id into an api key (token). The generated api key has no expiration time.

tocoli.auth.encrypt_password(password, rounds=100001)[source]

Encrypts a password as salted hash.

tocoli.auth.encrypt_token(user_id, secret_key, expiration=1800)[source]

Encrypts a user id into a timed token. Default expiration time is 30 minutes.

tocoli.auth.generate_salt(length=10, chars='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')[source]

Generates a salt. Defaults to a random salt from [0-9A-Za-z].

tocoli.auth.verify_api_key(api_key, secret_key)[source]

Verifies a given api key (token). Returns the decoded user id or None.

tocoli.auth.verify_password(hash, password)[source]

Verifies a password hash. Returns True on success else False

tocoli.auth.verify_token(token, secret_key)[source]

Verifies a given token. Returns the decoded user id or None.

tocoli.cmp module
tocoli.cmp.comparable(left, right, case_sensitive=False)[source]

Makes values comparable. Returns a tuple of values

tocoli.enc module
tocoli.enc.decode(input, encoding='utf-8', errors='strict', detect='utf-8')[source]

Decode any string. Returns a unicode decoded string otherwise the input.

Use this function to decode an encoded string. Its recommended to use this function as early as possible in your data flow, when you received data from an external resource (e.g. stdin, file, API, …). Once decoded you have access to the encoded information and can operate on it. This func- tion is a universal wrapper for the built-in codecs.decode() function.

Examples

Default encoding utf-8:

>>> decode('café')
u'café'     # utf-8 decoded string

Non default encoding:

>>> decode('café', 'latin-1')
u'café'     # utf-8 decoded string

Advanced codec support:

>>> decode('Y2Fmw6k=\n', 'base64')
u'café'     # utf-8 decoded string
Parameters:
  • input (str) – Any string you like to decode. The string can be a byte string or a unicode decoded string. The function makes sure to treat the input type in the correct manner.
  • encoding (Optional[str]) – Input encoding. Defaults to utf-8. Defines the encoding of the input.
  • errors (Optional[str]) –

    Error handling schemes. Defaults to ‘strict’ These string values are predefined:

    ’strict’ - raise a ValueError error (or a subclass) ‘ignore’ - ignore the character and continue with the next ‘replace’ - replace with a suitable replacement character;
    Python will use the official U+FFFD REPLACEMENT CHARACTER for the builtin Unicode codecs on decoding and ‘?’ on encoding.
    ’xmlcharrefreplace’ - Replace with the appropriate XML
    character reference (only for encoding).
    ’backslashreplace’ - Replace with backslashed escape sequences
    (only for encoding).
  • detect (Optional[str]) – Inner encoding. Defaults to utf-8. This parameter defines the recursive decoding type (e.g. contains a base64 encoded string inside another encoded string, which might be a utf-8 or something else). If the given inner decoding type is not correct, then the function tries to detect the encoding auto- matically.
Returns:

unicode (python2) str (python3): Encoded byte string.

tocoli.enc.encode(input, encoding='utf-8', errors='strict', input_encoding='utf-8')[source]

Encode any string. Returns a byte string otherwise the input.

Use this funtion to encode strings you like to write to any kind of output (e.g. stdout, file, API, …). This is useful to exchange information in a standardized way (the encoding). This function is a universal wrapper for the built-in codecs.encode() function.

Examples

Default usage:

>>> encode('café')
b'café'      # utf-8 encoded byte string

Transform encoding:

>>> encode('café', 'utf-8', input_encoding='latin-1')
b'café'      # utf-8 encoded byte string

Advanced codec support:

>>> encode('café', 'base64')
b'Y2Fmw6k=\n'       # base64 encoded byte string
                    # inner encoding is always utf-8
Parameters:
  • input (str) – Any string you like to encode. The string can be a byte string or a unicode decoded string. The function makes sure to treat the input type in the correct manner.
  • encoding (Optional[str]) – Output encoding. Defaults to utf-8. Defines the encoding for the resulting byte string.
  • errors (Optional[str]) –

    Error handling schemes. Defaults to ‘strict’ These string values are predefined:

    ’strict’ - raise a ValueError error (or a subclass) ‘ignore’ - ignore the character and continue with the next ‘replace’ - replace with a suitable replacement character;
    Python will use the official U+FFFD REPLACEMENT CHARACTER for the builtin Unicode codecs on decoding and ‘?’ on encoding.
    ’xmlcharrefreplace’ - Replace with the appropriate XML
    character reference (only for encoding).
    ’backslashreplace’ - Replace with backslashed escape sequences
    (only for encoding).
  • input_encoding (Optional[str]) – Given encoding. Defaults to utf-8. Set this parameter if your input is not encoded as utf-8.
Returns:

str (python2) bytes (python3): Encoded byte string.

tocoli.filter module
tocoli.filter.clean(str, include='', alpha='a-zA-Z', numeric='0-9')[source]

Filter unwanted characters. Returns the filtered string.

Examples

>>> clean('1.20€')
'120'
>>> clean('1.20€', '.')
'1.20'
>>> clean('1.20€', '.€')
'1.20€'
>>> clean('Hello', alpha='a-z')
'ello'
Parameters:
  • str (str) – input string
  • include (str) – Regular-expression raw string. Defaults to ‘’. This parameter defines which additional characters to include to the result.
  • alpha (str) – Regular-expression raw string. Defaults to ‘a-zA-Z’. This parameter defines which alpha characters should be in the result.
  • numeric (str) – Regular-expression raw string. Defaults to ‘0-9’. This parameter defines which numeric characters should be in the result.
Returns:

string which only contains accepted characters.

Return type:

str

tocoli.filter.filter_bytes(s, where, encoding='utf-8')[source]

Returns bytes.

tocoli.filter.filter_dict_by_key(d, keys, all_keys=False)[source]

Returns this ‘dict’ when it contains at least one of the specified keys else None.

tocoli.filter.filter_dict_by_value(d, keywords, keys=None, all_keys=False)[source]

Returns this ‘dict’ when it contains at least one of the keywords as value in the specified keys else None.

tocoli.filter.filter_dicts_by_keys(iterable, keys, all_keys=False)[source]

Returns a ‘list’ of ‘dict’ whose dictionaries contain at least one of the specified keys.

tocoli.filter.filter_dicts_by_values(iterable, keywords, keys=None, all_keys=False)[source]

Returns a ‘list’ of ‘dict’ whose dictionaries contain at least one of the keywords as value in the specified keys.

tocoli.filter.filter_iter(i, where)[source]

Returns a list.

tocoli.filter.filter_list(l, where)[source]

Returns a list.

tocoli.filter.filter_set(s, where)[source]

Returns a set.

tocoli.filter.filter_string(s, where)[source]

Returns a string.

tocoli.filter.filter_strings_by_keywords(iterable, keywords, case_sensitive=False)[source]

Filter strings by exact keywords. Returns a new list.

tocoli.filter.filter_strings_by_similarity(iterable, keywords, ratio=0.8, weights=(1, 4), case_sensitive=False)[source]

Filter strings by similar keywords. Returns a new list of strings.

tocoli.filter.filter_tuple(t, where)[source]

Returns a list.

tocoli.fn module
tocoli.fn.false(*args, **kwargs)[source]
tocoli.fn.first(ordered)[source]
tocoli.fn.first_arg(*args)[source]
tocoli.fn.first_kwarg(**kwargs)[source]
tocoli.fn.identities(*args)[source]
tocoli.fn.identities_args_kwargs(*args, **kwargs)[source]
tocoli.fn.identities_kwargs(**kwargs)[source]
tocoli.fn.identity(x)[source]
tocoli.fn.identity_values(*args, **kwargs)[source]
tocoli.fn.key(iterable, value=None, sort=False, reverse=False, first=False, position=0)[source]

Generic key getter. Returns one or more keys.

tocoli.fn.last(ordered)[source]
tocoli.fn.last_arg(*args)[source]
tocoli.fn.negate(x)[source]
tocoli.fn.no(*args, **kwargs)[source]
tocoli.fn.return_values_as_tuple(**kwargs)[source]
tocoli.fn.second(ordered)[source]
tocoli.fn.second_arg(*args)[source]
tocoli.fn.third(ordered)[source]
tocoli.fn.third_arg(*args)[source]
tocoli.fn.true(*args, **kwargs)[source]
tocoli.fn.value(iterable, key=None, position=1)[source]

Generic value getter. Returns containing value.

tocoli.fn.yes(*args, **kwargs)[source]
tocoli.join module
tocoli.join.join_strings_by_keywords(list, keywords, join=' ')[source]

Join strings by keywords. Returns a new list with joined strings.

tocoli.join.join_values_as_string(*args, **kwargs)[source]

Concatenates all values as one string. Returns a string.

tocoli.map module
tocoli.map.map_to_non_accented_characters(str)[source]

Returns a non accented string.

tocoli.map.recursive_map(item, function=<function first_arg>, flags=6487, parent=None)[source]

Maps any function recursivly to the item.

tocoli.ratio module
class tocoli.ratio.Nominator[source]
MAX = 'max'
MIN = 'min'
tocoli.ratio.count_equal_chars(str1, str2)[source]
tocoli.ratio.equal(str1, str2, nominator='max')[source]

A simple ratio function based on the equality.

tocoli.ratio.median(list)[source]
tocoli.ratio.meta(str1, str2, ratios, weights)[source]

A meta ratio function. Returns a weighted meta ratio.

The Wiesendahl ratio is a meta ratio which combines a weighted ratio of given ratio functions.

Parameters:
  • str1 (str) – first string
  • str2 (str) – second string
  • ratios (list(function(str, str) -> float)) – ratio functions This parameter is a list of ratio functions.
  • weights (list(float)) – list of weights Each weight gets applied to its corresponding function.
Returns:

the combined and weighted meta ratio

Return type:

float

tocoli.ratio.similarity(str1, str2, weights=(1, 1), case_sensitive=True)[source]
tocoli.regex module
class tocoli.regex.Anchors[source]
BEGINNING = '^'
END = '$'
NOT_WORD_BOUNDARY = '\\B'
WORD_BOUNDARY = '\\b'
class tocoli.regex.CharClass[source]
ANY = '.'
DIGIT = '\\d'
NOT_DIGIT = '\\D'
NOT_WHITESPACE = '\\S'
NOT_WORD = '\\W'
WHITESPACE = '\\s'
WORD = '\\w'
class tocoli.regex.Escaped[source]
CARRIAGE_RETURN = '\\r'
FORM_FEED = '\\f'
LINE_FEED = '\\n'
NULL = '\\0'
TAB = '\\t'
VERTICAL_TAB = '\\v'
class tocoli.regex.Flags[source]
IGNORE_CASE = 'i'
MULTILINE = 'm'
class tocoli.regex.Generator(options, dictionary)[source]
generate(input)[source]
class tocoli.regex.QuantifiedRe(expr=None)[source]

Bases: tocoli.regex.Re

lazy()[source]
class tocoli.regex.Quantifier[source]
OPTIONAL = '?'
PLUS = '+'
STAR = '*'
class tocoli.regex.Re(expr=None)[source]

Bases: unicode

add(expr, escape=False)[source]
add_backreference(n)[source]
add_escaped_control_char(char)[source]
add_escaped_hexadecimal(pair)[source]
add_escaped_octal(triplet)[source]
add_escaped_unicode(quadruple)[source]
add_negated_set(chars)[source]
add_set(chars)[source]
escape()[source]
group(capturing=True)[source]
lookahead(lookup, positive=True)[source]
lookbehind(lookup, positive=True)[source]
negated_set()[source]
optional()[source]
plus()[source]
quantify(min, max=None)[source]
set(chars=None)[source]
star()[source]
class tocoli.regex.Substitution[source]
AFTER_MATCH = "$'"
BEFORE_MATCH = '$`'
DOLLAR = '$$'
MATCH = '$&'
capture_group()[source]
tocoli.regex.alternate(left, right)[source]
tocoli.regex.backreference(n)[source]
tocoli.regex.escape(expr)[source]
tocoli.regex.escape_control_char(char)[source]
tocoli.regex.escape_hexadecimal(pair)[source]
tocoli.regex.escape_octal(triplet)[source]
tocoli.regex.escape_unicode(quadruple)[source]
tocoli.regex.generate(str, start=False, end=False, match=None, quantifier='{0, }', setLike=False, dictionary=None)[source]

Generates a python string for regular-expressions.

tocoli.regex.group(expr, capturing=True)[source]
tocoli.regex.group_capturing(expr)[source]
tocoli.regex.group_non_capturing(expr)[source]
tocoli.regex.lazy(quantified_expr)[source]
tocoli.regex.lookahead(expr, lookup, positive=True)[source]
tocoli.regex.lookahead_negative(expr, lookup)[source]
tocoli.regex.lookahead_positive(expr, lookup)[source]
tocoli.regex.lookbehind(expr, loookup, positive=True)[source]
tocoli.regex.lookbehind_negative(expr, lookup)[source]
tocoli.regex.lookbehind_positive(expr, lookup)[source]
tocoli.regex.negated_set(chars, expr=None)[source]
tocoli.regex.optional(expr)[source]
tocoli.regex.plus(expr)[source]
tocoli.regex.quantify(expr, min, max=None)[source]
tocoli.regex.quantify_lazy(expr, min, max=None)[source]
tocoli.regex.range(start, end)[source]
tocoli.regex.set(chars, expr=None)[source]
tocoli.regex.star(expr)[source]
tocoli.sort module
tocoli.sort.sort_bytes(b, key=None, reverse=False, encoding='utf-8')[source]

Returns bytes.

tocoli.sort.sort_dict_by_key(d, reverse=False)[source]

Returns a ‘list’ of tuple(key, value).

tocoli.sort.sort_dict_by_value(d, reverse=False)[source]

Returns a ‘list’ of tuple(key, value).

tocoli.sort.sort_dicts_by_similarity(iterable, keyword, keys, values=<function first_kwarg>, default=None, sequentially=True, reverse=False, weights=(1, 1), case_sensitive=False)[source]

Returns a sorted ‘list’ of ‘dict’.

tocoli.sort.sort_dicts_by_value(iterable, keys, values=<function first_kwarg>, default=None, sequentially=True, reverse=False)[source]

Sort a list of dictionaries as you like!

Sort a ‘list’ of ‘dict’ by value. For simple sorting define keys as ‘list’ of ‘str’, where the strings are keynames. The functional values parameter behaves like the functional key parameter from the built-in sorted function.

Examples

Simple sort by a key:

>>> dicts = [{'name': 'Eve'},
             {'name': 'Alice', 'email': 'alice@example.com'},
             {'email': 'bob@example.com', 'name': 'Bob'}]
>>> sort_dicts_by_value(dicts, ['name'])
[{'name': 'Alice', 'email': 'alice@example.com'},
 {'email': 'bob@example.com', 'name': 'Bob'},
 {'name': 'Eve'}]

Advanced sort for multiple keys and custom values function:

>>> dicts = [{'price': 100},
             {'price': 50, 'shipping': 40},
             {'shipping': 5, 'price': 55}]
>>> def total(price, shipping):
        return price + shipping
>>> sort_dicts_by_value(dicts,
                        ['price', 'shipping'],
                        values=total,
                        default=0,
                        sequentially=False)
[{'price': 55, 'shipping': 5},
 {'price': 50, 'shipping': 40},
 {'price': 100}]

Sort dictionaries with non string keys:

>>> dicts = [{1: False},
             {'right': True, 1: False},
             {1: True, 'right': True}]
>>> def both(left, right):
        return left and right
>>> sort_dicts_by_value(dicts,
                        [{'key': 1, 'alias': 'left'}, 'right'],
                        values=both,
                        default=False,
                        sequentially=False,
                        reverse=True)
[{1: True, 'right': True},
 {1: False},
 {1: False, 'right': True}]
Parameters:
  • iterable (list(dict)) – The input dictonaries.
  • keys (list(string or dict)) –

    Sort by defined keys. If you have non string keys then keys should be a ‘dict’ instead of a ‘list’ with plain keys, which defines an alternative keyname for the non string key (e.g. keys={'key': 1: alias: 'one'} instead of keys=[1]). Note: Those dictionaries which do not contain any key of keys will be removed from the result.

    Options:
    • 'key': defines the key
    • 'alias': alternative keyname for non string key names
    • 'sort': sort order (ASCENDING or DESCENDING)
  • values (function(**kwargs) -> value) – Returns the value to sort by. Defaults to the first value of given keywords (unordered). The values function receives the defined keys as **kwargs. Thus it is possible to process the values easily by their name (e.g. lambda firstname, lastname: firstname + ' ' + lastname). Note: Take the default and the sequentially parameter into account, when you specify a custom values function. In sequential mode you have to expect different keywords (e.g. lambda **kwargs: ).
  • default (value) – Default value. Defaults to None. Defines the default value which is passed to the values function if a specified key from keys is not present or None.
  • sequentially (bool) – Run in sequence. Defaults to True. If True each key in keys gets sorted one after the other else runs in batch mode and passes all keys to the values function. Note: The keys get sorted in reverse order. Thus the first key gets sorted as last and vice versa.
  • reverse (bool) – Reverse sorting. Defaults to False. Reverses sotring order for each key of keys idependently if the sequentially parameter is True else reverses the sorted ‘list’ as whole.
Returns:

A ‘list’ of ‘dict’.

Note: Those dictionaries which do not contain any key of keys will be removed from the result.

Return type:

list

tocoli.sort.sort_iter(i, key=None, reverse=False)[source]

Returns a list.

tocoli.sort.sort_list(l, key=None, reverse=False)[source]

Returns a list.

tocoli.sort.sort_set(s, key=None, reverse=False)[source]

Returns a list.

tocoli.sort.sort_string(s, key=None, reverse=False)[source]

Returns a string.

tocoli.sort.sort_strings_by_similarity(iterable, keyword, reverse=False, weights=(1, 1), case_sensitive=False)[source]

Returns a sorted ‘list’.

tocoli.sort.sort_tuple(t, key=None, reverse=False)[source]

Returns a list.

tocoli.spell module
class tocoli.spell.Dictionary[source]
latin = {'strict': {u'\xe1': u'a|\xe1|\xe4', u'\xfc': u'u|\xfa|ue|\xfce|\xfc', u'tt': u'tt|t', u'cc': u'cc|ck|c|k', u's': u'ss|s|z|c', u'\xf6': u'o|\xf3|\xf6', u'rr': u'rr|r', u'as': u'as|\xe1s|a|\xe1|s', u'z': u'z|c|s|ss', u'es': u'es|e|s', u'a': u'a|\xe1|\xe4', u'c': u'cc|c|z', u'b': u'b|v', u'e': u'e|\xe9', u'\xe4': u'a|\xe1|\xe4', u'g': u'g|j', u'v': u'v|b', u'\xe9': u'e|\xe9', u'k': u'k|c|q', u'j': u'j|g|y|c|h', u'\xed': u'i|\xed|y', u'l': u'll|l', u'o': u'o|\xf3|\xf6', u'n': u'n|nn|\xf1', u'\xf1': u'n|\xf1', u'i': u'i|\xed|y', u'\xf3': u'o|\xf3|\xf6', u'r': u'rr|r', u'u': u'u|\xfa|ue|\xfce|\xfc', u't': u'tt|t', u'nn': u'nn|n|\xf1', u'y': u'y|ll|i', u'\xfa': u'u|\xfa|ue|\xfce|\xfc', u'os': u'os|o|s|a', u'll': u'll|l|j|y'}, 'wide': {u'sch': u'ssh|sch|sh|ch|sc|h|s', u'cc': u'cc|c|k|s|z|q|x|h', u'll': u'll|l|j|y|ii|i', u'yi': u'yi|hi|i|y|ji|xi', u'\xe1': u'a|\xe1|\xe4|o|\xf3', u'b': u'b|v|w', u'd': u'd|t', u'\xe9': u'e|\xe9|i', u'h': u'h|g|x', u'j': u'j|g|y|c|h', u'\xed': u'i|\xed|y|e|l', u'l': u'll|l|j|i', u'n': u'nn|n|\xf1|m', u'\xf1': u'nn|n|\xf1|m', u'i': u'i|\xed|y|e|l', u'\xf3': u'o|\xf3|\xf6|u|a|\xe1', u'r': u'r|rr', u't': u'tt|d|t', u'v': u'v|b|w', u'x': u'cc|ch|ck|ks|c|j|s|x|z', u'z': u'z|c|s', u'ai': u'ai|i|y', u'is': u'is|\xeds|i|\xed|s|ls', u'ay': u'ay|i|y', u'ie': u'ie|\xede|i\xe9|i|e|le', u'es': u'es|\xe9s|e|s|al', u'rr': u'rr|r', u'nn': u'nn|n|\xf1|m', u'y': u'y|ll|i|j', u'ji': u'ji|j\xed|hi|h\xed|xi|x\xed|y|i|\xed', u'a': u'a|\xe1|\xe4|o|\xf3|e', u'sz': u'sz|s|z', u'c': u'cc|c|k|s|z|q|x|h', u'e': u'e|\xe9|i', u'\xe4': u'a|\xe1|\xe4|o|\xf3', u'g': u'g|j|h|c|q', u'ss': u'ss|s|\xdf', u'k': u'ca|k|c|q', u'm': u'm|n', u'o': u'o|\xf3|\xf6|u|a|\xe1', u'\xfc': u'ue|\xfce|u|\xfa|\xfc|v|w|a', u'q': u'q|k|c', u's': u'ss|c|s|z|\xdf|th', u'sh': u'ssh|sch|sh|ch|sc|h|s|ge', u'u': u'ue|\xfce|u|\xfa|\xfc|v|w|a', u'w': u'w|v|b|u', u'\xf6': u'o|\xf3|\xf6|u|a|\xe1', u'sc': u'ssc|sch|sc|sz|c|s', u'ou': u'ou|\xf3u|o\xfa|o|u', u'\xfa': u'ue|\xfce|u|\xfa|\xfc|v|w|a', u'os': u'os|\xf3s|o|\xf3|s|a', u'tt': u'tt|d|t', u'se': u'sse|ss\xe9|se|s\xe9|e|s'}}
tocoli.spell.lookup(word, dictionary)[source]
tocoli.test module
class tocoli.test.Bencher(rounds=1, collect=False, stopwatch=True, precision='0.8')[source]
bench(function, *args, **kwargs)[source]
class tocoli.test.FnPrinter(seperator='-', width=None)[source]
fnprint(function, *args, **kwargs)[source]
tocoli.test.arguments(args=None, kwargs=None)[source]
tocoli.test.seperator(str='', seperator='-', width=80)[source]
tocoli.type module
tocoli.type.to_bool(input)[source]

Returns ‘bool’.

tocoli.type.to_integer(input)[source]

Returns ‘int’.

tocoli.type.to_string(input, encoding='utf-8')[source]
Module contents

The tocolib is a multipurpose utility library.

class tocoli.Py[source]
class Enc[source]
default = 'utf-8'
preferred = 'utf-8'
class Ver[source]
hex = 34016496
major = 2
micro = 12
minor = 7
releaselevel = 'final'
serial = 0
text = '2.7.12 (default, Nov 19 2016, 06:48:10) \n[GCC 5.4.0 20160609]'
static std(io=<open file '<stdout>', mode 'w'>, encoding='utf-8')[source]

Indices and tables