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.1.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.1-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: watchdict-0.1.1.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.1.tar.gz
Algorithm Hash digest
SHA256 6dde9d0884ef2b96b39a4dffef47256fc92571776cd24a43daea16e196422113
MD5 534b9f0138d941c5fa4dc4d707c6da1c
BLAKE2b-256 10a292cc9c18120f3a349121be62243de92df48ff86715cfbc407bcd6230e363

See more details on using hashes here.

File details

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

File metadata

  • Download URL: watchdict-0.1.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a581c2cb3e46dc30844b94ccb482d5b47665105fda9cc3f0c17feb4b7cf17991
MD5 09d3519930a1d0a04842f3e0cfb8b4a1
BLAKE2b-256 fcbd7be7849bf4201973253187941e622d7dcdb9b162b670915c5f9e8db33280

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