Reference library

Python Code Samples

Copy-ready Python snippets by topic and difficulty — short, focused, and runnable in the browser editor.

4 matches
System design patterns easy

How to Build an Append-Only Event Store in Python

Implement a simple append-only event store class that stores events in a list and supports retrieval by index range.

event-sourcing append-only event-store
Python
class EventStore:
    def __init__(self):
        self._events = []

    def append(self, event):
        """Append an event to the store."""
        self._events.append(event)

    def get_events(self, start=0, end=None):
        """Return events from start index to end (exclusive)."""
        return self._events[sta…
14 0 Open
Streaming & messaging easy

Event sourcing append store replay in Python

A simple in-memory event store that appends events per aggregate and replays them on demand.

event-sourcing append-only replay
Python
import json
from collections import defaultdict


class EventStore:
    def __init__(self):
        self._events = defaultdict(list)

    def append(self, aggregate_id, event_type, data):
        event = {"type": event_type, "data": data}
        self._events[aggregate_id].append(event)

    def replay(self, aggregate…
14 0 Open
Streaming & messaging medium

How to mock a CQRS projector read model update in Python

Build a CQRS projector class that maintains denormalized read models by applying domain events in a mock order-processing service.

cqrs projector read-model
Python
from dataclasses import dataclass, field
from typing import Dict, List, Optional


@dataclass
class OrderReadModel:
    order_id: str
    customer_name: str
    total: float
    status: str = "pending"
    items: List[Dict] = field(default_factory=list)

    def apply_event(self, event_type: str, payload: Dict) -> Non…
11 0 Open
Microservices patterns easy

Event Sourcing Store in Python: Append-Only Log Mock

Mock an append-only event store in Python — record events, list them, and fetch by ID using a simple list-backed class.

event-sourcing microservices mock
Python
class EventStore:
    def __init__(self):
        self._events = []

    def append(self, event):
        event_id = len(self._events) + 1
        stored_event = {"id": event_id, "data": event}
        self._events.append(stored_event)
        return stored_event

    def get_events(self):
        return list(self._ev…
14 0 Open

Browse by section

Each section groups closely related Python snippets.

Guide: free Python code samples library

Copy-ready Python snippets for learners and developers

PythonSkillset code samples are short, focused examples organised by topic and difficulty. Every snippet is server-rendered HTML — readable by search engines and easy to copy. Open any sample, read the notes, copy the code, then press Try in editor to run it in the browser with Pyodide.

How to use this library

  1. Pick a topic section — strings, lists, files, functions, and more
  2. Open a sample, read How it works, and copy the code block
  3. Run it in the IDE, tweak values, then take a related quiz or tutorial lesson

Samples vs tutorials and challenges

Samples are quick reference — one concept per page. For step-by-step teaching, use our Python tutorials. To test yourself, try quizzes or coding challenges. Clean up style with the Python formatter.