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

Uploaded Python 3

File details

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

File metadata

  • Download URL: obj_observe-0.1.3.tar.gz
  • Upload date:
  • Size: 11.1 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.3.tar.gz
Algorithm Hash digest
SHA256 7be6484553d4959b3c6db1c537ab08192ce8a04ca955dc7c49b45d1cc4d16453
MD5 0cc8799d05b2b21fbafa30a42a675ed0
BLAKE2b-256 effba5bbdf57d739cc9ce09085f3a835650834621785dd7d72069c38b72fdb43

See more details on using hashes here.

File details

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

File metadata

  • Download URL: obj_observe-0.1.3-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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 f10c67512fa8964cb352619329f3f538eca3c6063f43325fa68cc149aa485d40
MD5 4d10b15cbcc32e40321456f3deb83f05
BLAKE2b-256 c7a12afff17efe7f12c189948f313dbb041649215ba7350a396d48b15b498c5b

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