-
Notifications
You must be signed in to change notification settings - Fork 0
/
budayaKB_model.py
258 lines (203 loc) · 6.14 KB
/
budayaKB_model.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
"""
The Model component of BudayaKB app.
Contains two classes:
- class BudayaItem : the representation of a data in BudayaKB app
- class BudayaCollection: the representation of the collection of BudataItem objects
Author: Ika Alfina ([email protected])
Last update: 26 November 2019
"""
import csv
"""
class BudayaItem
This class represents a data in BudayaKB app.
The data contains 4 field:
- nama (unique)
- tipe (the type of the data)
- prov (the province)
- URL
"""
class BudayaItem:
def __init__(self, nama = "", tipe = "", prov = "", url = ""):
"""
The constructor of BudayaItem
"""
self.nama = nama
self.tipe = tipe
self.prov = prov
self.url = url
def __str__(self):
"""
Return a string that deescribes an instance of BudayaItem
"""
return self.nama + ", " + self.tipe + ", " + self.prov + ", " + self.url
def __lt__(self, anotherBudayaItem):
"""
Override "less than" operation, so that this object can be sorted by "nama" field
"""
return self.nama < anotherBudayaItem.nama
def __eq__(self, anotherBudayaItem):
"""
Override "equal" operation, so that this object can be sorted by "nama" field
"""
return self.nama == anotherBudayaItem.nama
"""
class BudayaCollection
This class represents the data structure that stores the BudayaKB data
List of operations:
- import and export from a CSV file
- search by "nama", "tipe", and "prov"
- add, update and delete
- statistics (data size, data size by tipe and by prov)
"""
class BudayaCollection:
def __init__(self, koleksi = {}):
"""
The constructor of BudayaCollection object
"""
self.koleksi = koleksi
def importFromCSV(self, fileName):
"""
to import data from a CSV file, and create the BudayaCollection object
return the number of data imported
"""
with open(fileName) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
for line in csv_reader:
if len(line) == 4 and line[0] != "":
budItem = BudayaItem(line[0].strip(), line[1].strip(), line[2].strip(), line[3].strip())
if line[0] not in self.koleksi:
self.koleksi[line[0]] = budItem
def exportToCSV(self, fileName):
"""
to export the data from a BudayaCollection object to a CSV file
return None
"""
fh = open(fileName, "w")
resultStr = ""
for value in self.koleksi.values():
resultStr += str(value) + "\n"
print(resultStr, file=fh)
fh.close()
def cariByNama(self, aName):
"""
Return a list contains BudayaItem object of a certain name
"""
result = []
for item in self.koleksi:
if aName.strip().lower() in item.lower().replace("_", " "):
result.append(self.koleksi[item])
return result
def cariByTipe(self, aType):
"""
Return a list contains BudayaItem object of a certain type
"""
result = []
for item in self.koleksi.values():
if aType.strip('').lower() in item.tipe.lower():
result.append(item)
return result
def cariByProv(self, aProv):
"""
Return a list contains BudayaItem object of a certain prov
"""
result = []
for item in self.koleksi.values():
if aProv.strip().lower() in item.prov.lower().replace("_", " "):
result.append(item)
return result
def tambah(self, aName, aTipe, aProv, anURL):
"""
To add a new data to a collection of BudayaItem
return 1 if the new data has a new unique name and the addition has been done
return 0 otherwise, new data is not processed
"""
if aName not in self.koleksi:
newBudayaItem = BudayaItem(aName.strip(), aTipe.strip(), aProv.strip(), anURL.strip())
self.koleksi[aName] = newBudayaItem
return 1
else:
return 0
def hapus(self, aName):
"""
To remove a data to the collection of BudayaItem
return 1 if the removal has been done
return 0 if the data does not exist
"""
if aName in self.koleksi:
self.koleksi.pop(aName.strip())
return 1
else:
return 0
def ubah(self, aName, aTipe, aProv, anURL):
"""
To update a data in the collection of BudayaItem
return 1 if the data tobe updated is in the collection and the update has been done
return 0 if the old data with the same key (name) does not exist
"""
if aName in self.koleksi:
newBudayaItem = BudayaItem(aName.strip(), aTipe.strip(), aProv.strip(), anURL.strip())
self.koleksi[aName] = newBudayaItem
return 1
else:
return 0
def stat(self):
"""
Return the number of item in the collection
"""
return len(self.koleksi)
def statByTipe(self):
"""
Return a dictionary contains the number of occurences of each type
"""
result = {}
for v in self.koleksi.values():
if v.tipe not in result:
result[v.tipe] = 1
else:
result[v.tipe] +=1
return result
def statByProv(self):
"""
Return a dictionary contains the number of occurences of each prov
"""
result = {}
for v in self.koleksi.values():
if v.prov not in result:
result[v.prov] = 1
else:
result[v.prov] +=1
return result
def __str__(self):
"""
Return a string that describe the object
"""
resultStr = ""
for value in self.koleksi.values():
resultStr += str(value) + "\n"
return resultStr
#####################################################################################
# for testing
#####################################################################################
def main():
mydb = BudayaCollection()
#### Test import
print("=================================================")
print("Test Import Data")
mydb.importFromCSV("dataSmall.csv")
print("ImporCSV: Sukses menambahkan {} data baru\n".format(len(mydb.koleksi)))
print(mydb)
#### Test cari
print("=================================================")
print("Test Cari Data")
keyCari = "a"
result = mydb.cariByNama(keyCari)
result.sort()
if len(result) > 0:
print("CariByNama: Ditemukan {} data dengan nama {}".format(len(result), keyCari))
for item in result:
print(item)
print()
else:
print("CariByNama: Tidak ada data dengan nama {}\n".format(keyCari))
if __name__ == "__main__":
main()