A dictionary class with change detection and state persistence
Project description
WatchDict
WatchDict is a Python library that provides an enhanced data structure for monitoring dynamic changes in collections, allowing for efficient querying using trie-based search functionality. The WatchDict class enables logging of modifications and supports advanced search capabilities, while also saving all changes in a JSON file that acts as a database.
Features
-
WatchDict: Automatically logs and saves changes when items are added, removed, or modified.
-
Trie-Based Search: Fast searching for prefixes, suffixes, and exact matches using a trie data structure.
-
Flexible Querying: Supports exact matches, substring searches, and numerical comparisons (greater than, less than).
-
Persistent Storage: Saves all changes in a JSON file, allowing data to persist between sessions.
-
Debug Logging: Easily toggle logging of changes to standard output.
Installation
You can install the WatchDict package using pip:
pip install watchdict
Usage
Importing the Library
To use WatchDict, import it into your Python script:
from watchdict import WatchDict
Creating a WatchDict
You can create a WatchDict to monitor and manipulate a dictionary of items. Here’s how to do it:
# Initialize a WatchDict with a file path for persistence
watchdict = WatchDict(database='data.json', allow_search=True)
# Set items
watchdict['key1'] = 'value1' # Output: Set item: key1 = value1
watchdict['key2'] = 'value2' # Output: Set item: key2 = value2
# Get an item
value = watchdict['key1'] # Output: 'value1'
# Delete an item
del watchdict['key2'] # Output: Deleted item: key2 which had value value2
# Search for values
results = watchdict['$value1$'] # Exact match query
print(results) # Output: [{'key1': 'value1'}]
# Greater than query
greater_than_results = watchdict['>100']
print(greater_than_results) # Output: results of greater than 100
between_results = watchdict['>100&<200']
or_opration = watchdict['$hello|*bye']
Persistent Storage
All changes made to the WatchDict are saved in a specified JSON file. When the WatchDict is initialized, it loads existing data from the file, allowing for persistence between sessions.
Logging
To enable logging of changes, set the debug flag to True at the beginning of your script:
debug = True # Enable logging
To disable logging, set debug to False:
debug = False # Disable logging
Example Usage
Here's a complete example showcasing the features of WatchDict:
from watchdict import WatchDict
# Enable debug logging
debug = True
# Create and manipulate a WatchDict
watchdict = WatchDict(database='data.json', allow_search=True)
watchdict['key1'] = 'value1'
watchdict['key2'] = 'value2'
# Get an item
print(watchdict['key1']) # Output: 'value1'
# Delete an item
del watchdict['key2']
# Search for an exact value
print(watchdict['$value1$']) # Output: [{'key1': 'value1'}]
# Perform a greater than query
# Assume numerical values are stored in the WatchDict
print(watchdict['>100']) # Output: results for values greater than 100
Query Syntax
You can perform various types of queries on the WatchDict. Here are the supported query formats:
-
Exact match: Enclose the word in
$symbols (e.g.,$word$) -
Greater than: Start the query with
>(e.g.,>10) -
Less than: Start the query with
<(e.g.,<5)
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
- This project utilizes a trie data structure for efficient searching. The implementation is based on common patterns for prefix trees.
Project details
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 watchdict-0.1.4.tar.gz.
File metadata
- Download URL: watchdict-0.1.4.tar.gz
- Upload date:
- Size: 6.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f69a93f1766be939f27d854c6005089aaba54291055422729d6de105be28355d
|
|
| MD5 |
cd2a15a1605b42ec141cae5f8d3a2ba8
|
|
| BLAKE2b-256 |
d7055317bfebbba8d06b23626933969920b627c19d75932de82875b3a8fb5064
|
File details
Details for the file watchdict-0.1.4-py3-none-any.whl.
File metadata
- Download URL: watchdict-0.1.4-py3-none-any.whl
- Upload date:
- Size: 6.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d478feaeb2ce575d2c10db044af79e3b6630fbb1fc08f1118834e5f15ee148d6
|
|
| MD5 |
88b454621fc49a5a6b371a7368b0d71f
|
|
| BLAKE2b-256 |
5b285b8abfeeb0dfea3d81bf116d5ae61cd6a2ec573718197517e565e47a776c
|