简体   繁体   中英

Advice on how I should structure program

For the program I am trying to design, I am checking that certain conditions exist in configuration files. For example, that the line: ThisExists is in the program, or that ThisIsFirst exists in the file followed by ThisAlsoExists somewhere later down in the file.

I looked for an efficient approach which might be used in this situation but couldn't find any.

My current idea is basically to just iterate over the file(s) multiple times each time I want to check a condition. So I would have functions:

def checkA(file)

def checkB(file)

. . .

To me this seems inefficient as I have to iterate for every condition I want to check.

Initially I thought I could just iterate once, checking each line for every condition I want to verify. But I don't think I can do that as conditions which can be multi line require information about more than one line at a time.

Is the way I outlined the only way to do this, or is there a more efficient approach?

I am trying to provide an example below.

def main():
    file = open(filename)
    result1 = checkA(file)
    result2 = checkB(file)

    """This is a single line check function"""
    def checkA(file)
        conditionExists = False
        for line in file:
            if(line == "SomeCondition"):
                 conditionExists = True
         return conditionExists

    """This is a multi line check function"""
    def checkB(file)
        conditionExists = False
        conditionStarted = False
        for line in file:
             if(line == "Start"):
                 conditionStarted = True
             elif(line == "End" and conditionStarted):
                  conditionExists = True
        return conditionExists

For a software engineering perspective, your current approach has the some nice advantages. The logic of the functions are fully decoupled from one another and can be separately debugged and tested. The complexity of each individual function is low. And this approach allows you to easily incorporate various checks that do not have a parallel structure.

If available libraries (configparser etc.) aren't enough I would probably use regular expressions.

import re

check_a = re.compile('^SomeCondition$', flags=re.MULTILINE)
check_b = re.compile('^Start(?:.|\n)*?End$', flags=re.MULTILINE)

def main(file_name):
    with open(file_name, 'r') as file_object:
         file_content = file_object.read()

    result_1 = bool(check_a.search(file_content))
    result_2 = bool(check_b.search(file_content))

It's not the most user friendly approach – especially if the matching conditions are complex – but I think the pay-off for learning regex is great.

xkcd tells us that regex both can be a super power and a problem .

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