tocoli.dsl package¶
Submodules¶
tocoli.dsl.filter module¶
-
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.
-
static
-
class
tocoli.dsl.join module¶
tocoli.dsl.map module¶
tocoli.dsl.sort module¶
-
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 ofkeys=[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
orDESCENDING
)
- 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 thevalues
function if a specified key fromkeys
is not present orNone
. - sequentially (bool) – Run in sequence. Defaults to True.
If
True
each key inkeys
gets sorted one after the other else runs in batch mode and passes allkeys
to thevalues
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
-
static
-
class