Created
April 18, 2020 02:29
-
-
Save stvhay/76d299eb36f5e4eedb0066e585057a31 to your computer and use it in GitHub Desktop.
Shelve has inconsistent file naming, causing weirdness sometimes when you try to use it.
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
"""safe_shelve.py""" | |
import os | |
import shelve | |
DATA_DIR = 'data' | |
def open(db_name: str): | |
"""Opens and returns a shelve in an unambiguous place.""" | |
directory = os.path.join(DATA_DIR, db_name) | |
db = os.path.join(directory, db_name) | |
os.makedirs(directory, exist_ok=True) | |
return shelve.open(db) | |
def list(db_name): | |
"""List available shelves.""" | |
return os.listdir(DATA_DIR) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment