Hello World!
I have been wanting to start a blog for a while… and it’s finally happening! In fact, I’m currently working on my first post about CPython string interning and I hope to publish it in a few days.
Meanwhile, I will leave you with two neat Python tricks I discovered last year in Raymond Hettinger’s PyCon talk: “Transforming Code into Beautiful, Idiomatic Python”.
The ignored
context manager
Suppose you want to create a directory, but this directory might already exist:
import os
try:
os.makedirs('/foo/bar')
except OSError:
pass
Now, let’s define the ignored
context manager as follows:
from contextlib import contextmanager
@contextmanager
def ignored(*exceptions):
try:
yield
except exceptions:
pass
Finally, this allows you to write:
import os
with ignored(OSError):
os.makedirs('/foo/bar')
Nota bene: this is probably not the best example since you can handle this exception better by doing the following:
import errno
import os
try:
os.makedirs('/foo/bar')
except OSError as e:
if e.errno != errno.EEXIST:
raise e
Still, I am sure you will find appropriate use cases for this context manager.
Read N bytes at a time from a buffer
This time, suppose you want to read 1024 bytes at a time from a file:
with open('/foo/bar/qux') as f:
block = f.read(1024)
while block:
# do something with block
block = f.read(1024)
Instead, Raymond suggests:
from functools import partial
with open('/foo/bar/qux') as f:
for block in iter(partial(f.read, 1024), ''):
# do something with block
I guess this can be extended to many other uses, be creative!
comments powered by Disqus