Other articles


  1. Sqlalchemy Footgun: Discarding the statement

    This one has frustrated me for a while.

    It starts off with a REST API route. For example in fastAPI

    @app.get("/")
    def search_users(session: Session) -> list[User]:
        """Finds users optionally filter by user_id"""
        statement = select(User).order_by(User.name)
        return session.execute(statement).scalars().all()
    

    Then we get asked …

    read more
  2. Jamie's Pattern Matching Cookbook

    Structural pattern matching is probably the coolest new syntax introduced to Python. Added in 3.10, it's been a few years now and more people are writing apps in 3.10* than any other version now.

    Though even with the wide adoption of 3.10 and more people being exposed …

    read more
  3. Customising Pattern Matching Behaviour

    I've been doing advent of code again this year. There are two Python features I always rely on, iterators and pattern matching. Iterators allow for operations on each of its elements without allocating memory for a collection. Ever since pattern matching was introduced in Python 3.10, it's been particularly …

    read more
  4. Python-in-Python Sandboxing LLM Generated Code

    I've been experimenting with Langchain for GPT based queries. One problem we often encounter with GPT is hallucinations. This makes certain classes of problems unsuited to GPT, one example is maths and statistics. Whilst there are improvements for recent models often the maths cannot be trusted.

    When I try to …

    read more
  5. My SQLAlchemy Cookbook

    I've worked with SQLAlchemy for a while now, and in my opinion it's the best ORM in Python. It's feature rich with strong support for all major databases. And it maintains the SQL feel without losing things like typing.

    However there are some challenges here. Despite having very nice documentation …

    read more
  6. TypedDicts are better than you think

    TypedDict was introduced in PEP-589 which landed in Python 3.8.

    The primary use case was to create type annotations for dictionaries. For example,

    class Movie(TypedDict):
        title: str
    
    
    movie: Movie = {"title": "Avatar"}
    

    I remember thinking at the time that this was pretty neat, but I tend to use dataclass …

    read more

social