Skip to content

Instantly share code, notes, and snippets.

@KitWallace
KitWallace / trend.py
Created August 30, 2012 08:15
using a cyclic sequence to model a limited history
def __init__(self) :
....
self.history = Moving_Sequence(n)
...
def monitor...
for baro in reader:
...
self.history.add(baro)
...
@KitWallace
KitWallace / getstate.py
Created August 30, 2012 08:01
overridding getstate so that history is dropped from the pickled object
def __getstate__(self) :
odict = self.__dict__.copy()
del odict['history']
return odict
@KitWallace
KitWallace / baroMonitor.py
Created August 30, 2012 07:54
the monitor method of barometer
def monitor(self) :
reader = baroSmoother(self.interval_secs,self.smooth_secs)
for self.baro in reader :
self.history.add(self.baro)
self.updateTrend()
self.put()
@KitWallace
KitWallace / baroSmoother.py
Created August 30, 2012 07:53
Smooth readings
def baroSmoother(interval_secs,smooth_secs) :
baro = baroReader(interval_secs)
max = int(smooth_secs / interval_secs)
while True:
total_baro = 0
for i in range(max) :
reading = baro.next()
total_baro += reading
average_baro = round(total_baro / max, 2)
yield(average_baro)
@KitWallace
KitWallace / baroReader.py
Created August 30, 2012 07:51
barometer reader
def baroReader(interval_secs) :
""" dummy reader until the chip is added to the RPi
generator
"""
station = Weather("horfield","http://www.martynhicks.co.uk/weather/clientraw.txt")
while True :
station.refresh()
if station.baro is None :
time.sleep(10)
else :
@KitWallace
KitWallace / weather-extract.py
Created August 22, 2012 09:20
Part 2 - cache invalidation
import time
...
def __init__(self,id,url,rate) :
self.url = url
self.id = id
self.ts = 0
self.rate = int(rate)
self.refresh()
@KitWallace
KitWallace / invert.xq
Created August 22, 2012 07:29
Recursive invert a stick
declare function local:invert($stick) {
let $leaf := $stick//*[last()]
return local:invert1($leaf)
};
declare function local:invert1($leaf) {
if (exists($leaf))
then element {name($leaf)} {
local:invert1($leaf/..)
}
else ()
@KitWallace
KitWallace / invert.xq
Created August 21, 2012 23:52
invert a stick (a tree wih no branches) Challenge 2
declare function local:stick_to_seq($stick) {
if ($stick)
then (name($stick),local:stick_to_seq($stick/*))
else ()
};
declare function local:seq_to_stick($seq) {
if (exists($seq))
then element {$seq[1]} {local:seq_to_stick(subsequence($seq,2))}
else ()
@KitWallace
KitWallace / maxdepth.xq
Created August 21, 2012 22:37
Challenge 1
declare function local:max_depth($node) {
if (exists($node))
then max($node/*/(local:max_depth(.) + 1))
else 0
};
let $n := <a> dsds<b>dsd<c/>sdd</b><b><c>sds<d/>dsd</c></b></a>
return local:max_depth($n)
@KitWallace
KitWallace / expansion.py
Created August 21, 2012 08:05
expanding abbreviations for TTS
# in weather
substitutes = {
"W":"West","N":"North","S":"South","E":"East",
"km/h":"kilometers per hour","C":"Celsius","mph":"miles per hours",
"Mon":"Monday","Tue":"Tuesday","Wed":"Wednesday","Thu":"Thursday","Fri":"Friday","Sat":"Saturday","Sun":"Sunday",
"NNE":"Nor Nor East","NE":"Nor East","ENE":"East Nor East","ESE":"East Sow East",
"SE":"Sow East" ,"SSE" :"Sow Sow East", "SSW":"Sow Sow West","SW":"Sow West","WSW":"West Sow West",
"WNW":"West Nor West","NW":"Nor West","NNW":"Nor Nor West",
"%":"percent"