[sage-support] Re: What is needed in order to save objects of extension class?

2007-11-09 Thread William Stein

On Nov 8, 2007 5:25 PM, Simon King <[EMAIL PROTECTED]> wrote:
> Dear William,
>
> On Nov 8, 5:36 pm, "William Stein" <[EMAIL PROTECTED]> wrote:
> > By an extension class I assume you mean a class you defined using Cython?
>
> Yes.
>
> > Type dumps? for the docs on dumps.
>
> I even did dumps?? and found cPickle mentioned in the code.
> But it didn't tell me much, because...
>
> > In general you might want to read up on Python pickles, since it seems
> > perhaps you haven't already done so.  Check out:
> >http://docs.python.org/lib/module-pickle.html
> > to get started.
>
> ... i was missing such source of information.
>
> Thank you very much!

Make sure you look at some examples of how __reduce__ is used
by various sage extension classes (do search_src('__reduce__')).
It took me quite a long time and a lot of experimentation to get from
what the Python docs say to understanding how to actually use pickle
in the context of extension classes.  E.g., one key thing is that you
have to use cPickle.dumps with one of the *NON* default pickling
protocols or pickling Cython extensions won't work (at least, that
was true 2 years ago.)  Ask if you get really stuck.

William

--~--~-~--~~~---~--~~
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sage-support
URLs: http://sage.math.washington.edu/sage/ and http://sage.scipy.org/sage/
-~--~~~~--~~--~--~---



[sage-support] Re: What is needed in order to save objects of extension class?

2007-11-08 Thread Simon King

Dear William,

On Nov 8, 5:36 pm, "William Stein" <[EMAIL PROTECTED]> wrote:
> By an extension class I assume you mean a class you defined using Cython?

Yes.

> Type dumps? for the docs on dumps.

I even did dumps?? and found cPickle mentioned in the code.
But it didn't tell me much, because...

> In general you might want to read up on Python pickles, since it seems
> perhaps you haven't already done so.  Check out:
>http://docs.python.org/lib/module-pickle.html
> to get started.

... i was missing such source of information.

Thank you very much!

Cheers
 Simon


--~--~-~--~~~---~--~~
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sage-support
URLs: http://sage.math.washington.edu/sage/ and http://sage.scipy.org/sage/
-~--~~~~--~~--~--~---



[sage-support] Re: What is needed in order to save objects of extension class?

2007-11-08 Thread William Stein

On Nov 8, 2007 12:36 PM, Simon King <[EMAIL PROTECTED]> wrote:
> Dear support team,
>
> a basic question whose answer i was neither able to find in the Sage
> Programming Guide nor in 
> http://modular.math.washington.edu/sage/doc/html/tut/:
>
> Suppose i have an extension class Foo and an instance X of Foo.

By an extension class I assume you mean a class you defined using Cython?

> What
> methods must Foo provide in order to make things work like the
> following?
>  save(X,'bla')
>  Y=load('bla')

Either:

 (1) X must be pickle-able (a general Python notation), or

 (2) You must provide a save method that can be called like this:
  obj.save(filename=filename, compress=compress, **kwds)
  i.e., it takes a filename and a compression option.
  The result of calling obj.save must be a compressed or non-compressed
  pickle.

Regarding (1), for Cython extension classes, it is usually necessary
to define a __reduce__ method.  Type search_src('__reduce__') for
examples.

> The class Foo has a method "save" and a method "load", and the
> following works:

That will make it so it doesn't work, since (2) is violated.

>  X.save('bla')
>  Y=Foo('')  # initializes Y as instance of Foo
>  Y.load('bla')
>
> But apparently for supporting the simpler syntax "save(X,'bla')
> load('bla')" this is not enough.
>
> Also I found some rumor in the doc strings about a method "dumps", and
> that the result of "dumps" can be used by "loads" in order to produce
> an object.

Type dumps? for the docs on dumps.  It dumps an objects to a compressed
version of a Python pickle.  Also, loads recovers a compressed (or uncompressed)
Python pickle and makes an object out of it.


> But apparently "loads" is not the same as "eval": If i have
> a string s that makes "eval(s)" to return a copy of X, "loads(s)"
> fails.
> So what is Foo.dumps supposed to do?
>
> I'm sure that there is some written documentation on how to do those
> things. Simply i couldn't find them.

In general you might want to read up on Python pickles, since it seems
perhaps you haven't already done so.  Check out:
   http://docs.python.org/lib/module-pickle.html
to get started.   Basically, Python is designed from the ground up to
make it so nearly arbitrary objects can be turned into strings in a sensible
way from which the object can be recovered.  The strings are not human
readable.

William

--~--~-~--~~~---~--~~
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sage-support
URLs: http://sage.math.washington.edu/sage/ and http://sage.scipy.org/sage/
-~--~~~~--~~--~--~---