Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

MaildirMessage

1 view
Skip to first unread message

Tzury

unread,
Jul 12, 2007, 8:46:32 PM7/12/07
to
I am getting the following error when trying to iterate in a message
in a Maildir directory.
please help.

>>> from mailbox import Maildir, MaildirMessage
>>> mbox = Maildir('path/to/mailbox', create = False, factory = MaildirMessage)
>>> for msg in mbox:
... 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'
>>>

Gabriel Genellina

unread,
Jul 12, 2007, 9:19:48 PM7/12/07
to pytho...@python.org
En Thu, 12 Jul 2007 21:46:32 -0300, Tzury <Afro.S...@gmail.com>
escribió:

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.

--
Gabriel Genellina

Ben Finney

unread,
Jul 13, 2007, 2:26:59 AM7/13/07
to
"Gabriel Genellina" <gags...@yahoo.com.ar> writes:

> 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.

--
\ "If you continue running Windows, your system may become |
`\ unstable." -- Microsoft, Windows 95 BSOD message |
_o__) |
Ben Finney

Tzury

unread,
Jul 13, 2007, 6:38:01 AM7/13/07
to
> 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

Steve Holden

unread,
Jul 13, 2007, 8:14:11 AM7/13/07
to pytho...@python.org
Ben Finney wrote:
> "Gabriel Genellina" <gags...@yahoo.com.ar> writes:
>
>> 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 -------------

Gabriel Genellina

unread,
Jul 13, 2007, 9:38:15 PM7/13/07
to pytho...@python.org
En Fri, 13 Jul 2007 07:38:01 -0300, Tzury <Afro.S...@gmail.com>
escribió:

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()

--
Gabriel Genellina

Ben Finney

unread,
Jul 13, 2007, 9:58:41 PM7/13/07
to
Steve Holden <st...@holdenweb.com> writes:

> Ben Finney wrote:
> > 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.

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?

--
\ "Dyslexia means never having to say that you're ysror." -- |
`\ Anonymous |
_o__) |
Ben Finney

Steve Holden

unread,
Jul 13, 2007, 10:25:31 PM7/13/07
to pytho...@python.org
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?

Tzury

unread,
Jul 14, 2007, 5:26:43 AM7/14/07
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?

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.

Gabriel Genellina

unread,
Jul 14, 2007, 8:14:51 AM7/14/07
to pytho...@python.org
En Sat, 14 Jul 2007 06:26:43 -0300, Tzury <Afro.S...@gmail.com>
escribió:

>> 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)

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.

--
Gabriel Genellina

Ben Finney

unread,
Jul 14, 2007, 8:50:39 PM7/14/07
to
Steve Holden <st...@holdenweb.com> writes:

> 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.

--
\ "One of the most important things you learn from the internet |
`\ is that there is no 'them' out there. It's just an awful lot of |
_o__) 'us'." -- Douglas Adams |
Ben Finney

0 new messages