From 5fb3ea060025241105dc8e9a174513c112f9a133 Mon Sep 17 00:00:00 2001 From: rcoh Date: Thu, 27 Jan 2011 16:50:59 -0500 Subject: A metric $#%$-ton of changes. Added doc-strings to EVERYTHING. Phew. Fixed a massive bug that increases performance in by up to a factor of 60. A bunch of new behaviors for the class. --- operationscore/Behavior.py | 18 ++++++++++-------- operationscore/Input.py | 14 ++++++-------- operationscore/PixelEvent.py | 7 +++---- operationscore/PixelMapper.py | 8 ++++++-- operationscore/Renderer.py | 8 ++++---- operationscore/SmootCoreObject.py | 9 ++++++--- operationscore/ThreadedSmootCoreObject.py | 2 ++ 7 files changed, 37 insertions(+), 29 deletions(-) (limited to 'operationscore') diff --git a/operationscore/Behavior.py b/operationscore/Behavior.py index b3f7342..6424842 100644 --- a/operationscore/Behavior.py +++ b/operationscore/Behavior.py @@ -1,11 +1,3 @@ -#Abstract class for a behavior. On every time step, the behavior is passed the -#inputs from all sensors it is bound to as well as any recursive inputs that it -#spawned during the last time step. Inheriting classes MUST define -#processResponse. processResponse should return a list of dictionaries which -#define the properties of the light response, (outputs, recursions). They must give a location and -#color. They may define a PixelEvent to more closely control the outgoing -#data, however, this is normally handled by routing the event to a behavior -#specifically designed to do this (like DecayBehavior). import pdb from operationscore.SmootCoreObject import * @@ -13,6 +5,16 @@ from logger import main_log #timeStep is called on every iteration of the LightInstallation #addInput is called on each individual input received, and the inputs queue class Behavior(SmootCoreObject): + """Abstract class for a behavior. On every time step, the behavior is passed the + inputs from all sensors it is bound to as well as any recursive inputs that it + spawned during the last time step. Inheriting classes MUST define + processResponse. processResponse should return a list of dictionaries which + define the properties of the light response, (outputs, recursions). They must give a location and + color. They may define a PixelEvent to more closely control the outgoing + data, however, this is normally handled by routing the event to a behavior + specifically designed to do this (like AddPixelEvent). + timeStep is called on every iteration of the LightInstallation + addInput is called on each individual input received, and the inputs queue""" def init(self): self.validateArgs('Behavior.params') if type(self['Inputs']) != type([]): diff --git a/operationscore/Input.py b/operationscore/Input.py index 69375d3..d3d5644 100644 --- a/operationscore/Input.py +++ b/operationscore/Input.py @@ -1,16 +1,14 @@ import threading,time from logger import main_log, exception_log from operationscore.ThreadedSmootCoreObject import ThreadedSmootCoreObject -#Abstract class for inputs. Inheriting classes should call "respond" to raise -#their event. Inheriting classes MUST define sensingLoop. Called at the -#interval specified in RefreshInterval while the input is active. For example, if you are writing -#webserver, this is where the loop should go. -#Inheriting classes MAY define inputInit. This is called before the loop -#begins. import pdb class Input(ThreadedSmootCoreObject): - #Event scope is a function pointer the function that will get called when - #an Parent is raised. + """Abstract class for inputs. Inheriting classes should call "respond" to raise + their event. Inheriting classes MUST define sensingLoop. Called at the + interval specified in RefreshInterval while the input is active. For example, if you are writing + webserver, this is where the loop should go. + Inheriting classes MAY define inputInit. This is called before the loop + begins.""" def init(self): self.eventQueue = [] if not 'RefreshInterval' in self.argDict: diff --git a/operationscore/PixelEvent.py b/operationscore/PixelEvent.py index c41df17..5ed6163 100644 --- a/operationscore/PixelEvent.py +++ b/operationscore/PixelEvent.py @@ -1,6 +1,6 @@ -#Class defining a light response. Inheriting classes should define lightState, -#which should return a color, or None if the response is complete. Consider -#requiring a generate event. +"""PixelEvent is a class defining a light response. Inheriting classes should define state, +which should return a color, or None if the response is complete. Consider +requiring a generate event.""" from operationscore.SmootCoreObject import * from pixelevents.StepEvent import * import util.ColorOps as color @@ -26,6 +26,5 @@ class PixelEvent(SmootCoreObject): color = responseDict['Color'] else: raise Exception('Need Color. Probably') - pdb.set_trace() responseDict['PixelEvent'] = StepEvent.generate(300, color) diff --git a/operationscore/PixelMapper.py b/operationscore/PixelMapper.py index 7e2b0af..3937973 100644 --- a/operationscore/PixelMapper.py +++ b/operationscore/PixelMapper.py @@ -2,6 +2,9 @@ from operationscore.SmootCoreObject import * from logger import main_log import pdb class PixelMapper(SmootCoreObject): + """PixelMapper is the parent class for PixelMappers. Inheriting classes should define + mappingFunction which takes an eventLocation and a screen and returns a list of (weight, pixels). PixelMapper + handles caching automatically.""" def init(self): self.mem = {} #Dictionary of all seen events self.totalCalls = 0 @@ -19,6 +22,7 @@ class PixelMapper(SmootCoreObject): return self.mem[eventLocation] #Takes a Screen and returns a list of tuples #(pixel, weight), with the sum of weights = 1 - #TODO: consider abstracting away from pixels def mappingFunction(self,eventLocation, screen): - pass + """Takes a Screen and event location and returns a list of tuples (pixel,weight) with + sum(weights)=1""" + raise Exception('Mapping function not defined!') diff --git a/operationscore/Renderer.py b/operationscore/Renderer.py index b422304..0861c44 100644 --- a/operationscore/Renderer.py +++ b/operationscore/Renderer.py @@ -1,10 +1,10 @@ -#Renderer abstract class. Doesn't do much now, but might do more later. -#Inheriting classes MUST define render which takes a light system and renders it. -#Inheriting classes may define initRenderer which is called after the dictionary -#is pulled from config. #TODO: multithreaded-rendering from operationscore.SmootCoreObject import * class Renderer(SmootCoreObject): + """Renderer abstract class. Doesn't do much now, but might do more later. + Inheriting classes MUST define render which takes a light system and renders it. + Inheriting classes may define initRenderer which is called after the dictionary + is pulled from config.""" def init(self): self.initRenderer() def render(lightSystem): diff --git a/operationscore/SmootCoreObject.py b/operationscore/SmootCoreObject.py index 8b36f4d..0d32773 100644 --- a/operationscore/SmootCoreObject.py +++ b/operationscore/SmootCoreObject.py @@ -4,6 +4,10 @@ import thread import util.Config as configGetter class SmootCoreObject(object): + """SmootCoreObject is essentially a super-object class which grants us some niceties. It allows + us to use objects as if they are dictionaries -- we use this to store their arguments + convienently -- note that querying for a parameter that does not exist will return None. It + also offers some basic ThreadSafety.""" def __init__(self, argDict, skipValidation = False): self.dieListeners = [] self.argDict = argDict @@ -13,8 +17,6 @@ class SmootCoreObject(object): for key in argDict: setattr(self, key, argDict[key]) self.init() #call init of inheriting class - # self.__setitem__ = self.argDict.__setitem__ - # self.__getitem__ = self.argDict.__getitem__ def init(self): pass @@ -37,7 +39,8 @@ class SmootCoreObject(object): return self.argDict[item] else: return None - + def __contains__(self, item): + return item in self.argDict def __getiter__(self): return self.argDict.__getiter__() diff --git a/operationscore/ThreadedSmootCoreObject.py b/operationscore/ThreadedSmootCoreObject.py index 967ee35..4b5d513 100644 --- a/operationscore/ThreadedSmootCoreObject.py +++ b/operationscore/ThreadedSmootCoreObject.py @@ -4,6 +4,8 @@ import thread import util.Config as configGetter from operationscore.SmootCoreObject import SmootCoreObject class ThreadedSmootCoreObject(SmootCoreObject, threading.Thread): + """ThreadedSmootCoreObject is a version of SmootCoreObject for objects that want to run on their + own thread""" def __init__(self, argDict, skipValidation = False): SmootCoreObject.__init__(self, argDict, skipValidation) threading.Thread.__init__(self) -- cgit v1.2.3