Re: [Tutor] How to model objects aimed to persistence?

2010-06-17 Thread Alan Gauld


"Knacktus"  wrote

###
my_favourite_movies = FavouriteMovies([Movie("Gladiator", "Russel 
Crowe")])

###

When I want to rebuild the data from a xml file or database, my 
objects somehow need to know which references to other objects they 
hold.


OK, If you limit yourself to XML or a database then yes you will
need an ID and use the IDs as references. Thats why Pickle/Shelve
are so convenient for persistence - they don;t need the references,
you can just pickle the list of objects, or whatever.

HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to model objects aimed to persistence?

2010-06-16 Thread Knacktus

Am 17.06.2010 02:17, schrieb Alan Gauld:

"Steven D'Aprano"  wrote

Deliberate misspellings in class and variable names will cost you
*far* more in debugging time when you forget to misspell the words
than they will save you in typing. Trust me on this.


Thanks for that hint. I can imagine the nightmares ... It was just a 
spelling mistake. English is not my native language and I sometime tend 
to skip trailing "e"s ;-).


But I think I need to clarify my question a bit. The point I'm unsure 
with is how to best model references in python for persistence when not 
using pickle or any dedicated python. Python doesn't even need names as 
reference to the objects location in memory. I could do something like this:


###
my_favourite_movies = FavouriteMovies([Movie("Gladiator", "Russel Crowe")])
###

When I want to rebuild the data from a xml file or database, my objects 
somehow need to know which references to other objects they hold.
I don't now wether I have to use explicit indices in my python code to 
make it "xml-ready" and if that's the case, how to do it smoothly. Or 
whether there's some other trick.


Cheers,

Jan
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to model objects aimed to persistence?

2010-06-16 Thread Alan Gauld
"Steven D'Aprano"  wrote 

Deliberate misspellings in class and variable names 
will cost you *far* more in debugging time when you 
forget to misspell the words than they will save you 
in typing. Trust me on this.


I don't normally do "me too" postings but this is such 
a big deal that I just have to concur with Steven. It doesn't 
matter too much if you abbreviate local variables but 
anything that might be exposed at the module level 
and visible across imports should be spelled in full. 
It will only cause grief and pain later, if not to you 
then to anyone who imports your code...


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to model objects aimed to persistence?

2010-06-16 Thread Steven D'Aprano
On Thu, 17 Jun 2010 06:39:44 am Knacktus wrote:

> 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 ...

Oh wow, deja vu... this is nearly the same question as just asked a day 
or so ago, in the thread "conventions for establishing and saving 
default values for variables". The answer I give will be more or less 
the same as was already given in that thread:

Use ConfigParser for ini-style config files.

Use pickle or shelve for serialising arbitrary Python objects.

You probably shouldn't use marshal, as that's awfully limited and really 
only designed for serialising Python byte code.

You can roll your own XML solution using the various XML modules in the 
standard library, or use plistlib, an XML-based storage format borrowed 
from Mac OS X.

Or one of the other standard serialisers like JSON or YAML, although 
YAML is not in the standard library yet.

Or you can use any one of many different databases, although that's 
probably massive overkill.


> 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, 

"Have to"? Certainly not. If your data doesn't need an ID field, there's 
no need to add one just for serialisation. It would be a pretty bizarre 
serialiser that couldn't handle this:

{key: value}

but could handle this:

({id: key}, {key: value})



> but I 
> wouldn't like it and hope that there're some other sweet pythonic
> solutions?

There's a few... :)



By the way, looking at your class name:

class FavoritMovies(object):

I can forgive the American (mis)spelling of Favo(u)rite, but why have 
you dropped the E off the end of "favorite"? I'd almost think your 
keyboard was broken, but no, you have E in Movies and object.

Deliberate misspellings in class and variable names will cost you *far* 
more in debugging time when you forget to misspell the words than they 
will save you in typing. Trust me on this.


-- 
Steven D'Aprano
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to model objects aimed to persistence?

2010-06-16 Thread Mark Lawrence

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@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.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] How to model objects aimed to persistence?

2010-06-16 Thread Knacktus

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@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor