Advice on how I should structure program
<p>For the program I am trying to design, I am checking that certain conditions exist in configuration files. For example, that the line: <strong>ThisExists</strong> is in the program, or that <strong>ThisIsFirst</strong> exists in the file followed by <strong>ThisAlsoExists</strong> somewhere later down in the file.</p>

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

<p>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:</p>

<p>def checkA(file)</p>

<p>def checkB(file)</p>

<p>.
.
.</p>

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

<p>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.</p>

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

<p>I am trying to provide an example below.</p>

<pre><code>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
</code></pre>

<p>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.</p>

python parsing