[Tutor] About classes and databases

Kent Johnson kent37 at tds.net
Sun Nov 22 14:41:26 CET 2009


On Sun, Nov 22, 2009 at 4:12 AM, Michele Alzetta
<michele.alzetta at gmail.com> wrote:
> Hallo all!
>
> Long time since I last dabbled in python :-(
>
> I was thinking of a possible solution to a problem I have: I could make a
> Class which contains, amongst other things, a couple of datetime.datetime
> objects.
> The class also will have to contain an element taken from a Tuple and
> various elements taken from a series of dictionaries.
>
> First problem: when creating each instance of the class I have to pass it
> the information necessary to create the correct datetime objects - how would
> I do this?

What information do you have? You could pass that to the constructor.
Or perhaps a better approach is to create the datetime itself and pass
that to the constructor.

> Second problem: I would find myself to be working with something in the
> order of 500 instances of this class contemporaneously, how do I create them
> "automatically" from a series of data and once I've created them how do I
> grab the correct one when I need it?

Create the classes in a loop and put them in a data structure such as
list or dict. How do you know which is the "right" one? That will
determine what data structure to use.

> Or do you think my approach is all wrong, and it would be better for me not
> to use classes at all and to store the data in a database?

Your approach may be fine, a database may also work. You haven't given
enough information for us to know.

> But is it possible to store datetime objects and dictionaries in a database?

You can store dates and timestamps in a database and you can store
key-value pairs.

Kent


More information about the Tutor mailing list