Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import contextlib
@contextlib.contextmanager
def managed_records(shelve_db_names, file_names):
try:
shelve_dbs = []
for name in shelve_db_names:
db = shelve.open(name, protocol=pickle.HIGHEST_PROTOCOL)
shelve_dbs.append(db)
files = []
for name in file_names:
file_ = open(name)
files.append(file_)
yield shelve_dbs, files
finally:
for resource in shelve_dbs + files:
resource.close() # or whatever
with managed_records(..., ...) as (shelves, texts):
prod_db, proding_db, storeprod_db = shelves
# ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.