Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Organising data within a program

Reply
Thread Tools

Organising data within a program

 
 
Nick Evans
Guest
Posts: n/a
 
      12-13-2004
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.

From my understanding I would have something like person1
=("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


 
Reply With Quote
 
 
 
 
James Stroud
Guest
Posts: n/a
 
      12-14-2004
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"]





On Monday 13 December 2004 03:06 pm, Nick Evans wrote:
> 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.
>
> >From my understanding I would have something like person1

>
> =("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/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Conference Organising Program Ron McNulty NZ Computing 2 03-27-2006 02:04 PM
best practice in organising folders WindAndWaves HTML 18 01-17-2005 10:34 PM
organising newsgroups folders Walter Mitty Firefox 2 05-18-2004 10:48 AM
Organising Folders MB Digital Photography 9 12-28-2003 06:08 PM
Organising files m8ee Computer Support 1 08-21-2003 07:39 PM



Advertisments