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


Groups > comp.lang.python > #42846 > unrolled thread

a couple of questions: pickling objects and strict types

Started by"Littlefield, Tyler" <tyler@tysdomain.com>
First post2013-04-05 12:59 -0600
Last post2013-04-06 10:30 +0000
Articles 8 — 6 participants

Back to article view | Back to comp.lang.python


Contents

  a couple of questions: pickling objects and strict types "Littlefield, Tyler" <tyler@tysdomain.com> - 2013-04-05 12:59 -0600
    Re: a couple of questions: pickling objects and strict types John Gordon <gordon@panix.com> - 2013-04-05 19:27 +0000
    Re: a couple of questions: pickling objects and strict types Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-04-05 20:30 +0000
      Re: a couple of questions: pickling objects and strict types "Littlefield, Tyler" <tyler@tysdomain.com> - 2013-04-05 18:18 -0600
        Re: a couple of questions: pickling objects and strict types Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-04-06 02:37 +0000
          Re: a couple of questions: pickling objects and strict types Chris Angelico <rosuav@gmail.com> - 2013-04-06 13:49 +1100
          Re: a couple of questions: pickling objects and strict types Dave Angel <davea@davea.name> - 2013-04-05 23:22 -0400
          Re: a couple of questions: pickling objects and strict types mblume <foobar@invalid.invalid> - 2013-04-06 10:30 +0000

#42846 — a couple of questions: pickling objects and strict types

From"Littlefield, Tyler" <tyler@tysdomain.com>
Date2013-04-05 12:59 -0600
Subjecta couple of questions: pickling objects and strict types
Message-ID<mailman.159.1365188823.3114.python-list@python.org>
Hello all:
I've been using Python for a while now, but I have one larger problem.
I come from a c++ background; though it doesn't help in catching runtime 
errors, being able to compile a program helps catch a lot of syntax 
errors. I know about pychecker, which is somewhat useful. Do people have 
other methods for handling this?

Also, I'm depickling objects. Is there a way I can force pickle to call 
the object's ctor? I set up events per object, but when it just 
deserializes it doesn't set all that up.
Thanks,

-- 
Take care,
Ty
http://tds-solutions.net
The aspen project: a barebones light-weight mud engine:
http://code.google.com/p/aspenmud
He that will not reason is a bigot; he that cannot reason is a fool; he that dares not reason is a slave.

[toc] | [next] | [standalone]


#42847

FromJohn Gordon <gordon@panix.com>
Date2013-04-05 19:27 +0000
Message-ID<kjn8j2$6kr$1@reader1.panix.com>
In reply to#42846
In <mailman.159.1365188823.3114.python-list@python.org> "Littlefield, Tyler" <tyler@tysdomain.com> writes:

> Hello all:
> I've been using Python for a while now, but I have one larger problem.
> I come from a c++ background; though it doesn't help in catching runtime 
> errors, being able to compile a program helps catch a lot of syntax 
> errors. I know about pychecker, which is somewhat useful. Do people have 
> other methods for handling this?

The py_compile module can catch some obvious syntax errors, such as
incorrect indentation levels or a missing colon at the end of an if
statement.

But it won't catch other errors such as using a variable name before it's
defined.  For that, you can use an external program such as pylint or
pyflakes.

-- 
John Gordon                   A is for Amy, who fell down the stairs
gordon@panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"

[toc] | [prev] | [next] | [standalone]


#42849

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-04-05 20:30 +0000
Message-ID<515f345a$0$29995$c3e8da3$5496439d@news.astraweb.com>
In reply to#42846
On Fri, 05 Apr 2013 12:59:04 -0600, Littlefield, Tyler wrote:

> Hello all:
> I've been using Python for a while now, but I have one larger problem. I
> come from a c++ background; though it doesn't help in catching runtime
> errors, being able to compile a program helps catch a lot of syntax
> errors. I know about pychecker, which is somewhat useful. Do people have
> other methods for handling this?


Do you tend to make a lot of syntax errors?

Python also catches syntax errors at compile-time. I won't speak for 
others, but I hardly ever make syntax errors: between Python's simple, 
surprise-free syntax, and modern, syntax-colouring editors, I find that I 
rarely make syntax errors.


> Also, I'm depickling objects. Is there a way I can force pickle to call
> the object's ctor? I set up events per object, but when it just
> deserializes it doesn't set all that up. Thanks,

What's the object's ctor? What sort of objects are you dealing with?



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#42866

From"Littlefield, Tyler" <tyler@tysdomain.com>
Date2013-04-05 18:18 -0600
Message-ID<mailman.170.1365207530.3114.python-list@python.org>
In reply to#42849
On 4/5/2013 2:30 PM, Steven D'Aprano wrote:
> On Fri, 05 Apr 2013 12:59:04 -0600, Littlefield, Tyler wrote:
>
>> Hello all:
>> I've been using Python for a while now, but I have one larger problem. I
>> come from a c++ background; though it doesn't help in catching runtime
>> errors, being able to compile a program helps catch a lot of syntax
>> errors. I know about pychecker, which is somewhat useful. Do people have
>> other methods for handling this?
>
> Do you tend to make a lot of syntax errors?
Not a -lot-, but there are things I don't catch sometimes.

> Python also catches syntax errors at compile-time. I won't speak for
> others, but I hardly ever make syntax errors: between Python's simple,
> surprise-free syntax, and modern, syntax-colouring editors, I find that I
> rarely make syntax errors.

I am blind, so colorful editors don't really work all that well for me.

>> Also, I'm depickling objects. Is there a way I can force pickle to call
>> the object's ctor? I set up events per object, but when it just
>> deserializes it doesn't set all that up. Thanks,
> What's the object's ctor? What sort of objects are you dealing with?
>
>
>
     def __init__(self):
         self.events = {}
         self.components = []
         self.contents = []
         self.uid = uuid4().int
         self.events['OnLook'] = teventlet()


Basically events don't get initialized like I'd like after I depickle 
objects.

-- 
Take care,
Ty
http://tds-solutions.net
The aspen project: a barebones light-weight mud engine:
http://code.google.com/p/aspenmud
He that will not reason is a bigot; he that cannot reason is a fool; he that dares not reason is a slave.

[toc] | [prev] | [next] | [standalone]


#42878

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-04-06 02:37 +0000
Message-ID<515f8a6b$0$29995$c3e8da3$5496439d@news.astraweb.com>
In reply to#42866
On Fri, 05 Apr 2013 18:18:51 -0600, Littlefield, Tyler wrote:

> On 4/5/2013 2:30 PM, Steven D'Aprano wrote:
>> On Fri, 05 Apr 2013 12:59:04 -0600, Littlefield, Tyler wrote:
>>
>>> Hello all:
>>> I've been using Python for a while now, but I have one larger problem.
>>> I come from a c++ background; though it doesn't help in catching
>>> runtime errors, being able to compile a program helps catch a lot of
>>> syntax errors. I know about pychecker, which is somewhat useful. Do
>>> people have other methods for handling this?
>>
>> Do you tend to make a lot of syntax errors?
>
> Not a -lot-, but there are things I don't catch sometimes.

As we all do. But fortunately the Python compiler catches syntax errors.


>> Python also catches syntax errors at compile-time. I won't speak for
>> others, but I hardly ever make syntax errors: between Python's simple,
>> surprise-free syntax, and modern, syntax-colouring editors, I find that
>> I rarely make syntax errors.
> 
> I am blind, so colorful editors don't really work all that well for me.

Fair point.


>>> Also, I'm depickling objects. Is there a way I can force pickle to
>>> call the object's ctor? I set up events per object, but when it just
>>> deserializes it doesn't set all that up. Thanks,
>> What's the object's ctor? What sort of objects are you dealing with?
>>
>>
>>
>      def __init__(self):
>          self.events = {}
>          self.components = []
>          self.contents = []
>          self.uid = uuid4().int
>          self.events['OnLook'] = teventlet()
> 
> 
> Basically events don't get initialized like I'd like after I depickle
> objects.

Did you mean "constructor" rather than C T O R ? Perhaps your voice-to-
text software (if you are using such) misheard you.

Correct, by default pickle does not call the __init__ method, it just 
reconstructs the instance. Basically, it takes a snapshot of the 
instance's internal state (the __dict__) and reconstructs from the 
snapshot.

This is explained in the documentation here:

http://docs.python.org/2/library/pickle.html#the-pickle-protocol


You can force the __init__ method to be called in a couple of different 
ways. Perhaps this is the most straight-forward. Add a __setstate__ 
method to your class:


    def __setstate__(self, state):
        self.__dict__.update(state)
        self.events['OnLook'] = teventlet()



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#42879

FromChris Angelico <rosuav@gmail.com>
Date2013-04-06 13:49 +1100
Message-ID<mailman.179.1365216599.3114.python-list@python.org>
In reply to#42878
On Sat, Apr 6, 2013 at 1:37 PM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
> Did you mean "constructor" rather than C T O R ? Perhaps your voice-to-
> text software (if you are using such) misheard you.

Side point: "ctor" is a common abbreviation for "constructor".

ChrisA

[toc] | [prev] | [next] | [standalone]


#42882

FromDave Angel <davea@davea.name>
Date2013-04-05 23:22 -0400
Message-ID<mailman.182.1365218863.3114.python-list@python.org>
In reply to#42878
On 04/05/2013 10:49 PM, Chris Angelico wrote:
> On Sat, Apr 6, 2013 at 1:37 PM, Steven D'Aprano
> <steve+comp.lang.python@pearwood.info> wrote:
>> Did you mean "constructor" rather than C T O R ? Perhaps your voice-to-
>> text software (if you are using such) misheard you.
>
> Side point: "ctor" is a common abbreviation for "constructor".
>
> ChrisA
>

But neither term applies to the __init__() method, which is an 
initializer.  The constructor is __new__()


-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#42907

Frommblume <foobar@invalid.invalid>
Date2013-04-06 10:30 +0000
Message-ID<kjotg5$ml6$1@news.albasani.net>
In reply to#42878
Am Sat, 06 Apr 2013 02:37:31 +0000 schrieb Steven D'Aprano:
>>> [...]
>>      def __init__(self):
>>          self.events = {}
>>          self.components = []
>>          self.contents = []
>>          self.uid = uuid4().int
>>          self.events['OnLook'] = teventlet()
>> 
>> 
>> Basically events don't get initialized like I'd like after I depickle
>> objects.
> 
> 
> Correct, by default pickle does not call the __init__ method, it just
> reconstructs the instance. Basically, it takes a snapshot of the
> instance's internal state (the __dict__) and reconstructs from the
> snapshot.
> 
> [...]
>
To the OP: Did you really mean
         self.events['OnLook'] = teventlet()
as opposed to:
         self.events['OnLook'] = teventlet

The first one executes teventlet and then assigns the result of the function to 
self.events['OnLook']. The second one assigns the function teventlet to the dict
entry (presumably so that it will be called when the objct detects the 'OnLook'
event). Unless the teventlet() function returns itself a function, an 'OnLook' 
event won't do anything useful during the remaining life time of the object, I think.

Regards
Martin



[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web