Skip to content

Python

Pydantic series

alt text

The focus in this blog is on validation, as well as being able to return the same output as we got the input. Which is a json format. As an example, when parsing an IP address as a string, the output value should be a string again and not an ipaddress object. Or the MAC address which needs to retain a certain format.

The "bonus" of also dumping it back into json, is that a diff can be done between the original and the output to see if anything was changed or missed.

This is based on a series that I did on LinkedIn.

Convert poetry to uv

poetry2uv When I encountered #uv I fell for it right away. I was already using ruff for a while and started looking into rye, which was the intermediate step before it became uv.

Creating a uv repo is easy, just run uv init $project and you are off to the races. Using it for new projects is easy if you are already used to poetry. The commands are quite similar. Just add a few libraries with uv add $library or uv add --dev $library to make it part of the development group.

Improved shebang for your python standalone script

uv shebang

Sometimes you just want a script, without having to specify a virtualenv or polluting your global python environment. Given PEP-0723 we can build scripts as we would normally, though now add a few comments to it to specify the dependencies.

When we combine that with uv we can easily run the script without having to activate a virtualenv or specify the python interpreter. And it will just take care of it. Assume the script has the name script.py we would call it with uv run -s script.py.

Pypi Trusted Publisher Management and pitfalls

security pipeline Be the cool kid on pypi, I thought, use the Trusted Publisher Management and OpenID Connect (OIDC)... I thought...

While working on my latest repo convert_poetry2uv, I wanted to automatically push the builds to pypi. Traditionally a username/password combination was used, which was later replaced by an API token. These days OIDC can be used, which I tried. I'm here to share some pitfalls, so hopefully you don't fall in them.