Re: "pickle" vs. f.write()

Discussion in 'Python' started by Terry Reedy, Jan 26, 2005.

  1. Terry Reedy

    Terry Reedy Guest

    For basic builtin objects, repr(ob) generally produces a string that when
    eval()ed will recreate the object. IE
    eval(repr(ob) == ob # sometimes
    For writing and reading class instances, pickle is the way to go.

    Terry J. Reedy
     
    Terry Reedy, Jan 26, 2005
    #1
    1. Advertising

  2. On 1/26/05 at 1:48 pm, Terry Reedy wrote:
    > For basic builtin objects, repr(ob) generally produces a string that when
    > eval()ed will recreate the object. IE
    > eval(repr(ob) == ob # sometimes


    I've found extending this property to your own classes often fairly easy
    to implement (and useful). For example:

    > class person:
    > def __init__(self, name="", age=0, friends=None, comment=""):
    > if friends is None:
    > friends = []
    > self.name, self.age, self.friends, self.comment = name, age, friends, comment
    >
    > def __repr__(self):
    > return ("person(" + repr(self.name) + ", " + repr(self.age) + ", " +
    > repr(self.friends) + ", " + repr(self.comment) + ")")
    >
    > me = person()
    >
    > print "me =", repr(me)


    Which produces the following output:
    me = person('', 0, [], '')


    In addition, the following constructs are possible:

    > family = [
    > person("Martin", 50, ["Matt"], "eldest son"),
    > person("Matt", 43, ["Martin"], "youngest son"),
    > person("Merry", 72, ["Martin", "Matt"], "mother"),
    > person("Luther", 82, ["Merry"], "father")
    > ]
    >
    > print "family =", repr(family)


    Which output the following:
    family = [person('Martin', 50, [], 'eldest son'), person('Matt', 43,
    ['Martin'], 'youngest son'), person('Merry', 72, ['Martin', 'Matt'],
    'mother'), person('Luther', 82, ['Merry'], 'father')]

    Basically this approach allows you to store your data in Python source
    files -- which you can then import or execfile. The files can also
    contain comments and are relatively easy to edit by hand. In addition
    they're portable.

    Best,
    Martin
     
    mmiller at tx3 dot com, Feb 1, 2005
    #2
    1. Advertising

  3. On 1/26/05 at 1:48 pm, Terry Reedy wrote:
    > For basic builtin objects, repr(ob) generally produces a string that when
    > eval()ed will recreate the object. IE
    > eval(repr(ob) == ob # sometimes


    I've found extending this property to your own classes often fairly easy
    to implement (and useful). For example:

    > class person:
    > def __init__(self, name="", age=0, friends=None, comment=""):
    > if friends is None:
    > friends = []
    > self.name, self.age, self.friends, self.comment = name, age, friends, comment
    >
    > def __repr__(self):
    > return ("person(" + repr(self.name) + ", " + repr(self.age) + ", " +
    > repr(self.friends) + ", " + repr(self.comment) + ")")
    >
    > me = person()
    >
    > print "me =", repr(me)


    Which produces the following output:
    me = person('', 0, [], '')


    In addition, the following constructs are possible:

    > family = [
    > person("Martin", 50, ["Matt"], "eldest son"),
    > person("Matt", 43, ["Martin"], "youngest son"),
    > person("Merry", 72, ["Martin", "Matt"], "mother"),
    > person("Luther", 82, ["Merry"], "father")
    > ]
    >
    > print "family =", repr(family)


    Which output the following:
    family = [person('Martin', 50, [], 'eldest son'), person('Matt', 43,
    ['Martin'], 'youngest son'), person('Merry', 72, ['Martin', 'Matt'],
    'mother'), person('Luther', 82, ['Merry'], 'father')]

    Basically this approach allows you to store your data in Python source
    files -- which you can then import or execfile. The files can also
    contain comments and are relatively easy to edit by hand. In addition
    they're portable.

    Best,
    Martin
     
    Martin Miller, Feb 1, 2005
    #3
  4. In <>, Martin Miller wrote:

    > I've found extending this property to your own classes often fairly easy
    > to implement (and useful). For example:
    >
    >> class person:
    >> def __init__(self, name="", age=0, friends=None, comment=""):
    >> if friends is None:
    >> friends = []
    >> self.name, self.age, self.friends, self.comment = name, age, friends, comment
    >>
    >> def __repr__(self):
    >> return ("person(" + repr(self.name) + ", " + repr(self.age) + ", " +
    >> repr(self.friends) + ", " + repr(self.comment) + ")")


    I write __repr__() methods similar but I think a bit more readable:

    def __repr__(self):
    return "%s(%r, %r, %r, %r)" % (self.__class__.__name__, self.name,
    self.age, self.friends, self.comment)

    And it's robust against changing the class name. It even works in
    subclasses if the signature of the __init__() method stays the same.

    Ciao,
    Marc 'BlackJack' Rintsch
     
    Marc 'BlackJack' Rintsch, Feb 2, 2005
    #4
  5. Marc 'BlackJack' Rintsch wrote:
    > ...
    >
    > I write __repr__() methods similar but I think a bit more readable:
    >
    > def __repr__(self):
    > return "%s(%r, %r, %r, %r)" % (self.__class__.__name__,

    self.name,
    > self.age, self.friends,

    self.comment)
    >
    > And it's robust against changing the class name. It even works in
    > subclasses if the signature of the __init__() method stays the same.


    Yes, that's an excellent suggestion and improvement.

    Saw the following in a post by Steven Bethard on another thread
    <http://groups-beta.google.com/group/comp.lang.python/msg/ed6b6ba0346e3c80>
    that I think would be even better (in the sense of being more
    general/generic) which also ought to work in subclasses. Namely:

    def __repr__(self):
    return '%s(%s)' % (self.__class__.__name__,
    ', '.join('%s=%r' % (k, v)
    for k, v
    in self.__dict__.items()))

    Martin
     
    Martin Miller, Feb 6, 2005
    #5
    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. Ben Finney

    Securing 'pickle'

    Ben Finney, Jul 11, 2003, in forum: Python
    Replies:
    17
    Views:
    590
    Paul Rubin
    Jul 11, 2003
  2. Aki Niimura

    freeze utility and pickle

    Aki Niimura, Aug 21, 2003, in forum: Python
    Replies:
    1
    Views:
    541
    =?ISO-8859-1?Q?Gerhard_H=E4ring?=
    Aug 21, 2003
  3. Johan Kohler

    "pickle" vs. f.write()

    Johan Kohler, Jan 26, 2005, in forum: Python
    Replies:
    2
    Views:
    331
    Johan Kohler
    Jan 26, 2005
  4. a pickle's pickle

    , Aug 2, 2005, in forum: Python
    Replies:
    4
    Views:
    393
  5. Michele Simionato
    Replies:
    2
    Views:
    1,913
    Michele Simionato
    May 23, 2008
Loading...

Share This Page