-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenc.py
More file actions
66 lines (49 loc) · 1.73 KB
/
Copy pathenc.py
File metadata and controls
66 lines (49 loc) · 1.73 KB
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
"""Example on profiling memory"""
from datetime import datetime
from collections import namedtuple
from itertools import cycle, islice
Event = namedtuple('Event', ['type', 'time', 'user', 'url', 'site'])
class EncodeError(Exception):
pass
class Encoder:
"""Event encoder"""
def __init__(self, stream):
self.stream = stream
self._fields = {
'click': ['time', 'user'],
'view': ['time', 'user', 'url'],
'enter': ['user', 'url', 'site'],
}
def encode(self, event):
"""Encode event to stream"""
fields = self._fields.get(event.type)
if not fields:
raise EncodeError('unknown event type: {}'.format(event.type))
self.stream.write('{}'.format(len(fields)))
for field in fields:
value = getattr(event, field)
if isinstance(value, datetime):
value = value.isoformat()
self.stream.write('|{}={}'.format(field, value))
stream.write('\n')
def encode_event(event, stream):
"""Encode event to stream"""
enc = Encoder(stream)
return enc.encode(event)
if __name__ == '__main__':
import tracemalloc
from tempfile import NamedTemporaryFile
# Generate test cases
events = []
for typ in ('click', 'view', 'enter'):
events.append(
Event(typ, datetime.now(), 'bugs', '/buy/carrot', 'acme.com'))
events = islice(cycle(events), 1000)
stream = NamedTemporaryFile(mode='wt', delete=False)
print('encoding to {}'.format(stream.name))
tracemalloc.start()
for event in events:
encode_event(event, stream)
snapshot = tracemalloc.take_snapshot()
for stat in snapshot.statistics('lineno')[:10]:
print(stat)