-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
67 lines (57 loc) · 1.78 KB
/
__init__.py
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
from past.builtins import cmp
from builtins import str
from builtins import range
from future.utils import raise_with_traceback
from collections import OrderedDict
__author__ = 'zeraien'
import logging
import sys
def get_logger(extra_funnel=None):
funnel = 'odb'
if extra_funnel:
funnel = '.'.join((funnel,extra_funnel))
return logging.getLogger(funnel)
def reraise(exception):
raise_with_traceback(exception)
def group_by_key(iterable, keyfunc):
grouped = OrderedDict()
for item in iterable:
key = keyfunc(item)
items_in_group = grouped.get(key, [])
items_in_group.append(item)
grouped[key] = items_in_group
return grouped
def slice_generator(items, group_size):
items_iter = iter(items)
if group_size < 1:
raise ValueError('group_size must be larger than 0')
while True:
out = []
try:
for i in range(0, group_size):
out.append(next(items_iter))
finally:
if out:
yield out
def dict_from_list(the_list, key):
the_dict = {}
key = str(key)
for item in the_list:
is_dict = type(item) is dict
if is_dict:
the_dict[item[key]] = item
else:
the_dict[getattr(item,key)] = item
return the_dict
#http://stackoverflow.com/a/1144405/144838
def multikeysort(items, columns):
from operator import itemgetter
comparers = [ ((itemgetter(col[1:].strip()), -1) if col.startswith('-') else (itemgetter(col.strip()), 1)) for col in columns]
def comparer(left, right):
for fn, mult in comparers:
result = cmp(fn(left), fn(right))
if result:
return mult * result
else:
return 0
return sorted(items, cmp=comparer)