Auto persistency

Discussion in 'Python' started by Batista, Facundo, Aug 5, 2003.

  1. 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
    Batista, Facundo, Aug 5, 2003
    #1
    1. Advertising

  2. Batista, Facundo

    Timo Warns Guest

    Batista, Facundo wrote:
    >
    > [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
    Timo Warns, Aug 6, 2003
    #2
    1. Advertising

  3. Batista, Facundo

    Timo Warns Guest

    Timo Warns wrote:
    > Batista, Facundo wrote:
    > >
    > > [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
    Timo Warns, Aug 7, 2003
    #3
    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. Robert Buck
    Replies:
    0
    Views:
    448
    Robert Buck
    Feb 24, 2004
  2. Mauro

    Persistency problem

    Mauro, May 10, 2007, in forum: Java
    Replies:
    1
    Views:
    288
    Tom Hawtin
    May 10, 2007
  3. linkswanted
    Replies:
    1
    Views:
    898
  4. King
    Replies:
    2
    Views:
    245
  5. Christophe Poucet

    Concerning persistency techniques

    Christophe Poucet, Sep 29, 2005, in forum: Ruby
    Replies:
    3
    Views:
    80
    Dave Burt
    Sep 29, 2005
Loading...

Share This Page