SafeDB is a simple and secure database framework for Python.
Project description
SafeDB
What is SafeDB?
SafeDB is a simple and secure database framework for Python. Its purpose is to provide a simple yet fast way to store data while also allowing for more flexibility than SQL.
Wouldn't it be slower than SQL?
No, because even though SafeDB is written in Python, it uses different caching algorithms to store data on disk and also in RAM and also uses multiple indexing methods for different columns.
How is it more flexible than SQL?
Well, unlike SQL, SafeDB uses lists and dictionaries to store data. This means you can store any type of data (including custom objects).
Can I use threading and multiprocessing?
SafeDB is perfectly thread-safe (with or without GIL), but isn't made to be used with multiprocessing.
Installation
To install SafeDB, run the following command:
pip install -U safedb
To upgrade SafeDB, run the following command:
pip install --upgrade safedb
Documentation
Basic Usage
Creating a Database
To create a new database or open an existing one:
from safedb.database import SDB
# Create or open a database
db = SDB("my_database")
Working with Tables
# Create a new table
db.add_table("users")
# List all tables
print(db.tables)
# Remove a table
db.remove_table("users")
Adding and Retrieving Data
# Add data to a table
user = {"id": 1, "username": "john_doe", "email": "john@example.com"}
db.add_content("users", user)
# Get all data from a table
all_users = db.get_table("users")
# Get data by index position
user = db.get_data_from_index("users", 0) # Get the first user
Working with Indexes
Indexes improve query performance by creating lookup dictionaries for specific fields:
# Create an index on the "username" field
db.add_index("users", "username")
# Retrieve data using an index
user_index = db.get_index_from_index("users", "username", "john_doe")
user = db.get_data_from_index("users", user_index)
Transaction System
SafeDB uses an exchange system (similar to transactions) to ensure data integrity:
# Method 1: Using context manager (recommended)
with SDB("my_database") as db:
db.add_table("products")
db.add_content("products", {"id": 1, "name": "Laptop"})
# Changes are automatically committed if no exceptions occur
# and rolled back if an exception is raised
# Method 2: Manual transaction management
db = SDB("my_database")
db.start_exchange() # Begin transaction
try:
db.add_table("categories")
db.add_content("categories", {"id": 1, "name": "Electronics"})
db.commit() # Commit changes
except Exception as e:
db.rollback() # Rollback on error
finally:
db.save() # Save changes to disk
Data Persistence
# Save database to disk
db.save()
# Load database from disk
db.load()
# Close the database (removes temporary files)
db.close()
# Delete the database (removes all database files)
db.delete()
Best Practices
- Use the context manager when possible to ensure proper transaction handling.
- Create indexes for fields you frequently search on to improve performance.
- Use consistent data types within a table to avoid issues with indexing.
- Save regularly to ensure data persistence.
Data Structure Compatibility
SafeDB can store various Python data types:
- Dictionaries (recommended)
- Custom objects with attributes
- Lists (with numeric indexes)
- Basic data types (strings, numbers, booleans)
- Nested data structure
For best performance, use dictionaries with consistent keys across all records in a table.
Internal Workings
SafeDB uses an ordinary list to store data. Although, to fix the speed issue, there are multiple "indexes". Each index is a dictionary, where a key is a value of a column and a value is the index of a certain row in a list.
Please note that a "row" is an item in the table's list, and a "column" is a property/attribute of a row.
Storing multiple data types in a table is not recommended, and doing so will force you to make indexes based on common attributes.
SafeDB also has an implementation of the saving algorithm that includes durability and atomicity for the primary file.
Notes
Warnings
- Make sure to define any custom classes before you define the database object
- Don't forget to call
SDB.close()to delete the temporary files - Make sure all index-keys are valid attributes of every row in the table and are hash-able.
Notes
- If you need to store dictionaries in a table, you can create indexes with the index-keys being keys from your dictionaries (just make sure all dictionaries have the keys you use for indexes)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file safedb-0.1.0.tar.gz.
File metadata
- Download URL: safedb-0.1.0.tar.gz
- Upload date:
- Size: 5.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.4 Windows/11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9c9c897f12c8e3e23525bd1ad83f003ee4c15c6dfae1da22c54923c8765a0dc7
|
|
| MD5 |
f2e44747131dd64dd14d20315b619c8c
|
|
| BLAKE2b-256 |
01b3000083b0024c73cabf835fb4be3f77d73a726eeeafcceec6e8e0be702592
|
File details
Details for the file safedb-0.1.0-py3-none-any.whl.
File metadata
- Download URL: safedb-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.4 Windows/11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a1a1d739b3cfbff4d216f53b4a0af8a0d7e596c5bd3e49fa93e2e937a7a7263c
|
|
| MD5 |
1c9df54edb376755c738674d03225d6f
|
|
| BLAKE2b-256 |
88e449a9f9e0197073a741e0b5b51c0f4035486c4d95d6a93ee21d9aef9c69e4
|