With iterpipes, python is ready to replace bash for scripting. Really.
This has been a pet peeve of mine for years: programming shell scripts suck. They are ugly and error prone. The only reason why we still do it? There is no real replacement.
Or at least that was the case, until today I met iterpipes at python.reddit.com
Iterpipes is "A library for running shell pipelines using shell-like syntax" and guess what? It's brilliant.
Here's an example from its PYPI page:
# Total lines in *.py files under /path/to/dir,
# use safe shell parameters formatting:
>>> total = cmd(
... 'find {} -name {} -print0 | xargs -0 wc -l | tail -1 | awk {}',
... '/path/to/dir', '\*.py', '{print $1}')
>>> run(total | strip() | join | int)
315
Here's how that would look in shell:
find /path/to/dir -name '*.py' -print0 | xargs -0 wc -l | tail -1 | awk '{print $1}'
You may say the shell version looks better. That's an illusion caused by the evil that is shell scripting: the shell version is buggy.
Why is it buggy? Because if I control what's inside /path/to/dir
I can make that neat little shell command fail [1], but at least in python I can handle errors!
Also, in most versions you could attempt to write, this command would be unsafe because quoting and escaping in shell is insane!
The iterpipes version uses the equivalent of SQL prepared statements which are much safer.
It's nearly impossible to do such a command in pure shell and be sure it's safe.
Also, the shell version produces a string instead of an integer, which sucks if you intend to do anything with it.
And the most important benefit is, of course, not when you try to make python act like a shell, but when you can stop pretending shell is a real programming language.
Consider this gem from Arch Linux's /etc/rc.shutdown
script. Here, DAEMONS is a list of things that started on boot, and this script is trying to shut them down in reverse order, unless the daemon name starts with "!":
# Shutdown daemons in reverse order let i=${#DAEMONS[@]}-1 while [ $i -ge 0 ]; do if [ "${DAEMONS[$i]:0:1}" != '!' ]; then ck_daemon ${DAEMONS[$i]#@} || stop_daemon ${DAEMONS[$i]#@} fi let i=i-1 done
Nice uh?
Now, how would that look in python (I may have inverted the meaning of ck_daemon)?
# Shutdown daemons in reverse order
for daemon in reversed(DAEMONS):
if daemon[0]=='!':
continue
if ck_daemon(daemon):
stop_daemon(daemon)
Where stop_daemon used to be this:
stop_daemon() { /etc/rc.d/$1 stop }
And will now be this:
def stop_daemon(daemon):
run(cmd('/etc/rc.d/{} stop',daemon))
So, come on, people, we are in the 21st century, and shell scripting sucked in the 20th already.
What I do for a living
So, what do you do for a living?
—Hardest question ever
Whenever I am speaking with people who don't know me [1] that's the question I dread.
If someone asks my wife what she does, all she has to do is say "I'm a lawyer". If someone asks my mother, she'd say "I am a retired teacher". Everyone understands what a lawyer does, or what a retired teacher did.
If someone asks me... oh, boy, that's hard. I usually weasel out by saying "I work with computers" but that has several problems:
They assume I repair PCs
They start telling me how their windows box was slow until they installed some kropotkina which supergarbled their frobnozzles [4], then ask me my opinion on frobnozzle garbling. For or against?
It's really hard to explain that yes, I work with computers every day, but I almost never open one (in fact, I have a policy of not touching my customers computers), and I have no idea what a frobnozzle is.
I have tried saying "I work on server side things, like mail servers and such. I install them, support them and also consulting work, explaining companies what the best ways to improve their services are.".
That one usually gets glassy eyes and a general "what?" look.
I could lie and say I program for a living, but that's not true. While I program a lot, it's usually not for money, and what little I do for money is just using programming as a sysadmin tool.
I could say "I'm a sysadmin" but most people have no idea what that is. It does tend to end conversations, though, so it has one thing going for it.
Nowadays I could say "I have a company", which is true (we are awesome, you should hire us to do whatever it is we do, more details at http://www.netmanagers.com.ar )
So, I usually manage to work around this question, but I have a problem: I'm not telling the truth, or if I am, I am not telling the truth in spirit because I am not conveying what my work is, but only what I do.
So, this post is about trying to explain what the hell I do for a living, in another way, which is more ... internally true, so to speak. This is really hard to do, so I am trying to just let the writing flow, maybe you can understand what I do even if it's not clearly explained.
I work with computers. I make them do what I want them to do. Whenever a regular user sits before his keyboard, he tries to make his computer follow his orders, which variable rates of success. I always succeed.
Sometimes, I am logged into a computer that manages data for thousands of people. They all are on my care. No, it's not their lives at stake, but a little part of their fun, or work is under my care. I help them. I care about them, and I want their fun, their work to be smooth and pleasant.
Often the computer will not do what they need. I will try with my craft to make it happen. I will write little programs, search for others on the Internet, carefully piece together a puzzle and make their needs be fulfilled.
I will write or install and configure those programs and do it well, because I am skilled, I have literally decades of training and experience, but I will mostly do it because I like order and function. I like when things flow unimpeded, I like when serendipitous accidents make things just click together.
I do those things for a living, yes, because I need to make a living. And later, when I'm off the clock and my boy is asleep and I have my own time, you know what I do? I do the same things because they are fun. And I will bother writing a 1300 word post about how I migrated my blog's comments from one site to another because it was fun.
Yes, I know, to most people that would not be fun at all, it would be a boring job, and they would hate doing it. And that's one of the many reasons I am a lucky man [5]: I have fun doing unusual things. That's really lucky, because if my idea of fun was watching "Gossip Girl" I would never have found anyone to pay me to do that!
But going back to what I do for a living, I create things. I don't create large, impressive things, I am not a bridge builder, an architect, I create small, useful things and try to do it with a certain taste or elegance. I am more like a silversmith doing cutlery. Sure, I'll try to make it nice to look at, but it must cut a chunk of beef first.
Yes, I work with computers, but how does that convey what I feel when after a solid day of work I can see that what was a lot of stupid computers and cables are now a working machine that can make 50000 phone calls a day?
How can I make anyone see the beauty in 3 hard lines of code that do nothing but print a bunch of numbers?
How can someone who makes a living any other way understand that I think things and they become real? No, not real as in a puff of smoke and there they are, but they become real through work and effort and thinking and cursing, which is what makes them really real.
I know most of this will sound like mysticism, but it's not, it's my honest truth, I really feel all these things as I work, all these things are my work. Sometimes when I crack a hard problem I want to fucking sing [7] that's how awesome it feels.
So, that's what I do for a living. I work with computers.
Given my shyness problem, that's not too often [2]
To those who only know me from public speaking [3]: I am painfully shy.
Yes, it's perfectly possible to be a decent public speaker and be shy.
At least that's how windows users sound to me half the time
The others are of course y wife and boy, and if they ever read this: kisses for both. [6]
He's just 2.9 years old but you know, the Internet keeps things forever.
If you know my voice, you know why I don't. My own son says "no, don't sing, daddy", except for his good night song, which is the only one he lets me sing. Oh, and sorry for the cursing, but no other word fits.
python-keyring is seriously nice
Many programs require passwords from the user.
It's nice when a program can remember the password you give it.
It's nicer when it stores said password safely. However, it's not trivial to do that if you care for cross-platform support.
Or at least it wasn't until Kang Zhang wrote python keyring, a module that abstracts the password storage mechanisms for KDE, GNOME, OSX and windows (and adds a couple of file-based backends just in case).
So, how does it work?
Install it in the usual way. If it's not packaged for your distro/operating system, just use easy_install:
easy_install keyring
You could also get it from mercurial:
hg clone http://bitbucket.org/kang/python-keyring-lib/
The API is simplicity itself. This is how you save a secret:
import keyring
keyring.set_password('keyring_demo','username','thisisabadpassword')
You may get this dialog (or some analog on other platforms):
And here's the proof that it was saved correctly (this is KDE's password manager):
And how do you get the secret back?
import keyring
print keyring.get_password('keyring_demo','username')
This is how it runs:
$ python load.py thisisabadpassword
As you can see, the API is as easy as it could possible get. It even chose the KWallet backend automatically because I am in KDE!
Python-keyring is a module that fixes a big problem, so a big thank you to Kang Zhang and Tarek Ziadé (who had the idea)
The smartest thing I ever wrote
When I was migrating the comments I noticed a page looked wrong in the site, and started fixing it.
While I was reading, I noticed a couple of things:
It was published almost exactly 5 years ago
It may be the smartest thing I ever wrote
Sometimes, you get an idea, and you can give it shape. Since I really think it's not completely stupid, I translated it to spnish (there was only an english version) y "reprint" it today.
It's about programming, it's about evolution, and I hope you like Being a good Lamarckian froggie!.