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 3 1037
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
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>
Thanks Fredrik,
It helped a lot and this is really an amazing this I have discovered
today. :-)) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 not even relative with
implementation - I'm looking for a smart solution
My present system works like this:
|
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
|
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 instance
(in this case 'myDog'). Of course it would be better if I could
somehow know from within write() that the name of the object instance
was...
|
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 for each session, so that three
users can connect to three different databases. But I get crazy because:
-With interop all users share one...
|
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 experts.
I would like to produce Javascript classes that can be "subclassed"
with certain behaviors defined at subclass time. There are plenty of...
| |
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 telling me
that the script is "unresponsive", often more than once. I'm not sure
if it's simply execution time or memory usage.
My question is...
|
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 objects they are
associated with.
For example, lets say you are modelling studen ratings of teachers.
Let's say your applications needs to analyze...
|
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
|
|
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 top of the javascript */
/*###############################*/
window.onload=function() { setupLinks(); }
var red=0; /*base 16*/
var green=15;...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| | |