A walkthrough of building a shared functional core that powers both a CLI and a FastAPI web app without duplicating logic. Key patterns covered include pure functions as the shared spine, using Python's `@cache` decorator for memoized library loading, FastAPI's `lifespan` context for pre-warming the vector matrix and PyTorch model before the first request, and lazy imports to ensure Hugging Face offline environment variables are set before `sentence-transformers` loads. The post argues that classes are unnecessary here — NamedTuples and memoized functions handle all state — and that adding a third interface later requires only importing the same core functions.
Table of contents
Functional core, two interfacesI needed cachingPre-warm on startup, not on the first visitorLazy loadingFunctions vs classes1.4K Impressions