473,396 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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('shelve_test_01.txt', writeback=True)
shelf['node'] = a
shelf['lst'] = lst
shelf.sync()
shelf.close()
#Read objects from file
shelf = shelve.open('shelve_test_01.txt', '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

Jul 25 '08 #1
3 1026
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('shelve_test_01.txt', writeback=True)
shelf['node'] = a
shelf['lst'] = lst
shelf.sync()
shelf.close()
#Read objects from file
shelf = shelve.open('shelve_test_01.txt', '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
Jul 25 '08 #2
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>

Jul 25 '08 #3
Thanks Fredrik,

It helped a lot and this is really an amazing this I have discovered
today. :-))
Jul 25 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: R | last post by:
Hello everybody. I'm writing my own Content System in PHP5. I've written so far main classes for handling DB connections, XML, XForms and Sessions. But I've got problem with one thing - it's...
4
by: dagao | last post by:
I have an object and want to store it in a file. Then later I can read in the object and obtain all the info in it. How can I do this ? Thanks a lot. DG
6
by: Martin | last post by:
I'd like to be able to get the name of an object instance from within a call to a method of that same object. Is this at all possible? The example below works by passing in the name of the object...
2
by: Markus Prediger | last post by:
Hi NG, I have an asp.net project that uses an vb6 com object for some database-manipulation (I cannot rewrite it in .net, sorry, its not my decision). I want it to be instanciated seperately...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
8
by: Gawain Lavers | last post by:
I have a script which uses a lot of object augmentation (in order to extend the functionality of DOM elements), and I'm clearly stressing out my browser (I rapidly get to a point where Firefox is...
2
by: Veloz | last post by:
Hiya My question is whether or not you should associated related objects in your software using a scheme of id's and lookups, or wether objects should actually hold actual object references to...
7
by: joproulx | last post by:
Hi, I was wondering if there was a way with Reflection to find dynamically if an object was referencing indirectly another object. A simple example would be: Object1 | --Object2 |
2
iam_clint
by: iam_clint | last post by:
/*###############################*/ /* Link fade script by iam_clint, bytes.com */ /* www.bytes.com, If you use this script on your site */ /* Please keep this comment at the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.