structs API Documentation

A collection of convenient Python data structures.

class convutils.structs.SortedTupleKeysDict(items=None, **kwargs)

A dictionary that always sorts the items in its tuple keys.

Parameters:items – an iterable of pairs of keys and values; keys should be tuples
class convutils.structs.TwoWaySetDict(items=None, **kwargs)

A dictionary that has sets as values, and allows looking up the key of any item that is in at least one set within the values.

Parameters:items – an iterable of pairs of keys and values; values should be set instances
add_item(key, item)

Adds a item to the set belonging to the key.

Raises a KeyError if the key does not exist.

Parameters:
  • key – a key in the main dictionary
  • item – an item to be added to the set belonging to the key
clear()
copy()

Return a shallow copy.

get_item_keys(item)

Returns a set of all keys whose set values the item is present in.

Raises a KeyError if the item is not present in any of the values.

Parameters:item – an item in one of the value sets
has_item(item)

Performs a reverse-lookup to see if an item exists among the sets in the dictionary’s values.

Returns True if the item is among the values, or False if the item is not.

Parameters:item – an item that may be among the ``set``s in the main dictionary’s values
item_has_key(item, key)

See if an item in the values of the dictionary has a reverse mapping to an item.

Raises a KeyError if the item is not present in any of the values.

Returns True if the item has a reverse mapping to the key, or False if the item has no reverse mapping to the key.

Parameters:
  • item – an item in one of the value sets
  • key – a key of the dictionary
remove_item(key, item)

Removes an item from the set belonging to the key.

Raises a KeyError if the key does not exist, or if the item is not present in the set belonging to the key.

Parameters:
  • key – a key in the main dictionary
  • item – an item to be removed from the set belonging to the key
remove_item_from_all_keys(item)

Removes an item from all set values in the main dictionary to which it belongs.

Raises a KeyError if the item is not present in any of the values.

Parameters:item – an item to be removed from all sets of values
reverse_items()

Returns a list of tuples for reverse key and value pairs.

reverse_iteritems()

Yields individual key-value pairs for the reversed items.

reverse_iterkeys()

Returns an iterable of the keys of the reverse dictionary.

These items are the items in the value sets of the main dictionary.

reverse_itervalues()

Returns an iterable of the values of the reverse dictionary.

These values are sets of the keys in the main dictionary.

reverse_keys()

Returns a list of the keys of the reverse dictionary.

These items are the items in the value sets of the main dictionary.

reverse_values()

Returns a list of the values of the reverse dictionary.

These values are sets of the keys in the main dictionary.

convutils.structs.sample_list_dict(d, k)

Given a dictionary with lists as values, samples a given number of sub-elements uniformly at random.

Consumes less memory than dict_list_random_sample().

Parameters:
  • d – a dictionary whose values are lists or other enumerable, iterable types
  • k – number of sub-elements in the returned dictionary
Returns:

a dictionary with the given number of sub-elements

convutils.structs.sample_list_dict_low_mem(d, k)

Given a dictionary with lists as values, samples a given number of sub-elements uniformly at random.

Consumes less memory than dict_list_random_sample() for large dictionaries.

Parameters:
  • d – a dictionary whose values are lists or other enumerable, iterable types
  • k – number of sub-elements in the returned dictionary
Returns:

a dictionary with the given number of sub-elements

Previous topic

ConvUtils API Documentation

Next topic

utils API Documentation

This Page