[Patches] [ python-Patches-601959 ] replace_header method for Message class

noreply@sourceforge.net noreply@sourceforge.net
Fri, 30 Aug 2002 07:10:54 -0700


Patches item #601959, was opened at 2002-08-29 11:23
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=601959&group_id=5470

Category: Library (Lib)
Group: Python 2.3
Status: Open
Resolution: None
Priority: 5
Submitted By: Skip Montanaro (montanaro)
Assigned to: Barry A. Warsaw (bwarsaw)
Summary: replace_header method for Message class

Initial Comment:
There are certain situations where I want to replace the contents of a 
header and not change either the header's name or its position in the 
list of headers.  Accordingly, something like

    del msg["Subject"]
    msg["Subject"] = newsubj

doesn't quite cut it.  For example, de-SpamAssassin-ing a message 
for feeding to GBayes would change the header order (minor change, 
but annoying) and potentially change the actual subject string (some 
spam seems to use "SUBJECT:" instead of "Subject:").

Here's a replace_header method that does what I want.

    def replace_header(self, hdr, newval):
        """replace first value for hdr with newval"""
        hdr = hdr.lower()
        for (i, (k, v)) in enumerate(self._headers):
            if k.lower() == hdr:
                self._headers[i] = (k, newval)


----------------------------------------------------------------------

>Comment By: Skip Montanaro (montanaro)
Date: 2002-08-30 09:10

Message:
Logged In: YES 
user_id=44345

How about the attached?  Used zip() instead of enumerate.  I'll let you 
worry about the name... ;-)


----------------------------------------------------------------------

Comment By: Barry A. Warsaw (bwarsaw)
Date: 2002-08-30 08:12

Message:
Logged In: YES 
user_id=12800

+1 although I'd like to think some on the method name. 
Also, we'll need a Python 2.1 compatible version of that
method (no "enumerate").   The email package still has to be
Py2.1 compatible.  See PEP 291.

Care to upload a new version?

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=601959&group_id=5470