design question
Andrea Crotti
andrea.crotti.0 at gmail.com
Sun Apr 10 09:16:18 EDT 2011
Andrea Crotti <andrea.crotti.0 at gmail.com> writes:
[...]
I left the Timeline as before, but tried to rewrite some more classes.
This is the abstract class for a metric, and below another class for the
metric which involve only counting things.
In the end an example on how to use this.
I need to see synthetic values during my quick testings but also to
reuse a lot of data to actually create graphs later...
Any idea/suggestion is welcome, I would like finally to get it right...
--8<---------------cut here---------------start------------->8---
class AbsMetric(object):
def __init__(self, mode='all'):
# mode can be 'all, avg, senders, landmarks, receivers, mobiles'
self.mode = mode
self.result = {}
def __str__(self):
res = []
for k, val in self.result.items():
res.append("%d: %f" % (k, val))
return '\n'.join(res)
def __len__(self):
return len(self.events)
def filt_node(self, network):
modes = {
'all': network.nodes,
'senders' : network.senders,
'landmarks' : network.lands,
'receivers' : network.receivers,
'mobiles' : network.mobiles
}
return modes[self.mode]
--8<---------------cut here---------------end--------------->8---
--8<---------------cut here---------------start------------->8---
class CountingMetric(AbsMetric):
def __init__(self, mode, to_count):
super(CountingMetric, self).__init__(mode)
self.to_count = to_count
def compute(self, timeline, network):
for n in self.filt_node(network):
# there is a lot of computation going on every time
evts = Timeline.filt(timeline.events, evt=self.to_count, node=n)
if len(evts) > 0:
self.result[n] = len(evts)
addr_update = CountingMetric('all', 'ADDRESS_UPDATE_SENT')
--8<---------------cut here---------------end--------------->8---
More information about the Python-list
mailing list