A simple interface to manage collections in Redis.
Project description
RedisCollectionDB
Description
RedisCollectionDB is a Python library that provides an easy-to-use interface for managing collections and items within a Redis database. It allows for the addition and removal of collections and items, listing of current collections and items, and subscribes to changes in collections and items via Redis' publish/subscribe mechanism.
Installation
Install RedisCollectionDB using pip:
pip install rediscollectiondb
How to Use
First, ensure you have Redis running on your local machine or a server.
from rediscollectiondb import RedisDB
# Initialize the RedisDB instance
db = RedisDB(db_name='your_database_name')
# Add a collection
db.add_collection('books')
# Add items to the collection
db.add_item('books', 'The Great Gatsby')
db.add_item('books', '1984')
# List collections
print(db.list_collections())
# List items in a collection
print(db.list_items('books'))
# Remove an item
db.remove_item('books', '1984')
# Remove a collection
db.remove_collection('books')
Handling Events with Callbacks
You can extend the RedisDB class to handle item and collection changes by overriding the on_item_changed and on_collection_changed methods. Here's an example:
class MyRedisDB(RedisDB):
def on_item_changed(self, action, collection, item):
print(f"Custom handling for item: {item} in collection: {collection}, action: {action}")
def on_collection_changed(self, action, collection):
print(f"Custom handling for collection: {collection}, action: {action}")
# Use the subclass with custom event handling
db = MyRedisDB(db_name='myapp')
# Now, adding or removing items and collections will trigger custom handling
db.add_collection('movies')
db.add_item('movies', 'Inception')
Example
This example demonstrates adding a collection, adding items to it, and then listing and removing items and the collection, with custom event handling for changes.
if __name__ == '__main__':
db = MyRedisDB(db_name='myapp')
db.add_collection('books')
db.add_item('books', 'The Great Gatsby')
db.add_item('books', '1984')
print("Collections:", db.list_collections())
print("Books items:", db.list_items('books'))
db.remove_item('books', '1984')
print("Books items after removal:", db.list_items('books'))
db.remove_collection('books')
print("Collections after removing books:", db.list_collections())
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 rediscollectiondb-0.1.2.tar.gz.
File metadata
- Download URL: rediscollectiondb-0.1.2.tar.gz
- Upload date:
- Size: 3.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.10.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0b104f279d4d5518352c245167fb24b37ce402388ee0b03b3030ce37e1af42be
|
|
| MD5 |
ed8ae0dc4bc7414d5de9e6b8d13d7ce4
|
|
| BLAKE2b-256 |
e2c58d9928b1447b6ab56ab5589682c8ecbf7a589c9ea77de95625710f9a9bc9
|
File details
Details for the file rediscollectiondb-0.1.2-py3-none-any.whl.
File metadata
- Download URL: rediscollectiondb-0.1.2-py3-none-any.whl
- Upload date:
- Size: 3.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.10.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9a95f62f45ea571b6aad2e0d91571e96e04b40c88910cb78041125e172e7ee41
|
|
| MD5 |
25e37e4b405fdf9d96b4d898a9674fc1
|
|
| BLAKE2b-256 |
818e19fe8048f9d4c131c3f1a78c38dd3c933d9814671e9bd72c2e9dc16d610e
|