Skip to main content

A dictionary class with change detection and state persistence

Project description

WatchDict

WatchDict is a Python class that extends the standard dictionary to track changes and save/load its state to/from a JSON file. It supports lists and sets as values, with notifications on modifications. The class also integrates logging with configurable log levels and allows custom JSON indentation for readability.

Features

  • Change Detection: Automatically detects changes in lists and sets, and logs modifications.

  • State Persistence: Saves the current state to a JSON file and loads it on startup.

  • Logging: Configurable logging with different log levels and customizable JSON indentation.

  • Customizable File Handling: Specify the JSON file name and indentation level.

Installation

You don’t need to install WatchDict separately if you have the source code. Simply include the WatchDict class in your project or copy the code into your script.

Usage

  1. Create an Instance of WatchDict:

    from watchdict import WatchDict
    
    
    
    # Create an instance with a specific file name, log level, and JSON indentation
    
    watch_dict = WatchDict(filename="my_state.json", log_level=logging.DEBUG, indent=2)
    
  2. Working with Lists and Sets:

    # Working with lists
    
    watch_dict['test_list'] = []
    
    watch_dict['test_list'].append(1)
    
    
    
    # Working with sets
    
    watch_dict['test_set'] = set()
    
    watch_dict['test_set'].add(42)
    
    watch_dict['test_set'].remove(42)
    
  3. Logging:

    • The log_level parameter in the constructor determines the verbosity of logs. Common levels include logging.DEBUG, logging.INFO, logging.WARNING, etc.

    • If the log level is set to logging.NOTSET (0), no logs will be printed.

  4. Saving and Loading State:

    • The save_state() method is automatically called after modifications to save the current state.

    • The load_state() method is called when creating an instance to load the saved state from the JSON file.

Parameters

  • filename (str): The name of the JSON file for saving and loading the state. Defaults to "watchdict_state.json".

  • log_level (int): The logging level. Defaults to logging.INFO. Set to logging.NOTSET to disable logging.

  • indent (int): The number of spaces for JSON indentation. Defaults to 4.

Example

Here is a complete example of how to use WatchDict:

import logging

from watchdict import WatchDict



# Create an instance of WatchDict

watch_dict = WatchDict(filename="my_state.json", log_level=logging.DEBUG, indent=2)



# Working with lists

watch_dict['test_list'] = []

watch_dict['test_list'].append(1)



# Working with sets

watch_dict['test_set'] = set()

watch_dict['test_set'].add(42)

watch_dict['test_set'].remove(42)

Dependencies

  • Python 3.x

Contributing

Feel free to submit issues or pull requests. Contributions are welcome!

License

This project is licensed under the MIT License. See the LICENSE file for details.

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

watchdict-0.1.3.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

watchdict-0.1.3-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file watchdict-0.1.3.tar.gz.

File metadata

  • Download URL: watchdict-0.1.3.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for watchdict-0.1.3.tar.gz
Algorithm Hash digest
SHA256 1fd291838d7052d82b4bac09621538662f0f6c6332ba96567820fcdee74c41f9
MD5 46680b4fef7d05d6acd5a3b3f3be2518
BLAKE2b-256 896e9279d783639e83bbb116f9315faa69d3c2996532b937961503daf3bb4abf

See more details on using hashes here.

File details

Details for the file watchdict-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: watchdict-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 4.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for watchdict-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 ad40b74bc3f5d3e4ed7c55301a38a47cb3e9d4516d373941fe434be3d8fa288b
MD5 f3cbb0e7040d8cdcd58c48e3745ff459
BLAKE2b-256 ec3364c864d298405c53c9677349e0db422acd2ec34fda010f9d210e62ca6fb7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page