def __setattr__(self, key, value): # May have to optimize this later if protocol.active and (key in self._fields): field = self._fields[key] # Fix this ugliness? if hasattr(field, 'from_string'): value = field.from_string(value) original = getattr(self, key) if original != value: # maybe ignore completely? self._fields_previous[key] = original qvalue = field.quantize(value, original) # Tell the world the value has changed if qvalue is not None: # Again, experimental: qvalue = field.externalize(qvalue) output = filter(lambda x: x is not None, (self.stream_name, key, self.id(), qvalue)) # o = ' '.join(output) log.debug('%s: %r <- %r' % (key, qvalue, original)) protocol.send(output) object.__setattr__(self, key, value)
def __init__(self, simclass, simmodule=None): self.is_setup = False self.module = simmodule or sys.modules[simclass.__module__].__file__ self.simclass = simclass self.sim = simclass() log.info("Loading simulation %s" % simclass.__name__) # protocol.active = False protocol.active = True protocol.greet() # dirty hack to test the concept: protocol.send('sim name %s' % simclass.__name__) if simclass.__doc__: protocol.send(['sim', 'doc', simclass.__doc__]) protocol.flush(lambda x: ['preamble', x])
def sim_init(self, force=False): if force or not self.is_setup: self.sim.__init__() self.sim.setup() self.is_setup = True self.steppables = {} self.steps = [] self.stepnum = 0 # initialize steps if not self.sim.steps: raise Exception("Must specify simulation steps") else: for step in self.sim.steps: if isinstance(step, (list, tuple)): step, iterfun = step else: if inspect.ismethod(step) and step.im_self is not None: # a bounded method self.steppables[step.im_class] = [step.im_self].__iter__ self.steps.append(step) continue # raise Exception("Only unbounded methods can be steps") if issubclass(step.im_class, Agent): # multiple agent types supported, step() will filter iterfun = self.sim.agents.__iter__ elif issubclass(step.im_class, Cell): iterfun = self.sim.space.cells else: raise Exception('Unknown step class, must provide an iterator') self.steppables[step.im_class] = iterfun self.steps.append(step) log.debug("Steps: %s", self.steps) if self.sim.space: protocol.send('sim space grid'.split() + [self.sim.space.dimensions], ) protocol.flush(lambda x: ['preamble', x])
def wrapper(self, *args, **kw): try: return meth(self, *args, **kw) except Exception, e: protocol.send(['ERROR', str(e)], compress=False) raise
def __fire__(self, keys=None): keys = keys or self._fields.keys() for key in keys: protocol.send([self.stream_name, key, self.id(), original])