Pushing up Python on Android
A few days ago, I put on the manliest voice I could muster and made an announcement to my wife: "Stand aside, woman! I am going to the gym!"
Needless to say, she was thoroughly unimpressed but somewhat amused when, half an hour later, she found me at the computer, programming.
Despite what she might tell you, I hadn't given up on exercising. You see, I had recently taken up the One Hundred Pushups, Two Hundred Situps and Two Hundred Squats programs. These involve a few sets of a varying number of repetitions each, with timed pauses between each one. So, for instance, on my first day I'd do 10 pushups, rest for 60 seconds, do 12 pushups, rest, 7 pushups, rest, 7 pushups, rest, and finally as many pushups as I can (but at least 9). These numbers of repetitions vary as you progress. My problem was keeping track of how many repetitions of which exercise to perform on any given day.
Now, there are nice PDFs with the whole exercise program on each site, but they're supposed to be printed. On paper. How low tech! Some people use spreadsheets, but... Meh. So I decided I should turn to my Android phone for help.
There is an iPhone app for One Hundred Pushups et al, and at first I considered writing an Android app to match. And I still do, but, of course, that's a full-on project, one that would definitely not be usable in time for me to exercise that night. So: pragmatic program, must be running in a very short time (my wife was laughing out loud, by then) and improve as need arises. This looks like a job for... Python!
Python is not (yet?) a first-class citizen on the Android, but it's a respectable second-class one, thanks to Damon Kohler and his Android Scripting Environment. ASE lets you run several interpreted languages on the Android, amongst them Python, Lua, Perl and JRuby. However, these are limited on what they can access on the Android API. More specifically, you can't build arbitrary user interfaces or create new activities (though you can invoke existing ones).
Still, having a Python interpreter on your mobile can be handy. I needed to input three sets of repetitions (one for each exercise program), and have Android let me know how many repetitions to do next, and for how long to rest between them. I'm still meddling with this code (trying to weigh making it better versus building a proper app versus actually, you know, exercising), this is just a quick hack I cooked up to get going, but it's growing on me. Anyway, here it is:
from time import sleep
import android
droid = android.Android()
# How long to rest between repetitions
rest = 60
# Warning before starting next round
wake = 10
# One line per exercise set, each number of repetitions separated by spaces
# For instance (pushups, situps, squats):
# 10 12 7 7 >=9
# 9 9 6 6 >=8
# 19 24 19 19 >=27
user_input = droid.getInput("Series", "Describe all repetition sets in your series:")
series = [i for i in user_input["result"].splitlines() if i.strip()]
def interval(theres_more=True, rest=rest, wake=wake):
droid.makeToast("Rest for %d seconds..." % rest)
sleep(rest - wake)
if theres_more:
droid.makeToast("Ready? %d seconds to start!" % wake)
droid.vibrate(500)
sleep(wake)
if theres_more:
droid.makeToast("Go!")
droid.vibrate(3000)
for s in series:
droid.getInput("New series!", "Ready?")
sets = s.split()
l = len(sets)
for n, repetitions in enumerate(sets):
droid.getInput(repetitions, '(press Ok when finished)')
interval(theres_more=(n+1<l))
droid.makeToast("w00t! Congratulations!")
(you can also download it here)
As explained in the comments, it initially expects lines containing the number of repetitions on each set. So, if I'm undertaking pushups, situps and squats (respectively), I might input:
10 12 7 7 >=9 9 9 6 6 >=8 19 24 19 19 >=27
Of course, ">=9" is not a number, but the script will use whatever you input there as labels for prompting you to perform your repetitions.
You'll notice that the script uses getInput for displaying messages when it expects the user to press "Ok" (even though it doesn't expect any typed input at all). That's because, currently, getInput is the only graphical widget provided by the the Python proxy for the Android API. But more on that later.
So, try it out (if you're willing to exercise at all, or if you're just curious), and let me know what you think! Did it help you exercise?


Nice post!
Maybe you could put "add a feature to the app" in your set of exercises.
BTW, this script could use a name, so that we can "thingify" it.
Thanks :)
Hehe that'd be nice: 20 pushups, rest, 15 pushups, rest, add sqlite3 support, rest...
And it's called (for now, at least), nhundredthings.py :)
BTW, I am half serious about the sqlite3 support, I'm considering importing the exercise programs into it and making a proper "N Hundred <Things>" aid. But will anyone else use it? :)
Nice. Maybe add TTS support? It would be like having a personal trainer :)
You can also add a barcode for people to import that script from. Just encode a text QR code with the file name as the first line and the script as the rest using zxing.appspot.com.
Hi Damon!
Thanks for dropping by :)
Yeah, TTS is in my short-term improvement plans for this :)
As for the QR code, I'd considered creating one for the download link, but I tried downloading the script from the Android (Dev Phone) and was unable to, it gave me the "unsupported format" error. I'll try it the way you're suggesting, but is there a was to encode a download link (for a .py file)? That way I could leave one QR code permanently and people wouldn't have to worry about finding the latest version.
Thanks!
Hey, that was interesting,
Keep up the good work,
Thanks for writing about it
I hope you are still working on this application.
I've started the one hundred pushups course only to forget or give up a few days in a few times now, and an android app could give me the little techie edge I need :)
Hi Adam! Welcome to the site :)
The above application is not an "app" per se, as Python on the android still only allows limited access to Androids features (such as UI, services etc). If you've run the script above, you'll notice it's very limited. It's an exercise in programming for the platform, much more than a full-featured android app.
Having said that, I do intend to make it more complex, and more useful (both as a Python example and as a 100-pushups aid for the more technically inclined). To be honest, I too haven't followed along with the hundred pushups. However, your comment has brought my attention back to it :)
I'll see if I can get the changes I meant to implement done within the next few days, to post here.
Thanks for your comment!
Heyhoo, interesting that something like this Squat-PushUp-Sites exists! Perhaps everything that we can (or even cannot (yet) imagine) exists somewhere...
thanks for posting, I´ll start tomorrow (maybe)
:)
Hallo Oldi Mann! Welcome :)
Yeah, these programs are quite nice. The problem is keeping up. I haven't done them in about as long as I haven't posted any updates here :P
"Tomorrow" has gone past, I'm still waiting ;)
Bis bald!