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…

New Blog Post

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…

New Blog Post

How to use glob in Python

glob is a useful module included in Python. 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/ |– text_01.txt |– text_02.txt |– text_03.txt |– dir_b/ |– text_01.txt |– text_02.txt |– text_03.txt |– test_glob.py Find files in directory Let…

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

Python decorators a nice way to implement the decorator pattern. Perhaps you read the article about Python data classes and wondered how the @dataclass thingy worked. 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’) We can use a decorator to log entry and exit of a…

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…

Why you should use PyHamcrest in testing

  I’ve written about Software Testing Concepts  in general and also about pytest in particular. Now I want to introduce you to a nice little add-on which makes your testing -hopefully- easier. Hamcrest Hamcrest is around for a couple of years. I’ve used it together with JUnit back in the days. It had a bit…

How to use Python shutil

Sometimes you want to delete a folder in Python. You know that os.rmdir() can do that for you. But then you realize it can only remove empty folders. If you want to remove the content before deleting the folder you come up with: def remove_non_empty_dir(path_to_dir): if os.path.exists(path_to_dir): files_in_dir = os.listdir(path_to_dir) for file in files_in_dir: os.remove(os.path.join(path_to_dir,…

Logging in Python – Cheat Sheet

I’ve written a piece about logging Log4j2 for Kotlin and the motivation to use logging in Python is the same: Logging is a common good practice in software engineering. It enables you to monitor applications in production to gather information about crashes and other malfunctions for further analysis. It is the “little brother” of debugging…

Getting the file extension from a file path in Python

Determine the file extension os.path.splitext() is used to split the path name into a pair root and ext. e.g. C:\Users\memyselfandi\projects\file_extension_test\data.csv is split into C:\Users\memyselfandi\projects\file_extension_test\data and .csv Here is a working snippet for identifying CSV and XLS: import os if os.path.isfile(input_file): file_extension = os.path.splitext(input_file)[1].lower() if file_extension == “.csv”: logger.info(“It’s a CSV”) # do something with CSV…