#!/bin/env python import sys # simulate file # code "u" works; not "u2" IN = """# # keyword = code index = u name = mail_zipcode = c full_phone_number = u2 #""" infile = [ x for x in IN.split("\n") if not x.startswith('#') ] print(infile) vectors = {} # functions def ignore(*ignore): pass def count(field,data): pass def unique(field,data): pass functions = {'c':count, 'u':unique, '':ignore} works = True # process infile def fix(s): # workaround print(f'"{s}" == "u2" : {s == "u2"}' ) if len(s) <= 1: return s return s[0] def test1(): # fails on "u2" = expected (no func for "u2") print("\n>>>>>> test1: KeyError expected\n") vectors.update( [ [ func.strip(), functions[code.strip()] ] for [func,code] in [ each.split('=') for each in infile ] ]) def test2(): # works if code sent to fix() to get first char print("\n>>>>>> test2: works; expected\n") vectors.update( [ [ func.strip(), functions[fix(code.strip())] ] for [func,code] in [ each.split('=') for each in infile ] ]) def test3(): # fails on index: code.strip()[0] print("\n>>>>>> test3: IndexError unexpected\n") vectors.update( [ [ func.strip(), functions[code.strip()[0]] ] for [func,code] in [ each.split('=') for each in infile ] ]) # "2" is handled elsewhere. x = sys.argv[1] eval(f'test{x}()') print(vectors)