Handcre Handcre - 2 months ago 6
Python Question

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

Answer

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.

Comments