简体   繁体   中英

How can I simplify this function into a function that checks all error codes from a list?

My problem is that I have a many functions that essentially do the same thing depending on a given error code. How can I develop a loop to simplify these functions into one algorithm that checks the error code from a list or table of error codes. As well as detecting the error code string, I would like to count occurrences for each unique error code.

I've tried to make a table that includes column such as error and description. I am confused how I can match the error with the description

file_path = ('/Users/.../error.txt')


def check_error1():

    count = 0
    fault = ':error1'

    with open(file_path) as f:
        for line in f.readlines():
            if fault in line:
                count += fault.count(fault)
    return count != 0

if check_error1():
    print 'error 1 occurred ' , count, ' times || WARNING, Bot failed to read data'

print 'error 1 occurred ' , count, ' times || WARNING, Bot failed to read data'

You could find all faulty lines in the file and then take the len gth of that:

def check_error1():
    with open(file_path) as f:
        return len(line for line in f if ':error1' in line)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM