alasco-formulas 0.1.14__cp312-cp312-macosx_10_12_x86_64.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.
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
from typing import Literal, TypedDict
|
|
2
|
+
|
|
3
|
+
TokenType = Literal[
|
|
4
|
+
"number",
|
|
5
|
+
"operator",
|
|
6
|
+
"symbolreference",
|
|
7
|
+
"function",
|
|
8
|
+
"aggregation",
|
|
9
|
+
"parenthesis",
|
|
10
|
+
"unexpected",
|
|
11
|
+
]
|
|
12
|
+
|
|
13
|
+
class Node(TypedDict):
|
|
14
|
+
"""A node in the formula evaluation tree.
|
|
15
|
+
|
|
16
|
+
Attributes:
|
|
17
|
+
id: Unique identifier for the node
|
|
18
|
+
name: Display name for the node
|
|
19
|
+
formula: Formula string to evaluate (e.g., "=10+5" or "=@symbol1")
|
|
20
|
+
value: The evaluated result as a string (empty before evaluation)
|
|
21
|
+
children: List of child nodes
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
id: str
|
|
25
|
+
name: str
|
|
26
|
+
formula: str
|
|
27
|
+
value: str
|
|
28
|
+
children: list[Node]
|
|
29
|
+
|
|
30
|
+
def evaluate(formula: str) -> str:
|
|
31
|
+
"""Evaluates a single formula string.
|
|
32
|
+
|
|
33
|
+
Args:
|
|
34
|
+
formula: Formula string to evaluate (e.g., "=10+5" or "=@symbol1")
|
|
35
|
+
|
|
36
|
+
Returns:
|
|
37
|
+
The evaluated result as a string, or an error indication.
|
|
38
|
+
"""
|
|
39
|
+
...
|
|
40
|
+
|
|
41
|
+
def evaluate_tree(root: Node) -> Node:
|
|
42
|
+
"""Evaluates a tree of nodes with formulas.
|
|
43
|
+
|
|
44
|
+
This function evaluates all formulas in the tree, handling references
|
|
45
|
+
between nodes (e.g., "=@symbol1" references another node's value).
|
|
46
|
+
|
|
47
|
+
Args:
|
|
48
|
+
root: A Node dictionary representing the root of the tree to evaluate
|
|
49
|
+
|
|
50
|
+
Returns:
|
|
51
|
+
The same tree structure with all 'value' fields populated with evaluation results
|
|
52
|
+
|
|
53
|
+
Raises:
|
|
54
|
+
ValueError: If the input cannot be parsed or is invalid
|
|
55
|
+
"""
|
|
56
|
+
...
|
|
57
|
+
|
|
58
|
+
def get_tokens(formula: str) -> list[tuple[str, TokenType]]:
|
|
59
|
+
"""Parses a formula string into tokens with preserved number formats.
|
|
60
|
+
|
|
61
|
+
This function parses a formula and returns a list of tokens, where each token
|
|
62
|
+
contains the original string value and its type. Numbers preserve their original
|
|
63
|
+
decimal separator (comma or point) for locale-based formatting.
|
|
64
|
+
|
|
65
|
+
Args:
|
|
66
|
+
formula: Formula string to tokenize (e.g., "=3,14 + 2.5")
|
|
67
|
+
|
|
68
|
+
Returns:
|
|
69
|
+
A list of tuples, where each tuple contains (value, token_type)
|
|
70
|
+
|
|
71
|
+
Raises:
|
|
72
|
+
ValueError: If the input cannot be parsed or is invalid
|
|
73
|
+
"""
|
|
74
|
+
...
|
|
75
|
+
|
|
76
|
+
def replace_symbol(root: Node, old_symbol: str, new_symbol: str) -> Node:
|
|
77
|
+
"""Replaces all occurrences of a symbol reference with a new symbol in a node tree.
|
|
78
|
+
|
|
79
|
+
This function recursively traverses the node tree and replaces all references
|
|
80
|
+
to the old symbol (e.g., "@oldId" or "@{old item}") with the new symbol
|
|
81
|
+
(e.g., "@newId" or "@{new item}") in all formula strings. The function preserves
|
|
82
|
+
extra properties on nodes that are not part of the standard Node interface.
|
|
83
|
+
|
|
84
|
+
Args:
|
|
85
|
+
root: A Node dictionary representing the root of the tree
|
|
86
|
+
old_symbol: The symbol identifier to replace (e.g., "oldId" or "old item")
|
|
87
|
+
new_symbol: The new symbol identifier to use (e.g., "newId" or "new item")
|
|
88
|
+
|
|
89
|
+
Returns:
|
|
90
|
+
The same tree structure with all symbol references updated
|
|
91
|
+
|
|
92
|
+
Raises:
|
|
93
|
+
ValueError: If the input cannot be parsed or is invalid
|
|
94
|
+
"""
|
|
95
|
+
...
|
|
Binary file
|
alasco_formulas/py.typed
ADDED
|
File without changes
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
alasco_formulas-0.1.14.dist-info/METADATA,sha256=aL5og2oZyaoS_10sWwkgN8aDA6etSgT3_eyjsObJ8NM,84
|
|
2
|
+
alasco_formulas-0.1.14.dist-info/WHEEL,sha256=MaIE2g33Ws18PHdThiZqBSHdbWSY1CDh26xDJXEp6io,107
|
|
3
|
+
alasco_formulas/__init__.py,sha256=bC7Umk4oXd5MezocVdASaqu_JngtBwVKBfTtw9hXCcM,143
|
|
4
|
+
alasco_formulas/__init__.pyi,sha256=M2gkTOWlEx-1vD4ftSItYMhfhmwvG-qsR4xHxljJBqI,2948
|
|
5
|
+
alasco_formulas/alasco_formulas.cpython-312-darwin.so,sha256=aaaTEzcFqoCzyJQhpERP-seOJQwqnaeYIU3t6VWt0dY,667640
|
|
6
|
+
alasco_formulas/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
7
|
+
alasco_formulas-0.1.14.dist-info/RECORD,,
|