dautil.collect

This module contains utilities related to collections.

class dautil.collect.GridList(nrows, ncols, val)

A two-dimensional rectangular list.

Variables:
  • nrows – The number of rows in the grid.
  • ncols – The number of columns in the grid.
  • val – The initial value of cells in the grid.
  • logger – The logger of the class.
copy(other)

Copies the contents of another list into the current GridList.

Parameters:other – Another list.
dim_equal(other)

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.
fill(other)

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.
update(row, col, change)

Updates a specific grid cell.

Parameters:
  • row – The row number of the cell.
  • col – The column number of the cell.
  • change – The content with which to update the cell. If the cell is a dict and the update value too then the original dict will be updated.
class dautil.collect.IdDict(items={})

Dictionary for items identified by integers.

Variables:
  • curr – The current position.
  • items – The dictionary.
add_or_get(item)

Adds or gets the id of an item.

Parameters:item – The item.
Returns:The id.
get_id(item)

Gets the id of an item.

Parameters:item – The item.
Returns:The id.
dautil.collect.chunk(alist, size)

Yields equal sized chunks from a list.

Parameters:
  • alist – List-like sequence.
  • size – Size of the chunk.
>>> 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]
dautil.collect.dict_from_keys(adict, keys)

Selects a subset of a dictionary with a list of keys.

Parameters:
  • adict – A dictionary.
  • keys – A list of keys.
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}
dautil.collect.dict_updates(old, new)

This function reports updates to a dict.

Parameters:
  • old – A dictionary to compare against.
  • new – A dictionary with potential changes.
Returns:

A dictionary with the updates if any.

dautil.collect.filter_dict_keys(func, adict)

Filters the keys of a dictionary.

Parameters:
  • func – A function used to filter the keys.
  • adict – A dictionary.
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']
dautil.collect.filter_list(func, alist)

Filters a list using a function.

Parameters:
  • func – A function used for filtering.
  • alist – The list to filter.
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']
dautil.collect.flatten(iterable)

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]
dautil.collect.grid_list(arr)

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.
dautil.collect.is_rectangular(alist)

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
dautil.collect.isiterable(tocheck)

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
dautil.collect.longest_streak(arr, val)

Counts the number of elements in the longest streak for a given value.

Parameters:
  • arr – A 1-d list.
  • val – The value to search for.
Returns:

The count of the longest sequence.

>>> from dautil import collect
>>> collect.longest_streak([0, 1, 1, 0, 1, 1, 1], 1)
3
dautil.collect.sort_dict_by_keys(adict)

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)])

Previous topic

Welcome to dautil’s documentation!

Next topic

dautil.conf

This Page