/usr/share/pyshared/enthought/util/dict.py is in python-enthoughtbase 3.1.0-2build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | 'Non-standard dictionary functions'
from sequence import intersect
def map_keys(f, d):
''' Map a function over the keys in a dictionary.
>>> map_keys(str, {1: 2, 3: 4}) == {'1': 2, '3': 4}
True
>>> map_keys(lambda x: 0, {1: 2, 3: 4}) in [{0: 2}, {0: 4}]
True
'''
return dict([ (f(k), v) for k,v in d.items() ])
def map_values(f, d):
''' Map a function over the values in a dictionary.
>>> map_values(str, {1: 2, 3: 4}) == {1: '2', 3: '4'}
True
>>> map_values(lambda x: 0, {1: 2, 3: 4}) == {1: 0, 3: 0}
True
'''
return dict([ (k, f(v)) for k,v in d.items() ])
def map_items(f, d):
''' Map a binary function over the key-value pairs in a dictionary.
>>> map_items(lambda a,b: (a*2, b**2), {1: 2, 3: 4}) == {2: 4, 6: 16}
True
'''
return dict([ f(k,v) for k,v in d.items() ])
def filter_keys(p, d):
''' Filter a dictionary by a predicate on keys.
>>> filter_keys(lambda n: n % 2, {0: 1, 1: 2, 2: 3}) == {1: 2}
True
'''
return dict([ (k,v) for k,v in d.items() if p(k) ])
def filter_values(p, d):
''' Filter a dictionary by a predicate on values.
>>> filter_values(lambda n: n % 2, {0: 1, 1: 2, 2: 3}) == {0: 1, 2: 3}
True
'''
return dict([ (k,v) for k,v in d.items() if p(v) ])
def filter_items(p, d):
''' Filter a dictionary by a predicate on key-value pairs.
>>> import operator
>>> filter_items(operator.le, {0: 0, 1: 2, 3: 2}) == {0: 0, 1: 2}
True
'''
return dict([ (k,v) for k,v in d.items() if p(k,v) ])
def dict_zip(*dicts):
''' Zip dictionaries.
>>> dict_zip(dict(a=True), dict(a='foo'))
{'a': (True, 'foo')}
>>> dict_zip(dict(a=0, b=2), dict(a=1, c=3), dict(a=None, c=4))
{'a': (0, 1, None)}
>>> dict_zip(dict(a=0), dict(b=1, c=2, d=3))
{}
'''
keys = intersect([ set(d) for d in dicts ])
return dict([ (k, tuple([ d[k] for d in dicts ])) for k in keys ])
def sub_dict(d, keys):
''' Create a dictionary from a subset of another.
>>> sub_dict({1: 2, 3: 4, 'a': 'b'}, [1, 3]) == {1: 2, 3: 4}
True
>>> try:
... sub_dict({1: 2}, [1, 3])
... assert False
... except KeyError:
... print 'Key error!'
Key error!
'''
return dict([ (k, d[k]) for k in keys ])
|