I’ve been wanting to publish my favorite Python tools annually for a long time now. It’s practical for a lot of people, and it allows to see how they evolve over time and the projects I contribute to.
So here’s the first edition of this list.
Continue reading “My Python Toolbox, 2024 Edition”
The blog you are currently on used to be hosted on some Virtual Private Server. My hosting provider decided to quit the business and I had to leave. He kindly suggested that I look at AWS Lightsail as a migration option. I did, and this is where this blog is now hosted.
This blog is not new. It already had content. It also has customisations like plugins and themes. This post will focus on how to export your content and customisations from a former blog and import them on Lightsail. Its accuracy may vary depending on the level of customisation your blog has. Command line (CLI) experience is required.
Continue reading “Recipe: Migrating a WordPress Blog Over AWS Lightsail”
A handy code sample that I keep forgetting about.
from datetime import datetime, timezone
now = datetime.now(tz=timezone.utc)
A FastAPI equivalent for verify-events-came-from-stripe.
from http import HTTPStatus
from typing import Annotated
from fastapi import Depends, FastAPI, Header, HTTPException, Request
app = FastAPI()
async def get_body(request: Request) -> bytes:
return await request.body()
stripe_signature: Annotated[str, Header(alias="stripe-signature")],
body: bytes = Depends(get_body),
) -> None:
endpoint_secret = os.environ["ENDPOINT_SECRET"]
# signature validation
event = stripe.Webhook.construct_event(body, stripe_signature, endpoint_secret)
except ValueError as e:
# Invalid payload
raise HTTPException(status_code=HTTPStatus.BAD_REQUEST) from e
except stripe.error.SignatureVerificationError as e:
# Invalid signature
raise HTTPException(status_code=HTTPStatus.UNPROCESSABLE_ENTITY) from e
A possible catch, one that I ran into, is that I had FastAPI convert the
request.body to a dictionary in the function parameters (so
body: dict). Then I serialized it to a string for the validation step… and it failed because it was no longer identical to what came in.
The serverless documentation indicates how to share the ids of an API Gateway, but the technique described there seems to work only for a new deployment. (In my case, following this technique created a 2nd empty API Gateway whose ids were shared, which was, unsurprisingly, not fulfilling my needs).
Here is how to do it for an existing deployment.
Continue reading “Serverless: How to share the ids of an existing API Gateway”
During a code review, a colleague, Zachary Paden, asked me why I was calling the
Continue reading “Python Type Hinting: To hint or to cast?”
typing.cast function on my variables rather than creating temporary variables just to type hint. Well, just as he didn’t know about cast, I didn’t know that this approach worked. Being the nerdz that we are, he decided to measure the performance of each approach.
Today, at the suggestion of a college (Zachary Paden), I contemplated the idea of migrating my tests using python/monkeypatch to moto. Here I will share my journey and observations.
Continue reading “boto3 – From monkeypatch to moto”
Last April, I wrote a post about managing boto3 in deployed packages. (See Minimizing the size of lambdas by avoiding having boto3 and its stubs in the deployment package). This approach works when all dependencies can be set in the development section.
But, what about when dependencies are required in production? For example, we recently had to deploy a lambda that depends on awswrangler. So the technique suggested in the previous post did not work. Here I will cover a different approach that allows to remove boto3 from the package to be deployed in the example context. It is however less flexible and based on a specific technology: serverless.
Continue reading “Minimize the size of lambdas by deleting boto3 from the deployment package”
I recently decided to publish my articles in French and in English. First, I had to find and test plugins in order to choose one. So I spent some time, more than I expected, to make it work locally through Docker. Since I encountered some pitfalls, I decided to share the recipe.
Continue reading “Recipe: Run a local copy of a WordPress site with Docker”
In this article, I will explain how to create a pydantic model to validate and create polygons for GIS (geographic information systems).
Continue reading “Creating a pydantic model for GIS polygons”