[Tutor] Log file for Nested if-elif

Asad asad.hasan2004 at gmail.com
Wed Jan 2 10:09:53 EST 2019


Hi All ,

Need advice on the following piece of code :

with open(r"file1.log", 'r') as f:
    tail = deque(maxlen=8)  # the last eight lines
    script = None
    for line in f:
        tail.append(line)
        if
re.search('\?/patch/\d{8}/\d{8}/admin/load.sql',line,re.IGNORECASE):
            script = line
        elif re.search(r'Starting\s+apply\s+for\s+patch\s+\d{8}/\d{8}',
line, re.IGNORECASE):
            script = line
        elif re.search(r'set_metadata', line ,re.IGNORECASE) is not None:
            print "Reason of error \n", tail[-1]
            print "Script:\n", script
            print "Block of code:\n"
            for item in tail:
                 print item
            print " Danger "
            break
Now this is printing the last cached line in the variable line   . However
I would like to see the following output :

1) if it matches the pattern: \?/patch/\d{8}/\d{8}/admin/load.sql then

look for the line "set_metadata" in the file1.log  if it finds the pattern
then print the line which matches the pattern
\?/patch/\d{8}/\d{8}/admin/load.sql

print last 4 lines of the tail array  and exit

2) if it doesnot match '\?/patch/\d{8}/\d{8}/admin/load.sql'

then look of the anothern pattern
:Starting\s+apply\s+for\s+patch\s+\d{8}/\d{8} if it find the pattern

then look for line "set_metadata" in the file1.log  if it finds the pattern
then print the line which matches the pattern
\?/patch/\d{8}/\d{8}/admin/load.sql

print all the lines in tail

print a recommendation "Please check the installation"


3 ) if it doesnot match  the pattern: \?/patch/\d{8}/\d{8}/admin/load.sql
or '\?/patch/\d{8}/\d{8}/admin/load.sql'

print "No match found refer to install guide"

Can you advice what I can do to change the code .

Thanks,
--


More information about the Tutor mailing list