Organising data within a program

Discussion in 'Python' started by Nick Evans, Dec 13, 2004.

  1. Nick Evans

    Nick Evans Guest

    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
     
    Nick Evans, Dec 13, 2004
    #1
    1. Advertising

  2. Nick Evans

    James Stroud Guest

    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/
     
    James Stroud, Dec 14, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. WindAndWaves

    best practice in organising folders

    WindAndWaves, Jan 14, 2005, in forum: HTML
    Replies:
    18
    Views:
    740
    Jan Faerber
    Jan 17, 2005
  2. Jason Carney

    organising classes in c++?

    Jason Carney, Nov 19, 2003, in forum: C++
    Replies:
    1
    Views:
    334
    Jason
    Nov 19, 2003
  3. Replies:
    5
    Views:
    4,199
    clintonG
    Jun 2, 2006
  4. Replies:
    6
    Views:
    482
    beza1e1
    Sep 21, 2005
  5. jimburton

    Organising unit tests

    jimburton, Sep 29, 2006, in forum: Python
    Replies:
    1
    Views:
    322
    jimburton
    Sep 29, 2006
Loading...

Share This Page