looking for command-pipe performance advice

Steve Holden sholden at holdenweb.com
Sat Apr 7 17:23:51 EDT 2001


"Sheila King" <sheila at spamcop.net> wrote...
> On 6 Apr 2001 16:36:42 -0000, jason-dated-14a0ce692bc19405 at mastaler.com
wrote:
>
[Shiela's original sample code]
> :This returns a TypeError for me.
> :
> :First:
> :
> :    Traceback (most recent call last):
> :      File "./inject.py", line 10, in ?
> :        inject_message.write(headers+'\n')
> :    TypeError: __add__ nor __radd__ defined for these operands
>
> Oops. Sorry. I wasn't trying very hard there. I have a script that filters
> email/examines headers for me, and I do not use cStringIO or StringIO. I
read
> from standard input. Here are a few relevant lines from my code:
>
> origheaders=rfc822.Message(sys.stdin, 0)
> raw_body = sys.stdin.read()
>
> newheader = string.join(origheaders.headers,"")
> newheader += "X-Filter: some header field inserted here\n"
> line = "\n"
> body = string.join(raw_body, "")
>
Surely this line is completely redundant here, since you called read() and
not readlines()? Maybe you changed form the latter to the former and
overlooked this?

> mssg = newheader+line+body
>
>
> Then I send the message using the smtplib module.
>
> Um, so instead of using StingIO objects, I take the input and turn it into
> strings. Is this more effecient than what you were doing? I have no idea.
>
It is clearly going to be more efficient to use the file content directly as
you do above than to read it in, create a StrinIO (or, more efficiently, a
cStringIO) object and then use that to create the rfc822.Message. Sometimes,
of course, you just don't get a chance to do that -- if the data does not
start off in a file in the first place, it is necessary to create a
"file-like" object so rfc822.Message() can do its work. But this is a
deficiency in the rfc822 module.

I'm guessing (without having yet examined it) that Barry Warsaw's new
mimelib will be a lot more flexible about this kind of stuff.

regards
 Steve






More information about the Python-list mailing list