Python offers quite a few built-in decorators that can be used to give methods of classes certain superpowers. @property turning method into a read-only field-like attribute is a classic example. Or @classmethod – a method that receives a class as a first argument, not an instance. Fun fact, this kind of method is usually called […]
Category: python

How to implement a service layer in Django + Rest Framework
From this article you will learn: what is the service layer? the problem solved by a service layer how to refactor to services from ModelSerializers What is the service layer? A service layer is a set of classes or functions, called services, that together form an API for a single package or application. We can […]

Encapsulation is your friend, also in Python
What is encapsulation? Encapsulation is an act of deliberate limiting access to certain software components. The most common usage is to hide certain attributes of objects from other objects that use it. The most vivid example is the usage of so-called access modifiers, private and protected in languages that support it, for example, PHP: Running […]

How to patch in Python?
What is (monkey-)patching in Python? (monkey-) patching is a technique for changing code behaviour without altering its source. It is done in runtime, usually by overriding attributes of existing objects. An object can be an instance of some sort, a class or even a module. The technique is most commonly (ab)used for tests when we […]

Beware of chicken testing! (or mocks overuse)
Need for mocking Dealing with problematic dependencies is an indispensable part of software testing. Often, we cannot or do not want to rely on 3rd party service/network communication/hard drive etc., especially in unit-tests. The reasons vary; external dependencies are usually slow, fallible and difficult to put into the expected state before the actual test. Consider […]

Stop naming your python modules “utils”
Imagine the following situation: there is a software developer that is either adding new code or refactoring existing one by extracting a class/function. They have to place code somewhere, but it does not seem to fit anywhere. So what does a developer do? They create a new module – utils.py. Why utils is a terrible […]
When to use metaclasses in Python: 5 interesting use cases
Metaclasses are mentioned among the most advances features of Python. Knowing how to write one is perceived like having a Python black belt. But are they useful at all outside job interviews or conference talks? Let’s find out! This article will show you 5 practical applications of metaclasses. What metaclasses are – quick recap Assuming […]
What is Celery beat and how to use it – part 2, patterns and caveats
Celery beat is a nice Celery’s add-on for automatic scheduling periodic tasks (e.g. every hour). For more basic information, see part 1 – What is Celery beat and how to use it. In this part, we’re gonna talk about common applications of Celery beat, reoccurring patterns and pitfalls waiting for you. Ensuring a task is […]
When to use the Clean Architecture?
Enthusiasm, doubt, opposition There are few possible reactions after learning about the Clean Architecture or Hexagonal Architecture (AKA Ports & Adapters) or even merely innocent service layer in Django. Some developers are enthusiastic and try to apply these techniques immediately, some are hesitant, full of doubts. The rest is strongly opposing, declaring openly this is […]
How to mock in Python? – (almost) definitive guide
What is a mock? Mock is a category of so-called test doubles – objects that mimic the behaviour of other objects. They are meant to be used in tests to replace real implementation that for some reason cannot be used (.e.g because they cause side effects, like transferring funds or launching nukes). Mocks are used […]