165 Posts Tagged ‘python’ (Page 6)

(All tags.)


Python: fix SyntaxWarning: '<type>' object is not callable

Take this code, which we want to return a list of shopping items with desired quantities:

Read more...

How to Unit Test a Django Form

Django’s test client is really useful for writing integration tests for your project. It’s great because it has a simple API for testing your application similarly to how a web browser would interact with it. Unfortunately it can be slow, because each call creates a request, passes it through all your middleware, view, maybe a template, then the response comes back through the same layers.

Read more...

Avoid Hardcoding IDs in Your Tests

This is a test anti-pattern I’ve seen creep in on many Django test suites. I know of several large projects where it became a major undertaking to undo it. The good news is it’s easy to avoid adding it when you first write your tests. 🙂

Read more...

Introducing time-machine, a New Python Library for Mocking the Current Time

Whilst writing Speed Up Your Django Tests, I wanted to add a section about mocking the current time. I knew of two libraries for such mocking, but I found it hard to pick one to recommend due to the trade-offs in each. So I delayed adding that section and shaved a rather large yak by writing a third library.

Read more...

The Fast Way to Test Django transaction.on_commit() Callbacks

Django’s transaction.on_commit() hook is useful for running tasks that rely on changes in the current database transaction. The database connection enqueues callback functions passed to on_commit, and executes the callbacks after the current transaction commits. If the transaction is rolled back, the callbacks are discarded. This means they act if-and-when the final version of the data is visible to other database connections.

Read more...

“Speed Up Your Django Tests” is Out Now

My previously announced book “Speed Up Your Django Tests” is out now on Gumroad. I’ve been writing since the 3rd March, so it’s quite a relief to have launched it.

Read more...

How to Check the Running Django Command

It’s occasionally useful to be able to tell which Django manage.py command is being run, in a code path that otherwise has no way of telling. For example, in Speed Up Your Django Tests, I describe how to modify manage.py to default use a test settings file when the test command is run.

Read more...

Book Announcement: Speed Up Your Django Tests

At the start of March I started writing a blog post called “How to Speed Up Your Django Tests”. Before I knew it, the outline alone was 4,000 words! I realized writing it up would be a major undertaking. As lockdown arrived, I found the time to write it all up.

Read more...

How to Combine Two Python Decorators

Imagine you have some Django views using the same two decorators:

Read more...

Setting Python’s Decimal Context for All Threads

Python’s decimal module has concept of a “context”. This defines the default precision of new Decimals, how rounding works, and lots of other behaviour. Maths gets complicated!

Read more...

Use Pathlib in Your Django Settings File

Django’s default settings file has always included a BASE_DIR pseudo-setting. I call it a “pseudo-setting” since it’s not read by Django itself. But it’s useful for configuring path-based settings, it is mentioned in the documentation, and some third party packages use it.

Read more...

Use ‘python -m pip’ Everywhere

I’ve just moved all my open source repositories from using plain pip to python -m pip, in test scripts and documentation.

Read more...

Entering a Flaky Context Manager in Python

Here’s a little Python problem I encountered recently.

Read more...

Python: fix SyntaxWarning: "is" with a literal

Take this reasonable-looking code:

Read more...

Simplify Your If Statements That Return Booleans

Here’s a hint I have found myself repeating in code review. I learnt this from my university lecturer Tony Field, who probably repeated it in every lecture! He was teaching us Haskell, but it applies to Python, and most other programming languages.

Read more...

Feature Checking versus Version Checking

Bruno Oliveira, known for his work on the pytest project, tweeted this thread last July:

Read more...

My Most Used pytest Commandline Flags

pytest is quickly becoming the “standard” Python testing framework. However it can be overwhelming to new users.

Read more...

Limit Your Try Clauses in Python

Take this code:

Read more...

Python: how I import the datetime module

Python’s datetime module risks a whole bunch of name confusion:

Read more...

How to Add Database Modifications Beyond Migrations to Your Django Project

On several Django projects I’ve worked on, there has been a requirement for performing database modifications beyond Django migrations. For example:

Read more...

Tuples versus Lists in Python

One thing I often ask for in code review is conversion of tuples to lists. For example, imagine we had this Django admin class:

Read more...

All Is Turned to Black

If the title worried you about my mental state, you can relax. It’s about the Python code formatter Black!

Read more...

PyLondinium 2019 Notes

PyLondinium ran for the second time last weekend. It’s a three day conference from Friday to Sunday, held in central London at the Bloomberg office.

Read more...

The Simplest WSGI Middleware

My library apig-wsgi bridges between AWS API Gateway’s JSON format for HTTP requests and Python WSGI applications. Recently Théophile Chevalier opened an issue requesting the library add an extra WSGI environ variable. I closed it by pointing out that it’s not much code to add a custom WSGI middleware to do so (plus the exact key is a bit out of scope for the library).

Read more...

Testing Boto3 with pytest Fixtures

This is a recipe I’ve used on a number of projects. It combines pytest fixtures with Botocore’s Stubber for an easy testing experience of code using Boto3. (Botocore is the library behind Boto3.)

Read more...