codekingpro/portable-devtools
114k
1from collections.abc import Callable, Mapping
2from enum import Enum
3from typing import Any, Generic, Literal as L, ParamSpec, Self, TypeAlias, overload
4from typing_extensions import TypeVar
5
6__all__ = ["Expr"]
7
8###
9
10_Tss = ParamSpec("_Tss")
11_ExprT = TypeVar("_ExprT", bound=Expr)
12_ExprT1 = TypeVar("_ExprT1", bound=Expr)
13_ExprT2 = TypeVar("_ExprT2", bound=Expr)
14_OpT_co = TypeVar("_OpT_co", bound=Op, default=Op, covariant=True)
15_LanguageT_co = TypeVar("_LanguageT_co", bound=Language, default=Language, covariant=True)
16_DataT_co = TypeVar("_DataT_co", default=Any, covariant=True)
17_LeftT_co = TypeVar("_LeftT_co", default=Any, covariant=True)
18_RightT_co = TypeVar("_RightT_co", default=Any, covariant=True)
19
20_RelCOrPy: TypeAlias = L["==", "!=", "<", "<=", ">", ">="]
21_RelFortran: TypeAlias = L[".eq.", ".ne.", ".lt.", ".le.", ".gt.", ".ge."]
22
23_ToExpr: TypeAlias = Expr | complex | str
24_ToExprN: TypeAlias = _ToExpr | tuple[_ToExprN, ...]
25_NestedString: TypeAlias = str | tuple[_NestedString, ...] | list[_NestedString]
26
27###
28
29class OpError(Exception): ...
30class ExprWarning(UserWarning): ...
31
32class Language(Enum):
33 Python = 0
34 Fortran = 1
35 C = 2
36
37class Op(Enum):
38 INTEGER = 10
39 REAL = 12
40 COMPLEX = 15
41 STRING = 20
42 ARRAY = 30
43 SYMBOL = 40
44 TERNARY = 100
45 APPLY = 200
46 INDEXING = 210
47 CONCAT = 220
48 RELATIONAL = 300
49 TERMS = 1_000
50 FACTORS = 2_000
51 REF = 3_000
52 DEREF = 3_001
53
54class RelOp(Enum):
55 EQ = 1
56 NE = 2
57 LT = 3
58 LE = 4
59 GT = 5
60 GE = 6
61
62 @overload
63 @classmethod
64 def fromstring(cls, s: _RelCOrPy, language: L[Language.C, Language.Python] = ...) -> RelOp: ...
65 @overload
66 @classmethod
67 def fromstring(cls, s: _RelFortran, language: L[Language.Fortran]) -> RelOp: ...
68
69 #
70 @overload
71 def tostring(self, /, language: L[Language.C, Language.Python] = ...) -> _RelCOrPy: ...
72 @overload
73 def tostring(self, /, language: L[Language.Fortran]) -> _RelFortran: ...
74
75class ArithOp(Enum):
76 POS = 1
77 NEG = 2
78 ADD = 3
79 SUB = 4
80 MUL = 5
81 DIV = 6
82 POW = 7
83
84class Precedence(Enum):
85 ATOM = 0
86 POWER = 1
87 UNARY = 2
88 PRODUCT = 3
89 SUM = 4
90 LT = 6
91 EQ = 7
92 LAND = 11
93 LOR = 12
94 TERNARY = 13
95 ASSIGN = 14
96 TUPLE = 15
97 NONE = 100
98
99class Expr(Generic[_OpT_co, _DataT_co]):
100 op: _OpT_co
101 data: _DataT_co
102
103 @staticmethod
104 def parse(s: str, language: Language = ...) -> Expr: ...
105
106 #
107 def __init__(self, /, op: Op, data: _DataT_co) -> None: ...
108
109 #
110 def __lt__(self, other: Expr, /) -> bool: ...
111 def __le__(self, other: Expr, /) -> bool: ...
112 def __gt__(self, other: Expr, /) -> bool: ...
113 def __ge__(self, other: Expr, /) -> bool: ...
114
115 #
116 def __pos__(self, /) -> Self: ...
117 def __neg__(self, /) -> Expr: ...
118
119 #
120 def __add__(self, other: Expr, /) -> Expr: ...
121 def __radd__(self, other: Expr, /) -> Expr: ...
122
123 #
124 def __sub__(self, other: Expr, /) -> Expr: ...
125 def __rsub__(self, other: Expr, /) -> Expr: ...
126
127 #
128 def __mul__(self, other: Expr, /) -> Expr: ...
129 def __rmul__(self, other: Expr, /) -> Expr: ...
130
131 #
132 def __pow__(self, other: Expr, /) -> Expr: ...
133
134 #
135 def __truediv__(self, other: Expr, /) -> Expr: ...
136 def __rtruediv__(self, other: Expr, /) -> Expr: ...
137
138 #
139 def __floordiv__(self, other: Expr, /) -> Expr: ...
140 def __rfloordiv__(self, other: Expr, /) -> Expr: ...
141
142 #
143 def __call__(
144 self,
145 /,
146 *args: _ToExprN,
147 **kwargs: _ToExprN,
148 ) -> Expr[L[Op.APPLY], tuple[Self, tuple[Expr, ...], dict[str, Expr]]]: ...
149
150 #
151 @overload
152 def __getitem__(self, index: _ExprT | tuple[_ExprT], /) -> Expr[L[Op.INDEXING], tuple[Self, _ExprT]]: ...
153 @overload
154 def __getitem__(self, index: _ToExpr | tuple[_ToExpr], /) -> Expr[L[Op.INDEXING], tuple[Self, Expr]]: ...
155
156 #
157 def substitute(self, /, symbols_map: Mapping[Expr, Expr]) -> Expr: ...
158
159 #
160 @overload
161 def traverse(self, /, visit: Callable[_Tss, None], *args: _Tss.args, **kwargs: _Tss.kwargs) -> Expr: ...
162 @overload
163 def traverse(self, /, visit: Callable[_Tss, _ExprT], *args: _Tss.args, **kwargs: _Tss.kwargs) -> _ExprT: ...
164
165 #
166 def contains(self, /, other: Expr) -> bool: ...
167
168 #
169 def symbols(self, /) -> set[Expr]: ...
170 def polynomial_atoms(self, /) -> set[Expr]: ...
171
172 #
173 def linear_solve(self, /, symbol: Expr) -> tuple[Expr, Expr]: ...
174
175 #
176 def tostring(self, /, parent_precedence: Precedence = ..., language: Language = ...) -> str: ...
177
178class _Pair(Generic[_LeftT_co, _RightT_co]):
179 left: _LeftT_co
180 right: _RightT_co
181
182 def __init__(self, /, left: _LeftT_co, right: _RightT_co) -> None: ...
183
184 #
185 @overload
186 def substitute(self: _Pair[_ExprT1, _ExprT2], /, symbols_map: Mapping[Expr, Expr]) -> _Pair[Expr, Expr]: ...
187 @overload
188 def substitute(self: _Pair[_ExprT1, object], /, symbols_map: Mapping[Expr, Expr]) -> _Pair[Expr, Any]: ...
189 @overload
190 def substitute(self: _Pair[object, _ExprT2], /, symbols_map: Mapping[Expr, Expr]) -> _Pair[Any, Expr]: ...
191 @overload
192 def substitute(self, /, symbols_map: Mapping[Expr, Expr]) -> _Pair: ...
193
194class _FromStringWorker(Generic[_LanguageT_co]):
195 language: _LanguageT_co
196
197 original: str | None
198 quotes_map: dict[str, str]
199
200 @overload
201 def __init__(self: _FromStringWorker[L[Language.C]], /, language: L[Language.C] = ...) -> None: ...
202 @overload
203 def __init__(self, /, language: _LanguageT_co) -> None: ...
204
205 #
206 def finalize_string(self, /, s: str) -> str: ...
207
208 #
209 def parse(self, /, inp: str) -> Expr | _Pair: ...
210
211 #
212 @overload
213 def process(self, /, s: str, context: str = "expr") -> Expr | _Pair: ...
214 @overload
215 def process(self, /, s: list[str], context: str = "expr") -> list[Expr | _Pair]: ...
216 @overload
217 def process(self, /, s: tuple[str, ...], context: str = "expr") -> tuple[Expr | _Pair, ...]: ...
218 @overload
219 def process(self, /, s: _NestedString, context: str = "expr") -> Any: ... # noqa: ANN401
220 