Python Shelve Database Manager
This challenge will guide you through creating a simple database manager using Python's shelve module. The shelve module provides a persistent, dictionary-like object that can store Python objects, making it ideal for simple data storage and retrieval without requiring a full-fledged database system.
Problem Description
Your task is to create a Python program that acts as a simple database manager for storing and retrieving key-value pairs. You will use the shelve module to create a persistent storage. The program should allow users to perform three core operations: adding a new entry, retrieving an existing entry, and listing all entries.
Key Requirements:
- Create/Open Database: The program should initialize or open an existing shelve database file.
- Add Entry: Implement a function to add a new key-value pair to the database. Keys and values can be any pickleable Python object (strings, numbers, lists, dictionaries, etc.).
- Retrieve Entry: Implement a function to retrieve the value associated with a given key. If the key does not exist, the program should handle this gracefully.
- List All Entries: Implement a function to display all key-value pairs currently stored in the database.
- Error Handling: The program should handle potential errors such as trying to retrieve a non-existent key.
- Database Persistence: Ensure that data is saved to disk and persists between program executions.
Expected Behavior:
When the program runs, it should present a menu of options to the user (e.g., Add, Retrieve, List, Exit). Based on user input, it should perform the corresponding action. After operations like adding or retrieving, the database should be updated. When the user chooses to exit, the database should be properly closed.
Edge Cases:
- Empty Database: The program should function correctly when the database file does not yet exist or is empty.
- Duplicate Keys: If a user attempts to add an entry with a key that already exists, the new value should overwrite the old one.
- Non-existent Key Retrieval: The retrieval function should not raise an error if the requested key is not found, but rather indicate that the key is not present.
Examples
Example 1: Adding and Retrieving Data
Enter database filename: my_data.db
Menu:
1. Add Entry
2. Retrieve Entry
3. List All Entries
4. Exit
Enter your choice: 1
Enter key: name
Enter value: Alice
Entry 'name' added successfully.
Menu:
1. Add Entry
2. Retrieve Entry
3. List All Entries
4. Exit
Enter your choice: 1
Enter key: age
Enter value: 30
Entry 'age' added successfully.
Menu:
1. Add Entry
2. Retrieve Entry
3. List All Entries
4. Exit
Enter your choice: 2
Enter key to retrieve: name
Value for 'name': Alice
Menu:
1. Add Entry
2. Retrieve Entry
3. List All Entries
4. Exit
Enter your choice: 2
Enter key to retrieve: city
Key 'city' not found in the database.
Menu:
1. Add Entry
2. Retrieve Entry
3. List All Entries
4. Exit
Enter your choice: 4
Closing database. Goodbye!
Example 2: Listing Entries and Overwriting
(Continuing from Example 1, assuming my_data.db already contains 'name': 'Alice' and 'age': 30)
Enter database filename: my_data.db
Menu:
1. Add Entry
2. Retrieve Entry
3. List All Entries
4. Exit
Enter your choice: 3
Current entries in database:
- name: Alice
- age: 30
Menu:
1. Add Entry
2. Retrieve Entry
3. List All Entries
4. Exit
Enter your choice: 1
Enter key: name
Enter value: Bob
Entry 'name' updated successfully.
Menu:
1. Add Entry
2. Retrieve Entry
3. List All Entries
4. Exit
Enter your choice: 3
Current entries in database:
- name: Bob
- age: 30
Menu:
1. Add Entry
2. Retrieve Entry
3. List All Entries
4. Exit
Enter your choice: 4
Closing database. Goodbye!
Example 3: Storing Complex Data Types
Enter database filename: complex_data.db
Menu:
1. Add Entry
2. Retrieve Entry
3. List All Entries
4. Exit
Enter your choice: 1
Enter key: my_list
Enter value: [1, 2, 'three', {'a': 1}]
Entry 'my_list' added successfully.
Menu:
1. Add Entry
2. Retrieve Entry
3. List All Entries
4. Exit
Enter your choice: 2
Enter key to retrieve: my_list
Value for 'my_list': [1, 2, 'three', {'a': 1}]
Menu:
1. Add Entry
2. Retrieve Entry
3. List All Entries
4. Exit
Enter your choice: 4
Closing database. Goodbye!
Constraints
- The database filename will be a string and will end with
.db. - Keys can be any hashable Python object.
- Values can be any pickleable Python object.
- The program should be able to handle up to 1000 entries in the database without significant performance degradation for basic operations (add, retrieve, list).
- Input for choices and values should be handled as strings initially, and then converted to appropriate types for storage if necessary (though for this challenge, storing raw input strings is acceptable for simplicity, with the exception of
shelvehandling pickleable types automatically).
Notes
- The
shelvemodule works by creating a file (or a set of files) to store your data. The filename you provide will be the base name for these files. - Remember to close the shelve database when you are finished to ensure data is written correctly to disk.
- Consider how you will prompt the user for input and how you will display output clearly.
- Think about the structure of your program. Using functions for each operation (add, retrieve, list) will make your code more organized and readable.
- For retrieving values, Python's dictionary-like access (
db[key]) will raise aKeyErrorif the key doesn't exist. You'll need to handle this. The.get(key, default_value)method of dictionaries can be very useful here.