aboutsummaryrefslogtreecommitdiff
path: root/util
diff options
context:
space:
mode:
authorGravatar Russell <rcoh@rcoh.(none)>2011-01-10 12:21:19 -0500
committerGravatar Russell <rcoh@rcoh.(none)>2011-01-10 12:21:19 -0500
commitcbdc42af021f898e82d3e78ce7c636d3fb5eece0 (patch)
treef36ca72463702c104dd02f026e9fbcd6de3f0da8 /util
parent1679719e7ca8ce433c5714474a32c926161dc5b8 (diff)
Some performance improvements. Faster evaluation of range-based queries with
lambda expressions. Faster exp with approximated fastexp. Some changes to the component registry.
Diffstat (limited to 'util')
-rw-r--r--util/ComponentRegistry.py47
-rw-r--r--util/Geo.py6
-rw-r--r--util/PacketComposition.py15
3 files changed, 39 insertions, 29 deletions
diff --git a/util/ComponentRegistry.py b/util/ComponentRegistry.py
index 119ce18..0518f56 100644
--- a/util/ComponentRegistry.py
+++ b/util/ComponentRegistry.py
@@ -1,43 +1,40 @@
import pdb
-#component registry, a singleton
-import thread
-#class ComponentRegistry:
-# def __init__(self):
-# self.regDict = {}
-# @staticmethod
-# def getRegistry(self):
-# if self.instance == None:
-# self.instance = self.__class__()
-# return self.instance
-# def registerComponent(component, cid=None):
-# if cid != None:
-# globals()['Registry'][cid] = component
-# else:
-# try:
-# cid = component['Id']
-# globals()['Registry'][cid] = component
-# except:
-# raise Exception('Must specify Id, component did not store it')
-#def registerDefault(
+import hashlib
+from logger import main_log
+#TODO: make component registry a singleton
+def initRegistry():
+ #TODO: don't overwrite existing registry
+ globals()['Registry'] = {}
+
+def clearRegistry():
+ initRegistry()
def removeComponent(cid):
globals()['Registry'].pop(cid)
def getComponent(cid):
return globals()['Registry'][cid]
#Registry of all components of the light system
#TODO: pick a graceful failure behavior and implement it
-def initRegistry():
- globals()['Registry'] = {}
def registerComponent(component, cid=None):
if cid != None:
globals()['Registry'][cid] = component
else:
try:
cid = component['Id']
- globals()['Registry'][cid] = component
- except:
- raise Exception('Must specify Id, component did not store it')
+ except KeyError:
+ cid = getNewId()
+ component['Id'] = cid
+ main_log.debug(cid + 'automatically assigned')
+ globals()['Registry'][cid] = component
+ return cid
#def registerDefault(
def removeComponent(cid):
globals()['Registry'].pop(cid)
def getComponent(cid):
return globals()['Registry'][cid]
+def getNewId():
+ trialKey = len(globals()['Registry'])
+ trialId = hashlib.md5(str(trialKey)).hexdigest()
+ while trialId in globals()['Registry']:
+ trialKey += 1
+ trialId = hashlib.md5(str(trialKey)).hexdigest()
+ return trialId
diff --git a/util/Geo.py b/util/Geo.py
index a9243de..be3e93e 100644
--- a/util/Geo.py
+++ b/util/Geo.py
@@ -4,7 +4,6 @@ from bisect import *
import random
def pointWithinBoundingBox(point, bb): #this could be in 4 lines, but I'm lazy.
return sum([(point[i % 2] <= bb[i]) == (i>1) for i in range(4)]) == 4
-print pointWithinBoundingBox((118,21), (10,8,298,42))
def addLocations(l1,l2):
return tuple([l1[i]+l2[i] for i in range(len(l1))])
def gaussian(x,height,center,width):
@@ -13,9 +12,12 @@ def gaussian(x,height,center,width):
c=width
return a*math.exp(-((x-b)**2)/(2*c**2))
def dist(l1, l2):
- return math.sqrt(sum([(l1[i]-l2[i])**2 for i in range(len(l1))]))
+ return math.sqrt((l1[0]-l2[0])**2+(l1[1]-l2[1])**2)
+ #return math.sqrt(sum([(l1[i]-l2[i])**2 for i in range(len(l1))]))
def randomLoc(boundingBox): #TODO: make less shitty
loc = []
loc.append(random.randint(0, boundingBox[0]))
loc.append(random.randint(0, boundingBox[1]))
return tuple(loc)
+def approxexp(x):
+ return 1+x+x**2/2+x**3/6
diff --git a/util/PacketComposition.py b/util/PacketComposition.py
index b323d54..3574170 100644
--- a/util/PacketComposition.py
+++ b/util/PacketComposition.py
@@ -22,14 +22,25 @@ def composePixelStripData(pixelStrip,currentTime=timeops.time()):
#color = pixelStrip.pixels[i].state()
#packet[i:i+2] = color
# return bytearray(packet)
-def composePixelStripPacket(pixelStrip,port, currentTime):
+cache = {}
+def memoize(f):
+ def helper(x):
+ if x not in cache:
+ cache[x] = f(x)
+ return cache[x]
+ return helper
+@memoize
+def cachePacketHeader(port):
packet = bytearray()
- data = composePixelStripData(pixelStrip, currentTime)
subDict = dict(kinetDict)
subDict['len'] = 38000 #I have no idea why this works.
subDict['port'] = port
packet.extend(kinetPortOutPacket(subDict))
packet.append(0x0)
+ return packet
+def composePixelStripPacket(pixelStrip,port, currentTime):
+ packet = bytearray(cachePacketHeader(port))
+ data = composePixelStripData(pixelStrip, currentTime)
packet.extend(data)
return packet
def kinetHeader():