Category Archives: python

Integer to binary string in c#

Python has the terribly useful functionality of bin() to do this, but what about C#?

On the rare occasion I have to monkey around with bitwise operators, I always keep this handy. It has the added bonus of printing the leading zeros – I find this very useful when writing/debugging bitwise code, as it makes it easy to line up bitmasks etc. in the immediate window / console.

public static class BinaryPrinter
    public static string ToBinaryString(long _long)
        // have to do this or it won’t print the leading 0 values       
        return Convert.ToString(_long, 2).PadLeft(sizeof(long) * 8, ‘0’); 

Similarly, you could write a few of these (for all of the various inbuilt types) and chuck them in extension methods.

Python 3.4 and the three P’s: pyvenv, pip & pycharm


… is a nice little package manager for Python, but it was somewhat hamstrung by the clunky install procedure in Python 2.X and early versions of Python 3. With Python 2.x, you’d have to do something like use curl to get setuptools, then use easy install to get pip, then use pip to install packages. Happily, 3.4 now includes pip by default.


… is a virtual environment manager. Rather than installing packages in your site-packages directory (modifying your python environment – something that is a bit sucky when you have conflicting needs for different projects), developers have traditionally used virtualenv. In Python 3.X, you can use pyvenv instead and it seems to Just Work™.

All Together Now

On Windows, getting pip and pyvenv working together is pretty simple, but I had a little trouble finding the bare minimum “getting started” documentation, so here’s a version for frantic googlers.

First, set up a virtual environment using pyvenv:

Open a command prompt in the directory you’d like to create the virtual environment, then run the following command:

> C:\your_python_dir\python C:\your_python_dir\Tools\Scripts\pyvenv my_env_name

Then, run:

> my_env_name\Scripts\activate.bat

This will give you a command prompt with the environment name prepended to signify that it’s active.

At this point, you can start installing stuff with pip!

> pip install beautifulsoup4


Not bad. Much simpler than in olden times.

A bonus – PyCharm & pyvenv

PyCharm is my IDE of choice for python editing and it comes with a lot of niceties, including allowing you to associate projects with specific python interpreters. Even better, PyCharm allows you to associate a project with a particular virtual environment, too!

I’m running v3.0 of PyCharm at home, so I can’t speak for earlier versions. To associate a virtual environment with your project, click File –> Settings, then find “Python Interpreters” under “Project Interpreter”.


Click the + at the top right hand pane of the window, then select “Local”. Navigate to the virtual env directory you just created, then find the Python executable under the scripts dir.


You can now associate this virtual environment with your project. PyCharm automatically switches it up when you load your projects. No more cross contamination of packages!

Python data-driven testing, ddt and @unpack

Several eons ago, I wrote a blog post about using the python ddt package. My only criticism was that you had to manually pack/unpack the test case arguments.

Happily, the developers of ddt were nice enough to add an @unpack decorator! You can now provide lists/tuples/dictionaries and, via the magic of @unpack, the list will be split into method arguments.

Documentation is here:

A R#/Visual Studio style keymap for PyCharm

PyCharm is a lovely little IDE for Python by the splendid JetBrains, but the built-in Visual Studio keymap is not much like the default VS/R# settings I’m accustomed to. I’ve rustled up a quick version that restores all of the easy to remember refactoring key combinations (Ctrl-Shift-R, Ctrl-R, Action Key etc.) along with some of the common navigation ones (Ctrl, comma for recent files and so forth).

There’s a few things missing and I’ve unbound some of the PyCharm functionality as it clashed with the R# style keymap, so your mileage may vary. Give it a try, though!

You can download it here:

Data-Driven Testing with Python

Hello tests.  It’s been a while since I blogged about automated testing, so it’s nice to welcome an old friend back to the fold.  I’ve missed you.  I’ve recently started programming in python.  I say programming, but what I mean by that is ‘hopelessly jabbing at the keyboard and gawping as my lovingly written code explodes on being invoked’.  While I blub.

Python is a dynamic programming language.  It’s pretty dynamic in its ability to detonate in various ways, too.  There’s no compiler to sanity check my elementary mistakes like typos, or dotting onto a method that doesn’t exist.  Or accidentally comparing functions instead of values.  Or… well, you get the gist.

Anyzoom, the fundamentals of unit testing in python are very simple.  I can’t be bothered detailing how to use unittest or a test runner, as there’s nine zillion resources out there on it already. 

A Simple Setup

We’re using unittest as the framework, Nose as the runner and TeamCity Nose integration.  It worked very nicely out of the box, so I can’t complain.  Writing tests is simple, but then I wanted to make a data-driven test and…

Read more »