object persistency, store instances relationship externally

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • King

    object persistency, store instances relationship externally

    This is a new test for object persistency. I am trying to store the
    relationship between instances externally.
    It's not working as expected. May be I am doing it in wrong way. Any
    suggestions?


    import shelve

    class attrib(object):
    pass

    class node(object):
    def __init__(self):
    self.a = attrib()
    self.b = attrib()
    self.c = attrib()
    self.d = attrib()

    a = node()
    #store pair relationship. This relationship is created at run time.
    lst = [[a.a, a.b], [a.c, a.d]]
    #Write objects into file
    shelf = shelve.open('sh elve_test_01.tx t', writeback=True)
    shelf['node'] = a
    shelf['lst'] = lst
    shelf.sync()
    shelf.close()


    #Read objects from file
    shelf = shelve.open('sh elve_test_01.tx t', 'r')
    a = shelf['node']
    lst = shelf['lst']
    print a.a, a.b, a.c, a.d
    #lst does not contains the relationship of object 'a''s attributes,
    instead it's creating new instances
    #of 'attrib' class
    print lst

  • Larry Bates

    #2
    Re: object persistency, store instances relationship externally

    King wrote:
    This is a new test for object persistency. I am trying to store the
    relationship between instances externally.
    It's not working as expected. May be I am doing it in wrong way. Any
    suggestions?
    >
    >
    import shelve
    >
    class attrib(object):
    pass
    >
    class node(object):
    def __init__(self):
    self.a = attrib()
    self.b = attrib()
    self.c = attrib()
    self.d = attrib()
    >
    a = node()
    #store pair relationship. This relationship is created at run time.
    lst = [[a.a, a.b], [a.c, a.d]]
    #Write objects into file
    shelf = shelve.open('sh elve_test_01.tx t', writeback=True)
    shelf['node'] = a
    shelf['lst'] = lst
    shelf.sync()
    shelf.close()
    >
    >
    #Read objects from file
    shelf = shelve.open('sh elve_test_01.tx t', 'r')
    a = shelf['node']
    lst = shelf['lst']
    print a.a, a.b, a.c, a.d
    #lst does not contains the relationship of object 'a''s attributes,
    instead it's creating new instances
    #of 'attrib' class
    print lst
    >
    You may want to take a look at Zope's ZODB.

    -Larry

    Comment

    • Fredrik Lundh

      #3
      Re: object persistency, store instances relationship externally

      King wrote:
      This is a new test for object persistency. I am trying to store the
      relationship between instances externally.
      It's not working as expected. May be I am doing it in wrong way. Any
      suggestions?
      The shelve module pickles each stored item individually. To preserve
      inter-object relations, try putting all related things in a tuple, and
      store that instead. E.g.

      shelf['data'] = (a, lst)

      a, lst = shelf['data']

      </F>

      Comment

      • King

        #4
        Re: object persistency, store instances relationship externally

        Thanks Fredrik,

        It helped a lot and this is really an amazing this I have discovered
        today. :-))

        Comment

        Working...