Created
July 27, 2018 18:19
Star
You must be signed in to star a gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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