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/
-~--~~~~--~~--~--~---