-
Notifications
You must be signed in to change notification settings - Fork 0
/
line.py
178 lines (135 loc) · 4.32 KB
/
line.py
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
from itertools import count
from typing import Dict, List
from excel import BaseLine, BaseNode, Type
from utils import isDataEnd, isDataStart, isInfoEnd, isInfoStart
class TextNode(BaseNode):
def __init__(self, text: str) -> None:
self.text = text
def getText(self) -> str:
return self.text
class ResultNode(BaseNode):
type = Type.RESULT
def __init__(self, text: str) -> None:
self.text = text
def getText(self) -> str:
return self.text
class ValueNode(BaseNode):
type = Type.VALUE
def __init__(self, count: str, value: str) -> None:
super().__init__()
self.count = count
self.value = value
def getText(self) -> str:
return self.count
def getValue(self) -> str:
return round(float(self.count) * float(self.value), 2)
class TitleLine(BaseLine):
nodes: List[BaseNode] = []
price: Dict[str, str] = {}
def __init__(self, source: str) -> None:
super().__init__()
self.source = source
self.parse()
def parse(self):
text = self.source
texts = []
value = ''
goodsIndex = 0
infoIndex = 0
# parse text
for i in text:
if isInfoStart(i):
value = ''
elif isInfoEnd(i):
infoIndex += 1
texts.append(value)
value = ''
elif isDataStart(i):
value = ''
elif isDataEnd(i):
goodsIndex += 1
[name, v] = value.split(":")
texts.append(name)
self.price[str(goodsIndex)] = v
value = ''
else:
value += i
self.nodes = list(map(lambda x: TextNode(x), texts))
self.goodsNum = goodsIndex
self.infoIndex = infoIndex
def addEnd(self, text: str) -> None:
self.nodes.append(TextNode(text))
def getList(self) -> List[BaseNode]:
return self.nodes
class DataLine(BaseLine):
nodes: List[BaseNode] = []
def __init__(self, source: str, goodsNum: float, price: Dict[str, str]) -> None:
super().__init__()
self.source = source
self.goodsNum = goodsNum
self.price = price
self.parse()
def parse(self):
text = self.source
goods: Dict[str, str] = {}
nodes: List[BaseNode] = []
value = ''
# parse text
for i in text:
if isInfoStart(i):
value = ''
elif isInfoEnd(i):
nodes.append(TextNode(value))
value = ''
elif isDataStart(i):
value = ''
elif isDataEnd(i):
[id, count] = value.split("*")
goods[id] = count
value = ''
else:
value += i
for goodIndex in range(1, self.goodsNum + 1):
goodCount = goods.get(str(goodIndex))
if goodCount:
goodValue = self.price[str(goodIndex)]
nodes.append(ValueNode(str(goodCount), str(goodValue)))
else:
nodes.append(ValueNode('0', '0'))
self.nodes = nodes
def addEnd(self) -> None:
v = 0
for i in self.nodes:
v += i.getValue()
self.nodes.append(ResultNode(str(v)))
def getList(self) -> List[BaseNode]:
return self.nodes
class StatisticLine(BaseLine):
nodes: List[BaseNode] = []
def __init__(self, values: List[float]) -> None:
super().__init__()
self.values = values
self.parse()
def parse(self):
nodes: List[BaseNode] = []
for i in self.values:
nodes.append(ResultNode(str(i)))
self.nodes = nodes
def addEnd(self, text: str) -> None:
self.nodes.append(ResultNode(str(text)))
def getList(self) -> List[BaseNode]:
return self.nodes
def countLine(ls: List[BaseLine]):
values = []
lenLength = len(ls[0].getList())
for index in range(lenLength):
v = 0
for l in ls:
list = l.getList()
node = list[index]
if node.type == Type.VALUE:
v += float(node.getText())
elif node.type == Type.RESULT:
v += float(node.getText())
values.append(v)
return values