def
main():
histogram
=
Histogram()
for
line
in
fileinput.input():
timestamp
=
get_timestamp(line)
if
timestamp
is
not
None:
histogram.count(timestamp)
chart
=
AsciiChart(histogram)
chart.draw()
Slide 32
Slide 32 text
def
main():
histogram
=
Histogram()
for
line
in
fileinput.input():
timestamp
=
get_timestamp(line)
if
timestamp
is
not
None:
histogram.count(timestamp)
chart
=
AsciiChart(histogram)
chart.draw()
histogram.count(timestamp)
Slide 33
Slide 33 text
class
Histogram(object):
def
__init__(self):
"""
Construct
a
Histogram
instance.
"""
self.tree
=
PrefixTree()
def
count(self,
timestamp):
"""
Increment
the
count
for
this
timestamp.
"""
self.tree.incr(timestamp)
Slide 34
Slide 34 text
class
Histogram(object):
def
__init__(self):
"""
Construct
a
Histogram
instance.
"""
self.tree
=
PrefixTree()
def
count(self,
timestamp):
"""
Increment
the
count
for
this
timestamp.
"""
self.tree.incr(timestamp)
self.tree
=
PrefixTree()
Slide 35
Slide 35 text
Prefix Tree?
So what's a
Slide 36
Slide 36 text
>>>
tree.incr('pony')
Slide 37
Slide 37 text
tree
>>>
tree.incr('pony')
Slide 38
Slide 38 text
tree
P
>>>
tree.incr('pony')
Slide 39
Slide 39 text
tree
P
O
>>>
tree.incr('pony')
Slide 40
Slide 40 text
tree
P
O
N
>>>
tree.incr('pony')
Slide 41
Slide 41 text
tree
P
O
N
Y
>>>
tree.incr('pony')
Slide 42
Slide 42 text
tree
P
O
N
Y 1
>>>
tree.incr('pony')
Slide 43
Slide 43 text
tree
P
O
N
Y 1
>>>
tree.incr('pony')
>>>
tree.sum()
1
Slide 44
Slide 44 text
tree
P
O
N
Y 2
>>>
tree.incr('pony')
>>>
tree.sum()
1
>>>
tree.incr('pony')
>>>
tree.sum()
2
Slide 45
Slide 45 text
tree
P
O
N
Y
C
A
T
2
1
>>>
tree.incr('cat')
>>>
tree.sum()
3
Slide 46
Slide 46 text
tree
P
O
N
Y
C
A
T
O
W
2
1
1
>>>
tree.incr('cow')
>>>
tree.sum()
4
Slide 47
Slide 47 text
tree
P
O
N
Y
A
T
O
W
2
1
1
>>>
node
=
tree.find('c')
>>>
node.sum()
2 C
Slide 48
Slide 48 text
tree
P
O
N
Y
A
T
O
W
2
1
1
>>>
node
=
tree.find('cat')
>>>
node.sum()
1 C
class
PrefixTree(collections.defaultdict):
def
__init__(self):
super(PrefixTree,
self).__init__(PrefixTree)
self.value
=
0
Implemented as a "recursive" defaultdict