Algorithm for Labels like in Gmail

George Sakkis george.sakkis at gmail.com
Sat Jun 10 21:12:53 EDT 2006


rhcarvalho at gmail.com wrote:

> Hello there!
>
> I'm trying to make a simple Contact Manager using python (console
> only), however i'm having trouble implementing a division by "Groups"
> or "Labels" just like in Gmail. I don't have any real code to post
> because all i got now is a raw TXT file holding the names and phones of
> my contacts.
>
> The only idea I could figure out until now seems too weak, so that's
> why i'm asking for help. I thought of making a separate list (in a text
> file) holding all the possible groups, where each group hold the names
> of the contacts. Then if i access a group, i'll be able to see all
> contacts related to that group. On the other hand, i'll also need to
> attach to the contact instance a list of every group it is present.
> I think it's a bad idea because it seems to be very easy to get things
> messed up. Like I can get things corrupted or bad linked, and i'll
> always need to run functions to check all the realations between
> contact names and groups...
>
> I like the way i can "label" emails on Gmail, does anyone know how I
> can implement such kind of feature? What's the best/broadly used
> algorithm?
>
> Sorry for the long message, and thanks in advance
>
> Rodolfo Carvalho

Google for "many-to-many relationships". In short, you have two entity
classes (say emails and labels) where each instance of one entity may
be associated to zero or more instances of the other entity. In
databases you implement this by having three tables, one for each
entity and one for their association:

   Email     RelEmailLabel     Label
----------   --------------    ---------
ID        <--- EmailID          ID
subject        LabelID --->     name
...                             ...


Then you can associate mails to labels by joining all three tables
together on the IDs. Of course you can implement this in memory as well
but you should probably want to store them in some persistent area
anyway, so an rdbms the way to go. Sqlite (with pysqlite) would meet
your needs just fine.

HTH,
George




More information about the Python-list mailing list