Tag Archives: python

Python: Creating a singleton (to control an Arduino)

A while ago I was writing a tool in python to communicate with a sensor on an Arduino. The idea was to have a class for the Arduino that would hold the connection and handle the reading and writing to the serial port. However that way I needed to have the object maintain the connection and the open COM port (this was on windows), but this meant that I could not have multiple objects. Why? Because you can only have one connection to a COM port. The solution – use a Singleton design pattern.

Continue reading Python: Creating a singleton (to control an Arduino)

Python : generating unittests on the fly

When creating python unit tests you need to create a method in a unittest class. The problem however is when you don’t know until the code is running what these tests will be. In my particular case I need to run a series of tests against a number of Solaris packages. But I won’t know what the packages will be in advance. Continue reading Python : generating unittests on the fly

Python: Searching for a string within a list – List comprehension

The simple way to search for a string in a list is just to use ‘if string in list’. eg:

Continue reading Python: Searching for a string within a list – List comprehension