Mikael Lövqvist

A use case for __new__ in python

by Mikael Lövqvist on


Sometimes in python you may need to make sure an instance is of the right class without having to check it every time.
One could write a function that would check some condition and either call the class to create a new instance or simply return the one that was already given as the argument. But then you need that function and, on top of that, the actual class definition and that gets a bit ugly in the namespace.

By using __new__ we can put that condition in there together with the code we would put in __init__ to get around having to call __init__ with the same arguments as __new__ was called.

Note that this example is written for python 3

Useful links:

Mikael Lövqvist

Text manipulation in python

by Mikael Lövqvist on

I am currently working on a project were I need to be able to handle text in a trivial way.
In order to do this I made a simple construct in python for this, you can have a look at the implementation here text_manipulation.py

Here follows an example of how this could be used:

variable T is first assigned indented_block to make things easier to type.
Then a program template is created where we have two markers, ¶includes and ¶main.
We also create a template for c functions named function.

In the example we replace ¶includes with a list of T("#include <file>") replacing file with the actual filenames. Then we define the body of the main function. We finally use the replace_kw function that will perform several replacements, one for each keyword argument.
Using the function template we can create out main function and also match indent level.

And here is the output of the program:

Mikael Lövqvist

Plotting pretty ping times

by Mikael Lövqvist on

The other day my ISP wanted me to plot ping times using some software that would only run on Windows 7 and up which means it will not work in the current version of Wine. I decided to use gnuplot instead.

First I had to extract the ping times from the ping command

This script reads stdin, finds the time=NN ms and outputs the UNIX epoch time followed by the ping time on each line.

I used the following command
$ ping ping.sunet.se -i 5 | python ~/timestamp_ping.py | tee ~/sunet_ping_ts.log

ping will ping ping.sunet.se in intervals of 5 seconds
python (python3 on my system) is called to extract the ping times as they come in. The sys.stdout.flush command is used so that each line will be outputted as it is processed.
tee is saving the output to the log-file and also displaying the lines as they are processed.

After a few hours I had a nice data set of a few thousand ping times that I could plot using this gnuplot script

Note on line 22 I add 2*3600 to the time to adjust it to my local timezone. There might be a better way to do this but this was the quickest solution I could find for now.

And here is the result:

Plot of ping times

Useful links:

ping manpage, http://linux.die.net/man/8/ping
tee manpage, http://linux.die.net/man/1/tee
gnuplot demos, http://www.gnuplot.info/screenshots/index.html#demos
python3 documentation, https://docs.python.org/3/

Mikael Lövqvist

Approximate model of sg90 servo

by Mikael Lövqvist on

I wanted to do some mashups with sg90 servos so I decided to make an approximate model using openscad.
The model was later exported to stl and imported into blender.

When using the difference function it is important to make sure that the surfaces of the operation are not overlapping because this will yield artifacts. So if you want to make a hole using a cylinder trough a cube with the side 10 mm, the length of the cylinder should be > 10 mm to make sure no overlapping occurs.

When importing stl to blender there is no per vertex normals defined so it is a good idea to use the edge split modifier which will fix this automagically in most cases.


The cross type fastener has been updated because I made a mistake with it where the wide part was too short.


Useful links:


Mikael Lövqvist

Website up and running

by Mikael Lövqvist on

Welcome to LED Alert Hackerspace!

The new server is installed, website it up and running and so far some photos have been added to it.
We are pretty sparse with information for now but we will make effort to add information about ourselves soon.

What we aim to have here is information about projects we have done, will do and wish we could do.
Of course we will also share code, tips, schematics, ideas and miscellaneous things here (hopefully) soon.