The name list serves two practical roles. In Python, it is a collection whose membership checks and insertions usually grow with the number of items, while adding or removing at the end stays constant-time. In a persistence interface, list(order_by) retrieves all bookmarks in a requested ordering. section 4.2.2 section 10.4.3
Use the two forms for different jobs:
- Use sorted(values) when converting dictionary values into an ordered Python list. section 1.7.1
- Use persistence.list(order_by=...) when business logic needs bookmarks without calling database-specific methods directly. section 10.4.3
Ordered bookmark retrieval
A ListBookmarksCommand can store an order such as date_added, then return persistence.list(order_by=self.order_by). The command receives bookmark records while the persistence implementation handles database details. section 10.4.3
Listing 10.10. A persistence interface and implementation
from abc import ABC, abstractmethod
from database import DatabaseManager
class PersistenceLayer(ABC): #1
@abstractmethod
def create(self, data): #2
raise NotImplementedError('Persistence layers must implement a
create method')
@abstractmethod
def list(self, order_by=None):
raise NotImplementedError('Persistence layers must implement a
list method')
@abstractmethod
def edit(self, bookmark_id, bookmark_data):
raise NotImplementedError('Persistence layers must implement an
edit method')
@abstractmethod
def delete(self, bookmark_id):
raise NotImplementedError('Persistence layers must implement a
delete method')
class BookmarkDatabase(PersistenceLayer): #3
def __init__(self):
self.table_name = 'bookmarks' #4
self.db = DatabaseManager('bookmarks.db')
self.db.create_table(self.table_name, {
'id': 'integer primary key autoincrement',
'title': 'text not null',
'url': 'text not null',
'notes': 'text',
'date_added': 'text not null',
})
def create(self, bookmark_data): #5
self.db.add(self.table_name, bookmark_data)
def list(self, order_by=None):
return self.db.select(self.table_name, order_by=order_by).fetchall()
def edit(self, bookmark_id, bookmark_data):
self.db.update(self.table_name, {'id': bookmark_id}, bookmark_data)
def delete(self, bookmark_id):
self.db.delete(self.table_name, {'id': bookmark_id})
The interface listing shows list(order_by=None) alongside create, edit, and delete, making the persistence contract concrete. section 10.4.3
