Welcome to DBase documentation. For the latest documentation, visit our GitHub repository.
pip install dbasefrom dbase import DataBase
# Create or open a database
db = DataBase(file_path="data.json")
# Store data
db["name"] = "Alice"
db.score = 95
# Retrieve data
print(db["name"]) # Alice
print(db.score) # 95
# Use as context manager
with DataBase(file_path="session.json") as session:
session.user = "admin"
session.timestamp = "2024-01-01"class DataBase(file_path=None, show_logs=True, is_temp=False)Parameters:
file_path(str, optional): Path to JSON file for persistent storageshow_logs(bool): Enable/disable logging (default: True)is_temp(bool): Create temporary in-memory database (default: False)
Methods:
get(key, default=None): Get value with fallbackpop(key, default=None): Remove and return valueupdate(**kwargs): Update multiple valuesclear(): Remove all dataitems(): Return key-value pairskeys(): Return all keysvalues(): Return all values
db = DataBase("users.json")
# Create
db["user1"] = {"name": "Alice", "age": 30}
# Read
user = db["user1"]
# Update
db["user1"]["age"] = 31
# Delete
del db["user1"]config = DataBase("config.json")
# Set configuration
config.database.host = "localhost"
config.database.port = 5432
config.app.debug = True
# Get configuration
host = config.database.host# Temporary in-memory database
cache = DataBase(is_temp=True)
# Store temporary data
cache.session_token = "abc123"
cache.timestamp = "2024-01-01T12:00:00"
# Data is lost when program ends-
Use context managers for automatic cleanup:
with DataBase("data.json") as db: # Work with database
-
Handle missing keys safely:
value = db.get("missing_key", default="default_value")
-
Use appropriate storage:
- Persistent files for long-term storage
- Temporary databases for caching
-
Enable logging during development:
db = DataBase("data.json", show_logs=True)
- File not found errors: Ensure the directory exists before creating database
- Permission errors: Check file permissions
- JSON decode errors: Verify file contains valid JSON
- Type errors: Ensure you're using string keys
- Check the GitHub Issues
- Review the source code
- Submit a bug report with reproduction steps
MIT License - see LICENSE file for details.
This project is maintained by Daniil Alekseev. For support, please open an issue on GitHub.