Tag Archives: python

Moving RPizeBox to git

500D_1901

Now I have the proof of concept for the RPi, I wanted to improve the code and have long term plans to make it remember tracks we skip, as well as allowing a blacklist!  I wanted to keep some sort of version control, and this is where git comes in.  I have been aware of git for a while but don’t really know much about it.

I now have created my own git hub for RPizeBox at https://github.com/thomaswwp/RPizeBox. I also forked PyLMS (which appears to be abandoned) and will eventually include it in my build as a module, rather than simply importing the code.

I have reduced the code to two scripts, but this looks like it has increased the CPU overhead to about 6%, so I may revert to what I had before.

I have also made a Fritzing diagram of the setup for future reference.

RPizeBox_bb

I also found that the skip forward button was broken on the remote, so just remapped other buttons.

500D_1902

 

Notes on RPizeBox Display

I’ve got a nice scrolling script and had to hack pylms to get it to cope with accents.

This meant uninstalling the pylms module and referencing it instead with

sys.path.append(‘/home/pi/PyLMS/’)
from pylms.server import Server # to talk to LMS server
from pylms.player import Player # to talk to LMS player

That meant I could hack away at the code and it would still work.

I kept trying to find a way to intialise the display on boot.  I had little success.  I even made a start/stop script at /etc/init.d/lms-display in the hope it would get the display restarting on boot, but no dice.  In the end I found that

@reboot root /etc/init.d/lms-display start

in crontab did the job.

The Python Challenge – 7

This was pretty easy.  The hardest thing was realising I needed the PIL image library and then installing it.  Thank you to Samson for the link to the PIL download page.  Once up and running it was pretty easy to pick the grey squares and decode their RGB value to a letter…

The code was the same for both this time since I opted to open the page directly and not use print.

Python 2.7 and 3.2.3 code

#smarty?
from PIL import Image
import re, webbrowser
im = Image.open("oxygen.png")
#get height and width
xy = im.size
height = xy[1]
width = xy[0]
#grey lines appear to go down the middle, assume that the value corresponds to a letter and boxes are 7 square
count = 3
message = ""
while count < width-21:
pix = im.getpixel((count,45))
count += 7
message += chr(pix[0])
regex = r'((?<=\[).*(?=\]))'
numbers = re.findall(regex, message)
letters = numbers[0].split(", ")
final = ""
for letter in letters:
final += chr(int(letter))
webbrowser.open("http://www.pythonchallenge.com/pc/def/" + final + ".html",new=2)













The Python Challenge – 6

This was fun, using python to access zip files. I based it mostly on #4 with a small twist at the end to print out the final answer. The obvious answer didn’t work – I thought I was looking at challenge 7 but realised after banging my head against the wall for a while that I was still on number 6. What was really annoying was that I had already noticed the correct word whilst fiddling with the code.

****************************************************************
****************************************************************
** **
** OO OO XX YYYY GG GG EEEEEE NN NN **
** OO OO XXXXXX YYYYYY GG GG EEEEEE NN NN **
** OO OO XXX XXX YYY YY GG GG EE NN NN **
** OOOOOOOO XX XX YY GGG EEEEE NNNN **
** OOOOOOOO XX XX YY GGG EEEEE NN **
** OO OO XXX XXX YYY YY GG GG EE NN **
** OO OO XXXXXX YYYYYY GG GG EEEEEE NN **
** OO OO XX YYYY GG GG EEEEEE NN **
** **
****************************************************************
**************************************************************

2.7 Solution

#zip?
import zipfile, re, sys
data = zipfile.ZipFile("channel.zip")
#data is now some sort of object
#data.namelist() is all the files in the archive

count = 1
final = []

def getNextCode(lastCode):
while True:
try:
myfile = data.open(lastCode + '.txt')
myinfo = data.getinfo(lastCode + '.txt')
final.append(myinfo.comment)
code = re.findall('(?<=Next nothing is )\w*',myfile.read())
#print "Code %i is %s. \nPage read was '%s.txt'\n" % (count, code[0], lastCode)
#print "Code %i\n" % (count)
return code
except (IndexError, NameError):
print "Error" #: Page message was '%s'\nPage was: '%s.txt'." % (page,lastCode)
return False

nextCode = getNextCode('90052')

for count in range (1,len(data.namelist())- 2):
nextCode = getNextCode(nextCode[0])


myfile = data.open(nextCode[0] + '.txt').read()

#Collect the comments.
for character in final:
sys.stdout.write(character)
#look at the message and think about it...

3.2.3 Solution 

#zip?
import zipfile, re, sys
data = zipfile.ZipFile("channel.zip")
#data is now some sort of object
#data.namelist() is all the files in the archive

count = 1
final = []

def getNextCode(lastCode):
while True:
try:
myfile = data.open(lastCode + '.txt')
myinfo = data.getinfo(lastCode + '.txt')
final.append(myinfo.comment)
code = re.findall(r'(?<=Next nothing is )\w*',myfile.read().decode("utf-8"))
return code
except (IndexError, NameError):
print ("Error")
return False

nextCode = getNextCode('90052')

for count in range (1,len(data.namelist())- 2):
nextCode = getNextCode(nextCode[0])


myfile = data.open(nextCode[0] + '.txt').read()

#Collect the comments.
for character in final:
sys.stdout.write(character)

The Python Challenge – 5

A bit annoyed about this one.  The Peak Hell clue was completely lost on me and in searching I came across another blog that had all the solutions.  I got sucked in and saw what the end game was.  I guess I would have worked it out pretty quickly anyway.

The banner.p name gave it away too I guess. Serialization the python way was easy  enough.

2.7 Solution

#pickle?
import pickle
data = pickle.load( open( "banner.p", "rb" ) )

mycount = 0

for line in data:
myline = ""
for item in line:
counter = 0
mycount = int(item[1])
while counter myline += item[0]
counter += 1
print myline

3.2.3 Solution 

#pickle?
import pickle
data = pickle.load( open( "banner.p", "rb" ) )

mycount = 0

for line in data:
myline = ""
for item in line:
counter = 0
mycount = int(item[1])
while counter myline += item[0]
counter += 1
print (myline)