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>If available libraries (configparser etc.) aren't enough I would probably use regular expressions.</p>

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

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

<p>xkcd tells us that regex both can be a <a href="http://xkcd.com/208/" rel="nofollow">super power</a> and a <a href="https://xkcd.com/1171/" rel="nofollow">problem</a>.</p>

python parsing