Category: Python

2021-04-06T15:54:26Z

How to Write Unit Tests in Python, Part 2: Game of Life

This is the second part of my series on unit testing Python applications. In this article I will introduce you to Conway's Game of Life, an interesting simulation that plays animated patterns on a grid.

Game of Life

My implementation of this game has an engine part, where the data structures and algorithm of the simulation are implemented, and a graphical user interface (GUI) part. In this article I will focus on testing the engine (testing GUIs will be covered in a future article).

Testing this code will present us with a few new challenges. In this article you will learn about the following topics:

  • Test parametrization
  • Building test matrices
  • Basic mocking
  • Testing Python exceptions

4 comments

2021-02-17T11:36:00Z

How to Write Unit Tests in Python, Part 1: Fizz Buzz

Today I'm starting a new series of articles about a topic that does not get a lot of coverage: how to write Python unit tests. Unlike other testing tutorials, I'm going to focus on testing techniques more than on the testing tools themselves. The idea is that in each part of this series I will look at a particular piece of code and discuss the challenges it presents from a testing point of view and how to address them.

In this first part I'm going to introduce the testing set up that I use in my projects, which is based on pytest. Then I will show you how to write unit tests for an implementation of the popular fizz buzz game that is frequently the subject of coding interview exercises.

20 comments

2021-01-10T23:29:01Z

Learn Socket.IO with Python and JavaScript in 90 Minutes!

The video below contains a complete 90 minute Socket.IO course using Python and JavaScript.

This is the list of chapters, each with a link to the code for each part of the tutorial:

4 comments

2020-12-15T12:06:14Z

Why do we pass __name__ to the Flask class?

When you learn Flask, you are told to create your Flask application instances by passing __name__ as the first argument to the Flask class. Most developers do this without thinking, and without knowing what it achieves.

In this article we are going to look at Flask(__name__) in depth. By the end you will not only have a full understanding of this pattern, but you will also know when to deviate from it and pass other values.

6 comments

2020-10-19T10:19:50Z

How to Kill a Python Thread

I'm often asked how to kill a background thread, and the answer to this question makes a lot of people unhappy: threads cannot be killed. In this article I'm going to show you two options we have in Python to terminate threads.

15 comments

2020-09-15T12:09:29Z

Ignore All Web Performance Benchmarks, Including This One

Posted by Miguel Grinberg under Python.

A couple of months ago there was an article titled Async Python is Not Faster making the rounds on social media. In it, the author Cal Paterson made the point that contrary to popular belief, asynchronous web frameworks are not only "not faster" than their traditional synchronous counterparts, but they are also slower. He supports this by showing the results of a fairly complete benchmark that he implemented.

I wish things were as simple as this author puts them in his blog post, but the fact is that measuring web application performance is incredibly complex and he's got a few things wrong, both in the implementation of the benchmark and in his interpretation of the results.

In this article you can see the results of my effort in understanding and fixing this benchmark, re-running it, and finally arriving at a shocking revelation.

11 comments

2020-09-08T14:49:01Z

Sync vs. Async Python: What is the Difference?

Posted by Miguel Grinberg under Python.

Have you heard people say that async Python code is faster than "normal" (or sync) Python code? How can that be? In this article I'm going to try to explain what async is and how it differs from normal Python code.

24 comments

2020-09-01T09:24:09Z

How to Deploy a React-Router + Flask Application

This is the third article in my "React + Flask" series, in which I discuss applications that combine a Flask API server with a React single-page application. This time I'm going to show you how to work with the popular React-Router library for React, and in particular how this library affects the production deployment of the application.

This is the third article in my "React + Flask" series. Make sure you read the first and second parts, as this part builds on the project built up to this point.

11 comments

2020-08-06T20:40:09Z

Video: How To Fix an Internal Server Error in Flask

In this beginner level video I explain what steps you need to take when you get an Internal Server Error in your Flask application.

2020-07-30T21:55:00Z

Fixing ALTER TABLE errors with Flask-Migrate and SQLite

If you've done any work with SQLite databases you surely know that this database is very limited in terms of making changes to the database schema. When working with a migration framework such as Flask-Migrate, it is common to end up with migration scripts that fail to upgrade or downgrade just because they need to remove or modify a column in a table, something that SQLite does not support.

In this article I'm going to discuss this limitation of the SQLite database, and show you a workaround that is specific to Flask-Migrate and Alembic.

14 comments