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.0.tar.gz (4.0 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.0-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: watchdict-0.1.0.tar.gz
  • Upload date:
  • Size: 4.0 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.0.tar.gz
Algorithm Hash digest
SHA256 62e83a42e2dc63d9dc560605249c5e981905c1268a2c07c366dafff8a00e74d2
MD5 bf90e45831137eb592b22bb5fc79385a
BLAKE2b-256 6c4a3bfda84d34e1a5a6d1acb83cd1315e8d54b5f837e02427055741311d5ae9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: watchdict-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.2 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f871b287efd265f3645aa60bb7c21b418004521e8e5e25ca3a1e2a0454dee00f
MD5 56368e4edc27a6e70269f84b817421f6
BLAKE2b-256 33507560bb6cd23d72bfcb296e6d0df5f83f5ee6a4dd236261f6cdda50035057

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