design question

Discussion in 'Python' started by Andrea Crotti, Apr 10, 2011.

  1. 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:
    self.events = []
    else:
    self.events = events

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

    return "\n".join(res)

    def __eq__(self, other):
    return self.events == other.events

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

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

    def add_tuple(self, tup):
    self.events.append(tup)

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

    def sort(self):
    Timeline.sort_by(self.events)

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

    @staticmethod
    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: x.evt.name == evt, res)

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

    return res
    ....

    --8<---------------cut here---------------end--------------->8---
    Andrea Crotti, Apr 10, 2011
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ZackS
    Replies:
    5
    Views:
    6,780
    Just an Illusion
    Jul 9, 2004
  2. SpamProof
    Replies:
    3
    Views:
    636
    SpamProof
    Dec 1, 2003
  3. dave
    Replies:
    5
    Views:
    580
    William Brogden
    Jul 17, 2004
  4. Tim Smith
    Replies:
    2
    Views:
    841
    Tim Smith
    Dec 15, 2004
  5. Bartholomew Simpson

    class design/ design pattern question

    Bartholomew Simpson, Jun 12, 2007, in forum: C++
    Replies:
    2
    Views:
    440
    Daniel T.
    Jun 12, 2007
Loading...

Share This Page