Skip to content

Instantly share code, notes, and snippets.

@stvhay
Created April 18, 2020 02:29
Show Gist options
  • Save stvhay/76d299eb36f5e4eedb0066e585057a31 to your computer and use it in GitHub Desktop.
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.
"""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