Python context managers: they are easy!
This comes from this thread in the Python Argentina mailing list (which I strongly recommend if you read spanish).
I was the other day trying to do shell-script-like-things on python (as part of a monster setup.py) and I was annoyed that in shell it's easy to do this:
Or this:
Or this:
And on Python I had to do this, which is verbose and ugly:
This is what I wanted to have:
And of course, you can't do that. So, I asked, how do you implement that context manager? I got several answers.
-
That's available in Fabric:
-
It's not hard to do:
-
It's even easier to do:
That's cool, so let's add it to path.py
-
Maybe check for exceptions
All in all, I learned how to do context managers, about contextlib, about fabric and about path.py. Which is not bad for 15 minutes :-)
stackoverflow.com is also useful for such issues:
http://stackoverflow.com/qu...
Used in fabric: http://docs.fabfile.org/en/...
I’ve been using the shell for, like, six years, and I’ve never heard of `cd -`. This is going to save me some typing over the next six years. Thank you.
`cd -` is equivalent to `cd $OLDPWD` (the variable is managed by the shell), so you are not restricted to moving back to the previous directory; you can also do stuff like: `mv misplaced-file $OLDPWD`
I tend to use the dirstack a little differently. Sometimes cd - can take you strange places. But then again, popd'ing a few directories can also lead you astray.