Skip to content

Instantly share code, notes, and snippets.

@blackrobot
Created July 27, 2018 18:19
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save blackrobot/629f133c345608862f7094b748975c73 to your computer and use it in GitHub Desktop.
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