Home
Alexandra Zaharia
Cancel

Bitwise nuggets: XOR swap

XOR swap is a kind of in-place swap that only uses XOR operations. No temporary variable nor addition/subtraction operation is needed. However, it only works for integers. Reminder: XOR (short for...

std::map with pointers as keys

When the keys in a std::map are pointers, find() will fail unless the search query is one of the actual pointers in the map. However, in most cases this might not be exactly what we want to achieve...

Const and pointers in C++

The possible uses of the const qualifier with C++ pointers: pointer to const variable const pointer to variable const pointer to const variable All examples below will be using the same st...

Remove every occurrence of an item from a list in Python

A problem that I come across quite often is having to remove every occurrence of a given item from a Python list. While built-in mutable types have a remove() method, my_list.remove(x) only removes...

How to return a result from a Python thread

The problem Suppose you have a Python thread that runs your target function. Simple scenario: That target function returns a result that you want to retrieve. A more advanced scenario: You w...

Connect a bluetooth headset automatically in Linux

I can never remember how to automatically connect to my bluetooth headset so here is a very short recipe. Make sure your bluetooth headphones are already paired and trusted. Then proceed according...

How to stop a Python thread cleanly

Suppose a Python thread needs to be stopped cleanly (it might need to perform cleanup). For illustration, we will take a very simple program in with a single “worker” thread that displays a messag...

Kill a Python subprocess and its children when a timeout is reached

Suppose a Python script needs to launch an external command. This can be done using the subprocess module in one of two ways: either use the “convenience” function subprocess.run() or use the...

Stopping a Python systemd service cleanly

Suppose you are running a Python systemd service that opens some file descriptors (these can be regular files, named pipes, sockets, and so on). When the service is stopped, it needs to finish clea...

How to fix Python logger printing the same entry multiple times

The previous post explained how to get a simple colored formatter for your custom logger using the Python logging module. This one explains why a logger may print the same record multiple times, an...