You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

178 lines
5.2 KiB

# -*- coding: utf-8 -*-
#!/usr/bin/env python
from __future__ import absolute_import, print_function
import sys
import getopt
import os
import csv
from base import RAW_DIR, TARGET_DIR, create_csv
OCT_RAW_DIR = os.path.join(RAW_DIR, '..', 'oct')
verbose = False
writefile = False
def process_lo_data4search(model=None):
'''
return group of LO by model
if not asking for model, return dict with all models + LO
'''
groupy = {}
with open(os.path.join(OCT_RAW_DIR, 'LO_DataFor_Search.csv'), 'rU') as f:
'''
PEP278 explained what rU stands for:
In a Python with universal newline support open() the mode
parameter can also be "U", meaning "open for input as a text file
with universal newline interpretation". Mode "rU" is also allowed,
for symmetry with "rb".
'''
rows = csv.reader(f)
for row in rows:
if row[1] not in groupy:
groupy[row[1]] = []
groupy[row[1]].append(row)
if not model:
return groupy
elif model in groupy:
return groupy[model]
else:
return []
def get_lo_list_from_part7(model=None, rtype=None):
'''
Fetch data from part 7 putting in List for easy access
'''
if not model and not rtype:
return []
result = []
filename = '%s-%s.csv' % (model, rtype)
with open(os.path.join(TARGET_DIR, 'part7', filename), 'rb') as f:
rows = csv.reader(f)
for row in rows:
result.append(row)
return result
def isMatch(a, b):
'''
----> one limit : b needs to be 1-char since it will complicate things too much
possibility:
Y/N* --- N, Y
'''
match = False
aa = a.replace('*', '').split('/')
if b in aa:
return True
return False
def get_matched_lo_list(los, ros):
COMPARSION_PAIR = [ # ใสไป (#R, #LO) ค
(5, 2), (6, 3), (7, 6), (8, 7), (9, 10),
(10,12), (11,13), (12,15), (13,17), (14,18),
(15,19), (16,21) ,(17, 22), (18, 24), (19, 27),
(20, 29), (21, 30), (22, 31), (23, 33)
]
ok = []
isMatched = False
for lo in los:
if verbose:
print('\n----------------------------------------------')
for ro in ros:
if isMatched:
continue
# check if all columns are matched
count = 0
for r, l in COMPARSION_PAIR:
if isMatch(ro[r], lo[l]):
# print('MATCHED: %s === %s' % (ro[r], lo[l]), end=' | ')
if verbose:
print('_', end='')
count += 1
else:
# print(' X : %s !== %s' % (ro[r], lo[l]), end=' | ')
if verbose:
print('X', end='')
# if matched column count == len(COMPARSION_PAIR), then it's a match
if count == len(COMPARSION_PAIR):
isMatched = True
if verbose:
print('\n%s %s ==> %s | matched pair #%s/%s' % (
lo[0], ro[0], isMatched, count, len(COMPARSION_PAIR)))
if isMatched:
ok.append(lo)
# reset match boolean
isMatched = False
return ok
def startProcessingS10(model, rtype):
data4search = process_lo_data4search(model)
part7data = get_lo_list_from_part7(model, rtype)
output = get_matched_lo_list(data4search, part7data)
print('==============================================')
print('output: #%s' % len(output))
if len(output):
print([l[0] for l in output])
if writefile:
output_name = '%s-%s.csv' % (model, rtype)
create_csv(output_name, output, directory='part10')
def usage():
print(
'''Help:
[opts]
-m, --model Model Name
-r, --rtype R_Type
-v verbose
-w write output to file
Usage:
$ python s10.py [opts] [value]
Example:
$ python s10.py -mCFHD -r3
$ python s10.py -mCFHD -r3 -v << show more information
$ python s10.py -m CFHD -r 3 -v << same as above
$ python s10.py --model=CFHD --rtype=3 -v << same as above
$ python s10.py -m CFHD -r 3 -w << write output file too.
$ python s10.py -mCFHD -r3 -vw << show more information & output file.
''')
def main():
try:
opts, args = getopt.getopt(sys.argv[1:], 'm:r:vw', ['model=', 'rtype='])
except getopt.GetoptError:
usage()
sys.exit(2)
model_value = rtype_value = None
for opt, arg in opts:
if opt == "-v":
global verbose
verbose = True
elif opt == "-w":
global writefile
writefile = True
elif opt in ('-m', '--model'):
## TODO: filter model availabel and throw exception if invalid
if len(arg) != 4:
usage()
sys.exit(2)
model_value = arg.upper()
elif opt in ('-r', '--rtype'):
if arg not in '1234':
usage()
sys.exit(2)
rtype_value = arg
print('[s10] model: %s | r_type: %s' % (model_value, rtype_value))
startProcessingS10(model_value, rtype_value)
if __name__ == '__main__':
main()