Battery Duration Testing Program

Today, David Pogue (the NYTimes tech columnist) posted this on Twitter–a request for a program that would write the time every thirty seconds until the battery powering it died.

I rose to the challenge and wrote this, in Python:

'''
Copyright 2009 by Timmy Macdonald <timmy@tsmacdonald.com>
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses>.
'''
import time as t
f = file('battlog.txt', 'w')
total = 0
opener = "Testing begun at" + str(t.localtime(t.time()))
print opener
f.write(opener)
f.close()
while True:
    f = file('battlog.txt', 'w')
    t.sleep(30)
    print t.localtime(t.time())
    total += .5
    message = "30 seconds passed: it is now %s and this has been running \
for %s minutes."%(t.localtime(t.time()), total)
    f.write(message)
    print message
    f.close()

Pogue likes it, and since I GPLed it and it could come in handy for someone else, I decided I’d put it up here, too.

—–

David Pogue complimented me in front of 24,645 people!

!

Share/Save/Bookmark

Leave a Reply