bettering my python

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • setori88
    New Member
    • Sep 2008
    • 7

    bettering my python

    Hi guys

    Firstly I would like to thank all those people who answer these questions on Bytes. Really you guys are fantastic.

    Now I have seen guys post code on this site and some gurus come along and reduce it to a few lines of readable, frankly sexy code. I would love to see how optimised you guys can get this, just so I can learn from you.

    General overview:

    I create a folder with different files extracted from an incoming mail readying to be processed by different open office programs. Ie .xls for calc .doc for writer.
    Later in the process I will make use of unoconv to handle this but:
    I have created a whitelist of files I accept, though should there be an attachment with a file extension not supported the script then copies the downloaded email to an ignore folder for manual processing. But I will still process the remaining attachments, that is why i dont throw and exception.

    I would love to optimise _locate_not_sup ported and _file_extension _check and get it to lovely readable pythonic

    Code:
     
        def _file_extension_check(self):
            '''check for ext validity, if none still return true just'''
            self.logger.info("checking file extension validity")
            whitelist = "*.pdf","*.rtf","*.doc","*.xls","*.tif","*.tiff"
            root = root=self.currentEmailAttachmentsPath
            pdfgen  = self._locate(whitelist[0], root)
            rtfgen  = self._locate(whitelist[1], root)
            docgen  = self._locate(whitelist[2], root)
            xlsgen  = self._locate(whitelist[3], root)
            tifgen  = self._locate(whitelist[4], root)
            tiffgen = self._locate(whitelist[5], root)
            self._locate_not_supported(whitelist, root)
            return 1
        
        def _locate_not_supported(self, patterns, root=None):
            for path, dirs, files in os.walk(os.path.abspath(root)):
                for file in files:
                    name, ext = os.path.splitext(file)
                    if ext != patterns[0][1:] and \
                        ext != patterns[1][1:] and \
                        ext != patterns[2][1:] and \
                        ext != patterns[3][1:] and \
                        ext != patterns[4][1:] and \
                        ext != patterns[5][1:]:
                        self._email_to_ignore_folder()
                    else:
                        return
      
        def _locate(self,pattern, root=None):
            for path, dirs, files in os.walk(os.path.abspath(root)):
                for filename in fnmatch.filter(files, pattern):
                    yield os.path.join(path, filename)
                            
        def _email_to_ignore_folder(self):
            self.logger.info("email sent to ignore folder")
            print self._emailpath + self._ignorefolder
            shutil.move(self.currentEmailPath, self._emailpath + self._ignorefolder)
    Thank you so much!
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    You should call os.walk() once instead of multiple times. This example does just that. Maybe you could do something similar in your code.
    [code=Python]import os

    def dir_list(dir_na me, whitelist):
    outputList = []
    for root, dirs, files in os.walk(dir_nam e):
    for f in files:
    if os.path.splitex t(f)[1] in whitelist:
    outputList.appe nd(os.path.join (root, f))
    else:
    # send to ignore folder??
    return outputList

    dir_name = 'your_directory '
    print '\n'.join(dir_l ist(dir_name, ['.txt', '.tif']))[/code]

    Comment

    • setori88
      New Member
      • Sep 2008
      • 7

      #3
      fantastic, thank you so much that is exactly what I was looking for!

      I never realised that:
      Code:
      if os.path.splitext(f)[1] in whitelist:
      was possible, but now it makes complete sense!

      Kind regards

      Comment

      • setori88
        New Member
        • Sep 2008
        • 7

        #4
        How about this?

        Code:
            def _email_to_(self, where):
                self.logger.info("sent email to %s folder" % where)
                if self.emailDestination == None:
                    self.emailDestination = where
           
            def _pidgeon_hole_email(self):
                if self.emailDestination == "completed":
                    shutil.move(self.currentEmailPath, self._emailpath + self._completedfolder)
                elif self.emailDestination == "ignore":
                    shutil.move(self.currentEmailPath, self._emailpath + self._ignorefolder)
                elif self.emailDestination == "error":
                    shutil.move(self.currentEmailPath, self._emailpath + self._errorfolder)
                
                self.emailDestination = None
        What cool way is there to pythonise the above?

        this is the main method making use of these classes methods above
        Code:
            def process_inbox(self):
                self._getconfig()    
                self._setup_logger()
                self.logger.info("starting to digest emails")
                self._contains_emails()
                for self.currentEmailPath in self.emailsPathsInInbox:
                    if self._extract_attachments():
                        self._file_extension_check()
                        if self._convert():
                            if self._update_database():
                                self._email_to_("completed")
                            else:
                                self._email_to_("error")
                        else:
                            self._email_to_("error")
                    self._pidgeon_hole_email()
        Thanks so much

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          Not that there is anything "uncool" about your code, you could do this:
          [code=Python]def _pidgeon_hole_e mail(self):
          # possible values 'completed', 'ignore', 'error'
          folder = getattr(self, "_%sfolder" % self.emailDesti nation, None)
          if folder:
          shutil.move(sel f.currentEmailP ath, self._emailpath + folder)
          self.emailDesti nation = None[/code]

          Comment

          • setori88
            New Member
            • Sep 2008
            • 7

            #6
            oh i got you, actually your way is better!

            Thanks for that!

            Comment

            Working...