A data-driven app without the database

My January 2024 project was a new open source coding cookbook. Instead of primary keys and foreign key constraints, I’m using Python’s typing.Literal and Pydantic models. This makes sense to try because the final product is a statically generate website (built with Hugo). My Python code does all the content preparation.

So, for example:

Category = Literal[
    'Data Formats and Serialization',
    'Data Structures',
    'Data and Text Processing',
    # ...
]

Topic = Literal[
    'Calculating a date in the future or past',
    'Capitalizing a string',
    'Checking if a directory exists',
    # ...
]

CATEGORIES_AND_TOPICS: dict[Category, list[Topic]] = {
    "Getting Started": [
        "Starting a new project",
    ],
    "Strings": [
        "Capitalizing a string",
        "Concatenating strings",
        # ...
}

class ArticleSpec(BaseModel):
    """
    A specification of a programming article.
    It consists of a topic, programming language, and
    natural language.
    """
    model_config = ConfigDict(frozen=True)
    
    human_lang: HumanLang
    prog_lang:  ProgLang
    topic:      Topic

Etc. I can now make complicated data structures from these Literals and I have existence checking right in my IDE. It catches typos right away, etc.

My Python CLI app is idempotent, so when I run it, it only fills in missing data. So for me to add new content and regenerate, it’s pretty easy.

Leave a Comment