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


Groups > comp.lang.python > #35613

Re: pickle module doens't work

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <d@davea.name>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'string.': 0.04; 'importerror:': 0.05; 'works.': 0.07; 'python': 0.09; 'bytes,': 0.09; 'docs.': 0.09; 'encode': 0.09; 'function:': 0.09; 'modules.': 0.09; 'saved.': 0.09; 'subject:module': 0.09; 'useless': 0.09; '2.7': 0.13; '2.7:': 0.16; '3.3,': 0.16; 'dump': 0.16; 'importable': 0.16; 'similarly,': 0.16; 'stored.': 0.16; 'unicode.': 0.16; 'useless.': 0.16; 'wrote:': 0.17; 'fix': 0.17; 'instance': 0.17; 'pointed': 0.17; 'version.': 0.17; 'load': 0.19; 'module': 0.19; 'equivalent': 0.20; 'earlier': 0.21; 'regardless': 0.21; "skip:' 40": 0.22; 'class.': 0.23; 'installed': 0.23; 'machine': 0.24; 'header:In-Reply-To:1': 0.25; 'header:User- Agent:1': 0.26; 'am,': 0.27; 'bugs': 0.27; 'module.': 0.27; 'see,': 0.27; 'actual': 0.28; '>>>>': 0.29; 'pickle': 0.29; 'str': 0.29; 'definition': 0.29; 'objects': 0.29; 'probably': 0.29; 'class': 0.29; 'code': 0.31; 'url:python': 0.32; 'avoiding': 0.33; 'instances': 0.33; 'to:addr:python-list': 0.33; 'version': 0.34; 'done': 0.34; 'along': 0.35; 'same.': 0.35; 'add': 0.36; 'created': 0.36; 'url:org': 0.36; 'url:library': 0.36; "wasn't": 0.36; 'method': 0.36; 'skip:p 20': 0.36; 'enough': 0.36; 'does': 0.37; 'rather': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'url:docs': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'received:192.168': 0.40; 'save': 0.61; 'needing': 0.62; 'information': 0.63; 'charset:windows-1252': 0.65; 'header:Reply-To:1': 0.68; 'restore': 0.69; 'received:74.208': 0.71; 'reply-to:no real name:2**0': 0.72; 'gen': 0.84; 'otten': 0.84; 'pickled': 0.84; 'saving,': 0.91; 'str.': 0.91
Date Thu, 27 Dec 2012 07:34:11 -0500
From Dave Angel <d@davea.name>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121011 Thunderbird/16.0.1
MIME-Version 1.0
To python-list@python.org
Subject Re: pickle module doens't work
References <ee10f0f7-7713-4879-82a1-ec5804767af6@googlegroups.com> <mailman.1339.1356607732.29569.python-list@python.org> <f6ea95c2-2448-4f93-8aa4-e4e2aeb731ba@googlegroups.com>
In-Reply-To <f6ea95c2-2448-4f93-8aa4-e4e2aeb731ba@googlegroups.com>
Content-Type text/plain; charset=windows-1252
Content-Transfer-Encoding 8bit
X-Provags-ID V02:K0:uTmb+9QiCO+ZggtVkp8NOrkbIIo5gL7BqsvWJCLJmZs KmEL2h4dzcbJSHtMbFlQZ3MavvRnH1D1z4aL9zIbAq/6XH1dn6 owww+mnGnYDsanxl09Z9cyCzyS3wyArz+OPShpn6TSB4tRQwRV I+mO7hh5SBZawPLli8C9ixVQ5Vc/CDmrro2nLDoXVeNB6+K2YK FxWajWnAFFgjml73jK7mCNFcPJ/7OIzCNxlSbCyiKY0MFAjmo5 JcaCfeBYzI37p4ZZdTkXY9yFAkslF4TggYxIb8bwN2N6DChafr doPW2bccQysATcckR9FGUF5ciDieQrx9WtYXOwHfA0aSRTI9+/ OYKKWriJ+0Ata7wbpWAU=
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
Reply-To d@davea.name
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.1341.1356611938.29569.python-list@python.org> (permalink)
Lines 51
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1356611938 news.xs4all.nl 6935 [2001:888:2000:d::a6]:37903
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:35613

Show key headers only | View raw


On 12/27/2012 07:05 AM, Omer Korat wrote:
> You're probably right in general, for me the 3.3 and 2.7 pickles definitely don't work the same:
>
> 3.3:
>>>> type(pickle.dumps(1))
> <type 'bytes'>
>
> 2.7:
>>>> type(pickle.dumps(1, pickle.HIGHEST_PROTOCOL))
> <type 'str'>

That is the same. In 2.7, str is made up of bytes, while in 3.3, str
would be unicode. So 'bytes' is the 3.3 equivalent of str.

>
> As you can see, in 2.7 when I try to dump something, I get useless string. Look what I gen when I dump an NLTK object such as the sent_tokenize function:
>
> '\x80\x02cnltk.tokenize\nsent_tokenize\ng\x00'
>
> Now, this is useless. If I try to load it on a platform without NLTK installed on it, I get:
>
> ImportError: No module named 'nltk'
>
> So it means the actual sent_tokenizer wasn't saved. Just it's module.

As Peter Otten has already pointed out, that's how pickle works. It does
not somehow encode the whole module into the pickle, only enough
information to recreate the particular objects you're saving, *using*
the same modules. I don't know of any method of avoiding the destination
machine needing nltk, regardless of Python version.

Perhaps you'd rather see it in the Python docs.

http://docs.python.org/2/library/pickle.html
http://docs.python.org/3.3/library/pickle.html

pickle <http://docs.python.org/2/library/pickle.html#module-pickle>can
save and restore class instances transparently, however the class
definition must be importable and live in the same module as when the
object was stored.
and
Similarly, when class instances are pickled, their class’s code and data
are not pickled along with them. Only the instance data are pickled.
This is done on purpose, so you can fix bugs in a class or add methods
to the class and still load objects that were created with an earlier
version of the class.

-- 

DaveA

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


Thread

pickle module doens't work Omer Korat <animus.partum.universum@gmail.com> - 2012-12-27 02:46 -0800
  Re: pickle module doens't work Peter Otten <__peter__@web.de> - 2012-12-27 12:29 +0100
    Re: pickle module doens't work Omer Korat <animus.partum.universum@gmail.com> - 2012-12-27 04:05 -0800
      Re: pickle module doens't work Dave Angel <d@davea.name> - 2012-12-27 07:34 -0500
        Re: pickle module doens't work Omer Korat <animus.partum.universum@gmail.com> - 2012-12-27 05:16 -0800
          Re: pickle module doens't work Chris Angelico <rosuav@gmail.com> - 2012-12-28 00:20 +1100
        Re: pickle module doens't work Omer Korat <animus.partum.universum@gmail.com> - 2012-12-27 05:16 -0800
          Re: pickle module doens't work Tim Roberts <timr@probo.com> - 2012-12-28 21:41 -0800
            Re: pickle module doens't work Omer Korat <animus.partum.universum@gmail.com> - 2013-01-01 06:33 -0800
              Re: pickle module doens't work Tim Roberts <timr@probo.com> - 2013-01-01 11:14 -0800
                Re: pickle module doens't work Omer Korat <animus.partum.universum@gmail.com> - 2013-01-02 06:08 -0800
      Re: pickle module doens't work Terry Reedy <tjreedy@udel.edu> - 2012-12-27 16:19 -0500
    Re: pickle module doens't work Omer Korat <animus.partum.universum@gmail.com> - 2012-12-27 04:05 -0800

csiph-web