Auto persistency

  • Thread starter Batista, Facundo
  • Start date
B

Batista, Facundo

Example of class. Just an object between all of them in my program:


class Example:
def __init__(self, value):
self.value = value
return

def getValue(self):
return self.value

def setValue(self, value):
self.value = value
return


I want to implement persistency. I think it's a good idea to each object be
the responsible of it's own persistence in a file, in a sql server, anywhere
(opinions?).

Adding persistence:

class Example:
def __init__(self, value):
self.value = value
# create in sql server
return

def getValue(self):
return self.value

def setValue(self, value):
self.value = value
# modify in sql server
return


Now I have two questions:

1) How do I implement the dead of the object. I need it to erase
itself from the sql server. Should I define the __del__ method?

2) When the program starts, how do I retrieve all the objects from
the sql server to memory? (considering that only the object knows how it's
persisted).

Thanks for all.


.. Facundo
 
T

Timo Warns

[Example of class with get- and set-methods]

I want to implement persistency. I think it's a good idea to each object be
the responsible of it's own persistence in a file, in a sql server, anywhere
(opinions?).

This is some kind of a design pattern often called "data access
objects". Java has this already built-in with its JDOs.
Hava a look at this design pattern and how it is implemented in other
languages or python packages.
Do you need any kind of capabilities like transactions?
[...]

Now I have two questions:

1) How do I implement the dead of the object. I need it to erase
itself from the sql server. Should I define the __del__ method?

You probably should create another method like "drop". If you implement
the __del__ method, you will not have any persistency, because any time
your object is deleted, the data will be deleted in your data source.
2) When the program starts, how do I retrieve all the objects from
the sql server to memory? (considering that only the object knows how it's
persisted).

You could implement a factory, that generates the objects and loads the
data by a given primary key.

HTH, Timo
 
T

Timo Warns

Timo said:
[Example of class with get- and set-methods]

[...]

This is some kind of a design pattern often called "data access
objects". Java has this already built-in with its JDOs.

Actually "built-in" is a little misleading ;-) There is an optional
package from Sun available. Strictly it is not part of the language.
Sorry for that.

Timo
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top