-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdutch.py
151 lines (137 loc) · 4.94 KB
/
dutch.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
import os
import sys
import json
import random
class Card:
def __init__(self, dutch, english):
self.entry = {"dutch": dutch, "english": english}
class Dictionary:
@classmethod
def set_file_path(cls):
directory = os.path.dirname(__file__)
file_path = os.path.join(directory, "dictionary.json")
return file_path
@classmethod
def load_dictionary(cls):
file_path = cls.set_file_path()
with open(file_path, mode="r") as file:
dictionary = json.load(file)
return dictionary
@classmethod
def save_dictionary(cls, dictionary):
file_path = cls.set_file_path()
with open(file_path, "w") as file:
file.write(json.dumps(dictionary, indent=4))
@classmethod
def edit_card(cls, card):
print("\nedit card: \ndutch: {}\nenglish: {}".format(
card["dutch"], card["english"]))
new_dutch = input("\nnew dutch: ")
if new_dutch == "delete()":
return "delete()"
elif new_dutch != "":
card["dutch"] = new_dutch
new_english = input("new english: ")
if new_english == "delete()":
return "delete()"
elif new_english != "":
card["english"] = new_english
return card
@classmethod
def search_card(cls):
dictionary = cls.load_dictionary()
console_input = input("\nplease enter what you are looking for: ")
found_cards = [
card for card in dictionary if card["dutch"] == console_input
or card["english"] == console_input]
if len(found_cards) > 0:
print("these have been found:")
for card in found_cards:
print("dutch: {}\nenglish: {}\n".format(
card["dutch"], card["english"]))
confirmation = input("do you want to edit it? (y/n) ")
if confirmation == "y":
new_card = cls.edit_card(card)
dictionary.remove(card)
if new_card != "delete()":
dictionary.append(new_card)
cls.save_dictionary(dictionary)
else:
print("nothing has been found")
@classmethod
def run_input(cls):
dictionary = cls.load_dictionary()
while True:
dutch = input("\ndutch: ")
if dutch == "quit()":
break
elif dutch == "edit()":
dictionary[-1] = cls.edit_card(dictionary[-1])
if dictionary[-1] == "delete()":
dictionary.pop(-1)
cls.save_dictionary(dictionary)
continue
elif dutch == "search()":
cls.search_card()
continue
english = input("english: ")
if english == "quit()":
break
elif english == "edit()":
continue
elif english == "search()":
cls.search_card()
continue
new_card = Card(dutch, english)
if new_card.entry not in dictionary:
dictionary.append(new_card.entry)
cls.save_dictionary(dictionary)
@classmethod
def run_test(cls):
dictionary = cls.load_dictionary()
completed_cards = []
while True:
pool_cards = [
card for card in dictionary if
card not in completed_cards]
card = random.sample(pool_cards, 1)[0]
coin = random.randint(0, 1)
if coin == 0:
input_language = "dutch"
output_language = "english"
else:
input_language = "english"
output_language = "dutch"
answer = input("\n%s = " % card[input_language])
if answer == "quit()":
break
elif answer == "edit()":
new_card = cls.edit_card(card)
dictionary.remove(card)
if new_card != "delete()":
dictionary.append(new_card)
cls.save_dictionary(dictionary)
continue
elif answer == "search()":
cls.search_card()
elif answer == card[output_language]:
print("precies!")
else:
print(card[output_language])
completed_cards.append(card)
if len(completed_cards) == len(dictionary):
break
@classmethod
def run_script(cls):
try:
script_type = sys.argv[1]
print(
"possible commands: \"quit()\", \"search()\", \"edit()\", "
"\"delete()\"")
if script_type == "input":
cls.run_input()
elif script_type == "test":
cls.run_test()
except IndexError:
print("no argument provided, please enter \"input\" or \"test\"")
Dictionary.run_script()