Hi Marco,

On 2012-05-19, mmarco <mma...@unizar.es> wrote:
> Working on an implementation of free groups (ticket #12339), i have
> found the following problem. I can loads-dumps free groups correctly,
> but when i try to do the sme with free group elements, i get the
> following error:

I think that is a known problem: Gap allows to store a session, but (as
much as I know) does *not* allow to store a single object. Hence, if you
want to save something then you need to do it by yourself (you can't
just ask Gap to do it).

> My class has an attribute which is a gap object, and i guess that the
> problem happens there. But the same is true for the free group class.
>
> And the _init_ of FreeGroup is:

`__init__` is not of great relevance here. Pickling in Python can happen
in different ways: By providing a `__newargs__`, `__getstate__` and
`__setstate__` methods, or by providing a `__reduce__` method (that
returns an unpickle function and some arguments, such that the unpickle
function applied to the arguments returns a copy of the object). See the
Python documentation on that topic.

If neither of these methods is provided, then Python would by default try
to reconstruct the object from its class and the pickled contents of
its `__dict__` attribute.

But if `__dict__` contains non-picklable stuff (such as, in your case, Gap
objects), it would fail.

> Which would be the right way to implement this?

See above. You should think of information that allows to uniquely
determine an element of a free group. It is, in the first place, the
parent (which is the free group that contains the object); as you say,
free groups can be pickled. And in the second place, the element can be
described by a list of pairs giving the number of a generator and its
exponent. Hence, in FreeGroup(['a','b','c']), the element a*c^-2*b^3
could be described by [(0,1),(2,-2),(1,3)].

And then, you could create a function 
  unpickle_free_group_element(F, L)
that returns an element of the free group F, determined by the list of
pairs L, and you could provide your elements with a __reduce__ method
returning
   unpickle_free_group_element, (F,L)

Of course, if F(L) would return the element, then your __reduce__ method
can simply return
   F, (L,)

The gap attribute of your object would then of course be reconstructed
from F and L.

Best regards,
Simon


-- 
To post to this group, send an email to sage-devel@googlegroups.com
To unsubscribe from this group, send an email to 
sage-devel+unsubscr...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sage-devel
URL: http://www.sagemath.org

Reply via email to