Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > a couple of questions: pickling objects and strict types

Reply
Thread Tools

a couple of questions: pickling objects and strict types

 
 
Littlefield, Tyler
Guest
Posts: n/a
 
      04-05-2013
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.

 
Reply With Quote
 
 
 
 
John Gordon
Guest
Posts: n/a
 
      04-05-2013
In <(E-Mail Removed)> "Littlefield, Tyler" <(E-Mail Removed)> 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
http://www.velocityreviews.com/forums/(E-Mail Removed) B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      04-05-2013
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
 
Reply With Quote
 
Littlefield, Tyler
Guest
Posts: n/a
 
      04-06-2013
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.

 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      04-06-2013
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/pic...ickle-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
 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      04-06-2013
On Sat, Apr 6, 2013 at 1:37 PM, Steven D'Aprano
<(E-Mail Removed)> 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
 
Reply With Quote
 
Dave Angel
Guest
Posts: n/a
 
      04-06-2013
On 04/05/2013 10:49 PM, Chris Angelico wrote:
> On Sat, Apr 6, 2013 at 1:37 PM, Steven D'Aprano
> <(E-Mail Removed)> 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
 
Reply With Quote
 
mblume
Guest
Posts: n/a
 
      04-06-2013
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




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pickling the objects returned by array.array() John Machin Python 2 09-03-2005 08:17 PM
pickling extension types benevilent@optusnet.com.au Python 0 08-10-2004 11:34 AM
problem pickling objects created with the type function danny Python 2 04-23-2004 11:34 AM
Pickling Objects inherited from dict (Bug?) Thomas Guettler Python 1 11-05-2003 01:31 PM
Pickling/unpickling extensions types Bob Python 1 09-11-2003 06:13 AM



Advertisments