Python is an object oriented (OO) language. The very best thing if you have a
lot of time is to learn the language fully, read several books on database
design, and implement a gui driven app, OO from top to bottom.
If you need to get something to work before you have time to become a
pythonologist, try an array of dictionaries (AoD), a datastructure I find
very handy in a pinch. Here are some patterns for you to think about (also
have some masochistic fun trying to do something similar in perl--now try in
perl without pointers if you cheated!):
#
# my_aod.py : an aod example module
#
# top of your module
import pickle
# saving your database (3 steps recommended)
def my_save_aod(aod,filename):
afile = open(filename,"w")
pickle.dump(aod,afile)
afile.close()
# load your database (3 steps recommended)
def my_load_aod(filename):
afile = open(filename)
an_aod = pickle.load(afile)
afile.close()
return an_aod
# get a subset whose records match the key
def my_aod_find(aod,akey,avalue):
sub_aod = []
for element in aod:
if element[akey] == avalue:
sub_aod.append(element)
return sub_aod
# a simple key based sort
def my_aod_sort(aod,akey):
return_aod = aod[:]
return_aod.sort(lambda x,y : cmp(x[akey],y[akey]))
return return_aod
#
# methinks a module hath I begun
#
# end my_aod.py
###########################################################
# example usage of an aod (new file)
#
from my_aod import *
# some records to start you off
bob = {"name":"Bob","spouse":"Carol","phone":"8675309"}
carol = {"name":"Ted","spouse":"Alice","phone":"5551212"}
ted = {"name":"Alice","spouse":"Ted","phone":"5551212"}
alice = {"name":"Carol","spouse":"Bob","phone":"8675309"}
# adding records arbitrarily
database = [bob]
database.extend([carol,ted])
database.append(alice)
# playing with find and sort
for person in my_aod_find(database,"name","Bob"):
print "name:", person["name"], "; spouse:", person["spouse"]
for person in my_aod_sort(database,"name"):
print "name:", person["name"]
for person in my_aod_sort(database,"spouse"):
print "name:", person["name"], "; spouse:", person["spouse"]
my_save_aod(database,"people.pkl")
new_db = my_load_aod("people.pkl")
for person in new_db:
print "name:", person["name"], "; spouse:", person["spouse"]
Hey all,
I am currently working on a simple program (small group membership
database) just to get to grips with the language (bit of a newbie here
)
and am wondering about the best way of organising the data within the
program.
=("name","address","phone","subs-due")
Then you would put membershipdatabase = (person1, person2, personX....)
etc
Now is this the best way of organising this data within the program or is
there a better "known" way?
Also I want to just store this info to a standard txt file. Probably CSV or
something. Is there any particular way I should be saving this data to
file?
Thanks in advance
Nick
--
James Stroud, Ph.D.
UCLA-DOE Institute for Genomics and Proteomics
611 Charles E. Young Dr. S.
MBI 205, UCLA 951570
Los Angeles CA 90095-1570
http://www.jamesstroud.com/