-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchopmunk.py
199 lines (157 loc) · 4.96 KB
/
chopmunk.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# -*- coding: utf-8 -*-
from datetime import datetime
from functools import wraps
import json
import uuid
def coroutine(f):
"""Turn a generator function into a coroutine by calling .next() once."""
@wraps(f)
def started(*args,**kwargs):
cr = f(*args,**kwargs)
cr.next()
return cr
return started
def aslist(item):
if not isinstance(item, (list, tuple)):
item = [item]
return item
def replace_numpy_data(item):
"""Return a copy of item where each numpy array/scalar is replaced with a
list/scalar."""
if isinstance(item, dict):
item = dict((k, replace_numpy_data(item[k])) for k in item)
elif isinstance(item, list):
item = [replace_numpy_data(i) for i in item]
elif isinstance(item, tuple):
item = tuple(replace_numpy_data(i) for i in item)
elif hasattr(item, 'tolist'):
item = item.tolist()
return item
@coroutine
def jsonify(consumer):
"""Return a consumer which passes values on as json string."""
while True:
info = (yield)
info = replace_numpy_data(info)
consumer.send(json.dumps(info))
@coroutine
def print_sink():
"""Return a consumer that prints values received."""
while True:
info = (yield)
print info
@coroutine
def prettyprint_sink():
"""Return a consumer that prints values received prettily."""
while True:
info = (yield)
for key in info:
print '%s: %s' % (key, info[key])
print '-' * 20
@coroutine
def broadcast(*consumers):
"""Return a consumer that broadcasts values to all given consumers."""
while True:
info = (yield)
for c in consumers:
c.send(info)
@coroutine
def file_sink(filename, append=False, suffix='\n'):
"""Return a consumer that writes values to a file.
If `append` is True, the file is opened for appending.
Each value written to the files is followed by `suffix`."""
mode = 'a' if append else 'w'
with open(filename, mode) as f:
while True:
info = (yield)
f.write(str(info) + suffix)
@coroutine
def filelike_sink(file_like_object, suffix='\n'):
"""Return a consumer that writes values to a file like object.
Each value written to the files is followed by `suffix`."""
while True:
info = (yield)
file_like_object.write(str(info) + suffix)
@coroutine
def timify(consumer):
"""Returnt a consumer that adds a field 'datetime' to the received values
and passes them on."""
while True:
info = (yield)
info['datetime'] = datetime.now().isoformat()
consumer.send(info)
@coroutine
def taggify(consumer, tags):
"""Return a consumer that adds a list of tags into the 'tags' field of the
values and passes them on."""
tags = aslist(tags)
while True:
info = (yield)
info['tags'] = info.get('tags', [])
info['tags'] += tags
consumer.send(info)
@coroutine
def list_sink(lst):
"""Return a consumer that appends all received values to a list `lst`."""
while True:
info = (yield)
lst.append(info)
@coroutine
def exclude_tags(consumer, tags):
"""Return a consumer that only passes values on that have none of a given
set of `tags`."""
tags = aslist(tags)
while True:
info = (yield)
if not 'tags' in info or all(i not in info['tags'] for i in tags):
consumer.send(info)
else:
continue
@coroutine
def include_tags_only(consumer, tags):
"""Return a consumer that only passes values on that have all of a given
set of `tags`."""
tags = aslist(tags)
while True:
info = (yield)
if not 'tags' in info or not all(i in info['tags'] for i in tags):
continue
consumer.send(info)
@coroutine
def keep(consumer, keys):
"""Return consumer that only keeps a subset of the dictionary given by
`keys`."""
while True:
info = (yield)
new_info = dict((k, v) for k, v in info.items() if k in keys)
consumer.send(new_info)
@coroutine
def dontkeep(consumer, keys):
"""Return consumer that throws away a subset of the dictionary given by
`keys`."""
while True:
info = (yield)
new_info = dict((k, v) for k, v in info.items() if k not in keys)
consumer.send(new_info)
@coroutine
def deadend():
"""Return a consumer that does not do anything."""
while True:
(yield)
@coroutine
def uniquify(consumer):
"""Return a consumer that adds a field uuid to each value and places a uuid
(randomly generated per uniquify) in it."""
uid = str(uuid.uuid4())
while True:
info = (yield).copy()
info['uuid'] = uid
consumer.send(info)
@coroutine
def add_keyvalue(consumer, key, value):
"""Return a consumer that adds a (`key`, `value`) pair to each value passing
through."""
while True:
info = (yield).copy()
info[key] = value
consumer.send(info)