waving android

I am currently a software engineer at Google, where as a member of the Android platform team I build frameworks and user interfaces.

The blog here at is mostly historical; you can find more recent posts on .

‘battery’ script for Mac portables

March 7th, 2005

I found that the shell/awk monstrosity in this macosxhints tip doesn’t seem to work on my 10.3 PowerBook. This looked like a nail to me, so I of course rewrote it in Python.

Example output:

[100.0%] charge=4399/4400 voltage=125.700v cycles=92 flags=0x32000005

It makes unabashed use of the fact that the output of ioreg looks almost like a Python dictionary. Here’s the source for my battery script:

import os,re
P = os.popen('/usr/sbin/ioreg -p IODeviceTree -n "battery" -w 0')
info = None
for l in P.xreadlines():
    M = re.search(r'"IOBatteryInfo" = \((.*)\)', l)
    if M:
        info = eval(re.sub(r'=',':',M.group(1)))
        break

if info:
    info['Percent'] = 100.0 * info['Current'] / info['Capacity']
    info['Volts'] = float(info['Voltage']) / 100.0
    print "[%(Percent).1f%%] charge=%(Current)d/%(Capacity)d " \
        + "voltage=%(Volts).3fv cycles=%(Cycle Count)d " \
        + "flags=0x%(Flags)08x" % info

Update: Had to make a tweak to WP1.5 to get double-quotes to appear correctly in that code block. File functions-formatting.php: add $text = str_replace('\\"', '"', $text); to function clean_pre.

newer: older: