Improving your python code quality with automatic architecture contract validation

If you care about code quality you are probably already familiar with linting-tools like flake8 and pylint. These tools automatically check your python code for common code-smells and anti-patterns.

Another excellent tool in this category is import-linter. Import-linter can check that your dependency tree adheres to your chosen architecture. For example, in a Django project, it would be bad practice if your model would import a view or a form. With import-linter, you can specify this as a layer-contract:

[Read More]

MembershipNerd Day 6: Signing up without a password

I chose to use the third-party library django-allauth to handle user-authentication because that library is mature and well-maintained, as far as I can tell. However, since the main focus of this library seems to be providing integrations for third-party oauth-providers, there is no obvious way to handle my first use case: Provide a way for users to sign-up with only an email – no password, no third-party-login, nothing else required.

In a previous project I had solved a similar problem by overriding the library’s DefaultAccountAdapter-class, which seems to be main integration-point for this kind of customization. But today I found a way that is much more light-weight and straight-forward.

[Read More]

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xca in position 0: invalid continuation byte

tl;dr: You might need to change your Python Interpreter in the Pycharm settings. In my case (using pipenv), it was pointing to /Library/.../bin/Python with an uppercase P. Opening the folder and checking, I found that the file or symlink was actually python with a lowercase p. Changing it from .../Python to .../python in the Pycharm settings fixed this problem. (I had to restart Pycharm afterwards.) Screenshots below.

The longer version

While working on MembershipNerd today, I encountered a very annoying problem: I wasn’t able to run the server in Pycharm’s debug mode. This wasn’t happening outside of debugging, but when trying to run the debugger, it got the following errors:

[Read More]

Building MembershipNerd - Day 3

Today I replaced MembershipNerd’s default django user model with a custom user model, in order to authenticate user’s by their email, not some username. The reason for this should be obvious: The primary purpose of MembershipNerd is to allow people to sign-up for updates via email. So we definitely need their email address. Anything else is optional.

The django-improved-user library that I am using provides the following user attributes (besides the django permissions, groups etc.):

[Read More]

Professional Feature Flags For Django

Motivation

Any non-trivial project I’ve ever worked on sooner or later had a need for feature flags (also called feature toggles). They can be used for awesome stuff but they can also be used for terrible things. It’s really up to you and your team.

The goal

At the end of this tutorial you will know

  • how to implement feature flags in django
  • how to use a third-party feature-flag-provider to manage your feature-flags
  • have an idea how this can be used to roll-out new features to a subset of your users (e.g. only staff-members)

Getting the project source-code

The example-project can be found on github. Here are the steps I used to create the initial commit. I assume you have pipenv installed (and, of course, Python 3):

[Read More]

Using `pipenv` - The Basics

Just a couple of tiny recipes for pipenv

How to migrate an existing project from requirements.txt to a pipenv Pipfile

This will create a new virtual environment for your current folder and install all requirements from requirements.txt. Put another way: This will “convert” your requirements.txt into a Pipfile:

Run this from the project folder containing the requirements.txt file.

$ touch Pipfile
$ pipenv install -r requirements.txt

Output:

Creating a Pipfile for this project…
Requirements file provided! Importing into Pipfile…
Pipfile.lock not found, creating…
Locking [dev-packages] dependencies…
Locking [packages] dependencies…
✔ Success! 
Updated Pipfile.lock (037725)!
Installing dependencies from Pipfile.lock (037725)…
  🐍   ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 42/42 — 00:00:07
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.

Next step: verify that the generated Pipfile contains all your requirements.

[Read More]