Skip to main content

A simple library for functional reactive programming in Python

Project description

pypagate

What is pypagate?

Imagine you are working in a spreadsheet editor such as Excel. You write in a bunch of test scores and then use a formula to calculate the average of all the students. You mistakenly gave one student the wrong score. No worries, you update the score and the average is magically updated.

Now, imagine you are working with Python to do the same thing. You input the student scores in a list and calculate the mean:

>>> from statistics import mean
>>> scores = [80, 85, 95, 100]
>>> mean(scores)
90

You then realized you need to update the score of one student, so you change the corresponding entry in scores but unlike the Excel scenario, the mean is not magically updated. Sure, you could call mean again and recalculate the new scores, but would it not be cool if you just had a variable that always updated accordingly?

Enter functional reactive programming

The core idea in this library is that the cells in Excel which contain data we manually update, and there are cells in Excel which update based on data. The cells we manually update we refer to as Terms and the cells that automatically update are referred to as Formulas.

Let us look at a small example:

>>> from pypagate import Term
>>> x = Term(5)
>>> y = x + 1
>>> print(x.unwrap()) # (Call unwrap to get the value x currently stores)
5
>>> print(y.unwrap())
6
>>> x += 1
>>> print(y.unwrap())
7

y magically changed even though we never touched it. This is the power of functional reactive programming.

But wait, there's more!

An event listener is a construct that waits for an event to happen and then executes some code when the event does happen. pypagate gives an elegant way to make new event listeners and organize your code.

Consider we have a player object that has some health:

>>> from pypagate import Term, fire_on
>>> class Player:
...     def __init__(self, health):
...         self.health = Term(health)
...
>>> player = Player(3)
>>> @fire_on(player.health == 0)
>>> def game_over():
...     print("Game over")
>>> player.health -= 1
>>> player.health -= 1
>>> player.health -= 1
Game over

The fire_on decorator waits for a formula to evaluate to True and then fires the corresponding function. Now, we can compose sophisticated event listeners using simple formula!

Documentation

Further documentation can be found on readthedocs.

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

pypagate-0.1.0.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

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

pypagate-0.1.0-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file pypagate-0.1.0.tar.gz.

File metadata

  • Download URL: pypagate-0.1.0.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pypagate-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e8eeb92f9eb5e1d2c0772f38d235ee1e437a4b2e2ef2978523020163302b7b6f
MD5 983f1ee79fb688e2011c6f1f4b91b79f
BLAKE2b-256 2ec96342836a3d712455398dc7bcda5809783254bd8f9d8ba5027af32122588f

See more details on using hashes here.

Provenance

The following attestation bundles were made for pypagate-0.1.0.tar.gz:

Publisher: python-publish.yml on thyrgle/pypagate

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pypagate-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pypagate-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pypagate-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 64392a607b1d6c7aecb5ced0c5f6c5badb0d581fc6007a7e34be0556f69f789e
MD5 bb6f2a9e44e1b0981b4a2ee0cbc1f142
BLAKE2b-256 e3b7324cc78bef38fabc8879fe3359c7a678655ab54daefa676b6fb25088968a

See more details on using hashes here.

Provenance

The following attestation bundles were made for pypagate-0.1.0-py3-none-any.whl:

Publisher: python-publish.yml on thyrgle/pypagate

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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