generated from github/codespaces-blank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ibukota.py
280 lines (203 loc) · 6.52 KB
/
ibukota.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import os
def menu_update(menu):
alert = ' (PERLU DIBACA ULANG!!)'
if menu[0].endswith(alert):
return menu
menu[0] += alert
menu[1] += alert
return menu
def refresh(alert):
if alert:
alert = False
return alert
def main():
default_menu = ['1. Baca Semua dari File: "ibukota.txt"', '2. Baca Tertentu dari File: "ibukota.txt"', '3. Tampilkan Hasil Baca','4. Tambahkan di File: "ibukota.txt"', '5. Sisipkan di File: "ibukota.txt"', '6. Hapus di File: "ibukota.txt"', '7. Simpan Sebagai File Baru', '8. Keluar']
menu = list(default_menu)
refresh_alert = False
data = None
baris = None
max_negara = 0
max_ibukota = 0
while True:
display_menu(menu)
print()
masukan = get_choice('Pilih dari [1-8]? ')
print()
match masukan:
case '1':
print('>>>Sedang membaca...')
baris, refresh_alert, extract = read_file(refresh_alert)
data = extract[0]
max_negara, max_ibukota = extract[1]
print('>>> Selesai membaca...\n')
menu = list(default_menu)
case '2':
print('>>>Sedang membaca...\n')
baris, refresh_alert, extract = read_spec(refresh_alert)
data = extract[0]
max_negara, max_ibukota = extract[1]
print('>>> Selesai membaca...\n')
menu = list(default_menu)
case '3':
display_data(data, max_negara, max_ibukota)
print()
case '4':
max_negara, max_ibukota = write_data(max_negara, max_ibukota)
menu = menu_update(menu)
refresh_alert = True
case '5':
refresh_alert = insert_data(data)
print()
print('>>>Selesai menyisipi...')
if refresh_alert:
menu = menu_update(menu)
case '6':
refresh_alert = delete_data(data)
print( '>>> Selesai menghapus...')
print
if refresh_alert:
menu = menu_update(menu)
case '7':
print(baris)
new_save(baris)
print()
print('Selesai mengcopy file...')
case '8':
break
print('=' * 50)
print()
def display_menu(menu):
print('<<< M E N U >>>')
print(*menu, sep = '\n')
def get_choice(prompt : str) -> str:
masukan = ''
while not masukan.isdigit():
masukan = input(prompt).strip()
return masukan
def read_file(alert : bool) -> list: #choice 1
with open('ibukota.txt', 'r') as file:
baris = file.readlines()
return baris, refresh(alert), get_data(baris)
def read_spec(alert : bool) -> list: # choice 2
mulai = int(get_choice('Mulai baris? ')) - 1
sampai = int(get_choice('Sampai baris? ')) - 1
print()
if mulai > sampai:
print('Invalid Input')
return read_spec(alert)
with open('ibukota.txt', 'r') as file:
lines = []
for line_index, line in enumerate(file, start = 0):
if mulai <= line_index <= sampai:
lines.append(line)
elif line_index > sampai:
print('EOF reached!!')
break
return lines, refresh(alert), get_data(lines)
def get_data(baris : list) -> list[str]:
table_data = []
maks_negara = set()
maks_ibukota = set()
for row in baris:
negara, ibukota = row.strip().split(';')
maks_negara.add(len(negara))
maks_ibukota.add(len(ibukota))
table_data.append([negara, ibukota])
maks_negara = max(maks_negara) + 10
maks_ibukota = max(maks_ibukota) + 5
return table_data, (maks_negara, maks_ibukota)
def display_data(data : list[str, str], maks_negara : int, maks_ibukota : int) -> None: #choice 3
if data is not None:
lines = maks_negara + maks_ibukota + 4
print('-' * lines)
print('{:<4} {:<{}} {:<{}}'.format('No.', 'Negara', maks_negara, 'Ibukota', maks_ibukota))
print('=' * lines)
for number, (negara, ibukota) in enumerate(data, start = 1):
number_str = '%2d ' % number
print(f'{number_str} {negara.ljust(maks_negara)} {ibukota.ljust(maks_ibukota)}')
print('-' * lines)
else:
print('Pilih menu 1 sebelum menampilkan hasil baca!')
def write_data(max_negara : int, max_ibukota : int) -> None: #choice 4
print('>>> Sedang menambahkan...')
new_negara = 'p'
new_ibukota = 'p'
add = []
while new_negara != '' and new_ibukota != '':
new_negara = input('Negara? ').strip().title()
new_ibukota = input('Ibukota? ').strip().title()
max_negara = max(len(new_negara), max_negara)
max_ibukota = max(len(new_ibukota), max_ibukota)
add.append(new_negara + ';' + new_ibukota)
print()
add.pop()
with open('ibukota.txt', 'a') as file:
for line in add:
file.write((line) + '\n')
print()
print('>>> Selesai menambahkan...')
return max_negara, max_ibukota
def insert_data(data : list[str, str]) -> None: #choice 5
print('>>>Sedang menyisipi...')
if data is not None:
negara = input('Negara? ').strip().title()
ibukota = input('Ibukota? ').strip().title()
sisip = input('Disisipi setelah negara? ').strip().title()
index = 0
for line in data:
index += 1
if line[0] == sisip:
break
elif index >= len(data):
print('Negara tidak ditemukan!! Gagal menyisipkan!')
return False
data.insert(index, [negara, ibukota])
with open('ibukota.txt', 'w') as file:
for row in data:
file.write(f'{row[0]};{row[1]}\n')
else:
print('Pilih menu 1 sebelum menyisipi negara baru!')
return False
return True
def delete_data(data : list[str, str]) -> None: # choice 6
print('>>> Sedang menghapus...')
if data is not None:
del_negara = input('Negara? ').strip().title()
index = -1
for line in data:
index += 1
if line[0] == del_negara:
del data[index]
break
elif index >= len(data) - 1:
print('Negara tidak ditemukan! Gagal menghapus!!')
return False
with open('ibukota.txt', 'w') as file:
for row in data:
file.write(f'{row[0]};{row[1]}\n')
return True
else:
print('Pilih menu 1 sebelum menghapus negara!')
return False
return True
def new_save(baris : list) -> None:
if baris is not None:
new_file = input('Simpan sebagai file? ').strip()
if os.path.isfile(new_file):
pilih = input(f'File dengan nama {new_file} sudah ada! Teruskan [Y/T]? ').strip().lower()
match pilih:
case 'y':
pass
case 't':
print('Gagal mengcopy!!')
return False
case _:
print('Invalid input')
return new_save(baris)
with open(new_file, 'w') as file:
for row in baris:
file.write(row)
else:
print('Pilih menu 1 sebelum mengcopy data ke file baru!')
if __name__ == '__main__':
main()