[Tutor] Creating a class and calling an exception

Becky Mcquilling ladymcse2000 at gmail.com
Fri Jun 3 08:59:50 CEST 2011


So I'm new to doing creating classes in python and took a script I had
written to parse a log file and tried to create a class just to understand
it a little better. The first script below works fine.

It looks at the log, checks the date and if it's too old, raises the
exception and returns a set value or if the file doesn't exist it raises the
exception.  If the file is there and is no more than a day old, it parses
the log and returns what was backed up by the utility I'm using.

The Second script written here, always raises the exception and I'm missing
why, any advice?

*FIRST SCRIPT*

import re, os
import stat
import sys
from datetime import date, timedelta

log_file = 'd:/backup_logs/backups.log'
re_backup_status = re.compile(r'^\s+Files\s+:\s+\d', re.IGNORECASE)
"""Example of return from regexFiles:  Succeeded"168933 failed:0 """

def main():

  try:
    s = os.stat(log_file)
    mtime = date.fromtimestamp(s[stat.ST_MTIME])
    yesterday = date.today() - timedelta(days=1)
    mode = s[stat.ST_MODE]


    if stat.S_ISREG(mode):
      if mtime < yesterday:
        print "%s" % ('succeeded:0 failed:10000')
      else:
        log1 = (open(log_file, 'r'))
        for line in log1:
          if re_backup_status.search(line):
            status_out = line.split()
            print '%s succeeded:%s failed:%s'  % (
                  'map:files', status_out[2], status_out[6])
        log1.close()
  except:  print "%s" %('succeeded:0 failed:10000')

if __name__ == '__main__':
  main()
*
*
*SECOND SCRIPT*
*
*
import re
import os
import stat
import sys
from datetime import date, timedelta

class Log_Parser:
    def __init__(self):
        self.re_backup_status = re.compile(r'^\s+Files\s+:\s+\d',
re.IGNORECASE)
        """Example of return from regexFiles:  Succeeded"168933 failed:0 """

    def log_parse(self, log_file):

      try:
        s = os.stat(log_file)
        mtime = date.fromtimestamp(s[stat.ST_MTIME])
        yesterday = date.today() - timedelta(days=1)
        mode = s[stat.ST_MODE]

        if stat.S_ISREG(mode):
          print ('file is there')
          if mtime < yesterday:
            print('file is the wrong date')
            print "%s" % ('succeeded:0 failed:10000')
          else:
            log1 = (open(log_file, 'r'))
            for line in log1:
              if re_backup_status.search(line):
                status_out = line.split()
                print '%s succeeded:%s failed:%s'  % (
                      'map:files', status_out[2], status_out[6])
        log1.close()
      except:  print "%s" %('succeeded:0 failed:10000')


backup_log_parser = Log_Parser()
backup_log_parser.log_parse('d:/backup_logs/backups.log')
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110602/3ccd1cb6/attachment.html>


More information about the Tutor mailing list