Package gmisclib :: Module dictops
[frames] | no frames]

Module dictops

source code

Operations on dictionaries.


Version: $Revision: 1.9 $

Classes
  BadFileFormatError
This indicates a problem when reading in a dictionary via read2c.
  dict_of_lists
A dictionary of lists.
  dict_of_sets
A dictionary of lists.
  dict_of_accums
A dictionary of accumulators.
  dict_of_averages
A dictionary of accumulators.
  dict_of_maxes
A dictionary of maxima.
  dict_of_X
A dictionary of arbitrary things.
  list_of_dicts
Functions
dict
filter(d, l)
Passes through items listed in l.
source code
dict
remove(d, l)
Removes items listed in l.
source code
dict
intersection(*dlist)
Returns: a dictionary containing those key=value pairs that are common to all the dictionaries.
source code
 
test_intersection() source code
dict
rev1to1(d)
Reverse a 1-1 mapping so it maps values to keys instead of keys to values.
source code
dict
compose(d2, d1)
Compose two mappings (dictionaries) into one.
source code
tuple(dict(str:str), list(str))
read2c(f)
Read in a dictionary from a two-column file; columns are separated by whitespace.
source code
 
argmax(d)
For a dictionary of key=value pairs, return the key with the largest value.
source code
 
add_dol(dict, key, value)
OBSOLETE: replace with class dict_of_lists.
source code
 
add_doc(dict, key, value)
OBSOLETE: replace with class dict_of_accums.
source code
 
test() source code
Variables
  __package__ = None
hash(x)
Function Details

filter(d, l)

source code 

Passes through items listed in l.

Parameters:
  • l (list) - list of keys to preserve
  • d (mapping) - dictionary to copy/modify.
Returns: dict
a dictionary where all keys not listed in l are removed; all entries listed in l are copied into the (new) output dictionary.

remove(d, l)

source code 

Removes items listed in l.

Parameters:
  • l (list) - list of keys to remove
  • d (mapping) - dictionary to copy/modify.
Returns: dict
a dictionary where all keys listed in l are removed; all entries not listed in l are copied into the (new) output dictionary.

intersection(*dlist)

source code 
Parameters:
  • dlist (list(dict)) - dictionaries.
Returns: dict
a dictionary containing those key=value pairs that are common to all the dictionaries.

rev1to1(d)

source code 

Reverse a 1-1 mapping so it maps values to keys instead of keys to values.

Parameters:
  • d (dict)
Returns: dict
M such that if m[k]=v, M[v]=k
Raises:
  • ValueError - if the mapping is many-to-one.

compose(d2, d1)

source code 

Compose two mappings (dictionaries) into one.

Parameters:
  • d2 - second mapping
  • d1 - first mapping
Returns: dict
d such that d[k] = d2[d1[k]]

read2c(f)

source code 

Read in a dictionary from a two-column file; columns are separated by whitespace. This is not a completely general format, because keys cannot contain whitespace and the values cannot start or end in whitespace.

The format can contain comments at the top; these are lines that begin with a hash mark (#). Empty lines are ignored.

Parameters:
  • f - an open file
Returns: tuple(dict(str:str), list(str))
a dictionary and a list of comments

Note: You can get into trouble if you have a key that starts with a hash mark (#). If it happens to be the first key, and if there are no comments or if it directly follows a comment, it will be interpreted as a comment. To avoid this possibility, begin your file with a comment line, then a blank line.

argmax(d)

source code 

For a dictionary of key=value pairs, return the key with the largest value.

Raises:
  • ValueError - when the input dictionary is empty.

Note: When there are several equal values, this will return a single, arbitrary choice.

add_dol(dict, key, value)

source code 

OBSOLETE: replace with class dict_of_lists. Add an entry to a dictionary of lists. A new list is created if necessary; else the value is appended to an existing list. Obsolete: replace with dict_of_lists.

add_doc(dict, key, value)

source code 

OBSOLETE: replace with class dict_of_accums. Add an entry to a dictionary of counters. A new counter is created if necessary; else the value is added to an existing counter. Obsolete: replace with dict_of_accums.