Archive for the 'Python' Category

The Zen of Python

Friday, February 8th, 2008

I found this little note at the end of the python tutorial today:

Zen of Python
Listing of Python design principles and philosophies that are helpful in understanding and using the language. The listing can be found by typing “import this” at the interactive prompt.

Naturally, I was curious. This is what it said:

Python 2.5.1 (r251:54863, Oct 30 2007, 13:45:26)
[GCC 4.1.2 20070925 (Red Hat 4.1.2-33)] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.
>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren’t special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one– and preferably only one –obvious way to do it.
Although that way may not be obvious at first unless you’re Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it’s a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea — let’s do more of those!

Software Release: battery_status.py

Tuesday, August 21st, 2007

Annoyed that Fedora 7 doesn’t have the same CLI tools to get battery status, I coded up a little python script last night to do just that. It was a pretty straight-forward project that took me about an hour, but I was pretty happy with the end result. It reads the proper files out of the /proc/acpi/battery hierarchy, does some calculations on that data and the gives you what you really want.

You start with this:

thesis|18:46|~% cat /proc/acpi/battery/BAT0/info
present:                 yes
design capacity:         65520 mWh
last full capacity:      69150 mWh
battery technology:      rechargeable
design voltage:          14400 mV
design capacity warning: 3457 mWh
design capacity low:     200 mWh
capacity granularity 1:  1 mWh
capacity granularity 2:  1 mWh
model number:            42T5229
serial number:           35751
battery type:            LION
OEM info:                SANYO
thesis|18:46|~% cat /proc/acpi/battery/BAT0/state
present:                 yes
capacity state:          ok
charging state:          charging
present rate:            8595 mW
remaining capacity:      66620 mWh
present voltage:         16690 mV

And you get this:

Battery 0: 96.46% (0:17 until charged)

In the spirit of sharing and trying new things, I thought I would register it as Google Code project and give it away under a BSD License. It was about a ten minutes process, start to finish and now the world can have it:

http://code.google.com/p/batterystatus/

Hurray for non-scarce economics!