Should I prefer an external database

Jeremy Hylton jeremy at zope.com
Tue Apr 22 11:16:37 EDT 2003


On Tue, 2003-04-22 at 04:11, Brian Elmegaard wrote:
> I am working on a script for organizing a program for a conference. I
> started out using a connection to mySQL, but then I thought that it
> would be easier to keep it within python. So I implemented a class:
> 
> class Paper:
>         def __init__(title,author,session):
>                 self.title=title
>                 self.author=author
>                 self.session=session
> I then have a list to which each new paper instance is appended. I can
> sort by any of the paper instances variables by using the
> decorate-sort-undecorate method.
> 
> I now have it working, but I probably could do better, so:
> 
> Is this a more pythonic way to implement this?

You've chosen a simple and obvious way to implement it, which sounds
pythonic to me.  (Note that you forgot to include self in the argument
list to __init__.)

You may find ZODB helpful for this project, because it allows you to
store your objects in a database without warping your application's data
model to fit in an SQL database.  There's a short guide to help get you
started at:  http://www.zope.org/Wikis/ZODB/guide/index.html

> To do a select I think I need to use for loops specifying the names of
> the variables, i.e:
> 
> for paper in paperlist:
>         if paper.session=='foo':
>                 print paper.title
> 
> Probably this might be done more elegantly, but how do I know which
> variables (database columns) the instance has?

Who put the instances in the list?  That is, I assume that you have
control over what kind of objects get put in the list.  So you should
know what attributes they have.

> Would I be better of with a list of list than with a list of class
> instances? 
> What about a class of classes? Could such be made and inherit from the
> list class to support index and sort?

A list of class instances seems like the clearest way to write it.  In
ZODB, you could create an index of objects keyed by the session variable
if you expected to do this kind of query a lot.

Jeremy








More information about the Python-list mailing list