design question

Andrea Crotti andrea.crotti.0 at
Sun Apr 10 06:40:40 EDT 2011

I've been wondering for weeks now how to do but I still didn't get a
satisfying answer, so I hope someone can give a hint...

I have some logs which I extract from simulation results.
These logs are in the form

timestamp, nodeid, eventname, event_argument

and now I have to analyze the data.
I don't have so many metrics to analyze but for example I might look for
"symmetric events", in the form

0.0, 0: DATA_SENT(1)
0.1, 1: DATA_RECEIVED(0)
(so in short the nodeid and the argument are swapped)

After many changes now the timeline is something as below, where
everything almost all the functions are static methods and return a new
list of events.

But now I don't get anything I like for the metrics.

Every metric only has to filter and return a result, but the problem is
that maybe for some metrics I want the result on all the nodes.
Some only the average, sometimes I want to correlated with the time (or
time slots) and so on.

So it must be very flexible, but at the same time not a pain to write...
I first wrote a class for every metric subclassing a Metric, but it
wasn't nice.
Then I tried many other ways but actually I'm a bit stuck...

Any suggestions from someone that maybe had a similar problem?
--8<---------------cut here---------------start------------->8---
class Timeline(object):
    A timeline is generated by the list of events happened during the
    simulation and it's the main point to analyze the data.

    def __init__(self, events=None):
        if events is None:
   = []
   = events

    def __str__(self):
        res = []
        for evt_tuple in iter(self):

        return "\n".join(res)

    def __eq__(self, other):
        return ==

    def __iter__(self):
        return iter(sorted(, key=lambda x: x.time))

    def add(self, timestamp, nodeid, evt):
        "Add one line for the given nodeid"
        tup = EventTuple(timestamp, nodeid, evt)

    def add_tuple(self, tup):

    def first_event(self, _evt):
        first_evt = Timeline.filt(, evt=_evt)[0]
        return pretty_time(first_evt.time)

    def sort(self):

    def sort_by(events, par='time'):
        # side effecting method
        events.sort(key=lambda x: x.__getattribute__(par))

    def filt(events, node=None, evt=None, arg=None):
        "Takes a node, an event and a function to further filter the time"
        # copy locally the events and filter them
        res = events[:]
        if node is not None:
            res = filter(lambda x: x.node == node, res)

        if evt is not None:
            res = filter(lambda x: == evt, res)

        if arg is not None:
            res = filter(lambda x: x.evt.arg == arg, res)

        return res

--8<---------------cut here---------------end--------------->8---

More information about the Python-list mailing list