A package to manage interdependent fields
Project description
Revalidate
A package to manage interdependent fields.
Installation
Install the package with pip:
pip install revalidate
What revalidate does
revalidate is useful when some values in a class are derived from other values.
For example, in a shopping cart:
price,quantity, anddiscount_percentare user inputs,subtotal,discount_amount, andtotalare derived values.
You only want to recompute derived values when needed, and only if one of their dependencies changed.
That is exactly what revalidate handles.
Define your class
Here's a complete CartItem class that uses revalidate:
# coding=utf-8
from revalidate import variable, VariableBase, compute
class CartItem(VariableBase):
# Inputs users can set
price: float = variable()
quantity: int = variable()
discount_percent: float = variable()
# Derived values with their dependencies
subtotal = variable(depends_on=["price", "quantity"], read_only=True)
discount_amount = variable(depends_on=["subtotal", "discount_percent"], read_only=True)
total = variable(depends_on=["subtotal", "discount_amount"], read_only=True)
def __init__(self, price: float, quantity: int, discount_percent: float = 0.0):
self.price = price
self.quantity = quantity
self.discount_percent = discount_percent
@compute("subtotal")
def _compute_subtotal(self):
print("Computing subtotal")
return self.price * self.quantity
@compute("discount_amount")
def _compute_discount_amount(self):
print("Computing discount_amount")
return self.subtotal * (self.discount_percent / 100.0)
@compute("total")
def _compute_total(self):
print("Computing total")
return self.subtotal - self.discount_amount
CartItem inherits from VariableBase, and each field is declared with variable().
Dependencies tell revalidate what to invalidate when an input changes.
For example, if discount_percent changes, discount_amount and total are invalidated, but subtotal remains valid.
Usage
Here's how it works in practice. First access computes all dependencies:
>>> item = CartItem(price=50.0, quantity=2, discount_percent=10.0)
>>> print(item.total)
Computing total
Computing subtotal
Computing discount_amount
90.0
Access the value again—no recomputation, everything is cached:
>>> print(item.total)
90.0
Change only the discount. Only discount_amount and total are recomputed. subtotal is unaffected:
>>> item.discount_percent = 20.0
>>> print(item.total)
Computing total
Computing discount_amount
80.0
Change the price. This invalidates subtotal, which cascades to all dependent values:
>>> item.price = 60.0
>>> print(item.total)
Computing total
Computing subtotal
Computing discount_amount
96.0
Further reading
See the Github repo for further information.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file revalidate-0.1.1.tar.gz.
File metadata
- Download URL: revalidate-0.1.1.tar.gz
- Upload date:
- Size: 40.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
857cc7edb73fd0dc2c97a46362811dc1f7857c4a3848b80e505c7bd215acbcd2
|
|
| MD5 |
31605640c54dbdac27fe92eac0ad6dd1
|
|
| BLAKE2b-256 |
0763d4a4159d90c1aba586a9c77e110fbba66ae168f85edf8a17311260c9b68d
|
File details
Details for the file revalidate-0.1.1-py3-none-any.whl.
File metadata
- Download URL: revalidate-0.1.1-py3-none-any.whl
- Upload date:
- Size: 22.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e6d61aee3fb5b7b5089e311a87e1f38ebab1e4421c77476a351288b954401371
|
|
| MD5 |
f2d810569dc3f4506ac3a67347ac18f3
|
|
| BLAKE2b-256 |
b55dc41e74bc386036d0afed3be523bba3fafb4186b0b4db9d5575d139cc8de3
|