Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using python shelve cross-platform

I am hoping for a little advice on shelves/databases in Python.

Problem: I have a database created on the mac, that I want to use on windows 7. I use Python 3.2, MacOS 10.7, and win 7.

When I open and save my shelve on the mac all is good and well. I get a file with a ".db" extension. On my windows-python it is not recognized. I can however create a new db on the pc and get files with ".bak, dat, .dir" extensions.

I am guessing that the python on the pc does not have the same underlying database that my mac-python uses?

I am not sure which is the correct approach here, but maybe I could:

Change the default-db that my systems uses? Find out which db my mac-python uses and add that on the pc? Change the way I store my data all together?

Speed is not an issue, the datasize is a few megabytes, and it's not accessed very often.

Hope to find a helping hand out there. Thanks in advance - any help is much appreciated.

/Esben

What I am doing:

Import shelve
db = shelve.open('mydb')
entries = db['list']
db.close

It's pretty straight forward, I have a working db-file called "mydb.db" on the mac but when I try to open it on the pc-python i get:

Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/dbm/init.py", line 107, in whichdb f = io.open(filename + ".pag", "rb") IOError: [Errno 2] No such file or directory: 'mydb.pag'

like image 723
Esben Avatar asked Jan 02 '12 19:01

Esben


People also ask

How do I use shelve module in Python?

Easiest way to form a Shelf object is to use open() function defined in shelve module which return a DbfilenameShelf object. The filename parameter is assigned to the database created. Protocol parameter denotes pickle protocol writeback parameter by default is false. If set to true, the accessed entries are cached.

What is the use of shelf in Python?

A “shelf” is a persistent, dictionary-like object. The difference with “dbm” databases is that the values (not the keys!) in a shelf can be essentially arbitrary Python objects — anything that the pickle module can handle.

What is import shelve in Python?

The shelve module creates a persistent, file-based version of an object very similar to a Python dictionary, but data is only read from or written to the file when necessary, for example when you need to access a value stored in the file, or add a value to the file.


2 Answers

I ran into the same issue and implemented a dict-based class that supports loading and writing the contents of the dict from and to disk.

from pathlib import Path
import pickle


class DiskDict(dict):
    def __init__(self, sync_path: Path):
        self.path = sync_path

        if self.path.exists():
            with open(self.path, "rb") as file:
                tmp_dct = pickle.load(file)
                super().update(tmp_dct)
                print(f"loaded DiskDict with {len(tmp_dct)} items from {self.path}")

    def sync_to_disk(self):
        with open(self.path, "wb") as file:
            tmp_dct = super().copy()
            pickle.dump(tmp_dct, file)
            print(f"saved DiskDict with {len(tmp_dct)} items to {self.path}")
like image 162
pedjjj Avatar answered Nov 07 '22 06:11

pedjjj


Thank you for your reply!

I seems that shelves in python are not easily forced to use a specific db, however pickles works like a charm. At least from mac os -> windows 7.

So short answer: If you want portability, don't use shelves, use pickles directly.

/Esben

like image 44
Esben Avatar answered Nov 07 '22 07:11

Esben