1

Can anyone recommend a module or tell me way to get a object database for Python? One of the reason I´m interested in databases to save objects is because I suppose a database can provide a simpler piece of code than use pickle. In my question Saving an Object (Data persistence) someone gave me a piece of code using pickle and I think there should be a simpler way to save objects.

Community
  • 1
  • 1
Peterstone
  • 7,119
  • 14
  • 41
  • 49

4 Answers4

4

If the simplicity of the code is your main concern, pickle is hard to beat.

Basically, all you have to do in order to save an object data is:

with open('data.pickle', 'w') as pickle_file:
    pickle.dump(data, pickle_file)

You can then get it back with:

with open('data.pickle') as pickle_file:
    data = pickle.load(pickle_file)  # You get the data back!

There are some technical details that you might want to check out, if you need to save objects from your own classes, but saving objects to a file is basically as simple as the writing/reading code above!

Eric O. Lebigot
  • 91,433
  • 48
  • 218
  • 260
3

It is very vague field since you didn't tell what your requirements are. There are different possibilities depending on what exactly you want. You can search in the python index for database-related products.

Store and retrieve of python objects, no queries of any kind, everything in memory

I would use pickle or json to store and load objects into a single file on the disk.

Store and retrieve of objects to/from a relational database

There are a lot of Object relational mappers available for python

On-demand load and store of objects

There are some "pure object" databases for python like zope or cog (no, I have no experience with either of them).

Edit

Since you clarified that you want a a simple solution i second @EOL that pickle is the tool to use.

Community
  • 1
  • 1
Rudi
  • 19,366
  • 3
  • 55
  • 77
  • Don't forget the `shelf` module in the standard library, which is essentially a persistent dictionary where each object is associated with a key which can be used to retrieve, update, and delete it. – martineau Dec 25 '10 at 10:31
  • 1
    @martineau: Thank you I find it usefull! – Peterstone Dec 25 '10 at 10:57
3

The ZODB is an excellent object database. It takes some work to set up, but it's totally worth it.

Pickle is not a databases, so they can be used to load or save your data, but you can't use them as a database, only as a save-file format.

Shelve is a primitive key-data store. I.e, if all your objects have a unique key, you can use it, otherwise not.

Lennart Regebro
  • 167,292
  • 41
  • 224
  • 251
2

Here's an example of using the shelve module (I mistakenly called the shelf module in my comment to Rudi's answer). This is patterned after my answer to your other question, so you can easily compare it to using the pickle module that is shown in the other answer.

Notice how the objects are retrieved in a different order unrelated to how they were placed in the database file (which happens to be the reverse of that order since there are only two objects in the shelf).

import shelve

class Company:
    def __init__(self, name, value):
        self.name = name
        self.value = value

comp_db = shelve.open('company_database', 'n')
comp_db['company1'] = Company('banana', 40)
comp_db['company2'] = Company('spam', 42)

# update entry
a_company = comp_db['company1']
a_company.name = 'egg' # change name
comp_db['company1'] = a_company # update db

comp_db.close()

comp_db = shelve.open('company_database')

company2 = comp_db['company2']
print company2.name
# spam
print company2.value
# 42

company1 = comp_db['company1']
print company1.name
# egg
print company1.value
# 40

comp_db.close()
Community
  • 1
  • 1
martineau
  • 119,623
  • 25
  • 170
  • 301