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


Groups > comp.lang.python > #52791

RE: Unpickling data with classes from dynamic modules

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ramit.prasad@jpmorgan.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; 'completeness': 0.07; 'dynamically': 0.07; 'modify': 0.07; 'appropriate.': 0.09; 'currently,': 0.09; 'received:155': 0.09; 'subject:modules': 0.09; '(which,': 0.16; 'all?': 0.16; 'although,': 0.16; 'classes)': 0.16; 'detected': 0.16; 'disclaimers': 0.16; 'disclaimers,': 0.16; 'from:addr:jpmorgan.com': 0.16; 'obviously,': 0.16; 'overridden': 0.16; 'overriding': 0.16; 'received:155.180': 0.16; 'received:155.180.234': 0.16; 'received:155.180.234.122': 0.16; 'received:159': 0.16; 'received:159.53': 0.16; 'received:bankone.net': 0.16; 'received:exchad.jpmchase.net': 0.16; 'received:jpmchase.com': 0.16; 'received:jpmchase.net': 0.16; 'received:se3.svr.bankone.net': 0.16; 'received:svr.bankone.net': 0.16; 'securities,': 0.16; 'subclasses': 0.16; 'subclassing': 0.16; 'url:disclosures': 0.16; 'url:jpmorgan': 0.16; 'fix': 0.17; 'wrote:': 0.18; 'module': 0.19; 'trying': 0.19; 'normally': 0.19; 'to:name:python- list@python.org': 0.22; 'creating': 0.23; 'load': 0.23; "aren't": 0.24; 'module,': 0.24; 'extension': 0.26; 'header:In-Reply-To:1': 0.27; 'to:2**1': 0.27; 'along': 0.30; 'code': 0.31; 'pickle': 0.31; 'class': 0.32; 'received:169.254': 0.32; 'interface': 0.32; 'url:python': 0.33; 'skip:_ 10': 0.34; 'maybe': 0.34; 'subject:from': 0.34; 'could': 0.34; 'problem': 0.35; 'subject:with': 0.35; "can't": 0.35; 'classes': 0.35; 'skip:u 20': 0.35; 'but': 0.35; 'add': 0.35; 'there': 0.35; 'accuracy': 0.36; 'executing': 0.36; 'subject:data': 0.36; 'charset:us-ascii': 0.36; 'url:org': 0.36; 'received:169': 0.37; 'expected': 0.38; 'url:library': 0.38; 'to:addr:python-list': 0.38; 'list,': 0.38; 'rather': 0.38; 'that,': 0.38; 'to:addr:python.org': 0.39; 'either': 0.39; 'even': 0.60; 'skip:u 10': 0.60; 'course': 0.61; 'information,': 0.61; 'such': 0.63; 'more': 0.64; 'purchase': 0.65; 'dear': 0.65; '(that': 0.65; 'within': 0.65; 'believe': 0.68; 'subject': 0.69; 'containing': 0.69; 'wish': 0.70; 'legal': 0.71; 'sale': 0.75; 'behavior': 0.77; '(loading': 0.84; 'compiling': 0.84; 'itself?': 0.84; 'received:169.254.8': 0.84; 'url:html#object': 0.84
X-DKIM OpenDKIM Filter v2.1.3 sf1.jpmchase.com r7LJxoWC023121
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=jpmorgan.com; s=smtpout; t=1377115190; bh=kPXY7+d4dfLFfRoG4uDiIYjyXtKNm6JTDVtRKo3xB7w=; h=From:To:Subject:Date:Message-ID:References:In-Reply-To: Content-Type:content-transfer-encoding:MIME-Version; b=aTnS3D0Sw32c0CSEdliwQ0tEruc1+0+1nCq+Wsjq9L501/I3EM+FC9YWIJwg16dpU QdZqb4zFgvOVhknshPZSXNKsq9OcxeHlnZhioApqNGuaiYQd65z2hB3Oygpdjeeq+J zQwCsyv0ffDUJ7DXwjBlUdk7IuuDre1xn2JL+cSI=
X-AuditID a959a26a-b7fc68e0000023ec-8b-52151c35e9ef
From "Prasad, Ramit" <ramit.prasad@jpmorgan.com.dmarc.invalid>
To Fredrik Tolf <fredrik@dolda2000.com>, "python-list@python.org" <python-list@python.org>
Subject RE: Unpickling data with classes from dynamic modules
Thread-Topic Unpickling data with classes from dynamic modules
Thread-Index AQHOnoom7jIuiQd9r0a5cksFgOK/iZmgEmDA
Date Wed, 21 Aug 2013 19:59:47 +0000
References <alpine.DEB.2.10.1308211732440.18360@shack.dolda2000.com>
In-Reply-To <alpine.DEB.2.10.1308211732440.18360@shack.dolda2000.com>
Accept-Language en-US
Content-Language en-US
X-MS-Has-Attach
X-MS-TNEF-Correlator
x-originating-ip [10.67.79.47]
Content-Type text/plain; charset="us-ascii"
content-transfer-encoding quoted-printable
MIME-Version 1.0
X-DLP-FWD Yes
X-Brightmail-Tracker H4sIAAAAAAAAA+NgFvrFKsWRmVeSWpSXmKPExsUye4uJsK6pjGiQwb19VhY3v51itDjSeo3V gcnj3KOprB4v5r9hC2CKamC0SczLyy9JLElVSEktTrZVcsksTs5JzMxNLVLQVfAqyM0vSk/M 00vOz1VSyEyxVTJRUijISUxOzU3NK7FVSiwoSM1LUbLjUsAANkBlmXkKqXnJ+SmZeem2Sp7B /roWFqaWuoZKdiEZmcUKqbmJmTkKCRvZM7ZvWMFc8IWv4kfrL5YGxk6eLkZODgkBE4nJW8+y Q9hiEhfurWfrYuTiEBI4wijxcvYpFgjnIKNEx90bzCBVQgKbGCVudNeD2GwChhLHzz4B6xYR iJVY+Og/E4gtLGAnsfFgLyNE3F6ieQ3IVBDbSOLVtxUsIDaLgKrE3DcXWEFsXoEQiePNv6Hm u0osuvsGrJdTwE3iz6mbYPMZga77fmoN2HxmAXGJW0/mM0FcLSCxZM95ZghbVOLl43+sELa8 xIspL9kg6nUkFuz+BGVrSyxb+JoZYq+gxMmZT4Du4QDaqyaxb70tiCkhwCexva16AqPELCTL ZiEZNAvJoFlIBi1gZFnFKFmcm55mqldcVqRXWqyXVZCbnJFYnKqXl1qyiRGYZFZGLsrawXhu ke0hRgEORiUe3oZ9IkFCrIllxZW5hxglOJiVRHjnSRwKFOJNSaysSi3Kjy8qzUktPsRYBQyz icxSosn5wASYVxJvaGxmYWBqamhgYWhkQhVhJXHe/bncQUIC6cCUmp2aWpBaBLOciYNTqoGx LfZv6pYzkw+3zJZ/tO9dWv/u7AITdWazkLUmC2vfhYRt3z+hdbmeyHV7oYAfksUCs6dor9LU ecXa5Wu2Zq5N1aI61aYdke0XKl2vbbutrMWcPidnQ+zBi90v7oVX3rzBc+mpjMKlYzv+BodK tzFbXo9f99P5ofdqyZlKL01zXNYps7t9O/tfiaU4I9FQi7moOBEAFeE/GY0DAAA=
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
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.101.1377116305.19984.python-list@python.org> (permalink)
Lines 58
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1377116305 news.xs4all.nl 15877 [2001:888:2000:d::a6]:34537
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:52791

Show key headers only | View raw


Fredrik Tolf wrote:
> 
> Dear list,
> 
> I have a system in which I load modules dynamically every now and then
> (that is, creating a module with types.ModuleType, compiling the code for
> the module and then executing it in the module with exec()), and where I
> would wish to be able to have classes in those modules containing classes
> that could pickled, and then unpickled again.
> 
> The problem with that, currently, is of course two-fold: The dynamically
> loaded module may not be loaded at the time when the unpickling takes
> place, but even if it were, it isn't registered in sys.modules, so the
> unpickler can't find it either way. And, of course, that is detected
> already when pickling.
> 
> Is there any way to fix this, at all?
> 
> I considered trying to create subclasses of the pickler and unpickler that
> pickle a reference to a module loader and data for the particular module
> along with a class that comes from such a module, by overriding
> Pickler.save_global and Unpickler.load_global. Unfortunately, however,
> those functions aren't part of the public interface and can't be
> overridden when the C-pickle module is used instead (which, obviously, is
> what normally happens).
> 
> Is there any way around this without having to modify the pickle module
> itself?
> 
> --
> 
> Fredrik Tolf
> --

I believe rather than subclassing the pickler, you are expected to 
change the behavior from within the class via __getstate__ and __setstate__.

http://docs.python.org/2/library/pickle.html#object.__getstate__

Although, for your use case (loading unknown classes) the section on pickle and 
extension types may be more appropriate.

http://docs.python.org/2/library/pickle.html#pickling-and-unpickling-extension-types

Maybe all you need to add is implementation for obj.__reduce__


~Ramit



This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email.  

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


Thread

RE: Unpickling data with classes from dynamic modules "Prasad, Ramit" <ramit.prasad@jpmorgan.com.dmarc.invalid> - 2013-08-21 19:59 +0000

csiph-web