Should I use a dictionary?

Greg Jorgensen gregj at pdxperts.com
Tue Jun 26 00:06:48 EDT 2001


As you pick the messages you want store the message objects in a list (you
are actually just making a list of references to the message objects, so
don't worry about making copies of messages). For example:

messages = []
# get a message
m = rfc822.Message(file)

if m_is_an_interesting_message:
    messages.append(m)


When you want to sort the messages, use your own sort function to compare
the message date/time:

def cmp_msg_time(m1, m2):
    "compares time header from two message objects, returns -,0,+ compatible
with sort"
    import time

    t1 = time.mktime(m1.getdate('Date'))
    t2 = time.mktime(m2.getdate('Date'))

    return t1 - t2


then sort the list with that comparison function:

messages.sort(cmp_msg_time)

I'm haven't tried this on actual email messages but I think you get the
idea.

Greg Jorgensen
PDXperts LLC
Portland, Oregon USA



"Gustaf Liljegren" <gustafl at algonet.se> wrote in message
news:Xns90CCC3D73603gustaflalgonetse at 194.213.69.148...
> I have an object type for e-mail messages based on the rfc822 module.
After
> picking out some messages from several sources, I want to sort them in
> order of the date property of the message object (as far as I understand,
> everything in this process needs to be kept in memory). Anyway, the
problem
> is that I need some hints about how to sort the messages, as they are not
> sorted in way I get them.
>
> One idea I came up with was to put the date property of the message object
> as a key in a dictionary, and let the rest of the message be the value!
But
> even if it would work, I think it's ugly. If someone can convince me that
> it's not ugly, or give a better (i.e. faster) solution, I'd be happy.
>
> Regards,
>
> Gustaf Liljegren





More information about the Python-list mailing list