a couple of questions: pickling objects and strict types

Discussion in 'Python' started by Littlefield, Tyler, Apr 5, 2013.

  1. 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.
     
    Littlefield, Tyler, Apr 5, 2013
    #1
    1. Advertising

  2. Littlefield, Tyler

    John Gordon Guest

    In <> "Littlefield, Tyler" <> 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
    B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"
     
    John Gordon, Apr 5, 2013
    #2
    1. Advertising

  3. 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
     
    Steven D'Aprano, Apr 5, 2013
    #3
  4. 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.
     
    Littlefield, Tyler, Apr 6, 2013
    #4
  5. 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
     
    Steven D'Aprano, Apr 6, 2013
    #5
  6. On Sat, Apr 6, 2013 at 1:37 PM, Steven D'Aprano
    <> 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
     
    Chris Angelico, Apr 6, 2013
    #6
  7. Littlefield, Tyler

    Dave Angel Guest

    On 04/05/2013 10:49 PM, Chris Angelico wrote:
    > On Sat, Apr 6, 2013 at 1:37 PM, Steven D'Aprano
    > <> 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
     
    Dave Angel, Apr 6, 2013
    #7
  8. Littlefield, Tyler

    mblume Guest

    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
     
    mblume, Apr 6, 2013
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bob
    Replies:
    1
    Views:
    357
    Martin v. =?iso-8859-15?q?L=F6wis?=
    Sep 11, 2003
  2. Replies:
    0
    Views:
    339
  3. Ali Syed
    Replies:
    3
    Views:
    558
    Mark McIntyre
    Oct 13, 2004
  4. Sam Kong
    Replies:
    12
    Views:
    294
    John G Harris
    Oct 17, 2006
  5. Chris Angelico
    Replies:
    0
    Views:
    120
    Chris Angelico
    Apr 6, 2013
Loading...

Share This Page