liveBook · Manning

List

As explained in Practices of the Python Pro

list is a PersistenceLayer method that retrieves bookmark records in a specified ordering.

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

How Practices of the Python Pro covers it

How deep it goes
Defines interface behavior and performance characteristics, while stopping short of detailed database-query implementation.
Examples
Uses sorted dictionary values and ListBookmarksCommand to demonstrate ordered results.
What it assumes
Assumes basic Python collection use and functions, while introducing persistence abstraction through interfaces and concrete classes.
What you can do after
Apply it via a PersistenceLayer interface to retrieve ordered bookmarks without coupling commands to database-specific calls.
How it connects
Connects list with persistence layers, abstract base classes, database abstractions, commands, dictionaries, and tuples.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Practices of the Python Pro. The section links point to the parts of the book it draws on.