473,505 Members | 16,332 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Re: Related to Shelve Module

En Mon, 28 Apr 2008 02:08:31 -0300, tarun <ta**********@gmail.comescribió:
Hi All,
I want to store the class instance object variables persistenlty in one file
so that other file can also access for some filtering. I tired doing this
using the shelve module.

*Code:*
class A:
pass

import shelve
filename = 'test.db'
d = shelve.open(filename)

a = A()
print a
d['1'] = a

print d['1']
d.close()

*Output:*
<__main__.A instance at 0x018B56C0>
<__main__.A instance at 0x018B5760>

*Observation:*
I expect both the print statements to return the same value, The second
print statement just reads the dictonary, but still the adress (0x..) is
different from the first print statement. Can anyone tell me why. Also let
me know if you the way of getting same value both the times.
By default, each time you do d['1'] you get a *different* object. A shelve isn't very smart: it stores a string representation of your object (using pickle) and each time you ask for the object, it unpickles the stored string. When used as a persistence mechanism, it doesn't matter so much, the process that created the original instance may have died eons ago.
If object identity is important to you, you should remember to "fetch" the values only once in a single process, or use the writeback=True argument to the shelve constructor. But read the warning at <http://docs.python.org/lib/module-shelve.html>

pyd = shelve.open(filename, writeback=True)
pya = A()
pyd['1'] = a
pyx1 = d['1']
pyx2 = d['1']
pyx3 = d['1']
pyprint a, x1
<__main__.A instance at 0x00A3D5D0<__main__.A instance at 0x00A3D5D0>
pya is x1 is x2 is x3
True

--
Gabriel Genellina

Jun 27 '08 #1
0 842

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

Similar topics

6
4721
by: Rami A. Kishek | last post by:
Hi - this mysterious behavior with shelve is just about to kill me. I hope someone here can shed some light. First of all, I have this piece of code which uses shelve to save instances of some...
1
1427
by: Stuart Hungerford | last post by:
Hi all, I have a python module foo.py which contains: class A(object): ... class B(object): ... As well as a class that uses shelve (which in turn is using dumbdbm):
0
1106
by: Buster McFadden | last post by:
I've run into a problem on this platform (amd64) with code that works elsewhere, on x86, sparc, and ppc platforms. The shelve module is throwing an error raised from the anydbm call inside it: ...
1
3504
by: josegomez | last post by:
Hi! I want to use shelve to store lists which are indexed by an integer key. The keys are not concurrent or anything, they are just defined as integer values. However, shelve complains that I...
13
6467
by: 7stud | last post by:
test1.py: -------------------- import shelve s = shelve.open("/Users/me/2testing/dir1/aaa.txt") s = "red" s.close() --------output:------ $ python test1.py
5
1631
by: gluckj | last post by:
Hi, I'm not a Win ME fan myself (I'm a Mac user), but I'm here in Thailand developing software for special-needs kids, and the test PC in my home office is a Win ME machine (sigh). So when I...
1
4631
by: max.aginaga | last post by:
Hi everyone I've come across the following problem: on two different linux machines, both running python 2.5 (r25:51908), I have the same file 'd.dat'. The md5 checksums are the same. Now, on...
1
4546
by: Matthew Schibler | last post by:
I'm a newbie to Python, with some experience using perl (where I used nested arrays and hashes extensively). I am building a script in python for a MUD I play, and I want to use the shelve module...
1
2032
by: bluesmanu | last post by:
Hi all, I am trying to use the shelve module to save an object of a 'Electron' class I made into a file. The writing goes well but the reading goes : Traceback (most recent call last): File...
0
7307
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
7370
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7021
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7478
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...
1
5035
isladogs
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...
0
4701
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3188
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...
0
1532
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 ...
1
755
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.