157 Posts Tagged ‘python’ (Page 6)

(All tags.)


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...

Solving Algorithmic Problems in Python with pytest

I occasionally enjoy solving algorithmic problems, for example Project Euler or The Advent of Code. I’ve been doing them since I was a pimply PHP-slinging teenager, competing with my peers across the UK in the British Informatics Olympiad. It was terribly nerdy and terribly fun.

Read more...

What I Learned at PyCon Namibia 2019

I was at PyCon Namibia in Windhoek from the 19th to 21st of February, and had an amazing time! PyCon Namibia is one of the longest running PyCons in Africa, this being its fifth edition in as many years.

Read more...

Dropping Python 2 Support From Open Source Projects

I’ve recently dropped Python 2 support from most of the open source projects I maintain. Python 2 support ends 2020-01-01 (see pythonclock.org), and many major projects have signed the Python 3 Statement that declares that they will remove Python 2 support before, so this year is crunch time for migration.

Read more...

‘pip install’ From a Git Repository

It’s quite common to want to pip install a version of a package that hasn’t been released to PyPI, but is available on its Git repository host, such as GitHub. If the package is pure Python or has a relatively simple build process, you can normally install it directly via Git.

Read more...

Making My Own Black Mirror Bandersnatch Easter Egg

The TV program Black Mirror released an interactive choose-your-own-adventure film on Netflix called Bandersnatch over the holiday period. I enjoyed playing through it and finding the various endings, especially since I recently played The Stanley Parable which is in the same genre but with more humour. Bandersnatch has many endings and easter eggs, so naturally there’s a whole subreddit dedicated to tracking them down. The most impressive is an embedded Spectrum tape noise containing a QR code that leads to a real ZX Spectrum game made in 2018 by developer Matt Wescott.

Read more...

Using boto3? Think pagination!

This is a problem I've seen several times over the past few years.

Read more...

pytest-randomly history

My plugin pytest-randomly was recently moved into the pytest-dev organization on GitHub, making it a bit “more official” as a pytest plugin. Thanks to Bruno Oliveira for suggesting it, Florian Bruhin and Bruno for approving it on the pytest-dev mailing list, and Gordon Wrigley for helping with its development.

Read more...

Optimizing the construction of Django QuerySets

Django’s ORM is normally fast enough as-is, but if you’ve ever profiled a high traffic view with a fairly complicated query, you might have found that constructing QuerySet can take a noticeable portion of your request time. For example, I once found a query on the front page of the site I was working on that took 1ms to construct and 1ms for the database to answer. With a performance budget of 100ms, that was 1% gone on computing the exactly same SQL.

Read more...