-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
_SORTED = sorted | ||
|
||
class Grouper(dict): | ||
|
||
def plus(self, key, value_element): | ||
try: | ||
extant = self[key] | ||
except KeyError: | ||
self[key] = extant = [] | ||
extant.append(value_element) | ||
|
||
def plusdate(self, key_value_pairs): | ||
for key, value in key_value_pairs: | ||
try: | ||
extant = self[key] | ||
except KeyError: | ||
self[key] = extant = [] | ||
extant.append(value) | ||
|
||
def invert_mapping(mapping, single=False): | ||
"""Map from the values of `mapping` to their associated keys. | ||
:param mapping: a dict whose values are hashable or containers of hashables. | ||
:param single: True if values are to be keys in returned dict. | ||
:returns: a dict mapping from the values (or values' elements) to collection of keys. | ||
""" | ||
result = {} | ||
for k,V in mapping.items(): | ||
iterable_V = [V] if single else V | ||
for v in iterable_V: | ||
try: | ||
extant = result[v] | ||
except KeyError: | ||
result[v] = extant = set() | ||
extant.add(k) | ||
return result | ||
|
||
def sorted(dict): | ||
return {key:dict[key] for key in _SORTED(dict)} |