[READ-ONLY] Mirror of https://github.com/mrgnw/python-scripts.
4.8 kB
190 lines
1# TODO: Use =, and () as separators
2#
3import os.path
4from os import chdir
5import glob
6import re
7
8# commands to enter at any time to exit quiz or skip question
9# could create interpretAnswer function to accept multiple parameters? Nah
10exitKeys = ["x", "exit"]
11openKeys = ["open", "read"]
12newKeys = ["new", "add"]
13
14# put newKeys & openKeys in another list?
15commandList = exitKeys + newKeys + openKeys
16
17mainPrompt = "Whatcha want?"
18helpText = " read (open), new (add), exit (x)\n >>"
19
20terms = {}
21
22
23def remove_comments(dirty_line):
24 """ ADD ability to clear a multi-line string of comments """
25 if '#' in dirty_line:
26 dirty_line= dirty_line.partition('#')[0] # inspired by http://stackoverflow.com/questions/1706198/
27
28 return dirty_line.rstrip()
29
30
31def is_quote(string):
32 '''Says if a string is a quotation mark (single or double)
33 Doesn't handle triple quotes yet.
34 '''
35 if string == '\"':
36 return True
37 else :
38 return False
39
40
41def remove_strings(fat_string, verbose = False):
42 ''' currently just iterates through fat_string character-by-character to return a copy
43 '''
44
45 in_quote = False
46 quote_type = "" # Will be changed to represent the type of quote
47
48 new_string = ""
49 for c in fat_string:
50 if is_quote(c):
51 if in_quote:
52 if verbose: print c, "= quote #2"
53 in_quote = False # get out of the quote
54 else:
55 if verbose: print c, "skipped"
56 in_quote = True # start skipping characters
57 elif in_quote:
58 if verbose: print c, "skipped"
59 else :
60 new_string += c # Change new string to show each time a character is evaluated
61 if verbose : print "\t" + new_string
62
63 return new_string
64
65
66def clean(dirty_line):
67 new_line = remove_comments(dirty_line)
68 new_line = remove_strings(new_line)
69
70 new_line = re.sub("[^a-zA-Z_]", ' ', dirty_line)
71
72 return new_line
73
74
75def ask(prompt=mainPrompt+"\n"+helpText, starting_dictionary = terms):
76 response = raw_input(prompt + "\t")
77 if response.lower() in commandList:
78 return response.lower()
79 else : return response
80
81
82def read_dictionary(filename):
83 dictionary = {}
84
85 # Press *.whatever to read ALL files of .whatever type
86 if filename[0] == "*":
87 print filename
88 for files in glob.glob(filename):
89 print files
90
91 with open(files) as f:
92 for line in f:
93 line = clean(line)
94 print " >>", line
95
96 word_list = line.split(" ")
97 for x in word_list:
98 dictionary[x] = ""
99
100 # Read just one file.
101 elif os.path.isfile(filename):
102 with open(filename) as target:
103 for line in target.readlines():
104
105 line = clean(line)
106 print line
107 #print "\t" + line # + "\t>>\t",
108
109 word_list = line.split(" ")
110 for x in word_list:
111 dictionary[x] = ""
112
113 print # blank line
114 print dictionary
115 return dictionary
116
117
118def type_dictionary():
119 dictionary = {}
120 go = True
121 while go:
122 # ask for key
123 new_key = ask(" key")
124 if new_key in exitKeys :
125 return dictionary
126 else :
127 # ask for value - still creates key if an exitKey is entered for value
128 new_value = ask("value")
129 if new_value in exitKeys :
130 dictionary[new_key] = ""
131 return dictionary
132 else :
133 dictionary[new_key] = new_value
134 print
135
136
137def write_to_file(dictionary):
138 fname = raw_input("Write to file:")
139
140
141 f = open(fname, 'w')
142 for i in dictionary:
143 f.write(i + "\t" + dictionary[i] + "\n")
144 #target = open(filename, 'w')
145
146 f.close()
147 return dictionary
148
149
150def dict_string(dictionary):
151 p = ""
152
153 ordered = sorted(dictionary)
154
155 for x in ordered:
156 p += x + "\t" + dictionary[x] + "\n"
157
158 return p
159
160
161#terms = {"a" : "alpha", "b" : "bravo", "c" : "charlie"}
162terms = {}
163
164
165go = True
166while go:
167 main_input = ask()
168
169 if main_input in commandList:
170 if main_input in exitKeys:
171 write_to_file(terms)
172 go = False
173
174 elif main_input in newKeys:
175 print terms
176 terms.update(type_dictionary())
177 print terms
178
179 elif main_input in openKeys:
180 filename = ask("Read dictionary:")
181 terms.update(read_dictionary(filename))
182 else : print "Let's try that again...\n"
183
184
185
186dirty = "lovely...\nis the root of all \t 5*5+2/5\\ is silly."
187
188print "Here's what we ended up with:\n"
189print dict_string(terms)
190print clean(dirty)