/
CSV_Manager.py
164 lines (144 loc) · 5.42 KB
/
CSV_Manager.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
# File of CSV files manager
#
# Programmer
# Edisson López @ediloaz
# September, 2018
#
import csv
import Tabla as Tabla
class CSV_Manager(object):
def __init__(self):
self.path = "" # with "babylon tower" extention .by
self.initial_table = Tabla.getTablaInicial()
self.goal_table = Tabla.getTablaMeta()
def getInitialTable(self):
return self.initial_table
def setPath(self, path):
self.path = path
def getPath(self):
return selfpath
def IsPath(self):
if (self.path[-3:] == ".by" or self.path[-4:] == ".by/"):
return True
else:
return False
def LoadDataOLD(self):
try:
with open(self.path) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
line_count = 0
self.initial_table = []
self.goal_table = []
for row in csv_reader:
if (line_count == 0):
count = 0
temp_list = []
for i in row:
if (count < 3):
temp_list += [i.upper()]
count += 1
else:
temp_list += [i.upper()]
self.initial_table += [temp_list]
temp_list = []
count = 0
elif (line_count == 1):
count = 0
temp_list = []
for i in row:
if (count < 3):
temp_list += [i.upper()]
count += 1
else:
temp_list += [i.upper()]
self.goal_table += [temp_list]
temp_list = []
count = 0
line_count += 1
if (line_count > 2):
print ("The file had more lines. Just take the two first")
except:
print("The file not existed")
def LoadData(self):
with open(self.path) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
line_count = 0
for row in csv_reader:
if (line_count == 0):
self.initial_table.Llenar(row)
elif (line_count == 1):
self.goal_table.Llenar(row)
line_count += 1
if (line_count > 2):
print ("The file had more lines. Just take the two first")
def getInitialTable(self):
return self.initial_table
def getGoalTable(self):
return self.goal_table
def PrintInitialTable(self):
print()
print("Initial Table")
self.initial_table.PrintTorreDetallada()
print(" - - - - - - - ")
def PrintGoalTable(self):
print()
print("Goal Table")
self.goal_table.PrintTorreDetallada()
print(" - - - - - - - ")
# Check the format of Initial table
def CorrectFormatInitialOLD(self):
r_color = 0 # it must be 4
g_color = 0 # it must be 4
b_color = 0 # it must be 4
y_color = 0 # it must be 4
x_color = 0 # it must be 3
o_color = 0 # it must be 1
for row in self.initial_table:
r_color += row.count("R")
g_color += row.count("G")
b_color += row.count("B")
y_color += row.count("Y")
x_color += row.count("X")
o_color += row.count("O")
if (r_color == 4 and g_color == 4 and b_color == 4 and
y_color == 4 and x_color == 3 and o_color == 1):
return True
else:
return False
# Check the format of Goal table
def CorrectFormatGoalOLD(self):
r_color = 0 # it must be 4
g_color = 0 # it must be 4
b_color = 0 # it must be 4
y_color = 0 # it must be 4
x_color = 0 # it must be 3
o_color = 0 # it must be 1
for row in self.goal_table:
r_color += row.count("R")
g_color += row.count("G")
b_color += row.count("B")
y_color += row.count("Y")
x_color += row.count("X")
o_color += row.count("O")
if (r_color == 4 and g_color == 4 and b_color == 4 and
y_color == 4 and x_color == 3 and o_color == 1):
return True
else:
return False
def CorrectFormat(self):
print("Formato de la tabla inicial: ", self.initial_table.CorrectFormat())
print("Formato de la tabla meta: ", self.goal_table.CorrectFormat())
return (self.initial_table.CorrectFormat()[0] and self.goal_table.CorrectFormat()[0])
# return -2 => Wrong path
# return -1 => Wrong format
# return 0 => Good
def LoadCSV(self, path):
self.setPath(path)
if not (self.IsPath()):
return -2 # wrong path
self.LoadData()
if not (self.CorrectFormat()):
return -1 # wrong format
return 0
# csv_file = CSV_Manager()
# print(csv_file.LoadCSV("./data.by"))