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

Uploaded Python 3

File details

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

File metadata

  • Download URL: watchdict-0.1.2.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.2.tar.gz
Algorithm Hash digest
SHA256 0829c1779d745b32df2bc5c7457eddfeffcf6efb40ca4eb97960b6c95b6ab03d
MD5 54ba615b5fb64b25ac9c331c15d1c6cd
BLAKE2b-256 090f867e337b91c3fe60352589cedd2695b7d397e8e3196aaeca96e3bcc150b0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: watchdict-0.1.2-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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4a22a0b559a94e4ff1504bee0e73ba61ab2afec4f69f93a60cfb1b2c2cc213de
MD5 4369c05821b0a7aef1339b6f9c4326f6
BLAKE2b-256 bc043143c7bd8f18af4655adf45421bd2ef76e220ca0d96b89981a8c783ed637

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