[Tutor] How to model objects aimed to persistence?

Mark Lawrence breamoreboy at yahoo.co.uk
Wed Jun 16 23:07:11 CEST 2010


On 16/06/2010 21:39, Knacktus wrote:
> Hi everyone,
>
> within a python application I can easily model object association with
> simple references, e.g.:
>
> #################################################################
> class FavoritMovies(object):
> def __init__(self, movies):
> self.movies = movies
>
> class Movie(object):
> def __init__(self, name, actor):
> self.name = name
> self.actor = actor
>
> gladiator = Movie("Gladiator", "Russel Crowe")
> my_favorit_movies = FavoritMovies([gladiator])
>
> kung_fu_panda = Movie("Kung Fu Panda", "Jack Black")
> your_favorit_movies = FavoritMovies([gladiator, kung_fu_panda])
> ##################################################################
>
> So far, so good. But what is best practise to prepare this data for
> general persistence? It should be usable for serialisation to xml or
> storing to an RDBMS or ObjectDatabase ...
>
> I guess I have to incorporate some kind of id for every object and use
> this as reference with some kind of look-up dictionary, but I wouldn't
> like it and hope that there're some other sweet pythonic solutions?
>
> Cheers,
>
> Jan
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>

Hi Jan,

I guess you're looking for something like the shelve or pickle modules.
http://docs.python.org/library/shelve.html
http://docs.python.org/library/pickle.html

HTH.

Mark Lawrence.



More information about the Tutor mailing list