Tag Archives: Python

Packaging a Python app for Windows

This post gives an overview of the steps to package a Python application for distribution to Windows users in such a way that they don’t have to worry about installing Python, additional modules, and so on. This is significantly more convenient if they are not an experienced computer user, or if they simply have no need for Python otherwise. My particular approach is based around a combination of PyInstaller (to create a Windows executable) and NSIS (to create the installer). I’m not an expert in this and in fact this is my first experimentation outside of py2exe, so your own judgement should be applied. However, this technique worked for me and seems to have been used by people previously (for example, this Stack Overflow answer).

Continue reading

Plotting hardware stats via plotly

I recently signed up to try the beta of Plotly (@plotlygraphs) and decided to try it out by plotting the temperature, CPU load and RAM usage of a Raspberry Pi. Temperature, particularly, was of interest to me as I intend to stream video from the camera module, which makes heavy use of both the GPU and CPU. Plotly provide a Python API, which suits me perfectly.

Continue reading

OpenModelica and Python via CORBA (Part 3: Python wrapper)

Part 1 (getting started) | Part 2 (IDL compilation) | Part 3 (Python wrapper)

The final step in the process is to create a wrapper to allow easy programmatic control of OMC. I have posted a simple wrapper that provides the facilities required by the bouncing ball example here. It is not at the same level as the work by Ganeson et al. of course.

Continue reading

Python tips – List iteration and item removal

Occasionally my code is required to work through a list and delete items that are not wanted. Take, for example, the case of appending the data from two CSV files to create a new file with both sets of data in. It may be that they don’t have exactly the same columns in, but the common columns are the only ones you are interested in anyway. This is slightly more complex than simply ‘cat’ing the source files into the target file, so let’s assume you want to knock together a quick Python script to do it. I will first present the easy but wrong way to do it and then show how to adjust it to be correct.

Continue reading

Python tips – Simple sensor data handling 2

Last time, I described a simple way to take a series of data samples grouped by timestamp and reorganise them to group by the sensor from which they came. This time I will describe a simple way to read the data in from a file. Note that I will leave out error checking for the purpose of clarity, but this is always something which should be considered.

Continue reading

Python tips – Simple sensor data handling

Say you have a system which collects data from a group of sensors. Let’s have three sensors giving integer values for the sake of argument, but this doesn’t affect what I’m about to show you. The easiest way to store this data (assuming you want some history) is as a list of tuples, so if the data you get from the sensors is (1, 5, 3) at t=0, (2, 5, 3) at t=1, (2, 4, 4) at t=2, and (2, 5, 2) at t=3 then you would be storing

[(1, 5, 3), (2, 5, 3), (2, 4, 4), (2, 5, 2)]

Continue reading

Python tips – Reverse iteration

This post, the first in what will be a sporadic series, relates to a mistake I saw recently when looking at through some Python code. The author had used the following method to iterate backwards through a list:

for i in range(len(foo)):
    print foo[-i]

It wasn’t a print in the original, it was something else which would have been far harder to spot had it introduced subtle errors into the results of the code. Given Python’s ability to index from the end of a list using negative indices, this seems to be a suitable solution. The problem here is that foo[-0] isn’t the last item in the list, foo[-1] is. The correct code the author was looking for was:

for item in reversed(foo):
    print item

Or, if you need the index of the item as well:

for i,value in enumerate(reversed(foo)):
    print i,item