python-exercises/context-managers/context-managers.py

56 lines
1,019 B
Python
Raw Normal View History

2014-04-24 14:38:03 +02:00
#!/usr/bin/env python2.7
# Examples from:
# https://stackoverflow.com/questions/3012488/what-is-the-python-with-statement-designed-for
from contextlib import contextmanager
2014-04-24 14:38:03 +02:00
from tempfile import mkdtemp
2014-04-24 14:38:03 +02:00
import os
import shutil
2014-08-16 22:46:40 +02:00
@contextmanager
def working_directory(path):
current_dir = os.getcwd()
os.chdir(path)
try:
yield
finally:
os.chdir(current_dir)
@contextmanager
def just_a_test():
try:
print "just trying..."
yield
finally:
print "finally!"
2014-04-24 14:38:03 +02:00
@contextmanager
def temporary_dir(*args, **kwds):
name = mkdtemp(*args, **kwds)
try:
yield name
finally:
shutil.rmtree(name)
with working_directory("/tmp"), just_a_test():
# do something within data/stuff
print os.getcwd()
# here I am back again in the original working directory
print os.getcwd()
2014-04-24 14:38:03 +02:00
with temporary_dir() as tmpdir:
print tmpdir
tmpfile = tmpdir + "/foo"
with open(tmpfile, "w") as f:
f.write("foo")
# tmpdir is now gone