-
Notifications
You must be signed in to change notification settings - Fork 0
/
extractValues.py
executable file
·298 lines (169 loc) · 6.88 KB
/
extractValues.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#! /usr/bin/python
import re
import sys
import math
import copy
from engineering_notation import to_eng_notation
from component import *
import copy
capacitor_suffixes = {'m': -3, 'u': -6, 'n':-9, 'p':-12}
resistor_suffixes = {'k':3, 'K':3, 'M': 6, 'G': 9}
resistor_tolerances = \
{'brown':0.01, 'red':0.02, 'green':0.005,\
'blue':0.0025, 'violet':0.001,\
'grey':0.0005, 'gold':0.05, 'silver':0.1}
def read_file_values(fileName, component_maker, suffix_dict, tolerance_dict = None):
f = open(fileName, 'r')
print('Opened ', fileName)
text = f.read()
f.close()
suffix_keys = suffix_dict.keys()
valid_suffixes = ''
for key in suffix_keys: valid_suffixes = valid_suffixes + key
valid_suffixes = valid_suffixes
search_expr = r'([\d.#]+)([' + valid_suffixes + ']?)([\+/-]*)([\w%]*)\s'
matches = re.findall(search_expr,text)
if not matches:
print('no matches found')
sys.exit(1)
values = []
for match in matches:
#ignore the ones prefixed with #
value = match[0]
suffix = match[1]
tolerance_exp = match[2]
tolerance = match[3]
if value[0] == '#':
continue
power = 0
if suffix:
#is the si suffix m, u, n, p ?
try:
power = suffix_dict[suffix]
except KeyError as ke:
print('unknown suffix: ' + suffix)
continue
else:
#no si suffix
power = 0
value = float(value) * math.pow(10, power)
if not tolerance or not tolerance_exp:
values.append(component_maker(value))
continue
#Find tolerances? Quantity?
if tolerance_exp.find('+/-') == -1:
print('Disregarding: ' + tolerance_exp)
continue
tolerance = tolerance.lower()
#check to see if the tolerance value matches with
#a particular tolerance key
done = False
if tolerance_dict:
for key in tolerance_dict:
if tolerance.find(key) != -1:
tolerance = tolerance_dict[key]
values.append(component_maker(value, tolerance = tolerance))
done = True
break
if done:
continue
#did find a tolerance value that matches a key
#try to force the tolerance value into a number
#look for 5%
match = re.search(r'([\d\.]+)%', tolerance)
if match:
tolerance = float(match.group(1)) / 100.0
values.append(component_maker(value, tolerance = tolerance))
continue
#look for just a fraction: 0.05
try:
tolerance = float(tolerance)
values.append(component_maker(value, tolerance = tolerance))
except ValueError as ve:
pass
#the for loop ends right here
return values
def strip_suffix(string, suffixes):
for suffix in suffixes:
indx = string.find(suffix)
if indx != -1:
return string[0:indx]
return string
def capacitorValues(filename = 'rawCapData1.txt'):
component_maker = lambda *args, **kwargs: Capacitor(*args, **kwargs)
return read_file_values(filename, component_maker, capacitor_suffixes)
def resistorValues(filename = 'rawResistorData.txt'):
component_maker = lambda *args, **kwargs: Resistor(*args, **kwargs)
return read_file_values(filename, component_maker, resistor_suffixes, resistor_tolerances)
def combination(array, r):
data = list(range(0, r))
global holder
holder = []
combinationUtil(array, data, 0, len(array)-1, 0, r);
return
def combinationUtil(array, data, start, end, index, r):
if index == r:
#print data
holder.append(copy.deepcopy(data))
return
i = start
while (i <= end) and (end-i+1) >= (r-index):
data[index] = array[i];
combinationUtil(array, data, i+1, end, index+1, r);
i += 1
return
def getComponentCombinations(componentList = [] , numComponentsInGroup = 1, \
op = None, operationName = '?' , \
addOriginalComponents = True, componentType = 'component'):
componentType = componentType.lower()
make_comp = None
if componentType == 'component':
make_comp = lambda *args, **kwargs: Component(*args, **kwargs)
elif componentType == 'resistor':
make_comp = lambda *args, **kwargs: Resistor(*args, **kwargs)
elif componentType == 'capacitor':
make_comp = lambda *args, **kwargs: Capacitor(*args, **kwargs)
else:
make_comp = lambda *args, **kwargs: Component(*args, **kwargs)
#Component(self, components, operation = None , label = '', value = None):
combinations = []
if numComponentsInGroup > 1:
#SCRAMBLE the components
combination(componentList, numComponentsInGroup)
for thing in holder:
should_add = True
group = make_comp(thing, operation = op, label = operationName)
res = group.getValue()
res = int(res * 10000)
for val in thing:
val = int(val * 10000)
if res == val:
should_add = False
break
if should_add:
combinations.append(group)
#account for combinations of itself...
for component in componentList:
copies = [copy.deepcopy(component)]
for i in range(1, numComponentsInGroup):
copies.append(copy.deepcopy(component))
c = make_comp(copy.deepcopy(copies), operation = op, label = operationName)
combinations.append(c)
#Add the single, non permuted values
if addOriginalComponents:
for component in componentList:
c = None
if type(component) == int or type(component) == float:
c = make_comp(component)
else:
c = component;
combinations.append(c)
#sort the return list
combinations.sort(key=lambda x: x.getValue())
return combinations
def main():
rValues = resistorValues('test_tolerances.txt')
for rValue in rValues:
print(rValue)
if __name__ == '__main__':
main()