pyformula 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pyformula/__init__.py +26 -0
- pyformula/compiler/__init__.py +0 -0
- pyformula/compiler/compiler.py +160 -0
- pyformula/compiler/models.py +330 -0
- pyformula/exceptions.py +29 -0
- pyformula/formula.py +181 -0
- pyformula/math.py +562 -0
- pyformula/models.py +13 -0
- pyformula/operator.py +52 -0
- pyformula/registry.py +123 -0
- pyformula/variable.py +60 -0
- pyformula-0.1.0.dist-info/METADATA +941 -0
- pyformula-0.1.0.dist-info/RECORD +15 -0
- pyformula-0.1.0.dist-info/WHEEL +4 -0
- pyformula-0.1.0.dist-info/entry_points.txt +3 -0
pyformula/formula.py
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import decimal
|
|
2
|
+
from collections.abc import Callable
|
|
3
|
+
from typing import cast
|
|
4
|
+
|
|
5
|
+
from .models import Number, Operator
|
|
6
|
+
from .operator import OPERATORS
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class Formula[T]:
|
|
10
|
+
"""Represent a lazily evaluated numeric transformation over an input object.
|
|
11
|
+
|
|
12
|
+
Formula instances can be composed with arithmetic operators, wrapped with
|
|
13
|
+
mathematical functions, and later evaluated by calling the formula with an
|
|
14
|
+
instance of the source type.
|
|
15
|
+
|
|
16
|
+
# Examples:
|
|
17
|
+
```python
|
|
18
|
+
from dataclasses import dataclass
|
|
19
|
+
from pyformula import Formula
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@dataclass
|
|
23
|
+
class Rectangular:
|
|
24
|
+
width: float
|
|
25
|
+
height: float
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def main() -> None:
|
|
29
|
+
width = Formula[Rectangular](lambda rectangle: rectangle.width)
|
|
30
|
+
height = Formula[Rectangular](lambda rectangle: rectangle.height)
|
|
31
|
+
|
|
32
|
+
perimeter = (width + height) * 2
|
|
33
|
+
area = width * height
|
|
34
|
+
|
|
35
|
+
r1 = Rectangular(width=10, height=20)
|
|
36
|
+
print(perimeter(r1)) # 60.0
|
|
37
|
+
print(area(r1)) # 200.0
|
|
38
|
+
|
|
39
|
+
r2 = Rectangular(width=5, height=10)
|
|
40
|
+
print(perimeter(r2)) # 30.0
|
|
41
|
+
print(area(r2)) # 50.0
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
if __name__ == "__main__":
|
|
45
|
+
main()
|
|
46
|
+
```
|
|
47
|
+
"""
|
|
48
|
+
|
|
49
|
+
def __init__(
|
|
50
|
+
self,
|
|
51
|
+
fn: Callable[[T], Number],
|
|
52
|
+
*,
|
|
53
|
+
name: str | None = None,
|
|
54
|
+
) -> None:
|
|
55
|
+
"""Create a formula from a callable that derives a numeric value."""
|
|
56
|
+
self._fn = fn
|
|
57
|
+
self._name = name
|
|
58
|
+
|
|
59
|
+
def __call__(self, obj: T) -> Number:
|
|
60
|
+
"""Evaluate the formula against the provided object."""
|
|
61
|
+
return self._fn(obj)
|
|
62
|
+
|
|
63
|
+
def __add__(self, other: "Formula[T] | Number", /) -> "Formula[T]":
|
|
64
|
+
"""Return the sum of this formula and another value or formula."""
|
|
65
|
+
return self._operate("add", other, reverse=False)
|
|
66
|
+
|
|
67
|
+
def __radd__(self, other: "Formula[T] | Number", /) -> "Formula[T]":
|
|
68
|
+
"""Return the sum of another value and this formula."""
|
|
69
|
+
return self._operate("add", other, reverse=True)
|
|
70
|
+
|
|
71
|
+
def __sub__(self, other: "Formula[T] | Number", /) -> "Formula[T]":
|
|
72
|
+
"""Return this formula minus another value or formula."""
|
|
73
|
+
return self._operate("subtract", other, reverse=False)
|
|
74
|
+
|
|
75
|
+
def __rsub__(self, other: "Formula[T] | Number", /) -> "Formula[T]":
|
|
76
|
+
"""Return another value minus this formula."""
|
|
77
|
+
return self._operate("subtract", other, reverse=True)
|
|
78
|
+
|
|
79
|
+
def __mul__(self, other: "Formula[T] | Number", /) -> "Formula[T]":
|
|
80
|
+
"""Return the product of this formula and another value or formula."""
|
|
81
|
+
return self._operate("multiply", other, reverse=False)
|
|
82
|
+
|
|
83
|
+
def __rmul__(self, other: "Formula[T] | Number", /) -> "Formula[T]":
|
|
84
|
+
"""Return another value multiplied by this formula."""
|
|
85
|
+
return self._operate("multiply", other, reverse=True)
|
|
86
|
+
|
|
87
|
+
def __truediv__(self, other: "Formula[T] | Number", /) -> "Formula[T]":
|
|
88
|
+
"""Return this formula divided by another value or formula."""
|
|
89
|
+
return self._operate("divide", other, reverse=False)
|
|
90
|
+
|
|
91
|
+
def __rtruediv__(self, other: "Formula[T] | Number", /) -> "Formula[T]":
|
|
92
|
+
"""Return another value divided by this formula."""
|
|
93
|
+
return self._operate("divide", other, reverse=True)
|
|
94
|
+
|
|
95
|
+
def __mod__(self, other: "Formula[T] | Number", /) -> "Formula[T]":
|
|
96
|
+
"""Return the modulo of this formula with another value or formula."""
|
|
97
|
+
return self._operate("modulo", other, reverse=False)
|
|
98
|
+
|
|
99
|
+
def __rmod__(self, other: "Formula[T] | Number", /) -> "Formula[T]":
|
|
100
|
+
"""Return the modulo of another value with this formula."""
|
|
101
|
+
return self._operate("modulo", other, reverse=True)
|
|
102
|
+
|
|
103
|
+
def __floordiv__(self, other: "Formula[T] | Number", /) -> "Formula[T]":
|
|
104
|
+
"""Return the floor division of this formula and another value or formula."""
|
|
105
|
+
return self._operate("floor_divide", other, reverse=False)
|
|
106
|
+
|
|
107
|
+
def __rfloordiv__(self, other: "Formula[T] | Number", /) -> "Formula[T]":
|
|
108
|
+
"""Return the floor division of another value by this formula."""
|
|
109
|
+
return self._operate("floor_divide", other, reverse=True)
|
|
110
|
+
|
|
111
|
+
def __pow__(self, other: "Formula[T] | Number", /) -> "Formula[T]":
|
|
112
|
+
"""Return this formula raised to the power of another value or formula."""
|
|
113
|
+
return self._operate("power", other, reverse=False)
|
|
114
|
+
|
|
115
|
+
def __rpow__(self, other: "Formula[T] | Number", /) -> "Formula[T]":
|
|
116
|
+
"""Return another value raised to the power of this formula."""
|
|
117
|
+
return self._operate("power", other, reverse=True)
|
|
118
|
+
|
|
119
|
+
def __pos__(self) -> "Formula[T]":
|
|
120
|
+
"""Return a formula that preserves the value of the current formula."""
|
|
121
|
+
return Formula(self, name=f"+{self}")
|
|
122
|
+
|
|
123
|
+
def __neg__(self) -> "Formula[T]":
|
|
124
|
+
"""Return a formula that negates the current formula's value."""
|
|
125
|
+
return Formula(lambda obj: -self(obj), name=f"-{self}")
|
|
126
|
+
|
|
127
|
+
def __abs__(self) -> "Formula[T]":
|
|
128
|
+
"""Return a formula that computes the absolute value of this formula."""
|
|
129
|
+
return Formula(lambda obj: abs(self(obj)), name=f"|{self}|")
|
|
130
|
+
|
|
131
|
+
def __round__(self, ndigits: int | None = None) -> "Formula[T]":
|
|
132
|
+
"""Return a formula that rounds the current result to the given precision."""
|
|
133
|
+
|
|
134
|
+
def inner(obj: T) -> Number:
|
|
135
|
+
value = self(obj)
|
|
136
|
+
|
|
137
|
+
if isinstance(value, decimal.Decimal):
|
|
138
|
+
rounded = round(value, ndigits)
|
|
139
|
+
if isinstance(rounded, decimal.Decimal):
|
|
140
|
+
return rounded
|
|
141
|
+
|
|
142
|
+
return decimal.Decimal.from_float(rounded)
|
|
143
|
+
|
|
144
|
+
if isinstance(value, float):
|
|
145
|
+
return float(round(value, ndigits))
|
|
146
|
+
|
|
147
|
+
return int(round(value, ndigits))
|
|
148
|
+
|
|
149
|
+
return Formula(inner, name=f"round({self}, {ndigits})")
|
|
150
|
+
|
|
151
|
+
def __str__(self) -> str:
|
|
152
|
+
if self._name is not None:
|
|
153
|
+
return self._name
|
|
154
|
+
|
|
155
|
+
if self._fn.__name__ == "<lambda>":
|
|
156
|
+
return "anonymous"
|
|
157
|
+
|
|
158
|
+
return self._fn.__name__
|
|
159
|
+
|
|
160
|
+
def __repr__(self) -> str:
|
|
161
|
+
return f"Formula({self})"
|
|
162
|
+
|
|
163
|
+
def _operate(
|
|
164
|
+
self,
|
|
165
|
+
operator: Operator,
|
|
166
|
+
other: "Formula[T] | Number",
|
|
167
|
+
*,
|
|
168
|
+
reverse: bool,
|
|
169
|
+
) -> "Formula[T]":
|
|
170
|
+
operator_fn, symbol = OPERATORS[operator]
|
|
171
|
+
other_fn = other if isinstance(other, Formula) else Formula(lambda _: other)
|
|
172
|
+
|
|
173
|
+
def inner(obj: T) -> Number:
|
|
174
|
+
value = self(obj)
|
|
175
|
+
other_value = other_fn(obj)
|
|
176
|
+
return cast(
|
|
177
|
+
"Number",
|
|
178
|
+
(operator_fn(other_value, value)) if reverse else operator_fn(value, other_value),
|
|
179
|
+
)
|
|
180
|
+
|
|
181
|
+
return Formula(inner, name=f"({self} {symbol} {other})")
|