Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #104212 > unrolled thread
| Started by | "Veek. M" <vek.m1234@gmail.com> |
|---|---|
| First post | 2016-03-07 18:17 +0530 |
| Last post | 2016-03-10 09:06 +0100 |
| Articles | 6 — 3 participants |
Back to article view | Back to comp.lang.python
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
| From | "Veek. M" <vek.m1234@gmail.com> |
|---|---|
| Date | 2016-03-07 18:17 +0530 |
| Subject | Pickle __getstate__ __setstate__ and restoring n/w - beazley pg 172 |
| Message-ID | <nbjt3b$69k$1@dont-email.me> |
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?
[toc] | [next] | [standalone]
| From | dieter <dieter@handshake.de> |
|---|---|
| Date | 2016-03-09 09:10 +0100 |
| Message-ID | <mailman.71.1457511067.15725.python-list@python.org> |
| In reply to | #104212 |
"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"))
[toc] | [prev] | [next] | [standalone]
| From | "Veek. M" <vek.m1234@gmail.com> |
|---|---|
| Date | 2016-03-09 14:44 +0530 |
| Message-ID | <nbopc0$3eh$1@dont-email.me> |
| In reply to | #104394 |
dieter wrote:
> "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"))
hmm.. yeah, as in the fh - file handle will be passed for
pickling/unpickling - that's not important..
what i wanted to know was, x = Client('192.168.0.1') will create an
object 'x' with the IP inside it. When I do:
pickle.dump(x)
pickle doesn't know where in the object the IP is, so he'll call
__getstate__ and expect the return value to be the IP, right?
Similarly, whilst unpickling, __setstate__ will be called in a manner
similar to this:
x.__setstate__(self, unpickledIP)
__setstate__ can then populate 'x' by doing
self.x = str(unpickledIP)
the type info is not lost during pickling is it, therefore 'str' is not
required is it?
[toc] | [prev] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2016-03-09 14:28 -0700 |
| Message-ID | <mailman.97.1457558969.15725.python-list@python.org> |
| In reply to | #104398 |
On Wed, Mar 9, 2016 at 2:14 AM, Veek. M <vek.m1234@gmail.com> wrote:
> what i wanted to know was, x = Client('192.168.0.1') will create an
> object 'x' with the IP inside it. When I do:
> pickle.dump(x)
> pickle doesn't know where in the object the IP is, so he'll call
> __getstate__ and expect the return value to be the IP, right?
pickle doesn't care what the return value is, other than it needs to
be something picklable. I suggest returning a dict containing the IP
address, to make it easier to add additional state later if needed.
That said, I wouldn't pickle a TCP client in the first place. You may
be able to restore the client state, but you can't easily restore any
server state, which could easily lead to mismatching network states.
Also, what do you think should happen if the connection fails while
unpickling?
> Similarly, whilst unpickling, __setstate__ will be called in a manner
> similar to this:
> x.__setstate__(self, unpickledIP)
>
> __setstate__ can then populate 'x' by doing
> self.x = str(unpickledIP)
>
> the type info is not lost during pickling is it, therefore 'str' is not
> required is it?
Correct.
[toc] | [prev] | [next] | [standalone]
| From | "Veek. M" <vek.m1234@gmail.com> |
|---|---|
| Date | 2016-03-10 06:31 +0530 |
| Message-ID | <nbqgr0$n0b$1@dont-email.me> |
| In reply to | #104454 |
Ian Kelly wrote:
> On Wed, Mar 9, 2016 at 2:14 AM, Veek. M <vek.m1234@gmail.com> wrote:
>> what i wanted to know was, x = Client('192.168.0.1') will create an
>> object 'x' with the IP inside it. When I do:
>> pickle.dump(x)
>> pickle doesn't know where in the object the IP is, so he'll call
>> __getstate__ and expect the return value to be the IP, right?
>
> pickle doesn't care what the return value is, other than it needs to
> be something picklable. I suggest returning a dict containing the IP
> address, to make it easier to add additional state later if needed.
>
> That said, I wouldn't pickle a TCP client in the first place. You may
> be able to restore the client state, but you can't easily restore any
> server state, which could easily lead to mismatching network states.
> Also, what do you think should happen if the connection fails while
> unpickling?
>
Actually that's just what I was asking: when __getstate__ is called,
it's upto getstate to return the object to be pickled - it could be an
IP passed in by the user or pretty much anything 'hello world' even.
Anyway, i think the point of that example (from the book) is that he's
not saving TCP state info. Instead he provides a seamless iface so the
user won't even realize the connection had failed (meaning he'll reopen
the link using the pickled IP and pretend the link never went down).
[toc] | [prev] | [next] | [standalone]
| From | dieter <dieter@handshake.de> |
|---|---|
| Date | 2016-03-10 09:06 +0100 |
| Message-ID | <mailman.112.1457597177.15725.python-list@python.org> |
| In reply to | #104398 |
"Veek. M" <vek.m1234@gmail.com> writes:
> ...
> what i wanted to know was, x = Client('192.168.0.1') will create an
> object 'x' with the IP inside it. When I do:
> pickle.dump(x)
> pickle doesn't know where in the object the IP is, so he'll call
> __getstate__ and expect the return value to be the IP, right?
It does not expect anything. It pickles, whatever "__getstate__"
returns. The unpickling will fail (either
explicitely or implicitely), when "__setstate__" does not "fit"
with "__getstate__" or does not restore the state in the way
you expect.
> Similarly, whilst unpickling, __setstate__ will be called in a manner
> similar to this:
> x.__setstate__(self, unpickledIP)
Yes.
> __setstate__ can then populate 'x' by doing
> self.x = str(unpickledIP)
>
> the type info is not lost during pickling is it, therefore 'str' is not
> required is it?
Yes.
Note however, that "__setstate__" is called automatically
during "unpickling". No need that you do this yourself.
You can also easily try out things yourself (e.g. in
an interactive Python session). The builtin "vars" allows you
to look into an object. As an example, you could do:
client = Client(...)
vars(client)
pickle.dump(client, open(fn, "wb"))
....
recreated_client = pickle.load(open(fn, "rb"))
vars(recreated_client)
and then compare the two "vars" results.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web