Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 72687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:05:01+00:00 2026-05-10T20:05:01+00:00

Hi Guys could you please help me refactor this so that it is sensibly

  • 0

Hi Guys could you please help me refactor this so that it is sensibly pythonic.

import sys import poplib import string import StringIO, rfc822 import datetime import logging  def _dump_pop_emails(self):     self.logger.info('open pop account %s with username: %s' % (self.account[0], self.account[1]))     self.popinstance = poplib.POP3(self.account[0])     self.logger.info(self.popinstance.getwelcome())      self.popinstance.user(self.account[1])     self.popinstance.pass_(self.account[2])     try:         (numMsgs, totalSize) = self.popinstance.stat()         for thisNum in range(1, numMsgs+1):             (server_msg, body, octets) = self.popinstance.retr(thisNum)             text = string.join(body, '\n')             mesg = StringIO.StringIO(text)                                            msg = rfc822.Message(mesg)             name, email = msg.getaddr('From')             emailpath = str(self._emailpath + self._inboxfolder + '\\' + email + '_' + msg.getheader('Subject') + '.eml')             emailpath = self._replace_whitespace(emailpath)             file = open(emailpath,'wb')             file.write(text)             file.close()             self.popinstance.dele(thisNum)     finally:         self.logger.info(self.popinstance.quit())  def _replace_whitespace(self,name):     name = str(name)     return name.replace(' ', '_')    

Also in the _replace_whitespace method I would like to have some kind of cleaning routine which takes out all illegal characters which could cause processing.

Basically I want to write the email to the inbox directory in a standard way.

Am i doing something wrong here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T20:05:02+00:00Added an answer on May 10, 2026 at 8:05 pm

    This isn’t refactoring (it doesn’t need refactoring as far as I can see), but some suggestions:

    You should use the email package rather than rfc822. Replace rfc822.Message with email.Message, and use email.Utils.parseaddr(msg[‘From’]) to get the name and email address, and msg[‘Subject’] to get the subject.

    Use os.path.join to create the path. This:

    emailpath = str(self._emailpath + self._inboxfolder + '\\' + email + '_' + msg.getheader('Subject') + '.eml') 

    Becomes:

    emailpath = os.path.join(self._emailpath + self._inboxfolder, email + '_' + msg.getheader('Subject') + '.eml') 

    (If self._inboxfolder starts with a slash or self._emailpath ends with one, you could replace the first + with a comma also).

    It doesn’t really hurt anything, but you should probably not use ‘file’ as a variable name, since it shadows a built-in type (checkers like pylint or pychecker would warn you about that).

    If you’re not using self.popinstance outside of this function (seems unlikely given that you connect and quit within the function), then there’s no point making it an attribute of self. Just use ‘popinstance’ by itself.

    Use xrange instead of range.

    Instead of just importing StringIO, do this:

    try:     import cStringIO as StringIO except ImportError:     import StringIO 

    If this is a POP mailbox that can be accessed by more than one client at a time, you might want to put a try/except around the RETR call to continue on if you can’t retrieve one message.

    As John said, use ‘\n’.join rather than string.join, use try/finally to only close the file if it is opened, and pass the logging parameters separately.

    The one refactoring issue I could think of would be that you don’t really need to parse the whole message, since you’re just dumping a copy of the raw bytes, and all you want is the From and Subject headers. You could instead use popinstance.top(0) to get the headers, create the message (blank body) from that, and use that for the headers. Then do a full RETR to get the bytes. This would only be worth doing if your messages were large (and so parsing them took a long time). I would definitely measure before I made this optimisation.

    For your function to sanitise for the names, it depends how nice you want the names to be, and how certain you are that the email and subject make the filename unique (seems fairly unlikely). You could do something like:

    emailpath = ''.join([c for c in emailpath if c in (string.letters + string.digits + '_ ')]) 

    And you’d end up with just alphanumeric characters and the underscore and space, which seems like a readable set. Given that your filesystem (with Windows) is probably case insensitive, you could lowercase that also (add .lower() to the end). You could use emailpath.translate if you want something more complex.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 71k
  • Answers 72k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Here's a less sophisticated, recursive version like chmod 700 described.… May 11, 2026 at 1:29 pm
  • added an answer You can create a HttpHandler which handles image requests and… May 11, 2026 at 1:29 pm
  • added an answer To scale through the center you can add in your… May 11, 2026 at 1:29 pm

Related Questions

No related questions found

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.