Hi All , Let me answer :
> Where are you looking? *Anywhere* in the log file? >> Yes anywhere in the log file . > Only in the immediate next line? Anywhere forward of the "patch" line? >> Anywhere forward from the line which matches the search > You are looking for two patterns, but does the order they appear, or the > distance apart, matter? > > Yes the order does matter : 1) first it looks for : \?/patch/\d{8}/\d{8}/admin/load.sql - Condition if there are 10 lines containing the same pattern then it should save and print the last line containing the pattern means the 10th line - if it find the above string it start searching from that line with successful match regex match until it gets to the line containing set_metadata - Once it get the line containing set_metadata print last 4 lines prior to the line containing set_metadata 2) if it doesnot find the above pattern look for second in order :Starting\s+apply\s+for\s+patch\s+\d{8}/\d{8} - Condition if there are 10 lines containing the same pattern then it should save and print the last line containing the pattern means the 10th line - if it find the above string it start searching from that line with successful match regex match until it gets to the line containing set_metadata - Once it get the line containing set_metadata print last 10 lines prior to the line containing set_metadata 3) if it doesnot match the pattern: \?/patch/\d{8}/\d{8}/admin/load.sql or '\?/patch/\d{8}/\d{8}/admin/load.sql' anywhere in the log file . print "No match found refer to install guide" 4) If I have two errors how do I prioritize one above other ? Thanks, > > ---------- Forwarded message ---------- > From: Asad <asad.hasan2...@gmail.com> > To: tutor@python.org > Cc: > Bcc: > Date: Wed, 2 Jan 2019 20:39:53 +0530 > Subject: [Tutor] Log file for Nested if-elif > 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, > -- > > > > > ---------- Forwarded message ---------- > From: "Steven D'Aprano" <st...@pearwood.info> > To: tutor@python.org > Cc: > Bcc: > Date: Thu, 3 Jan 2019 15:16:45 +1100 > Subject: Re: [Tutor] Log file for Nested if-elif > On Wed, Jan 02, 2019 at 08:39:53PM +0530, Asad wrote: > > Hi All , > > > > Need advice on the following piece of code : > > Let me write it in a more "Pythonic" style: > > > > PATCH = r'\?/patch/\d{8}/\d{8}/admin/load.sql' > APPLY = r'Starting\s+apply\s+for\s+patch\s+\d{8}/\d{8}' > ERROR = r'set_metadata' > > tail = deque(maxlen=8) # the last eight lines > script = None > with open("file1.log", 'r') as f: > for line in f: > tail.append(line) > if (re.search(PATCH, line, re.IGNORECASE) > or re.search(APPLY, line, re.IGNORECASE): > script = line > elif re.search(ERROR, line, re.IGNORECASE): > print "Reason for error \n", line > print "Script:", script > print "Tail:\n", tail > print " Danger " # Seriously? This is dangerous? > 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 > > Where are you looking? *Anywhere* in the log file? Only in the > immediate next line? Anywhere forward of the "patch" line? You are > looking for two patterns, but does the order they appear, or the > distance apart, matter? > > > blah blah blah set_metadata blah blah > ... 100 lines ... > blah blah blah /patch/ ... admin/load.sql > ... 100 lines ... > > Is that a match? > > > print last 4 lines of the tail array and exit > > print tail[-4:] > sys.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 > > This is a contradiction: you just said that it DOESN'T match the > patch...load.sql pattern, but now you want it to print the line that > matched. There is no line that matches! > > > 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 . > > > > You need to tighten the specifications to make it more clear what you > are trying to do. > > > > -- > Steve > > > > > ---------- Forwarded message ---------- > From: Peter Otten <__pete...@web.de> > To: tutor@python.org > Cc: > Bcc: > Date: Thu, 03 Jan 2019 12:11:08 +0100 > Subject: Re: [Tutor] Log file for Nested if-elif > Asad wrote: > > > 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 . > > You have "nested" in the subject, do not make an attempt to structure your > loop. Why is that? > > In my experience using lots of small functions makes code easier to > understand. Below is a suggestion how you might break up your code. > > # untested; expect a few bugs! > def with_tail(items, maxlen): > tail = deque(maxlen=maxlen) > > def gen_items(): > for item in items: > tail.append(item) > yield item > > return tail, gen_items() > > > def search_end_of_section(lines): > for line in lines: > if re.search(r'set_metadata', line, re.IGNORECASE) is not None: > break > else: > raise ValueError("Reached end of file...panic!") > > > def dump(script, tail, advice=None): > print "Reason of error \n", tail[-1] > print "Script:\n", script > print "Block of code:\n" > for item in tail[-limit:]: > print item > print " Danger " > if advice is not None: > print advice > > > RE_PATCH = re.compile(r'\?/patch/\d{8}/\d{8}/admin/load.sql', > re.IGNORECASE) > RE_APPLY = re.compile( > r'Starting\s+apply\s+for\s+patch\s+\d{8}/\d{8}', re.IGNORECASE > ) > > with open(r"file1.log", 'r') as f: > tail, lines = with_tail(f) > for line in lines: > if RE_PATCH.search(line) is not None: > search_end_of_section(lines) > dump(script=line, tail=tail[-4:]) > break > elif RE_APPLY.search(line) is not None: > search_end_of_section(lines) > dump( > script=line, tail=tail, > advice="Please check the installation" > ) > break > > > > _______________________________________________ > Tutor maillist - Tutor@python.org > https://mail.python.org/mailman/listinfo/tutor > -- Asad Hasan +91 9582111698 _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor