RAII in C++ and Python for custom types

Resource Acquisition Is Initialization (RAII) is a programming idiom that describes how resource acquisition and deallocation should be tied to object lifetime. Wikipedia demonstrates how the C++ standard library implements this idiom using the types std::lock_guard<> and std::ofstream as examples. But is it really necessary to implement this idiom in every type you create? Can’t…

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…

Introduction to VBA

A horrifying amount of business is still driven by Microsoft Excel. Need some stats? Excel. Need some number crunching? Excel Most of the time you can work with formulas in your sheet and you will be fine. But sometimes you need more power: Time to level up your VBA skills History VBA stands for “Visual…

Reading files with Kotlin

Before Kotlin BufferedReader br = new BufferedReader(new FileReader(“data.txt”)); try { StringBuilder sb = new StringBuilder(); String line = br.readLine(); while (line != null) { sb.append(line); sb.append(System.lineSeparator()); line = br.readLine(); } String content = sb.toString(); } finally { br.close(); } This is a code sample reading a files content in plain old Java. What a mess…

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 write unit tests in Kotlin

Dependency First you need to add the following dependency to your build.gradle file dependencies { testImplementation ‘org.jetbrains.kotlin:kotlin-test’ As a second step you need to add the task test { useJUnitPlatform() } Implementation When you are familiar with JUnit you may recognized the @Test annotation. Assertions work pretty much the same. You can choose from a…