Saving data with shelve

A shelf, is a persistent dictionary-like object. The beauty of it is that the values you save into a shelf can be any object you can pickle, so you're not restricted like you would be if you were using a database. Albeit interesting and useful, the shelve module is used quite rarely in practice. Just for completeness, let's see a quick example of how it works:

# persistence/shelf.py
import shelve

class Person:
def __init__(self, name, id):
self.name = name
self.id = id

with shelve.open('shelf1.shelve') as db:
db['obi1'] = Person('Obi-Wan', 123)
db['ani'] = Person('Anakin', 456)
db['a_list'] = [2, 3, 5]
db['delete_me'] = 'we will have to delete this one...'

print(list(db.keys())) # ['ani', 'a_list', 'delete_me', 'obi1']

del db['delete_me'] # gone!

print(list(db.keys())) # ['ani', 'a_list', 'obi1']

print('delete_me' in db) # False
print('ani' in db) # True

    a_list = db['a_list']
a_list.append(7)
db['a_list'] = a_list
print(db['a_list']) # [2, 3, 5, 7]

Apart from the wiring and the boilerplate around it, the previous example resembles an exercise with dictionaries. We create a simple Person class and then we open a shelve file within a context manager. As you can see, we use the dictionary syntax to store four objects: two Person instances, a list, and a string. If we print the keys, we get a list containing the four keys we used. Immediately after printing it, we delete the (aptly named) delete_me key/value pair from shelf. Printing the keys again shows the deletion has succeeded. We then test a couple of keys for membership, and finally, we append number 7 to a_list. Notice how we have to extract the list from the shelf, modify it, and save it again.

In case this behavior is undesired, there is something we can do:

# persistence/shelf.py
with shelve.open('shelf2.shelve', writeback=True) as db:
db['a_list'] = [11, 13, 17]
db['a_list'].append(19) # in-place append!
print(db['a_list']) # [11, 13, 17, 19]

By opening the shelf with writeback=True, we enable the writeback feature, which allows us to simply append to a_list as if it actually was a value within a regular dictionary. The reason why this feature is not active by default is that it comes with a price that you pay in terms of memory consumption and slower closing of the shelf.

Now that we have paid homage to the standard library modules related to data persistence, let's take a look at the most widely adopted ORM in the Python ecosystem: SQLAlchemy.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset