diff --git a/s10.py b/s10.py index c999d89..d48777e 100644 --- a/s10.py +++ b/s10.py @@ -3,33 +3,161 @@ from __future__ import absolute_import, print_function import sys import getopt -import time +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:') + 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(): - override = False try: - opts, args = getopt.getopt(sys.argv[1:], 'm:r:v', ['model=', 'rtype=']) + opts, args = getopt.getopt(sys.argv[1:], 'm:r:vw', ['model=', 'rtype=']) except getopt.GetoptError: usage() sys.exit(2) - verbose = False 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: @@ -42,8 +170,9 @@ def main(): sys.exit(2) rtype_value = arg - print('model: %s | r_type: %s | verbose: %s' % ( - model_value, rtype_value, verbose)) + print('[s10] model: %s | r_type: %s' % (model_value, rtype_value)) + + startProcessingS10(model_value, rtype_value) if __name__ == '__main__': main() diff --git a/s8.py b/s8.py index 5ba69b4..f9623a9 100755 --- a/s8.py +++ b/s8.py @@ -48,9 +48,10 @@ def get_ro_data(m, suffix): return result -def get_similarity_count(l, r): +def get_similarity_count(l, r, **kwargs): + C_PAIR = kwargs.get('pair', COMPARSION_PAIR) cnt = 0 - for (x, y) in COMPARSION_PAIR: + for (x, y) in C_PAIR: r_value = r[y].replace('*', '') if l[x] == r_value: cnt += 1 @@ -98,4 +99,6 @@ def main(): result = [__h] + result create_csv(output_name, result, directory='part8') -main() + +if __name__ == '__main__': + main()