The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

import-bot (20211) [Avatar] Offline
#1
[Originally posted by alankarp]

I am trying to pickle an object that takes a long time to compute. However, I
get

pickle.PicklingError: can't pickle 'instance method' object: <unbound method >
Attribute.defaultNext>

I tried to use "marshal" instead, but it told me

File "C:ECONOM~1NEGOTI~1softwareDEBUG_~1.PY", line 38, in ?
marshal.dump(node,pickleFile)
ValueError: unmarshallable object

The problem is that Attribute.defaultNext is a method passed to the
constructor. This same feature prevents me from doing a deepcopy of any
object containing an Attribute instance. In the case of copy, I fixed the
problem by doing a shallow copy in Attribute. Is there a similar trick I can
use with pickle?
import-bot (20211) [Avatar] Offline
#2
Re: Pickle problem
[Originally posted by d. harms]

> I am trying to pickle an object that takes a long time to compute. However, I
> get
>
> pickle.PicklingError: can't pickle 'instance method' object: <unbound>
method
> Attribute.defaultNext>
>
> I tried to use "marshal" instead, but it told me
>
> File "C:ECONOM~1NEGOTI~1softwareDEBUG_~1.PY", line 38, in ?
> marshal.dump(node,pickleFile)
> ValueError: unmarshallable object
>
> The problem is that Attribute.defaultNext is a method passed to the
> constructor. This same feature prevents me from doing a deepcopy of any
> object containing an Attribute instance. In the case of copy, I fixed the
> problem by doing a shallow copy in Attribute. Is there a similar trick I can
> use with pickle?

Hi Alan,

I don't have a clear enough grasp of your problem to be sure. But you might be
able to get it to work by defining __getstate__() and __setstate__() methods
(See "3.14.5.1 Pickling and unpickling normal class instances" in the Python
Library Reference Manual).

Daryl
import-bot (20211) [Avatar] Offline
#3
Re: Pickle problem
[Originally posted by alankarp]

> > I am trying to pickle an object that takes a long time to compute.
However, I
> > get
> >
> > pickle.PicklingError: can't pickle 'instance method' object: <unbound>
> method
> > Attribute.defaultNext>
> >
> > I tried to use "marshal" instead, but it told me
> >
> > File "C:ECONOM~1NEGOTI~1softwareDEBUG_~1.PY", line 38, in ?
> > marshal.dump(node,pickleFile)
> > ValueError: unmarshallable object
> >
> > The problem is that Attribute.defaultNext is a method passed to the
> > constructor. This same feature prevents me from doing a deepcopy of any
> > object containing an Attribute instance. In the case of copy, I fixed the
> > problem by doing a shallow copy in Attribute. Is there a similar trick I
can
> > use with pickle?
>
> Hi Alan,
>
> I don't have a clear enough grasp of your problem to be sure. But you might be
> able to get it to work by defining __getstate__() and __setstate__() methods
> (See "3.14.5.1 Pickling and unpickling normal class instances" in the Python
> Library Reference Manual).
>
> Daryl

I tried defining __getstate__(), but it didn't work for reasons beyond my ken.
I even tried replacing the function reference with a string, even though I
wouldn't be able to restore the object. For copying, I found a workaround. I
defined a copy() method in each class, with the leaf class using copy.copy().
I haven't found a comparable solution for pickle.

Here's the code I tried. The function reference in this early version of the
code is "matchingRule", instead of "defaultNext" as in my original question,
but the problem is the same.

# Function used by deepcopy - DOESN'T WORK BECAUSE OF MATCHINGRULE
#
def __getstate__(self):
return { 'name':self.name,
'valueType':self.valueType,
'multiplicity':self.multiplicity,
'mustMatch':self.mustMatch,
'required':self.required,
'designation':self.designation,
'value':self.value,
'matchingRule':'xxx'
}
@}