Writing a demonstration script

We'll use a technology spike to show you how an application might use this Access class to process the microblog objects. The spike script will save some Blog and Post objects to a database to show a sequence of operations that an application might use. This demonstration script can be expanded into unit test cases.

More complete unit tests would show us that all the features are present and work correctly. This small spike script shows us how Access works:

from contextlib import closing
from pathlib import Path

path = Path.cwd() / "data" / "ch11_blog" with closing(Access()) as access: access.new(path)

# Create Example access.create_blog(b1) for post in p2, p3: access.create_post(b1, post)
# Retrieve Example
b = access.retrieve_blog(b1._id) print(b._id, b) for p in access.post_iter(b): print(p._id, p)

We've created the Access class on the access layer so that it's wrapped in a context manager. The objective is to be sure that the access layer is closed properly, irrespective of any exceptions that might get raised.

With Access.new(), we've created a new shelf named 'blog'. This might be done by a GUI through navigating to File New. We added the new blog, b1, to the shelf. The Access.create_blog() method will update the Blog object with its shelf key. Perhaps someone filled in the blanks on a page and clicked on New Blog on their GUI application.

Once we've added Blog, we can add two posts to it. The key from the parent Blog entry will be used to build the keys for each of the child Post entries. Again, the idea is that a user filled in some fields and clicked on New Post on the GUI.

There's a final set of queries that dumps the keys and objects from the shelf. This shows us the final outcome of this script. We can perform Access.retrieve_blog() to retrieve a blog entry that was created. We can iterate through the posts that are part of that blog using Access.post_iter().

The use of the contextlib.closing() context manager ensures that a final Access.close() function evaluation will save the database to persistent storage. This will also flush the self.max dictionary used to generate unique keys.

The next section talks about how to create indexes to improve efficiency.

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

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