Skip to main content

Reactive properties and owners for Python classes.

Project description

ReactivePy

A simple library to manage reactive properties within an object using custom descriptors and update methods.

About ReactivePy

ReactivePy lets you create objects that contain reactive properties. Those can be updated in bulk, which allows ReactivePy to notify its observers of changes. The callback can then read the history, update value as well as the name of the attribute.

from reactive import ReactiveOwner, ReactiveProperty

class Foo(ReactiveOwner):
    def __init__(self):
        super().__init__()
        self.name = ReactiveProperty("Foo")
        self.age = ReactiveProperty(6)

foo = Foo()

def on_change(*args):
    for arg in args: print(arg.name, "updated to", arg)

def on_name_change(curr: Any, org: Any):
    print("Name updated from", org, "to", curr)

foo.on_change(on_update, foo.name, foo.age)
foo.name.on_change(on_name_update)

foo._bulk_update(name="Bar", age=12)

Reactive properties can also be strong-typed raising a TypeError if the value they're being set to doesn't match the field_type defined in the constructor. Strong-typing a property looks like this:

class Foo(ReactiveOwner):
    def __init__(self):
        super().__init__()
        self.name = ReactiveProperty("Foo", field_type=str)

all_reactive Decorator

The ReactiveOwner.all_reactive owner can be used on classes, where all public attributes should be reactive, which will additionally override the __setattr__ method to convert any attribute writes.

from reactive import all_reactive, ReactiveOwner

@all_reactive
class Foo(ReactiveOwner):
    def __init__(self):
        super().__init__()
        self.name = "Foo"

Additionally the parameters only_type and not_type can be specified, as a single type, list or tuple of types which will have only those types converted to class ReactiveProperty or not.

Using Type bool

Since the type bool cannot be used as a base class, when retrieving its value, users must explicitly use ReactiveProperty.value attribute:

from reactive import ReactiveOwner, ReactiveProperty

class Foo(ReactiveOwner):
    def __init__(self):
        super().__init__()
        self.boolean = ReactiveProperty(True)

foo = Foo()
print(foo.boolean.value)

>>> True

async / await in ReactivePy

Oftentimes reactive models get updated from an external API source in which case it makes sense to use asynchronous programming to avoid the main program from being blocked by network requests. ReactivePy supports the async/await syntax by offering a _async_bulk_update() function, with an equivalent signature to _bulk_update, but will also call coroutine callbacks.

Reactive properties cannot async change callbacks, but this can be compensated for by supplying the properties a callback should respond to by adding it as an argument to on_change:

from reactive import ReactiveOwner, ReactiveProperty

class Foo(ReactiveOwner):
    def __init__(self):
        super().__init__()
        self.name = ReactiveProperty("Foo")
        self.age = ReactiveProperty(6)

foo = Foo()

async def on_change(*args):
    for arg in args: print(arg.name, "updated to", arg.value)

async def on_name_change(*args):
    print("Name updated from", args[0].history[-1], "to", args[0].value)

foo.on_change(on_update, foo.name, foo.age)
foo.on_change(on_name_update, foo.name)

foo._async_bulk_update(name="Bar", age=12)

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

ReactivePy-2.0.0.dev0.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

ReactivePy-2.0.0.dev0-py3-none-any.whl (17.9 kB view details)

Uploaded Python 3

File details

Details for the file ReactivePy-2.0.0.dev0.tar.gz.

File metadata

  • Download URL: ReactivePy-2.0.0.dev0.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.3

File hashes

Hashes for ReactivePy-2.0.0.dev0.tar.gz
Algorithm Hash digest
SHA256 d811e491a22ce480b6fb9b7969ede1bdb34bcc2034bf5e5bcf119bedab29a98a
MD5 7f002f3696607635343a3a7dd88664b8
BLAKE2b-256 56e51421d7530933ecfeeba9aa79a12aa90379dbee2f4514058dbf7a1c89490b

See more details on using hashes here.

File details

Details for the file ReactivePy-2.0.0.dev0-py3-none-any.whl.

File metadata

  • Download URL: ReactivePy-2.0.0.dev0-py3-none-any.whl
  • Upload date:
  • Size: 17.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.3

File hashes

Hashes for ReactivePy-2.0.0.dev0-py3-none-any.whl
Algorithm Hash digest
SHA256 30b43d8b58a117e9dfc1c98601650c038330483b0f24c279557e219a532ed823
MD5 d0dd47d1d02f21b4f46ee20e0510bfe7
BLAKE2b-256 17e44a70baf19270a34c64b2b1c0ca2898a72223f2517e7cf33a908b13dc3f26

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page