Skip to content

Instantly share code, notes, and snippets.

@KitWallace
KitWallace / lazy stations
Created August 21, 2012 07:46
lazy instantiation of objects and object reference by name
stations = {}
def station(name) :
try :
station = stations[name]
return station
except :
station_item = menu.item(name)
url = station_item.getAttribute("url")
refresh = station_item.getAttribute("refresh")
station = Weather(name,url,refresh)
@KitWallace
KitWallace / mark_ids
Created August 21, 2012 07:40
Mark id attributes as IDs
def mark_ids(node) :
""" mark id attributes as IDs so that getElementById() works
"""
try:
node.setIdAttribute("id")
except:
pass
for child in node.childNodes :
@KitWallace
KitWallace / simple_clock.py
Created August 19, 2012 10:22
A simple talking clock
#!/usr/bin/python
import time
import speak
while True :
message = "The Raspberry Pi time is " + speak.escape_XML(speak.ssml_break(500)) + time.strftime('%I %M %p')
speak.say(message)
time.sleep(60)
@KitWallace
KitWallace / run_menu.py
Created August 18, 2012 01:03
Execute actions in the yacht menu
#!/usr/bin/python
""" talking yacht
"""
import sys
import speak
# application specific imports
import time
@KitWallace
KitWallace / menu.py
Created August 18, 2012 00:35
Menu class supporting the Presenter
#!/usr/bin/python
from xml.dom.minidom import parse
import presenter
from xmlutils import remove_whitespace_nodes
class Menu :
""" a menu represents a menu and its current status
attributes
@KitWallace
KitWallace / say_menu.py
Created August 18, 2012 00:24
Walk through a menu with the presenter
#!/usr/bin/python
import sys
import speak
from menu import *
def visit(item) :
text = item.getAttribute('title')
speak.say(text)
print text
@KitWallace
KitWallace / yacht_menu.xml
Created August 18, 2012 00:22
Example menu
<menu name="yacht" title="Main yacht menu">
<item title="Navigation">
<item title="Position"/>
<item title="Speed over ground"/>
<item title="Heading"/>
</item>
<item title="Weather">
<item title="Windspeed"/>
<item title="Barometer"/>
</item>
@KitWallace
KitWallace / talk_presenter.py
Created August 17, 2012 23:21
Testing the presenter
#!/usr/bin/python
import speak
import presenter
for key in presenter.read_key() :
print key
speak.say(key)
@KitWallace
KitWallace / presenter.py
Created August 17, 2012 23:09
Read the Labtec Presenter keys
#!/usr/bin/python
import sys
import tty
def read_key() :
tty.setcbreak(sys.stdin)
last = 0
while True :
code = ord(sys.stdin.read(1))
@KitWallace
KitWallace / simple_clock.py
Created August 17, 2012 22:38
A simple talking clock
#!/usr/bin/python
import time
import speak
while True :
message = "The Raspberry Pi time is " + speak.escape_XML(speak.ssml_break(500)) + time.strftime('%I %M %p')
speak.say(message)
time.sleep(60)