A complete and utter starter Python script

A friend said that it was the duty of a computer scientist to learn a new computer language a year and asked me to introduce him to Python.

Ignoring the fact that he’ll probably outstrip my two-year-old Python abilities in about a week, I’ll drop one of my first Python scripts here in case he or anyone else learns something.

I learnt Python in the first place to solve a problem I was having getting a GPS to talk to Pure Data (pd), namely that pd was not dealing with string splitting and parsing very well. This first script reflects that by having an NMEA sentence as its example but it could be any comma separated string.

#!/usr/bin/env python 
# splittest.py
#
# A very simple python script to show string splitting to
# a set of named variables
#
# One of the first scripts I wrote to get my head around how you might
# begin to parse an NMEA sentence from a GPS
# 
# Put this anywhere, call it splittest.py and run from the terminal
# with $ python splittest.py
#
# Daniel Belasco Rogers 2008, 2011

# declare string
line = '$GPSGV, 234932, A, 2345999940, 234, 9, 23, 593'

# print it to the terminal
print 'original string:'
print line
print

# assign a set of variables
header, one, two, three, four, five, six, seven = line.split(',')

# print these
print 'list of variables:'
print header, one, two, three, four, five, six, seven

# as a separate excercise, show that .split makes a list and iterate
# over the list

stringlist = line.split(',')

# notice the \n newline character at the beginning of the print string
# to force a newline first
print '\nstringlist =', stringlist 

# iterate over list and print each item
print '\nlist of items:'
for item in stringlist:
    print item
Posted in Code, GPS, Python | Comments Off on A complete and utter starter Python script

Sap Rising

Just before it got colder (this morning), I could have sworn that Spring was in the air. I went for a walk yesterday with my Nordic sticks in Treptower Park and it was not half as bleak mid Winter as I’d thought. Green leaves poking through – something in the Geranium family with round leaves under the trees – Herb Robert? Even some not so frost-bitten looking Celandine (that stuff is really year-round). And I remembered to wonder – how can I combine my love for plants with coding?

No idea yet but this is a peg in the ground (blogosphere) so I can come back and say what I thought of

Posted in Diary | Tagged , , | Comments Off on Sap Rising

Hmm, not so sure about last post

Already come a cropper, hoist by my own petard, etc etc

I put a warning in my code that its a hack and I’ve already seen its limitation. Basically, I really need to re-write it getting to grips with the xml and not just hacking away at the file as if it’s just a set of strings. [LATER EDIT: please don’t use this script – see here for an improved version] After trying to name a gpx file that was produced in a different way to my normal ones (normal = from a Garmin etrex, read through GPSbabel. Different = from a Qstarz Datalogger, read through BT-747), I found myself having to use the albeit brilliant xml parser I seem to have built into this Ubuntu box (see Validate GPX file in Linux here) and manually check the tags on a huge file.

Still, Soph is currently doing a lot of unpicking of her new knitting, so I feel pretty on a par.

Posted in Diary, GPS, Linux | Tagged , , | Comments Off on Hmm, not so sure about last post

Python script to rename all tracknames in a gpx file with the first trackpoint date

[LATER EDIT: please don’t use this script – see here for an improved version]
At last, I’m posting here what I think to be a useful script. It is, however, more of a hack since for brevities sake and lack of experience, I just used Python’s string operations to do the job of taking the first trackpoint timestamp of a track and using that as the name of the track. The script was prompted by the useless names that get placed into the track names by either the garmin unit itself – thinks like ACTIVELIST#1 etc or even worse, every track in a gpx file has the name ‘track’. Having a timestamp as the name of each track at least means that in something like Viking, you can see each tracks timestamp in order which helps a lot.

You can download the file here renameTracks.py (right click and Save Link As)

Posted in Code, Diary, GPS, Python | Tagged | Comments Off on Python script to rename all tracknames in a gpx file with the first trackpoint date

Install an Android APK file

I asked Peter to write us an Android application for reporting our mood at periodic intervals to go with recording everywhere we go with a GPS and saving all our text messages. Eventually, we could geo-locate these moods and match them up with any text messages we might be writing.

Again, he came up trumps and managed to produce a very good application in a matter of days which I was able to tailor to suit just our requirements – it’s been amazing to work with him and I hope the relationship continues to develop. There are two mood tracking applications for Android at the time of writing, one called ‘Mood Tracker’ is only partially translated and gives you feedback in some kind of Asian script, the other is from the American Army (I kid you not), T2 Mood Tracker and has very sophisticated methods for setting up your own categories but they both lack ways of getting this data out. They both produce graphs, but the T2 mood tracker doesn’t report the numbers behind what you rated, which makes it pretty much useless for all its gloss and the much more basic Asian one, even though it reports the moods with number indicators, doesn’t let you export this report. Hence Peter’s app.

Anyhow, I always forget how to install the .apk files that I download from Peter’s Redmine project management system server so I thought I’d note here how I did it (didn’t know I was going to waffle on this much though)

cd to the ‘platform-tools’ directory you unpacked the Android SDK in:
~$ cd ~/bin/android-sdk-linux_86/platform-tools
then
~/bin/android-sdk-linux_86/platform-tools$ ./adb install [path/to/application].apk
which for me was
~/bin/android-sdk-linux_86/platform-tools$ ./adb install ~/bin/moodtracker.apk
NB obviously, you just enter the command after the $ prompt

Posted in Android, Code, Software | Comments Off on Install an Android APK file