RSS Amplifier

Kevin’s Blog · Dec 18, 2024

My first Python package: pydantic-sqlalchemy-deco

0
Sign in to vote or save

This page cannot be shown here. You can still read it on the original site — the toolbar below keeps your place in the directory.

My foray into Python

Today, I released my first Python package published on pypi!

It doesn’t do much, but it’s something that has been bugging me for a while. I often include Pydantic models in SQLAlchemy-created databases. Normally, I just use JSONB and use the regular ol’ model_validate functions. While this works, I’d prefer it to be automatic. Here’s where pydantic-sqlalchemy-deco comes in.

It’s a simple TypeDecorator class that automatically converts between Pydantic and SQLAlchemy JSONB types.

Installation

Since it’s pip installable, you can use pip install pydantic_sqlalchemy_deco.

Usage

All you need to do is set the Mapped[] type and include PydanticJSON in the mapped_column(), like such:

from pydantic_sqlalchemy_deco.decorator import PydanticJSON
...
# Define Pydantic model somewhere:
class MyCustomModel(BaseModel):
    custom_id: int = 0
# Define your SQLAlchemy model:
class MyEntry(Base):
    custom_data: Mapped[MyCustomModel] = mapped_column(PydanticJSON(MyCustomModel))

Then it’s easy as doing

entry = MyEntry(
    custom_data=MyCustomModel(custom_id=1234)
)
...
print(entry.custom_data.custom_id)

Pretty simple, eh?

Further remarks

I never realized how easy it is to build Python packages. It’s basically:

  1. write your code
  2. define your pyproject.toml file
  3. then setup the default Github action to push releases to pypi.

That’s it! I was expecting a whole mess of generating and keeping track of tokens, writing and rewriting Github actions, and some CLI work to get it published.

I’ll eventually need to do tests, but I can hold off on those til later.

Links

Read on /2024/12/18/first-python-package.html

Comments

Nothing yet. Say the first thing.

    Sign in to join the conversation.