473,805 Members | 2,119 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

shelve in a ZipFile?

I only just recently had a look at the shelve module, and it
looks pretty handy, my only question being, what if I really
want two shelves? Must I use two files?

Also, it seems strange that shelve only works with
filenames. I would've expected there to at least be
a variant that would put a shelve database on a file
that is opened for read/write (that is a file object).

That would be handy if, for example, I wanted to couple
(and compress into the bargain) by putting my two
shelf files into a single zip archive.

Of course, I could do this with temporary files,
but I wonder if there's a simpler way?

--
Terry Hancock ( hancock at anansispacework s.com )
Anansi Spaceworks http://www.anansispaceworks.com

Jul 19 '05 #1
4 2341
Terry Hancock wrote:
I only just recently had a look at the shelve module....
That would be handy if, for example, I wanted to couple
(and compress into the bargain) by putting my two
shelf files into a single zip archive.


You are, however, fooling yourself if you think a shelve
solution can be made to gracefully interact with a zip-
compressed version. In order to zip the shelve data, it
must all be seen in a pass, so every update would necessarily
rewrite the entire shelve storage. much better to extract the
entire shelve file, operate on it, and re-compress it.

Even if uncompressed, the zip archive format is not going to
happily allow you to change the size of any of the "files" it
stores.

--Scott David Daniels
Sc***********@A cm.Org
Jul 19 '05 #2
On Friday 01 July 2005 12:53 pm, Scott David Daniels wrote:
Terry Hancock wrote:
I only just recently had a look at the shelve module....
That would be handy if, for example, I wanted to couple
(and compress into the bargain) by putting my two
shelf files into a single zip archive.


You are, however, fooling yourself if you think a shelve
solution can be made to gracefully interact with a zip-
compressed version. In order to zip the shelve data, it
must all be seen in a pass, so every update would necessarily
rewrite the entire shelve storage. much better to extract the
entire shelve file, operate on it, and re-compress it.


Yeah, I've already decided to just go with the two linked
shelf files.

The only drawback to this is that the user can accidentally
separate the two files, which causes one of them to become
useful (1st shelf relies on named references to objects in the
2nd shelf). I used to have problems like that with IRAF, which
stored image headers and images in two related files with
different file extensions. Once they're two separate units,
you have to consider the cases where they get separated.

OTOH, the 2nd shelf file has independent uses, which don't
depend on the 1st, so in that way, it's desireable for it to
be separable.

Probably if I was really worried about it, I'd come up with
a more serious solution, but this is a lightweight script, so
I think I'll just stick with shelve for convenience. Actually,
as small as the databases are in this project, I probably
could've just pickled a tuple of dictionaries (or something
similar). But I think shelves will probably do a better job.

Thanks though,
Terry

--
Terry Hancock ( hancock at anansispacework s.com )
Anansi Spaceworks http://www.anansispaceworks.com

Jul 19 '05 #3
Am Freitag, den 01.07.2005, 10:53 -0700 schrieb Scott David Daniels:
Terry Hancock wrote:
I only just recently had a look at the shelve module....
That would be handy if, for example, I wanted to couple
(and compress into the bargain) by putting my two
shelf files into a single zip archive.
You are, however, fooling yourself if you think a shelve
solution can be made to gracefully interact with a zip-
compressed version. In order to zip the shelve data, it
must all be seen in a pass, so every update would necessarily
rewrite the entire shelve storage. much better to extract the
entire shelve file, operate on it, and re-compress it.

Even if uncompressed, the zip archive format is not going to
happily allow you to change the size of any of the "files" it
stores.


It's even worse: shelve is basically a class that wraps a dictionary. It
provides a dictionary string -> pickable object based on a dictioary
string -> string. bsddb, gdbm etc. probably access files via lowlevel
calls that are not interceptable.

One way to achieve your goals would be to add compression and/or a key
prefix (which would allow multiple dictionaries or at least key spaces
in one file)

Andreas


--Scott David Daniels
Sc***********@A cm.Org


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQBCxbhYHJd udm4KnO0RAlf+AK CY5TN0hm2x3JJ+m D6I4ynAVqqIcQCg uKbe
0ceAcZcaHiER/7+kPTocIKU=
=YXfO
-----END PGP SIGNATURE-----

Jul 19 '05 #4
On Friday 01 July 2005 04:40 pm, Andreas Kostyrka wrote:
It's even worse: shelve is basically a class that wraps a dictionary. It
provides a dictionary string -> pickable object based on a dictioary
string -> string. bsddb, gdbm etc. probably access files via lowlevel
calls that are not interceptable.

One way to achieve your goals would be to add compression and/or a key
prefix (which would allow multiple dictionaries or at least key spaces
in one file)


Yeah, I'm already using a character prefix for header data in the
1st file. Right now, one of the headers tells where to find the 2nd file.

Seems to be working okay.

I'm a little bothered by the idea of lumping both into one dictionary,
though I see this could be done the same way.

I wasn't really looking for a way to compress the data (just thought it
was a nice side benefit), but your post reminded me that I could do
it with zlib on the data *before* storing them in the shelf. I guess if
bulk becomes an issue I'll try that.

--
Terry Hancock ( hancock at anansispacework s.com )
Anansi Spaceworks http://www.anansispaceworks.com

Jul 19 '05 #5

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

Similar topics

6
4765
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 class I define. It works perfectly on an old machine (PII-400) running Python 2.2.1 under RedHat Linux 8.0. When I try to run it under Python for windows ME on a P-4 1.4 GHz, however, it keeps crashing on reading from the shelved file the second...
1
6754
by: Kris Caselden | last post by:
Python's docs say that Shelve uses Pickle to serialize its data. However, I've noticed that Pickle can maintain internal links, while Shelve cannot. For instance: >>> d = shelve.open('shelvedata.txt',writeback=True) >>> d= >>> d=d >>> print d {'a': , 'b': } >>> d=2 >>> print d {'a': , 'b': }
0
2477
by: seth | last post by:
Last week I encountered an AttributeError in my unit tests that I wasn'table to catch with an "except AttributeError" statement. The problem stemmed from a class that raised an error inside __init__and defined a __del__ method to clean up resources. I then discovered asimilar problem in the shelve module. This led me to two importantdiscoveries: 1. Attributes defined in __init__ after an error is raised will not be apart of the...
0
2041
by: ex laguna | last post by:
Hi, I have ran into a problem with py2exe 0.5.0 and shelve in python 2.3.3. The script works fine standalone, but not with py2exe. Does anyone have a solution of workaround for this? Thanks much. # Begin of setup.py from distutils.core import setup import py2exe setup(console=)
3
7925
by: Michele Petrazzo | last post by:
Hi, I'm trying a script on a debian 3.1 that has problems on shelve library. The same script work well on a fedora 2 and I don't know why it create this problem on debian: #extract from my code import shelve class XX: def __init__(self): self._data = shelve.open("/tmp/myfile")
13
6522
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
1650
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 ported my Python program today to the PC, it quickly crashed. Everything seems to work except for shelve. Using the latest version of Python (2.5.1), when I do the following in IDLE on the Win ME machine here's the result: Python 2.5.1...
1
4593
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 to store persistent information between script executions. The following code does not work for me, import shelve, sys, os, string db = shelve.open(os.path.abspath(os.path.dirname(sys.argv)) + '/' + 'sandbox.dat', 'c') db = 'apple'
5
5171
by: Neil Crighton | last post by:
I'm using the zipfile library to read a zip file in Windows, and it seems to be adding too many newlines to extracted files. I've found that for extracted text-encoded files, removing all instances of '\r' in the extracted file seems to fix the problem, but I can't find an easy solution for binary files. The code I'm using is something like: from zipfile import Zipfile z = Zipfile(open('zippedfile.zip'))
0
9718
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, 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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9596
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10613
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, 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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10368
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10107
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7649
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6876
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5544
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4327
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 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.