What are Python properties?

In “What’s the difference between classmethod and staticmethod in Python?” we looked at static and class methods. The third big decorator is the @property decorator import pytest class Person: def __init__(self, name): self._name = name @property def name(self): print(‘Getting name’) return self._name @name.setter def name(self, value): print(f’Setting name to {value}’) self._name = value @name.deleter def…

How to let test cases run in a predefined order

Motivation pytest executes tests in a randomly order. Sometimes it can be useful to have a predefined order in which test cases can run. pytest has a neat plugin for this specific use case. Installation pip install pytest-order Usage import pytest @pytest.mark.order(1) def test_foo(): assert True @pytest.mark.order(2) def test_bar(): assert True @pytest.mark.order(3) def test_foobar(): assert…

How to manipulate nested dictionaries with dotty

Motivation Sometimes a task which should be easy can be hard to accomplish in practice. Manipulating nested dictionaries in Python seems to be such a task. Without dotty Accessing nested dictionaries looks like this: data = { ‘a’: { ‘b’: { ‘c’: ‘d’ } } } assert data[‘a’][‘b’][‘c’] == ‘d’ You have to chain the…

How to store sensible data in a .env file

Motivation Whether you need credentials to log into a system or some configuration parameters for your application, the .env concept might help you. Installation pip install python-dotenv Usage .env file Create an .env file in your project root folder You should not share this file or commit/push it to your version control. You should add…

How to implement Python Decorators – Part 2

When you’ve finished reading How to implement Python Decorators you might wonder if it is possible to hand over some parameters to the decorator function. And yes that is possible and can come in quite handy. For a Flask project I wrote a decorator to manage authorization of endpoints. I wanted to grant access to…

How to use glob in Python

Motivation Sometimes you need to find files across multiple directories and / or directory hierarchies according to certain pattern. E.g. find all image files with the jpg extension You can use it to find files in a directory by using wildcard semantics. Let’s say we have a directory structure like this: glob_test/ |– dir_a/ |–…

How to write your __init__.py

What should you put into your __init__.py? Perhaps you already know that a python package is a directory which contains a __init__.py file In this article we will solve the mystery around the __init__.py For this article let’s assume the following project structure: Empty init file Sometimes doing nothing isn’t so bad at all. You…

How to implement Python Decorators

Motivation Python decorators are a nice way to implement the decorator pattern. Perhaps you read the article about Python data classes and wondered how the @dataclass thingy worked. Example Let’s say we created the following function: def un_decorated_function(): print(‘–> Starting un_decorated_function’) print(‘Inside un_decorated_function’) print(‘<– un_decorated_function finished’) This works but violates the DRY principle if repeated…

How to structure your Python project

This is not a “my way or the highway” kind of advice. Just a bunch of good practices. A starting point if you are new to Python. You will likely inspect and adapt your own project structure over time. The bare minimum This is a setup I recommend when just want to start with a…