O'Reilly logo

Python Testing Beginner's Guide by Daniel Arbuckle

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Writing persistence code

Now that there are at least basic unit tests covering the persistence mechanism, it's time to write the persistence code itself. The following goes in planner/persistence.py:

import sqlite3
from cPickle import loads, dumps
class file:
def __init__(self, path):
self.connection = sqlite3.connect(path)
try:
self.connection.execute("""
create table objects (tag, pickle)
""")
except sqlite3.OperationalError:
pass
def store_object(self, tag, object):
self.connection.execute('insert into objects values (?, ?)',
(tag, dumps(object)))
def load_objects(self, tag):
cursor = self.connection.execute("""
select pickle from objects where tag like ?
""", (tag,))
return [loads(row[0]) for row in cursor]

The store_object method runs a ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required