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 Distribution

obj_observe-0.1.2.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

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

obj_observe-0.1.2-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: obj_observe-0.1.2.tar.gz
  • Upload date:
  • Size: 7.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for obj_observe-0.1.2.tar.gz
Algorithm Hash digest
SHA256 1161ebc135833e30a9f9eb5d682cd954aef1660999f25b1726a1c74b593f159c
MD5 b03caffea5ae9c71869a6ac5e2a2c9f7
BLAKE2b-256 e8ec3a69fa39c18f4d874a58e65e0dc57cbcb09f974e79b3fa95fbd48fc20ced

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for obj_observe-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 968203e6d14ad8d99da5dc51e25fb340c967c329cdc343103379ddcb1fecdb68
MD5 80c58977ee7a11ac3b256512c5095076
BLAKE2b-256 5f3ebecae4d722671662a913f6ced293fa2f494d446b270b8a13fe174f82602a

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