-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelementifier.py
More file actions
114 lines (77 loc) · 3.12 KB
/
elementifier.py
File metadata and controls
114 lines (77 loc) · 3.12 KB
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
import os
from optparse import OptionParser
def init():
elements = load_list('elements.txt')
word_list = load_list('dictionary.txt')
parser = config_parser()
handle_options(parser, elements)
def config_parser():
parser = OptionParser(usage = 'Usage: %prog [options] OR %prog [input-string]')
parser.add_option('-f', '--file', help = 'generate encodings for each line-seperated string in FILE')
parser.add_option('-d', '--destination', help = 'output result to DESTINATION file')
return parser
def handle_options(parser, elements):
(options, args) = parser.parse_args()
if options.file is not None:
if len(args) > 0:
parser.error('[input-string] and any [options] are mutually exclusive')
if os.path.isfile(options.file):
print('Generating all possible encodings of all strings in {}'.format(options.file))
if options.destination is not None:
print('Output will be put in {}'.format(options.destination))
dest_path = options.destination
else:
print('Output will be put in encodings.txt')
dest_path = 'encodings.txt'
save_all_encodings(options.file, dest_path, elements)
print('Finished')
else:
parser.error('option -f: FILE does not exist')
else:
if options.destination is not None:
parser.error('option -d requires option -f')
try:
input_str = args[0]
encodings = encode_in_elements(input_str, elements)
if len(encodings) > 0:
print('Found {} possible encodings of \'{}\' in elemental symbols:'.format(len(encodings), input_str))
print('\n'.join(encodings))
else:
print('Found no encodings of \'{}\' in elemental symbols'.format(input_str))
except IndexError:
parser.error('no [input-string] or [options] provided')
def save_all_encodings(file_path, dest_path, elements):
input_list = load_list(file_path)
all_encodings = encode_all_in_elements(input_list, elements)
with open(dest_path, 'w') as dest_file:
if len(all_encodings) > 0:
for string_encoding in all_encodings:
dest_file.write(string_encoding[0].lower() + ': ' + ', '.join(string_encoding) + '\n')
else:
dest_file.write('No encodings found for all strings in {}'.format(file_path))
# Returns a list of lists of encodings of all strings in the provided string_list
def encode_all_in_elements(string_list, elements):
all_encodings = []
for index, word in enumerate(string_list):
elements_encoding = encode_in_elements(word, elements)
if len(elements_encoding) > 0:
all_encodings.append(elements_encoding)
return all_encodings
# Returns a list of all possible encodings of str in element symbols
def encode_in_elements(str, elements):
encodings = []
for element in elements:
if element.lower() == str[:len(element)].lower():
if len(str) <= len(element):
encodings.append(element)
resulting_encodings = encode_in_elements(str[len(element):], elements)
for encoding in resulting_encodings:
new_encoding = element + encoding
encodings.append(new_encoding)
return encodings
# Loads the file at the given path and returns a list of lines
def load_list(path):
with open(path) as list_file:
return list_file.read().splitlines()
if __name__ == '__main__':
init()