Algorithm for Labels like in Gmail
Gerard Flanagan
grflanagan at yahoo.co.uk
Sun Jun 11 07:45:54 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
There's a program called 'buzhug' (http://buzhug.sourceforge.net/)
which is described as "a pure-Python database engine, using a Pythonic,
no-SQL syntax". I think it's in its early stages of development but it
might be suitable for your project. I spent the morning playing about
with it using your example of a (Very Simple) Contact Manager, and
there's some runnable but unfinished code below. It only implements a
'Many-to-one' relationship between Contacts and Groups - in other
words, a Contact can only belong to one Group. There's probably a lot
I haven't considered, it will break easily, and the docstrings are in
the post, but there you go.
Have fun!
Gerard
(also - http://groups.google.com/group/buzhug)
------------------------------------------------------------
print
from buzhug import Base
import os
def get_bases():
if os.path.exists('data'):
return Base('data/dt_groups'), Base('data/dt_contacts')
else:
#Bases don't exist, so create them
os.mkdir('data')
groups = Base('data/dt_groups')
groups.create( ('name', str) )
groups.insert( 'Family' )
groups.insert( 'Friends' )
groups.commit()
contacts = Base('data/dt_contacts')
contacts.create( ('group', groups), ('first_name', str),
('last_name', str), ('email', str) )
contacts.insert( groups[0], 'Jack', 'Jones', 'jj at abcd.com' )
contacts.insert( groups[0], 'John', 'Jones', 'jj at wxyz.com' )
contacts.insert( groups[1], 'James', 'diGriz',
'digriz at stainless.com' )
contacts.insert( groups[1], 'Dirk', 'Gently',
'dirk at dogstoerd.com' )
contacts.commit()
return groups, contacts
def usage():
print '''
ADD - Add a Contact
eg. ADD Family, Susan, Smith, suzie at hatmail.com
eg. ADD Work, Jason, Jones, jj at mywork.com
DEL - Delete a Contact or Group
eg. DEL firstname=Susan, lastname=Smith
eg. DEL group=Family
FIND - Search for contacts
eg. FIND lastname=Smith
eg. FIND group=Family
EXIT - End the program
'''
def intro():
print '\n' * 5
print '#' * 52
print '#' * 20, ' CONTACTS ', '#' * 20
print '#' * 52
usage()
print '\n' * 2
def get_input(prompt):
s = raw_input(prompt).strip()
if s.upper() == 'EXIT':
raise EOFError
return s
def ADD(groupname, firstname, lastname, email):
groups, contacts = get_bases()
try:
groups.open()
#see if a group with this name exists
records = [ g for g in groups if g.name == groupname ]
if len(records) == 0:
#no group with this name, so create it
gid = groups.insert( name=groupname.strip() )
group = groups[gid]
else:
group = records[0]
finally:
groups.commit()
try:
contacts.open()
contacts.insert(group, firstname.strip(), lastname.strip(),
email.strip())
finally:
contacts.commit()
def test():
groups, contacts = get_bases()
try:
contacts.open()
for contact in contacts:
print contact.group.name, contact.first_name,
contact.last_name
finally:
contacts.commit()
if __name__ == '__main__':
intro()
while True:
try:
s = get_input('-> ')
if s[0] == '?':
usage()
elif s == 'test':
test()
elif s[:4] == 'ADD ':
grp, fname, lname, email = s[4:].split(',')
try:
ADD(grp, fname, lname, email)
except Exception, e:
print e
except EOFError:
break
print '\nbye'
--------------------------------------------------------------------
More information about the Python-list
mailing list