Kaizen Today I Learned by Ville Säävuori

Posts tagged python

Using Spatialite With Django on MacOS

I’ve been working on several GeoDjango projects latey and one thing common with most of them is the difficulty of setting up Spatialite on CI and macOS. The usual error message from pytest is: django.core.exceptions.ImproperlyConfigured: SpatiaLite requires SQLite to be configured to allow extension loading. The error happens because Python is built with extensions loading disabled.

· Read the full article →

Silencing Errors in Production With a Sentry Context Manager

I found a really neat usage for Python context managers in the codebase of my new job over at Custobar. We’re running sometimes large and long-running ElasticSearch indexing tasks with Celery, and exceptions inside these tasks would sometimes clog up our Sentry. Someone smarter than me wrote a context manager that would queue any exceptions and do some magic before handing them to Sentry.

· Read the full article →

Installing Python With Shared Library on macOS

I needed a Python version with shared library in order to use PyO3 to run Python in Tauri to fix this error: “error: The auto-initialize feature is enabled, but your python installation only supports embedding the Python interpreter statically. If you are attempting to run tests, or a binary which is okay to link dynamically, install a Python distribution which ships with the Python shared library.

· Read the full article →

Using And Building Legacy Python 2 Libraries on macOS Monterey

Getting an old Python project working on a M1 Mac running macOS Monterey was somewhat of an battle. M1 and old Docker images I first fixed the Dockerfile to the point where it would run MySQL and Redis properly. The M1 architecture is still a bit of a hurdle with some Docker images, and especially the older ones.

· Read the full article →

Setting Up macOS for Web Development

Getting your development environment running and configured has become much easier over time but it’s still a hassle to get everything set up and configured from scratch. These notes are a continuously evolving task list for my personal setup. Applications Install Docker desktop, configure it as needed and remember to authenticate it.

· Read the full article →

Encrypted Data Archives With Django And Backblaze B2

I needed to implement user data export feature to a Django project. The static and user media is handled by Backblaze B2 cloud storage (similar to Amazon S3) which also supports server side encryption that allows your data to be encrypted at rest. Usually when working with filed in Django you want to use the native storage api and storage backends.

· Read the full article →

Migrating to a Custom Django User Model Mid-Project

Using a custom user model in a Django project is almost always a good idea. The documentation for adding one when starting a project is good and clear but switching to a custom model mid-project is not that easy. This blog post explains one way, and ticket #25313 has several helpful comments.

· Read the full article →

Pytest Cheat Sheet

Pytest Official Docs Running Fail fast / stop after first failure: pytest -x Run from module or directory: pytest dir/tests/footest.py, pytest dir/ Run specific markers: pytest -m marker (mark with @pytest.mark.mymarker) Drop to pdb on failure: pytest --pdb (set breakpoint w/ breakpoint()) Recreate database using pytest-django: --create-db Configuration pytest.ini conftest.

· Read the full article →

Python SSL Issues On macOS

I was bumping to SSL issues when creating new virtualenvs with pyenv. Found tons of similar issues but the main problem for me wasn’t a faulty pyenv or SSL installation but an old version of a specific pyenv Python version. As soon as I installed a new Python version, everything worked fine.

· Read the full article →

Casting Types With MyPy

Typing in Python is not yet very mature. This small example displays several issues: if request.user.is_authenticated: return JsonResponse( { "user": { "uid": request.user.uid, "username": request.user.username, "email": request.user.email, } } ) The original type of request.user is Union[AbstractBaseUser, AnonymousUser] (which itself is already wrong; the user is actually a Django model that inherits from AbstractBaseUser, not AbstractBaseUser).

· Read the full article →