Skip to main content
Engineering LibreTexts

5.7: Pickling

  • Page ID
    17067
  • A limitation of anydbm is that the keys and values have to be strings. If you try to use any other type, you get an error.

    The pickle module can help. It translates almost any type of object into a string suitable for storage in a database, and then translates strings back into objects.

    pickle.dumps takes an object as a parameter and returns a string representation (dumps is short for “dump string”):

    >>> import pickle
    >>> t = [1, 2, 3]
    >>> pickle.dumps(t)
    '(lp0\nI1\naI2\naI3\na.'
    

    The format isn’t obvious to human readers; it is meant to be easy for pickle to interpret. pickle.loads (“load string”) reconstitutes the object:

    >>> t1 = [1, 2, 3]
    >>> s = pickle.dumps(t1)
    >>> t2 = pickle.loads(s)
    >>> print t2
    [1, 2, 3]
    

    Although the new object has the same value as the old, it is not (in general) the same object:

    >>> t1 == t2
    True
    >>> t1 is t2
    False
    

    In other words, pickling and then unpickling has the same effect as copying the object.

    You can use pickle to store non-strings in a database. In fact, this combination is so common that it has been encapsulated in a module called shelve.

    Exercise \(\PageIndex{1}\)

    If you download my solution to Exercise 12.11.2 from http://thinkpython.com/code/anagram_sets.py, you’ll see that it creates a dictionary that maps from a sorted string of letters to the list of words that can be spelled with those letters. For example, ’opst’ maps to the list [’opts’, ’post’, ’pots’, ’spot’, ’stop’, ’tops’].

    Write a module that imports anagram_sets and provides two new functions: store_anagrams should store the anagram dictionary in a “shelf;” read_anagrams should look up a word and return a list of its anagrams.

    Solution:

    http://thinkpython.com/code/anagram_db.py

    • Was this article helpful?