Data Structures
PolePosition exposes a small runtime data-structure namespace:
from polepos.data import LRUCache, SortedDict, Trie
This API is for application code that wants a few practical structures Python does not provide as first-class built-in containers. It is intentionally small, pure Python, and dependency-free.
Import Boundary
polepos.data is a runtime package. It is separate from the internal
pole_position CLI implementation package.
Use it from generated applications like this:
from polepos.data import IndexedPriorityQueue
Do not import from pole_position.cli... in generated app code. That package
is for the generator and command implementation.
Available Structures
Current exports:
LRUCache: bounded least-recently-used cacheTTLCache: lazy-expiring in-memory cacheOrderedSet: insertion-ordered setSortedList: sorted list with bisect helpersSortedSet: unique sorted valuesSortedDict: mapping that iterates keys in sorted orderIndexedPriorityQueue: min-priority queue with update/remove by keyTrie: prefix tree for string keysUnionFind: disjoint-set unionGraph: adjacency-set graph with BFS, DFS, shortest path, and topological sort
Examples
LRU Cache
from polepos.data import LRUCache
cache = LRUCache[str, dict](max_size=500)
cache["user:1"] = {"id": 1}
user = cache["user:1"]
Indexed Priority Queue
from polepos.data import IndexedPriorityQueue
jobs = IndexedPriorityQueue[str, int, dict]()
jobs.push("sync-users", priority=10, value={"kind": "sync"})
jobs.update("sync-users", priority=1)
next_job = jobs.pop()
Trie
from polepos.data import Trie
names = Trie[int]()
names.insert("customer", 1)
names.insert("customs", 2)
matches = names.keys("cust")
Sorted Dict
from polepos.data import SortedDict
scores = SortedDict[str, int]()
scores["alice"] = 10
scores["bob"] = 8
for name, score in scores.items():
...
Runtime Caveat
These structures are in-memory and process-local. In a FastAPI app with multiple Uvicorn workers, each worker has its own copy. Use them for local indices, request-time algorithms, bounded caches, and test doubles.
For shared or persistent state, prefer infrastructure-backed structures:
- Redis sets, sorted sets, streams, and TTL keys
- PostgreSQL tables, indexes, materialized views, and full-text search
- Kafka or RabbitMQ for cross-process event streams
PolePosition may add higher-level module templates around these structures, but the import surface remains explicit application code:
from polepos.data import Graph