Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #18190

Re: pickling instances of metaclass generated classes

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!newsfeed.eweka.nl!feeder3.eweka.nl!81.171.88.15.MISMATCH!eweka.nl!lightspeed.eweka.nl!194.134.4.91.MISMATCH!news2.euro.net!amsnews11.chello.com!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'classes.': 0.05; 'definitions': 0.07; 'dynamically': 0.09; 'namespace': 0.09; 'am,': 0.12; 'anonymous': 0.15; 'dumps': 0.16; 'instances.': 0.16; 'metaclass': 0.16; 'metaclasses': 0.16; 'subject:classes': 0.16; 'unbound': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.18; '>>>': 0.18; 'instance': 0.18; 'this?': 0.19; 'cc:no real name:2**0': 0.20; 'cheers,': 0.20; 'trying': 0.21; '(most': 0.21; 'later': 0.21; 'maybe': 0.21; 'dec': 0.22; 'header:In-Reply-To:1': 0.22; 'module,': 0.23; 'cc:2**0': 0.24; 'traceback': 0.24; 'classes': 0.26; 'module': 0.26; 'import': 0.27; 'van': 0.28; 'message-id:@mail.gmail.com': 0.28; 'pass': 0.29; 'explicit': 0.29; 'cc:addr:python.org': 0.29; 'class': 0.29; 'anyone': 0.31; 'thu,': 0.32; "can't": 0.32; 'object': 0.33; 'it.': 0.34; 'someone': 0.34; 'received:209.85.212': 0.34; 'calling': 0.34; 'last):': 0.34; 'something': 0.35; 'skip:" 20': 0.35; 'file': 0.36; 'explain': 0.36; 'but': 0.37; 'hello,': 0.37; 'received:google.com': 0.37; 'could': 0.37; 'using': 0.38; 'received:209.85': 0.38; 'help': 0.39; "it's": 0.40; 'received:209': 0.40; '2011': 0.61; 'bear': 0.64; '29,': 0.73; 'why?': 0.77
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=U3anxP5oM+kB7YD6kzeVQdD9Sx7ZNqTE4jV9Kz2aBwE=; b=V7LnNdqcZ1Hra72dtSHj+mQewKRw9l1N69g31pB3LWFl1HE6eR2jjjDbsfOhQ3rfyI 8CJq/uvvA6KaHCQWF0qsEf+QERec8DFKLAdqYprTwgu4UFDviCZ1q6oekUfaHP7DYiPt W0MgykMPoTIRPTyFoq/Te2Q7OTsdbE4R5VF3U=
MIME-Version 1.0
In-Reply-To <2d221d0f-458e-4821-8786-f064b44b3125@p16g2000yqd.googlegroups.com>
References <2d221d0f-458e-4821-8786-f064b44b3125@p16g2000yqd.googlegroups.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Thu, 29 Dec 2011 12:55:04 -0700
Subject Re: pickling instances of metaclass generated classes
To lars van gemerden <lars@rational-it.com>
Content-Type text/plain; charset=ISO-8859-1
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.4232.1325188538.27778.python-list@python.org> (permalink)
Lines 58
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1325188538 news.xs4all.nl 6916 [2001:888:2000:d::a6]:33192
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:18190

Show key headers only | View raw


On Thu, Dec 29, 2011 at 2:55 AM, lars van gemerden <lars@rational-it.com> wrote:
> Hello,
>
> Can someone help me with the following:
>
> I am using metaclasses to make classes and these classes to make
> instances. Now I want to use multiprocessing, which needs to pickle
> these instances.
>
> Pickle cannot find the class definitions of the instances. I am trying
> to add a line to the __new__ of the metaclass to add the new class
> under the right name in the right module/place, so pickle can find
> it.
>
> Is this the right approach? Can anyone explain to me where/how to add
> these classes for pickle to find and maybe why?

It sounds like you're trying to do something like this?

>>> class MetaClass(type):
...     pass
...
>>> instance = MetaClass('<Anonymous>', (object,), {})()
>>> instance
<__main__.<Anonymous> object at 0x00CC00F0>
>>> import pickle
>>> pickle.dumps(instance)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "c:\python27\lib\pickle.py", line 1374, in dumps
    Pickler(file, protocol).dump(obj)
  File "c:\python27\lib\pickle.py", line 224, in dump
    self.save(obj)
  File "c:\python27\lib\pickle.py", line 331, in save
    self.save_reduce(obj=obj, *rv)
  File "c:\python27\lib\pickle.py", line 401, in save_reduce
    save(args)
  File "c:\python27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "c:\python27\lib\pickle.py", line 562, in save_tuple
    save(element)
  File "c:\python27\lib\pickle.py", line 295, in save
    self.save_global(obj)
  File "c:\python27\lib\pickle.py", line 748, in save_global
    (obj, module, name))
pickle.PicklingError: Can't pickle <class '__main__.<Anonymous>'>:
it's not found as __main__.<Anonymous>


Yeah, pickle's not going to work with anonymous classes.  As you
suggest, you could dynamically add the classes to the module namespace
so that pickle.dumps will find them, but bear in mind that they will
also have to exist when calling pickle.loads, so you will need to be
able to reconstruct the same anonymous classes before unpickling later
on.

Cheers,
Ian

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

pickling instances of metaclass generated classes lars van gemerden <lars@rational-it.com> - 2011-12-29 01:55 -0800
  Re: pickling instances of metaclass generated classes Robert Kern <robert.kern@gmail.com> - 2011-12-29 11:08 +0000
  Re: pickling instances of metaclass generated classes Ian Kelly <ian.g.kelly@gmail.com> - 2011-12-29 12:55 -0700
    Re: pickling instances of metaclass generated classes lars van gemerden <lars@rational-it.com> - 2011-12-30 01:50 -0800
      Re: pickling instances of metaclass generated classes Peter Otten <__peter__@web.de> - 2011-12-30 12:41 +0100
      Re: pickling instances of metaclass generated classes Peter Otten <__peter__@web.de> - 2012-01-01 13:51 +0100
    Re: pickling instances of metaclass generated classes lars van gemerden <lars@rational-it.com> - 2011-12-30 03:16 -0800
      Re: pickling instances of metaclass generated classes lars van gemerden <lars@rational-it.com> - 2011-12-30 07:56 -0800
        Re: pickling instances of metaclass generated classes lars van gemerden <lars@rational-it.com> - 2011-12-30 08:51 -0800
          Re: pickling instances of metaclass generated classes Ian Kelly <ian.g.kelly@gmail.com> - 2011-12-30 10:09 -0700
  Re: pickling instances of metaclass generated classes lars van gemerden <lars@rational-it.com> - 2012-01-03 03:43 -0800

csiph-web