Articles
A collection of technical articles, primarily on programming.
Pickling Python collections with non-built-in type keys and cycles
A presentation of various problems and an effective solution for pickling Python collections with non-built-in types as keys and cycles or self references. Applies to dictionaries, default dictionaries, ordered dictionaries, sets, and ordered sets, at all levels of the pickle and cPickle protocol, in Python 2 and 3.
Collatz sequence generation performance profiling in Clojure (Project Euler Problem 14)
I investigate the performance of several Collatz sequence generation algorithms through profiling Clojure code, inspired by Project Euler Problem 14 (no spoilers as I won’t show the solution number in this article, however, if you run the code you will get the solution as the return value). My approach focuses on the number of specific operations of interest rather than clock time (but clock figures are also discussed).
Automating tree and graph visualisation unit tests in Python and Django
We deal with how to systematically test visualisations of tree and graph data structures in a frontend browser (rendered as SVG in JavaScript) from within a Python backend test framework.
Django application import and missed class_prepared signals
Due to opaque application loading semantics within the Django i18n code, several model classes may be imported before a class_prepared
signal listener connects, resulting in missing the signal entirely. Specifically, connecting to this signal from an application which appears first in INSTALLED_APPS
, does not guarantee that it will execute when a class is first imported by any of the subsequent applications.
Propagating field changes in Django Model instance aliases
Django’s ORM creates multiple aliases for the same Model instance. In certain situations this may result in problematic behavior, as multiple operations on the same instance are interleaved, however, field access happens on different aliases of the instance, ultimately resulting in loss of updates.
Capturing invoking method information in Python
The following decorator will capture all available information regarding the method which invokes the decorated method (the invoked method).
Ordering on a field in the "through" Model of a recursive ManyToMany relation in Django
It is not possible to order a Django Model on a field of the Model acting as the intermediate through = ...
of a ManyToMany
relation, because queries will not return items in the correct order, and in addition will include duplicate items, even when using .distinct()
.
Duplicate items when ordering by Generic Relation in Django
When the optional ordering = ...
attribute of a Django Model’s Meta class contains a GenericRelation
from the Content Types framework, there is no way to eliminate duplicate items being returned, even when using .distinct()
(since .order_by(...)
is applied by the ORM only after the SQL SELECT DISTINCT
clause is built).