Package mosp :: Module group
[hide private]
[frames] | no frames]

Source Code for Module mosp.group

  1  """Group persons for better handling and the ease of use""" 
  2   
  3  from heapq import heappush 
  4   
  5  __author__ = "F. Ludwig" 
  6  __maintainer__ = "B. Henne" 
  7  __contact__ = "henne@dcsec.uni-hannover.de" 
  8  __copyright__ = "(c) 2010-2011, DCSec, Leibniz Universitaet Hannover, Germany" 
  9  __license__ = "GPLv3" 
10 11 12 -class Message(object):
13 """Encapsulates a single methods that will be executed by the simulation. 14 15 For example used to implement Person interaction/communication as the zombie infect. 16 @author: F. Ludwig""" 17 18 sim = None 19 """the simulation this Message will be executed by (in mosp.core.Simulation.run)""" 20
21 - def __init__(self, func, args, kwargs, delay):
22 """Initializes Message object and 23 schedules it for execution by mosp.core.Simulation.run() 24 @param func: method to be executed if this Message is called 25 @param args: arguments for func 26 @param kwargs: keyword arguments for func 27 @param delay: tick delay before func is called""" 28 self.func = func #: method to be called 29 self.args = args #: arguments for self.func 30 self.kwargs = kwargs #: keyword arguments for self.func 31 self.time = self.sim.now() + delay #: execution time of self.func 32 heappush(self.sim.messages, self) # schedule for execution
33
34 - def __call__(self):
35 """If Message is finally called, execute self.func 36 with its arguments and keyword arguments""" 37 self.func(*self.args, **self.kwargs)
38
39 40 -class Call(object):
41 """Encapsulates a Call of a method for a group of objects/Persons with a given delay. 42 @author: F. Ludwig""" 43
44 - def __init__(self, group, func_name, delay):
45 """If Call is instantiated, group is set up, func_name and delay is stored.""" 46 self.group = group #: the group of things for each the method func_name will be executes on 47 self.func_name = func_name #: the name of the method that will be executed for each group member 48 self.delay = delay #: the execution delay of the method func_name in ticks
49
50 - def __call__(self, *args, **kwargs):
51 """If Call instance is called, for all members of self.group a Message instance 52 is created and stored to schedule the execution of self.func_name 53 after self.delay ticks. 54 @param args: arguments for the methods called func_name to be executed 55 @param kwargs: keyword arguments for the methods called func_name to be executed""" 56 for pers in self.group: 57 Message(getattr(pers, self.func_name), args, kwargs, self.delay)
58
59 60 -class CallGroup(object):
61 """A grouping object containing a group of things (typically a PersonGroup(set)), 62 which if a generic attribute (method) is requested, returns a Call instance for 63 the group and this attribute=method that shall be called. 64 @author: F. Ludwig"""
65 - def __init__(self, group, delay=1):
66 """Inits the CallGroup, setup group and execution delay. 67 @param group: the group for which a methods shall be executed 68 @param delay: execution delay in ticks""" 69 self.group = group 70 self.delay = delay
71
72 - def __getattr__(self, func_name):
73 """If any func_name is requested, return an instance of Call 74 with the group, the function name and the defined execution delay as parameter. 75 @param func_name: generic requested function that shall be called for the group""" 76 return Call(self.group, func_name, self.delay)
77
78 - def __call__(self, delay):
79 """If the CallGroup is called, setup the execution delay and return the CallGroup. 80 @param delay: execution delay in ticks""" 81 self.delay = delay 82 return self
83
84 85 -class PersonGroup(set):
86 """A set of Persons. 87 @author: F. Ludwig"""
88 - def filter(self, **kwargs):
89 """Filter Persons in PersonGroup by instances' attributes 90 91 Can only filter on equality of key-value-pair.""" 92 re = PersonGroup() 93 for pers in self: 94 for key, value in kwargs.items(): 95 if hasattr(pers, key) and getattr(pers, key) == value: 96 re.add(pers) 97 return re
98 99 @property
100 - def call(self):
101 """Returns a CallGroup with group=self=this PersonGroup.""" 102 return CallGroup(self)
103