-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
116 lines (88 loc) · 3.06 KB
/
utils.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import random, time
import Queue
class Timer(object):
def __init__(self, name=None):
self.name = name
def __enter__(self):
self.tstart = time.time()
def __exit__(self, type, value, traceback):
if self.name:
print '[%s]' % self.name,
print 'Elapsed: %s' % (time.time() - self.tstart)
def get_all_from_queue(Q):
""" Generator to yield one after the others all items
currently in the queue Q, without any waiting.
"""
try:
while True:
yield Q.get_nowait( )
except Queue.Empty:
raise StopIteration
def get_item_from_queue(Q, timeout=0.01):
""" Attempts to retrieve an item from the queue Q. If Q is
empty, None is returned.
Blocks for 'timeout' seconds in case the queue is empty,
so don't use this method for speedy retrieval of multiple
items (use get_all_from_queue for that).
"""
try:
item = Q.get(True, 0.01)
except Queue.Empty:
return None
return item
def flatten(iterables):
""" Flatten an iterable of iterables. Returns a generator.
list(flatten([[2, 3], [5, 6]])) => [2, 3, 5, 6]
"""
return (elem for iterable in iterables for elem in iterable)
def argmin_list(seq, func):
""" Return a list of elements of seq[i] with the lowest
func(seq[i]) scores.
>>> argmin_list(['one', 'to', 'three', 'or'], len)
['to', 'or']
"""
best_score, best = func(seq[0]), []
for x in seq:
x_score = func(x)
if x_score < best_score:
best, best_score = [x], x_score
elif x_score == best_score:
best.append(x)
return best
def argmin_random_tie(seq, func):
""" Return an element with lowest func(seq[i]) score; break
ties at random.
"""
return random.choice(argmin_list(seq, func))
def argmin(seq, func):
""" Return an element with lowest func(seq[i]) score; tie goes
to first one.
>>> argmin(['one', 'to', 'three'], len)
'to'
"""
return min(seq, key=func)
def argmax_list(seq, func):
""" Return a list of elements of seq[i] with the highest
func(seq[i]) scores.
>>> argmax_list(['one', 'three', 'seven'], len)
['three', 'seven']
"""
return argmin_list(seq, lambda x: -func(x))
def argmax_random_tie(seq, func):
""" Return an element with highest func(seq[i]) score; break
ties at random.
"""
return random.choice(argmax_list(seq, func))
def argmax(seq, func):
""" Return an element with highest func(seq[i]) score; tie
goes to first one.
>>> argmax(['one', 'to', 'three'], len)
'three'
"""
return max(seq, key=func)
#-----------------------------------------------------------------
if __name__ == "__main__":
#~ print list(flatten([[1, 2], (4, 5), [5], [6, 6, 8]]))
#~ print argmin_random_tie(['one', 'to', 'three', 'or'], len)
print min(['one', 'to', 'three', 'or'], key=len)
print argmin(['one', 'to', 'three', 'or'], len)