Home
Alexandra Zaharia
Cancel

Bitwise nuggets: flip bit to win

Given an integer, suppose we can flip exactly one of its bits from 0 to 1. We need to determine the longest sequence of 1s that can be obtained. Let us first see some examples: For number 183 (...

Set up an automatic SSH login on Linux

Suppose you want to connect to/from a remote Linux machine (that might as well be a virtual machine) via SSH, but without having to type in the password each time. In the following, I will be refe...

Use cases for Python environment variables

Today’s post focuses on environment variables in Python. They are one of several possible mechanisms for setting various configuration parameters. We can: read environment variables (through os...

The overloaded insertion operator in C++ exceptions

The << operator, when applied to streams, is called the insertion operator. (Likewise, the extraction operator is >>). If we overload it for a class (as non-member), we can output insta...

Merge Mercurial branch into default

In a previous post I wrote a quick recipe for doing a merge in Mercurial when pulling from the remote repository. This post is another quick Mercurial merge recipe, but this time for merging a dev...

Python color enum class for ANSI color codes

When you want to print something with color and/or styling in a terminal, you can use one of the existing modules, such as colorama or sty. They are well-documented, flexible and easy to use. Enum...

Custom logger in Python for stdout and/or file log

The Python logging module may be used to build your own custom logger featuring special functionality according to your use case, such as: Adding both console and file handlers (i.e. logging to...

Python configuration and data classes

I stumbled upon an interesting article in which the author describes best practices for working with configuration files in Python. One of the points he makes is that configuration settings should ...

Standalone Python script to run other Python scripts

In the previous post we’ve seen how to use setuptools to package a Python project along with a standalone executable that can be invoked on the command-line, system-wide (or rather environment-wide...

Distribute a Python package with a standalone script using setuptools

Suppose you’ve written a Python package that you want to be able to pip install locally. Additionally, you also want to be able to run one of the scripts in the package via its name, without the .p...