-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpression_tree_node.py
More file actions
54 lines (44 loc) · 1.65 KB
/
expression_tree_node.py
File metadata and controls
54 lines (44 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from typing import Optional, Union
from parse_token import Token
import errors
class Node:
__slots__ = ['text', 'position', 'aleft', 'aright', 'token_type', 'power',
'mult', 'num_coef']
def __init__(self, val: Token, type_: str):
self.text: Union[float, str] = val.text
self.position = val.position
self.token_type = None
self.__define_type(type_)
self.aleft: Optional[Node] = None
self.aright: Optional[Node] = None
self.power: float = 1.0
self.mult: float = 1.0
self.num_coef: float = 0.0
def __repr__(self):
if self.mult == 1.0:
text_ = ""
elif self.mult.is_integer():
text_ = f"{int(self.mult)}"
else:
text_ = f"{round(self.mult, 2):.2}"
return f"{text_}" \
f"{self.text.upper()}" \
f"^{int(self.power)}" \
f"{self.num_coef if self.num_coef else ''}"
def __define_type(self, type_: str):
if not self.token_type:
self.token_type = type_
elif type_ == 'var' or self.token_type == 'var':
self.token_type = 'var'
elif type_ in ['op', 'unary'] and self.token_type in ['op', 'unary']:
raise errors.ExpressionTreeError(
"Последовательно несколько операций", self)
else:
self.token_type = 'num'
self.text = float(self.text)
def update_node(self, text: Union[str, float],
type_: Optional[str] = None) \
-> None:
self.text = text
if type:
self.__define_type(type_)