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.4.tar.gz (11.2 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.4-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for obj_observe-0.1.4.tar.gz
Algorithm Hash digest
SHA256 11ba3607bb413e20cf890cf9459dab6289fa5e06b9cb3ec93f4b715401d2b5d9
MD5 59c4d9b2055b5f9abc698e83c29b25a6
BLAKE2b-256 749dc10fb004ba3da4b00e959189d9c40e0b10c4ba05be8a4f5a3b318bcdf5f3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: obj_observe-0.1.4-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.4-py3-none-any.whl
Algorithm Hash digest
SHA256 66175d2c1bd4d52b403c1e3ca1dbad6cca2bbf527f888f82c4274f1b78264fcc
MD5 c37e8a6c69df846b3c65b5679b847ff7
BLAKE2b-256 dd72a8b263b14963fc05fdffc0770e725129c7d8dd6dd1631bcf38145a7ad0e8

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