How to Update a Project using a Copier Template

In this article, I will show you how you can update a project, that was created from a copier template. In my previous article, we learnt how we can create a project template. I listed the only reason I choose to use copier over say cookiecutter was that it provided an “easy” way to update downstream projects. Existing Repository From here on out we will assume you have a template repository that uses copier. ...

How to use copier to create project templates

Hi 👋 everyone, in this blog post I’m going to over how we can use Copier to create templates for our repositories. Why create a template repo ? I’m sure some of you are wondering why do we even need templates ? Well for a few reasons, one it gives you a consistent way to create new services from a repository. For my personal project Banter Bus I have a FastAPI Template, which I use to create new FastAPI services from. ...

TIL: You can Hash `None` in Python? What?

TIL you can hash None in Python Recently I saw some Python code which looked something like: d = { None: "value", "another_key": "another_value", } It had never really occurred to me you could use None (null) as a key to a dictionary before. What this also meant is that None must be hashable as only hashable objects can be keys in a dictionary. These objects include strings, tuples, sets but don’t include lists as they are mutable and not hashable. ...

 2022-11-15 253 words 2 min

Separate function handler modules when using Python Socketio

In this article I will show you how you can have separate modules for your Socketio event handlers. Rather than keeping them all in the same file. Hopefully this should be a relatively short article, lets get into it Main In this example I will be using SocketIO alongside FastAPI, but you can easily change this code to be SocketIO only. I also will be using a uvicorn to run the server. ...

Testing a socketio Web App written in Python

In this article I will show you how you can test an async Socketio application in Python, where the ASGI server we are running is uvicorn. I will be referring to these tests as integration tests, though depending on who you ask they could be called E2E tests, system tests, slow test etc. What I am referring to is simply testing out the entire “flow” of a socketio event i.e. emitting an event from a client, then receiving it on the web service and for my actual projects interacting with an actual database. ...

What does yield do in Python

In this article, we will go over what the yield keyword is used for. We will also cover how you can use a yield with a pytest fixture to allow us to “teardown” tests, after all of our tests have run. A common job being removing test data from the database, so that next time your run the tests your tests won’t fail. Due to the database being in a different (unexpected) state. ...

How to use Gitlab CI, Pytest and docker-compose together

On a recent project, I was working on, I wanted to test my web service using docker-compose where I can run and kill Docker containers used by the application and see how my web application reacts to that. In this article, we will go over how you start docker containers using docker-compose from within Gitlab CI. The diagram above is a visualisation of what we are trying to achieve. We want to spawn Docker containers using docker-compose from within our job. ...

Testing a Connexion/Flask Application with Pytest

In this article, I will show you how you can test a Python web service that was built using Connexion (a wrapper library around Flask). We will go over how you can mock functions and how you can test your endpoints. There are two related articles I have written in the past listed below. In the first one we go over how to create a web service using Connexions, the same web service we will in this article. ...

Using Tox with a Makefile to Automate Python related tasks

In this article, we will go over how we can use a makefile and tox to automate various Python related tools. This article assumes you are running bash (or equivalent). Tox Tox is an automation tool used primarily to add in testing. On the Tox website, it describes itself as tox aims to automate and standardize testing in Python. It is part of a larger vision of easing the packaging, testing and release process of Python software. ...

Creating a Simple RESTful App using OpenAPI, Flask & Connexions

RESTful APIs are very popular at the moment and Python is a great language to develop web APIs with. In this article we will go over a documentation first approach to building APIs. We will be using Flask, Swagger Code-Gen (OpenAPI) and Connexions. I will go over an API/documentation first approach to building a RESTful API in Python. Which will try to minimise the differences between what’s defined in the API specification and the actual API logic itself. ...

Testing a Flask App with pytest-mock and pytest-flask

Pytest is a popular Python library used for testing. It is my preferred testing library because it requires less boilerplate code than the alternatives such as (the builtin) unittest, the built in testing library. In this article, I will show you how you can use pytest-flask and pytest-mock to test your Flask app. These two libraries are plugins for Pytest which build upon some of the features that Pytest provides us. ...

Building A Simple Flask App with SQLalchemy and Docker

SQLAlchemy is an object-relational mapper (ORM), it allow us to interact with a database using Python functions and objects. For example, if we have a table called Cats we could retrieve every row with a command like Cats.query.all(). The main advantage of this is that it allows us to abstract away the SQL. Docker 🐳 allows us to quickly bring up a database within a Docker container, this means we don’t have to set up and configure a database on our local machine. ...

Using Multiple Docker Containers to Setup Nginx, Flask and Postgres

Terminology Docker Image: Is a file used to execute code in a Docker container, built from a set of instructions. Docker Container: Is a Docker image that is being executed or run. Docker 🐳 is a relatively new and exciting technology, Docker is a containerisation tool. The main benefits of using Docker is that you can use the same environment for development, testing and production. Since Docker environments are consistent this means if the application works in the testing environment it will also work in production. ...

Pytest with Background Thread Fixtures

Recently I had to test some of my Python 🐍 🐍 🐍 code which required an external dependency and communicating by using TCP sockets 🔌 . You can think of this dependency as essentially a database because it stored information. However, when testing my Python code, I couldn’t rely on there always being a TCP server to send messages to. So I ended up creating a simplified mocked version in Python. ...

Inheritance in SQLAlchemy (with Flask)

SQLAlchemy is an Object-relational mapping (ORM) made for the Python programming language. ORMs in theory allow programmers to abstract away SQL. In simple terms they allow us to interact with a database using purely Python (objects/functions). I will be using the flask-SQLAlchemy extension for my examples. Each table is referred to as a model, each model is simply just a python class and each attribute of that class becomes a column in an SQL table. ...