473,386 Members | 1,812 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,386 software developers and data experts.

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 anansispaceworks.com )
Anansi Spaceworks http://www.anansispaceworks.com

Jul 19 '05 #1
4 2315
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***********@Acm.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 anansispaceworks.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***********@Acm.Org


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

iD8DBQBCxbhYHJdudm4KnO0RAlf+AKCY5TN0hm2x3JJ+mD6I4y nAVqqIcQCguKbe
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 anansispaceworks.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
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
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 =...
0
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...
0
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...
3
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...
13
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
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
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...
5
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
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,...

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.