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


Groups > comp.lang.python > #104394

Re: Pickle __getstate__ __setstate__ and restoring n/w - beazley pg 172

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From dieter <dieter@handshake.de>
Newsgroups comp.lang.python
Subject Re: Pickle __getstate__ __setstate__ and restoring n/w - beazley pg 172
Date Wed, 09 Mar 2016 09:10:56 +0100
Lines 39
Message-ID <mailman.71.1457511067.15725.python-list@python.org> (permalink)
References <nbjt3b$69k$1@dont-email.me>
Mime-Version 1.0
Content-Type text/plain; charset=us-ascii
X-Trace news.uni-berlin.de A04NboD7teMDJkhaolLfrwOVsmXT5RmqRqlalBiExgHQ==
Cancel-Lock sha1:plrtq3zHvE/oMwru/Xr2X/eiavI=
Return-Path <python-python-list@m.gmane.org>
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; 'socket': 0.07; 'addr': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'self.sock': 0.09; 'def': 0.13; 'client()': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; "we'd": 0.21; 'import': 0.24; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'skip:_ 20': 0.26; 'pickle': 0.29; 'restart': 0.29; 'thus,': 0.29; 'objects': 0.29; 'subject:/': 0.30; 'skip:s 30': 0.31; 'skip:_ 10': 0.32; 'class': 0.33; 'skip:- 10': 0.34; 'gets': 0.35; '???': 0.35; 'something': 0.35; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'client': 0.37; 'received:org': 0.37; 'charset:us-ascii': 0.37; 'skip:s 40': 0.38; 'skip:p 20': 0.38; 'skip:x 10': 0.40; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'called': 0.40; 'internally.': 0.84
X-Injected-Via-Gmane http://gmane.org/
X-Gmane-NNTP-Posting-Host p57b39384.dip0.t-ipconnect.de
User-Agent Gnus/5.1008 (Gnus v5.10.8) XEmacs/21.4.22 (linux)
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.21
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:104394

Show key headers only | View raw


"Veek. M" <vek.m1234@gmail.com> writes:

> import socket
> class Client(object):
>  def __init__(self,addr):
>   self.server_addr = addr
>   self.sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
>   self.sock.connect(addr)
>
>  def __getstate__(self):
>   return self.server_addr
>  
>  def __setstate__(self,value):  
>   self.server_addr = value
>   self.sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
>   self.sock.connect(self.server_addr)
> -----------------
>
> We'd use it like so:
> x = Client(192.168.0.1)
> import pickle
> pickle.dump(x) #getstate gets called and returns IP for pickling.
>
> #program exits, we restart it
> x=Client(None)
> x = pickle.load(fh) #Is the IP passed to setstate??????
> x.__setstate__(self, unpickledIP) ???
> Is this correct?

Not completely.

"pickle" operates on objects - and calls "__getstate__" and "__setstate__"
internally. Thus, you get something like:

      client = Client()
      pickle.dump(client, open(fn, "wb"))
      ....
      recreated_client = pickle.load(open(fn, "rb"))

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


Thread

Pickle __getstate__ __setstate__ and restoring n/w - beazley pg 172 "Veek. M" <vek.m1234@gmail.com> - 2016-03-07 18:17 +0530
  Re: Pickle __getstate__ __setstate__ and restoring n/w - beazley pg 172 dieter <dieter@handshake.de> - 2016-03-09 09:10 +0100
    Re: Pickle __getstate__ __setstate__ and restoring n/w - beazley pg 172 "Veek. M" <vek.m1234@gmail.com> - 2016-03-09 14:44 +0530
      Re: Pickle __getstate__ __setstate__ and restoring n/w - beazley pg 172 Ian Kelly <ian.g.kelly@gmail.com> - 2016-03-09 14:28 -0700
        Re: Pickle __getstate__ __setstate__ and restoring n/w - beazley pg 172 "Veek. M" <vek.m1234@gmail.com> - 2016-03-10 06:31 +0530
      Re: Pickle __getstate__ __setstate__ and restoring n/w - beazley pg 172 dieter <dieter@handshake.de> - 2016-03-10 09:06 +0100

csiph-web