Skip to main content

Transparent Object Change Observers for Python objects and dicts

Project description

obj-observe

obj-observe is a simple, zero-dependency Python library for observing changes on object attributes and dictionary keys.

PyPI version Python versions

Installation

pip install obj-observe

Example

from obj_observe import observe

class Player:
    def __init__(self):
        self.hp = 100

p = Player()

@observe(p, "hp")
def on_hp_change(old, new):
    print(f"HP changed: {old} -> {new}")

p.hp = 50

Core Functions

observe(obj, attr, callback=None)

This is the main function for attaching an observer to an attribute or key.

  • As a Decorator:

    from obj_observe import observe
    
    class Player:
        def __init__(self, hp):
            self.hp = hp
    
    p = Player(100)
    
    @observe(p, 'hp')
    def on_hp_change(old_value, new_value):
        print(f"HP changed from {old_value} to {new_value}")
    
    p.hp = 150  # Prints: HP changed from 100 to 150
    
  • With a Callback Function:

    def hp_watcher(old, new):
        print(f"HP was {old}, now it is {new}")
    
    observe(p, 'hp', hp_watcher)
    
    p.hp = 50 # Prints: HP was 150, now it is 50
    
  • Observing Dictionary Keys:

    When observing a dictionary, observe returns an ObservableDict. You must use this new object to track changes.

    my_dict = {'status': 'idle'}
    
    def on_status_change(old, new):
        print(f"Status changed from '{old}' to '{new}'")
    
    my_dict = observe(my_dict, 'status', on_status_change)
    
    my_dict['status'] = 'running' # Prints: Status changed from 'idle' to 'running'
    

remove_observers(obj, attr=None)

Detaches observers from an object.

  • Remove Observers from a Specific Attribute:

    remove_observers(p, 'hp')
    p.hp = 0 # No notification will be sent
    
  • Remove All Observers from an Object:

    remove_observers(p)
    

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

obj_observe-0.1.5-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file obj_observe-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: obj_observe-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 8.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.10

File hashes

Hashes for obj_observe-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 d18b7e07400c85dc4e41b2cdd423267c5089e5ed831c3f5ea7f08acd651a95f3
MD5 9a5e275516acb5409fd17e6b5704e8a7
BLAKE2b-256 9f94ec89148b9639a759ec6777ba908bbf4c4dc13fe7d79b9e19e2a0544565a0

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