This module contains utilities related to collections.
A two-dimensional rectangular list.
Variables: |
|
---|
Copies the contents of another list into the current GridList.
Parameters: | other – Another list. |
---|
Checks whether the dimensions of the current GridList and another rectangular list are equal.
Parameters: | other – Another rectangular list. |
---|---|
Returns: | True if both data structures have the same dimensions. |
Fills the current GridList with the contents of another GridList as much as possible, unless there is a chance of overflow.
Param: | other: Another list. |
---|
Updates a specific grid cell.
Parameters: |
|
---|
Dictionary for items identified by integers.
Variables: |
|
---|
Adds or gets the id of an item.
Parameters: | item – The item. |
---|---|
Returns: | The id. |
Gets the id of an item.
Parameters: | item – The item. |
---|---|
Returns: | The id. |
Yields equal sized chunks from a list.
Parameters: |
|
---|
>>> from dautil import collect
>>> collect.chunk(list(range(7)), 3)
<generator object chunk at 0x1006a7480>
>>> next(collect.chunk(list(range(7)), 3))
[0, 1, 2]
Selects a subset of a dictionary with a list of keys.
Parameters: |
|
---|---|
Returns: | A subset of the input dictionary. |
>>> from dautil import collect
>>> adict = {'a.latex': 1, 'b.latex': 2, 'c': 3}
>>> collect.dict_from_keys(adict, ['b.latex', 'a.latex'])
{'a.latex': 1, 'b.latex': 2}
This function reports updates to a dict.
Parameters: |
|
---|---|
Returns: | A dictionary with the updates if any. |
Filters the keys of a dictionary.
Parameters: |
|
---|---|
Returns: | A list of keys selected by the filter. |
>>> from dautil import collect
>>> adict = {'a.latex': 1, 'b.latex': 2, 'c': 3}
>>> collect.filter_dict_keys(lambda x: x.endswith('.latex'), adict)
['a.latex', 'b.latex']
Filters a list using a function.
Parameters: |
|
---|---|
Returns: | The filtered list. |
>>> from dautil import collect
>>> alist = ['a', 'a.color', 'color.b']
>>> collect.filter_list(lambda x: x.endswith('color'), alist)
['a.color']
Flattens an iterable, where strings and dicts are not considered iterable.
Parameters: | iterable – The iterable to flatten. |
---|---|
Returns: | The iterable flattened as a flat list. |
>>> from dautil import collect
>>> collect.flatten([[1, 2]])
[1, 2]
Creates a 2D cartesian product grid from an array:
>>> from dautil import collect
>>> arr = list(range(2))
>>> arr
[0, 1]
>>> collect.grid_list(arr)
[[(0, 0), (0, 1)], [(1, 0), (1, 1)]]
Parameters: | arr – A 1-d array-like list. |
---|---|
Returns: | A 2-d list containing tuples as elements. |
Checks whether a list is rectangular.
Parameters: | alist – A list or similar data structure. |
---|---|
Returns: | True if the argument is rectangular. |
>>> from dautil import collect
>>> collect.is_rectangular([[2, 1], [3]])
False
>>> collect.is_rectangular([[2, 1], [3, 4]])
True
Checking for an iterable argument using a somewhat modified definition ie strings and dicts are considered not iterable.
Parameters: | tocheck – The data structure to check. |
---|---|
Returns: | True if iterable |
Counts the number of elements in the longest streak for a given value.
Parameters: |
|
---|---|
Returns: | The count of the longest sequence. |
>>> from dautil import collect
>>> collect.longest_streak([0, 1, 1, 0, 1, 1, 1], 1)
3
Sorts a dictionary by keys.
Parameters: | adict – A dictionary. |
---|---|
Returns: | An OrderedDict sorted by keys. |
>>> from dautil import collect
>>> food = {'spam': 42, 'eggs': 28}
>>> collect.sort_dict_by_keys(food)
OrderedDict([('eggs', 28), ('spam', 42)])