Skip to main content

Posts

Showing posts from July, 2007

"If this were Rails I'd..."

Mark has finally convinced me to give Ruby and Rails a shot. I'm starting here. Despite failing horribly at my previous goal of learning Haskell, my frustration with Django has reached the point where I am willing to look in other directions.
Read more

Python One-Liners

I find myself wanting to do simple text mangling on the command line a lot. Most recently, I wanted to add up the size of all my hard drives from df -h. So, the other day I decided to pick up my old camel book and learn how to do one-liners in Perl. With Mark's help, I got a working script:df -h | perl -e '$i=0; while (<>) {$i+=$1 if m/(\d+)G/;} print "$i\n";'After that, I decided having something like perl -ne for Python would be great. So, here it is:
#!/usr/bin/python
import fileinput, sys
from re import *
code = sys.argv.pop(1)
_ = fileinput.input()
exec codeI call it pyne. Instead of an implicit loop like Perl has, I use list comprehensions to make it useful. Here's the one-liner (requires Python 2.4+):df -h | pyne "print sum(int(m.group(1)) for m in [search(r'(\d+)G', m) for m in _] if m)"
Read more

Courtesy of Neal...

Read more