MaildirMessage

T

Tzury

I am getting the following error when trying to iterate in a message
in a Maildir directory.
please help.
.... for m in msg:
.... print m
....
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "email/message.py", line 286, in __getitem__
File "email/message.py", line 352, in get
AttributeError: 'int' object has no attribute 'lower'
 
G

Gabriel Genellina

I am getting the following error when trying to iterate in a message
in a Maildir directory.
please help.

... for m in msg:
... print m
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "email/message.py", line 286, in __getitem__
File "email/message.py", line 352, in get
AttributeError: 'int' object has no attribute 'lower'

msg is an instance of MaildirMessage (subclass of Message) - it has no
specific iterator, so "for m in msg" tries to use the sequence protocol,
starting at 0; that is, tries to get msg[0]. Message objects support the
mapping protocol, and msg[0] tries to find a *header* 0, converting the
name 0 to lowercase, and fails miserably.
Try with:

for msg in mbox:
print msg

or read the MaildirMessage (and Message) docs to see the ways you can
handle it.
 
B

Ben Finney

Gabriel Genellina said:
msg is an instance of MaildirMessage (subclass of Message) - it has
no specific iterator, so "for m in msg" tries to use the sequence
protocol, starting at 0; that is, tries to get msg[0]. Message
objects support the mapping protocol, and msg[0] tries to find a
*header* 0, converting the name 0 to lowercase, and fails miserably.

Which is a bug in the 'email.message' module, in my view. If it's
attempting to support a mapping protocol, it should allow iteration
the same way standard Python mappings do: by iterating over the keys.
 
T

Tzury

Which is a bug in the 'email.message' module, in my view. If it's
attempting to support a mapping protocol, it should allow iteration
the same way standard Python mappings do: by iterating over the keys.

I thought it is a bug as well, but who am I a python newbie to say so.
I found inspect.getmembers(msg) as a good solution to map the message
properties.

10x,
Tzury
 
S

Steve Holden

Ben said:
Gabriel Genellina said:
msg is an instance of MaildirMessage (subclass of Message) - it has
no specific iterator, so "for m in msg" tries to use the sequence
protocol, starting at 0; that is, tries to get msg[0]. Message
objects support the mapping protocol, and msg[0] tries to find a
*header* 0, converting the name 0 to lowercase, and fails miserably.

Which is a bug in the 'email.message' module, in my view. If it's
attempting to support a mapping protocol, it should allow iteration
the same way standard Python mappings do: by iterating over the keys.
Stop assuming that everyone sees requirements the same as you - the
author of the email module has long years of experience, and has
provided an excellent and overall usable piece of software.

Criticisms such as yours are easy (we can all experess our opinions, to
which we are all entitled), but mere expression of an opinion isn't
going to change anything. Instead, change the module to do what you
think it should. Then you can at least use it yourself, and if your code
is a genuine improvement you can submit it as a patch.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------
 
G

Gabriel Genellina

I thought it is a bug as well, but who am I a python newbie to say so.
I found inspect.getmembers(msg) as a good solution to map the message
properties.

Hmmm, I don't see why a message should support iteration, or even why
iterating over a message should mean iterating over its headers... Anyway,
if you want to iterate over all the message headers, the simplest way is
using msg.keys()
 
B

Ben Finney

Steve Holden said:
Stop assuming that everyone sees requirements the same as you - the
author of the email module has long years of experience, and has
provided an excellent and overall usable piece of software.

I don't see that this fact (which I agree is the case) in any way
makes the code immune from bugs, nor from the discussion of the
possibility.
Criticisms such as yours are easy (we can all experess our opinions,
to which we are all entitled), but mere expression of an opinion
isn't going to change anything.

It can, though, lead to a discussion about whether it *is* a bug or
not.

Care to contribute something other than the lashing you put into your
message?
 
S

Steve Holden

Ben said:
I don't see that this fact (which I agree is the case) in any way
makes the code immune from bugs, nor from the discussion of the
possibility.


It can, though, lead to a discussion about whether it *is* a bug or
not.

Care to contribute something other than the lashing you put into your
message?
Apart from the lashing (lashing? I take it you've never visited
comp.lang.perl regularly) I think Gabriel said it well enough: there
just doesn't seem to be a compelling case that "iteration over a
message" should do what you apparently expected it to.

What do you actually think

.... for m in msg:
.... print m

should do? Why do you believe that what you think it should do would be
a natural choice?

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------
 
T

Tzury

What do you actually think
... for m in msg:
... print m

should do? Why do you believe that what you think it should do would be
a natural choice?

I needed to know how to extract particular parts of a message, such as
the message 'body', 'subject', 'author', etc.
I couldn't find it in the documentation this kind of information until
I ran the inspect.getmembers(msg) and found out that the message body
stored in _payload. that is msg._payload is the content of the
message.

Regarding the *bug* thing it is very simple. If you iterate an int you
get an exception <TypeError: 'int' object is not iterable>, clear
enough. Yet here I got the following exception <AttributeError: 'int'
object has no attribute 'lower'> which suggest an attempt to iterate
---. I do think that a message should support iteration for the very
fact that each can contain a different type and amount of headers, and
iteration in this case will make application's code clean and unified.
 
G

Gabriel Genellina

I needed to know how to extract particular parts of a message, such as
the message 'body', 'subject', 'author', etc.
I couldn't find it in the documentation this kind of information until
I ran the inspect.getmembers(msg)

Well, you took the hard way... the easy way would be to read the
documentation: http://docs.python.org/lib/module-email.message.html
and found out that the message body
stored in _payload. that is msg._payload is the content of the
message.

You should perceive the initial _ in _payload as a yellow light - a
warning. It's an implementation detail and one should avoid messing with
it - best to stick to the documented API, get_payload() in this case.
Regarding the *bug* thing it is very simple. If you iterate an int you
get an exception <TypeError: 'int' object is not iterable>, clear
enough. Yet here I got the following exception <AttributeError: 'int'
object has no attribute 'lower'> which suggest an attempt to iterate
---. I do think that a message should support iteration for the very
fact that each can contain a different type and amount of headers, and
iteration in this case will make application's code clean and unified.

I don't see what you mean. You can always enumerate the message headers
using msg.keys(), along with has_key(), items(), etc. And what you want to
unify the code with?

Even if Message becomes iterable, I still don't see why should iteration
over its *headers* be the preferred meaning. For a multipart/* MIME
message, iterating over the parts looks much more interesting.
 
B

Ben Finney

Steve Holden said:
What do you actually think

.... for m in msg:
.... print m

should do? Why do you believe that what you think it should do would
be a natural choice?

I think it odd for a Message to support the mapping protocol. However,
since that's what is announced, then I expect it to actually support
the mapping protocol, which in my expectation includes iterating over
the keys.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top