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