Skip to content

Commit 59edf10

Browse files
committed
-
1 parent 93d3b07 commit 59edf10

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

python_toolbox/dict_tools.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
'''Defines several functions that may be useful when working with dicts.'''
55

6+
import collections
7+
68
from python_toolbox import cute_iter_tools
79
from python_toolbox import comparison_tools
810

@@ -141,4 +143,43 @@ def sum_dicts(dicts):
141143
result = {}
142144
for dict_ in dicts:
143145
result.update(dict_)
144-
return result
146+
return result
147+
148+
149+
def remove_keys(d, keys_to_remove):
150+
'''
151+
Remove keys from a dict.
152+
153+
`keys_to_remove` is allowed to be either an iterable (in which case it will
154+
be iterated on and keys with the same name will be removed), a container
155+
(in which case this function will iterate over the keys of the dict, and if
156+
they're contained they'll be removed), or a filter function (in which case
157+
this function will iterate over the keys of the dict, and if they pass the
158+
filter function they'll be removed.)
159+
160+
If key doesn't exist, doesn't raise an exception.
161+
'''
162+
if isinstance(keys_to_remove, collections.Iterable):
163+
for key in keys_to_remove:
164+
try:
165+
del d[key]
166+
except KeyError:
167+
pass
168+
else:
169+
if isinstance(keys_to_remove, collections.Container):
170+
filter_function = lambda value: value in keys_to_remove
171+
else:
172+
assert isinstance(keys_to_remove, collections.Callable)
173+
filter_function = keys_to_remove
174+
for key in list(d.keys()):
175+
if filter_function(key):
176+
del d[key]
177+
178+
179+
180+
181+
182+
183+
184+
185+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Copyright 2009-2013 Ram Rachum.
2+
# This program is distributed under the MIT license.
3+
4+
import numbers
5+
6+
from python_toolbox.dict_tools import remove_keys
7+
8+
9+
def test():
10+
'''Test the basic workings of `sum_dicts`.'''
11+
origin_dict = {1: 2, 3: 4, 5: 6, 7: 8, 9: 10, 11: 12, 13: 14, 15: 16,}
12+
13+
not_divide_by_three_dict = dict(origin_dict)
14+
remove_keys(not_divide_by_three_dict, xrange(0, 50, 3))
15+
assert not_divide_by_three_dict == {1: 2, 5: 6, 7: 8, 11: 12, 13: 14}
16+
17+
below_ten_dict = dict(origin_dict)
18+
remove_keys(below_ten_dict, lambda value: value >= 10)
19+
assert below_ten_dict == {1: 2, 3: 4, 5: 6, 7: 8, 9: 10}
20+
21+
class HoledNumbersContainer(object):
22+
'''Contains only numbers that have a digit with a hole in it.'''
23+
def __contains__(self, number):
24+
if not isinstance(number, numbers.Integral):
25+
return False
26+
return bool(set(str(number)).intersection({'0', '4', '6', '8', '9'}))
27+
28+
29+
non_holed_numbers_dict = dict(origin_dict)
30+
remove_keys(non_holed_numbers_dict, HoledNumbersContainer())
31+
assert non_holed_numbers_dict == {1: 2, 3: 4, 5: 6, 7: 8, 11: 12, 13: 14,
32+
15: 16,}
33+

0 commit comments

Comments
 (0)