I’ve recently been re-discovering an app I purchased almost a year ago, but never really started to use seriously. Pythonista is a full-featured IDE for Python that runs on iOS, and it’s really quite impressive. When I initially heard about the app, and decided (on a whim) to shell out the full $9.99 to download it, and I poked around in the examples a bit. Initially, I was impressed; the app has tons of cool coding potential, but for me it likely wouldn’t get more use than simply showing off the examples to friends. “Look! I can code on my iPhone and do cool things, like check my battery level using code… and stuff.”
It wasn’t until I saw a random post on my feed reader from Dr. Drang (about using a super-simple Python to print out a special unicode character) that I realized how I could make practical use of the Pythonista app. Having a code editor app, and one for Python to boot, almost makes my iPhone feel like it’s infinitely more hackable. So I stored that idea away for sometime when I really needed to use Python to calculate or automate something… and again let Pythonista gather some dust.
Finally, a few weekends ago, I ran into an annoying automation problem. I have this incredible two-volume book I’m reading, called Bodyguard of Lies by Anthony Cave Brown, and it has a little problem with the page numbering. Usually, when reading a book this great (it’s awesome, by the way… never before have I been so engaged with a non-fiction history book), I wouldn’t complain about page numbering. However, this year, I’ve started tracking my reading progress in the excellent GoodReads app, which allows you to enter a page number or percent complete and make notes as you track your progress. It’s a handy database for what you’ve read, what you want to read in the future, and what’s currently in progress. Anyways, I went into GoodReads after reading a few chapters of the second volume of Bodyguard of Lies and entered a page number, only to find out that GoodReads got confused. Turns out that the publisher didn’t restart the page numbering on the second volume, but instead continued with the pages as they left off in Volume 1. I realized I could use the “percent complete” feature of Goodreads to deal with this, but then I had a math problem. How do I get the percent complete for just the second volume?
Here’s the simple formula that comes to mind:
pct = (currentPg / totalPgs) * 100. That works great until you realize that
totalPgs isn’t the same as the last page number in the book. Instead,
totalPgs is actually
totalPgs - startPg + 1, which is actually useful since I’m not going to read through the fairly lengthy references and index that the author included at the end of Volume 2. So now, my formula looks something like this:
pct = (currentPg / (totalPgs - startPg + 1)) * 100
currentPg isn’t accurate now either, because I’m making it all relative to the number of pages in this volume. So, the pages I’ve really read through the book is actually
currentPg - startPg. Thus, the final calculation would look something like this:
pct = ((currentPg - startPg) / (totalPgs - startPg + 1)) * 100
I used the handy calculator on Alfred on our family iMac to do this calculation once, but then promptly forgot how I had put it together in my head the next time I finished reading a chapter. So on that bright weekend morning, while watching the kids tear around the house (my wife was gone at a ladies retreat), I fired up Pythonista and wrote a quick Python script.
def calcPercent(total, start, current): pct = (current - start + 1) / (total - start + 1) return round(pct * 100, 1)
Pythonista also has a handy UI library, and so I tried that out to make it even easier to calculate the percent complete from my phone. The form painter makes it reasonably easy to set up fields, labels, and buttons. It took some poking around in the Pythonista documentation to figure out how to get at the values from the fields, and I’m not 100% sure I did it “right”. Obviously, I’m not a Python expert, but it works!
For anyone interested in the code, I’ve uploaded the two Pythonista files to a Gist on Github for sharing/reference. After this experience, I’m definitely interested in keeping this app around and using it more frequently for “on the go” coding an automation.