Skip to main content

Reactive state management for python

Project description

Nuclear - Reactive state management for python

Atoms and Derives

The base element in nuclear is called an atom. You can create one with atom({starting value}).

To create a derivative of an atom (or of another derivative) you can user the derivative({source}) function.

To get or set any atoms or derivatives, you can use the .set() and .get() methods.

To add reactions to changed, you can use the .react() method.

Example program:

base = atom(10) #Creates an atom with value 10
derived = derive(lambda: base.get() * 2) #Creates a derive which will always be equal to base * 2
derived.react(lambda: print('The value of derived was changed))

print(base.get()) # prints 10
print(derived.get()) # prints 20

base.set(20) # prints 'The value of derived was changed'
print(derived.get()) # prints 40

Events and Messages

To allow nuclear to implicitly send events and message, you must initialise it with nuclear.init(globals()).

To create a class that can recieve and send messages, it needs to inherit from Receiver.

To respond to events or messages, you can add methods to your class that follow the format:

For events, on_{your event name}

For messages, receive_{your message name}

Then to send events and messages you can use:

For events, broadcast({your event name})

For messages, send({your message name}, {data})

Example program:

nuclear.init(globals())

class TestClass(Receiver):
    def __init__(self):
        pass
    def on_TestEvent(self):
        print('Test Event received')
    def receive_TestMessage(self, data):
        print(f'TestMessage sent data {data}')

test1 = TestClass()

broadcast('TestEvent') # prints 'Test Event received'
send('TestMessage', 200) # prints 'TestMessage sent data 200'

You can also change the format with nuclear.format({events}, {messages}).

For better performance, you can change to explicit mode with nuclear.eventMode('explicit'). If you do this, you must tag every object that will receive an event or message with nuclear.event_r({event name}, {object}) or nuclear.message_r({message name}, {object}).

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

nuclearstate-0.0.1.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

nuclearstate-0.0.1-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

Details for the file nuclearstate-0.0.1.tar.gz.

File metadata

  • Download URL: nuclearstate-0.0.1.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

File hashes

Hashes for nuclearstate-0.0.1.tar.gz
Algorithm Hash digest
SHA256 96dc4cd1ac66d1ba8bba8e88726fb1ca6d86b4bf09f6689e0b331e96261ccc55
MD5 ec774af4739350029a05745c61e5cd00
BLAKE2b-256 b74b49edf1bb0ab9e7cbaabb9ee5dacdf08a152f9e53fc4fe9b71a68f059432c

See more details on using hashes here.

File details

Details for the file nuclearstate-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: nuclearstate-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 3.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

File hashes

Hashes for nuclearstate-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b278e707cee24abfb45fce3f20d2bf9b504ca22d36e8598631a002dbf696308c
MD5 00e7213bcd17446bfb91c62cd8ba5ff3
BLAKE2b-256 b6abd102442833dad2ed897c4733f3f253809a741712c91e506d09067a1ccd5c

See more details on using hashes here.

Supported by

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