This repository has been archived by the owner on Mar 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsource.py
351 lines (243 loc) · 9.33 KB
/
source.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
"""
RNS Generator v1.0.1
A Python script for generating random number sequences from an external reference table. This script can be used for random, systematic and monetary
unit sampling.
For installation, use and troubleshooting, please refer to the README file. Code is available in the author's public Github repository by following the
link below.
(C) 2021 Carmelo Julius Paz
(email) [email protected]
(github) https://github.com/carmelopaz0708
Released under the GNU General Public License (gpl-3.0)
"""
from openpyxl import load_workbook
import os, csv, sys
def main():
# Ensure proper usage
if len(sys.argv) != 2:
print("Usage: python source.py reference")
sys.exit(1)
# Check correct file extension
allowableFileTypes = ('.xlsx', '.xlsm', '.xltx', '.xltm')
if not sys.argv[1].endswith(allowableFileTypes):
print("Invalid file type used as reference.")
sys.exit(2)
# Check if tables directory exists
referenceDir = "tables"
if not os.path.isdir(referenceDir):
os.makedirs(referenceDir)
print("Generated table directory in root folder. Please save your tables here.")
# Check if file is in tables directory
referenceFile = os.path.join(referenceDir, sys.argv[1])
if not os.path.exists(referenceFile):
print("Reference file \"{}\" missing from tables directory.".format(sys.argv[1]))
sys.exit(3)
sep = "----------------------------------------------------------"
print("{}\n\tRANDOM NUMBER SEQUENCE GENERATOR\n{}".format(sep, sep))
# Load variables into memory.
wb = load_workbook(referenceFile)
tbName = getTableName(wb, sep)
tbData = getTableData(wb, tbName)
pSize = getPopulation(sep)
sSize = getSampleSize(sep)
rowLength = len(tbData)
colLength = len(tbData[0])
pos = getPosition(rowLength, colLength, tbData, sep)
swp = getSweep(sep)
# Check user input
isFinal = finalize(tbName, pSize, sSize, pos, swp, sep)
if isFinal == False:
print("Closing program.")
sys.exit(0)
outSeq = []
# Generate RNS sequence with Left to Right sweep
if swp == "Left to Right":
slicedTab = tbData[pos[1]:]
slicedTab[0] = slicedTab[0][pos[2]:]
for row in slicedTab:
for cell in row:
if cell == 0 or cell in outSeq:
continue
if cell <= pSize and len(outSeq) < sSize:
outSeq.append(cell)
if len(outSeq) < sSize:
print("Temp: {}".format(outSeq))
isLoopBack = promptLoopBack(sep)
if isLoopBack:
for row in tbData:
for cell in row:
if cell == 0 or cell in outSeq:
continue
if cell <= pSize and len(outSeq) < sSize:
outSeq.append(cell)
# Generate RNS sequence with Down, Left to Right sweep
if swp == "Down, Left to Right":
zipTab = [*zip(*tbData)]
slicedTab = zipTab[pos[2]:]
slicedTab[0] = slicedTab[0][pos[1]:]
for row in slicedTab:
for cell in row:
if cell == 0 or cell in outSeq:
continue
if cell <= pSize and len(outSeq) < sSize:
outSeq.append(cell)
if len(outSeq) < sSize:
print("Temp: {}".format(outSeq))
isLoopBack = promptLoopBack(sep)
if isLoopBack:
for row in zipTab:
for cell in row:
if cell == 0 or cell in outSeq:
continue
if cell <= pSize and len(outSeq) < sSize:
outSeq.append(cell)
print("Output: {}".format(outSeq))
# Export as .txt
outDir = "output"
if not os.path.exists(outDir):
os.makedirs(outDir)
outFile = "out.txt"
outPath = os.path.join(outDir, outFile)
with open(outPath, "w") as file:
file.write("{}\nWorkbook: {}".format(sep, sys.argv[1]))
file.write("\nWorksheet: {}".format(tbName))
file.write("\nPopulation: {}".format(pSize))
file.write("\nSample: {}".format(sSize))
file.write("\nRNS Start Value: {}".format(pos[0]))
file.write("\nRNS Start Position: row {}, col. {}".format(pos[1] + 1, pos[2] + 1))
file.write("\nSweep: {}".format(swp))
file.write("\n{}\nOUTPUT:\n".format(sep))
wr = csv.writer(file)
wr.writerow(outSeq)
print("Exit with success. Please check {} in the {} folder of root directory.".format(outFile, outDir))
sys.exit(0)
def getTableName(workbook, s):
'''Returns the name of the worksheet'''
sheets = tuple(workbook.sheetnames)
print("INFO: Loaded {} sheets from {}\n{}\n\t\tENTER VARIABLES\nTable Names:".format(len(sheets), sys.argv[1], s))
for index, title in enumerate(sheets):
print("({}) {}".format(index + 1, title))
while True:
try:
usrTable = int(input("Select desired table: "))
if (usrTable - 1) < 0 or (usrTable - 1) > len(sheets):
raise ValueError
break
except ValueError:
print("Enter number from available options.")
except KeyboardInterrupt:
sys.exit(0)
print(s)
table = sheets[usrTable - 1]
return table
def getTableData(key, value):
'''Returns the cell values of worksheet_name as a 2D list'''
ws = key[value]
data = []
for row in ws.iter_rows(min_row = 1, values_only = True):
data.append(list(row))
return data
def getPopulation(s):
'''Returns user input for population size'''
while True:
try:
populationLimit = int(input("Enter population limit(inclusive): "))
if populationLimit <= 0:
raise ValueError
break
except ValueError:
print("Value must be a valid positive integer")
except KeyboardInterrupt:
sys.exit(0)
print(s)
return populationLimit
def getSampleSize(s):
'''Returns user input for sample size'''
while True:
try:
sampleSize = int(input("Enter sample size: "))
if sampleSize <= 0:
raise ValueError
break
except ValueError:
print("Value must be a valid positive integer.")
except KeyboardInterrupt:
sys.exit(0)
print(s)
return sampleSize
def getPosition(maxRows, maxColumns, table, s):
'''Obtains starting cell position from worksheet_name'''
while True:
try:
usrRow = int(input("Enter starting row(-y): "))
if usrRow <= 0 or usrRow > maxRows:
raise ValueError
break
except ValueError:
print("Value exceeds maximum number of rows in table.")
except KeyboardInterrupt:
sys.exit(0)
while True:
try:
usrCol = int(input("Enter starting column(+x): "))
if usrCol <= 0 or usrCol > maxColumns:
raise ValueError
break
except ValueError:
print("Value exceeds maximum number of columns in table.")
except KeyboardInterrupt:
sys.exit(0)
print(s)
rStart = table[usrRow - 1][usrCol - 1]
return rStart, usrRow - 1, usrCol - 1
def getSweep(s):
'''Returns general sweeping algorithm as string'''
allowableSweep = ("Left to Right", "Down, Left to Right")
print("Sweep patterns:")
for index, sweep in enumerate(allowableSweep):
print("({}) {}".format(index + 1, sweep))
while True:
try:
usrSweep = int(input("Select desired sweep pattern: "))
if (usrSweep - 1) < 0 or (usrSweep - 1) > len(allowableSweep):
raise ValueError
break
except ValueError:
print("Invalid sweep pattern option")
except KeyboardInterrupt:
sys.exit(0)
print(s)
return allowableSweep[usrSweep - 1]
def finalize(tableName, populationSize, sampleSize, position, sweep, s):
'''Returns flag for proceed with sequence'''
print("Table: {}\nPopulation Size: {}\nSample Size: {}".format(tableName, populationSize, sampleSize))
print("RNS Start Value: {}\nRNS Start Position: row {}, col. {}".format(position[0], position[1] + 1, position[2] + 1))
print("Sweep: {}".format(sweep))
while True:
try:
response = input("Is this correct(y/n)? ").rstrip().lower()
if response not in ['y', 'n']:
continue
else:
print(s)
if response == 'y':
return True
else:
return False
except KeyboardInterrupt:
sys.exit(0)
def promptLoopBack(s):
while True:
try:
isloopBack = input("Sequence less than sample size. Continue from start(y, n)? ").rstrip().lower()
print(s)
if isloopBack not in ['y', 'n']:
continue
else:
if isloopBack == 'y':
return True
else:
return False
except KeyboardInterrupt:
sys.exit(0)
if __name__ == "__main__":
main()