How to Combine Two Python Decorators
Imagine you have some Django views using the same two decorators:
(All tags.)
Imagine you have some Django views using the same two decorators:
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!
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.
I’ve just moved all my open source repositories from using plain pip to python -m pip, in test scripts and documentation.
Here’s a little Python problem I encountered recently.
SyntaxWarning: "is" with a literalTake this reasonable-looking code:
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.
Bruno Oliveira, known for his work on the pytest project, tweeted this thread last July:
pytest is quickly becoming the “standard” Python testing framework. However it can be overwhelming to new users.
Take this code:
datetime modulePython’s datetime module risks a whole bunch of name confusion:
On several Django projects I’ve worked on, there has been a requirement for performing database modifications beyond Django migrations. For example:
One thing I often ask for in code review is conversion of tuples to lists. For example, imagine we had this Django admin class:
If the title worried you about my mental state, you can relax. It’s about the Python code formatter Black!
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.
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).
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.)
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.
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.
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.
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.
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.
This is a problem I've seen several times over the past few years.
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.
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.