Cross-compile Python packages with Docker

Cross-compiling is the action of building a package or a binary for a different system thatn the current used for the compilation process; for example compiling ARM binaries on a x86 architecture. In this post I’m going to cross-compile Python packages for a specific Linux distribution using Docker as a...


Create a plugin for Google Protocol Buffer

Google’s Protocol Buffer is a library to encode and decode messages in a binary format optimised for compactness and portability between different platforms. At the moment the core library can generate code for C/C++, Java and Python but additional languages can be implemented by writing a plugin for the Protobuf’s...


Restricting npm semver rules

The npm package manager uses semver to declare the version of the external dependancies of your package in a more flexible way. Unfortunately the current version of npm by default uses the caret ^ as a default prefix for package’s versions which means the required package must have the same...


HTML augmentation with React

These days is all about React. Most of the people are leaving the bandwagon of AngularJS in favour of the React’s bandwagon for a lot of different technical and not-technical reasons (#TROLLING they switch because is the new JavaScript Toy of the Year). But today and here I’ll not talk...


Customised Git flow

At my team in Plentific we needed a workflow for developing, test and deploying our projects, so I wrote a customised Git workflow to be as simple as possible but to be able to cover all our needs. The document is available on my Github account ready to be forked....


Hide JavaScript code into images

In this post I’m going to explain how it’s possible to hide JavaScript code into a PNG image and executing it into the browser without leaving any trace in the HTML document. This kind of hiding technique can be used to load JavaScript code into the browser bypassing the common...


Out of Memory on AWS instance

Do you have a micro instance on Amazon Web Services and you run out of RAM and you don’t want to upgrade to a small one? Or do you need just extra RAM to compile Numpy, Scipy and Pandas only once? Here a simple and dirty solution. AWS instances comes...


Notes about JavaScript code optimisation

A couple of days ago YouTube suggested me this video about optimising JavaScript games on Google Chrome. The video is a talk from the GDC 2012 so it’s pretty old (2 years in IT’s time is really a lot) but the content of the talk is still valid today and...


Vim

New year, new programming languages, new technologies and new IDE. I finally switched to Vim as a primary editor for Python, JavaScript, HTML and CSS. The learning curve of Vim is not so steep as you think, the main roadblock is not the editor itself but the correct configuration of...


Git-rebase no jutsu

In a DVCS rebasing is the action performed by re-applying all the commits done on the current active branch since it diverged from a remote branch. That is, all the commits are taken away and re-applied over the latest version of the remote branch. This will rewrite the history of...


Restore MySQL database with progress bar

Restoring a copy of a medium/big database can be a boring task, it can be worse when the process takes some times (let’s say more than 10 minutes) and you don’t have any idea of the state of the restore process and/or it’s ETA. On Unix you can monitor the...


Simple hardening of the Python interpreter

For companies protecting their source code form reverse engineering is between very to vitally important. Using languages based on virtual machines easily expose the bytecode to simple un-compile techniques which revert the op-codes back to human readable code. One solution involves obfuscating the source code but with Python this is...


How to NOT write an exception handler

Exception handlers are important but more important is to code them in the correct way. Today I’ll show you an example of how to NOT write an exception handler. This example is taken from a real piece of code I found laying around in the code base, so “True story...


Disassembly C code for fun – Part 9: arrays

Arrays are a list of fixed number of elements of the same type stored into a continuos block of memory. String are a type of array with a fixed number of char with a NULL as the last element of the array. The assembly generated by an array will be...


Mocking objects in unit tests

Unit testing is important to ensure the stability and the correctness of the code throughout all lifespan of the project. However, testing some code requires a connection to external services (like SMTP, web, etc.) which in all cases must be avoided to simplify the test and development environment and to...


Disassembly C code for fun – Part 8: strings

Following the last post about the buffer overflow protection, today we finally talk about strings. Strings We’ll start back from the last piece of code used in the previous post: #include <stdio.h> int main() { char text[] = "hello world"; return 0; } and its disassembly: 0x0000000100000ed0 <main+0>: push %rbp...


Detect if a number is power of 2

Easy post today: simple method to detect if a number is a power of 2 in O(1) complexity. Yes, O(1), no naive loops involved :-). The code is very simple: bool powerof2(unsigned int n) { return n > 0 & !(n & (n - 1)); } The code looks cryptic,...


Cross-compile 64-bit code on 32-bit machine

Now that I have a spare machine where I can do all my experiments, I can start again to write posts about Disassembly C code for fun but, wait, my current hardware is a 32-bit Pentium-M CPU and I want to build and disassembly 64-bit code! Let’s set up my...


Install Ubuntu without PAE support

Last night I brought to a new life my old laptop to be used during my commuting time to write posts, to experiments some technologies and last but not least show how old hardware can be recycled and used efficiently. With my great surprise I discovered most of the 32-bit...


Hamming weight's Python implementation

Counting the number of 1’s in a binary representation of a number (aka Hamming weight aka popcount when binary numbers are involved) with Python using different implementations (naive implementations are obviously excluded :-) ). Implementation and benchmarking To compute the Hamming weight of a number in binary representation two implementations...


Disassembly C code for fun – Part 7: buffer overflow protection

Originally this article was about strings but as soon as I disassembled the first C test code I saw an interesting bit of assembly code so I switched the topic: today we will talk about buffer overflow protection and the canary (obviously I’m not talking about the little bird but...


Disassembly C code for fun – Part 6: inlining expansion

Today we will talk about code inlining which means the ability of the compiler to replace a function call with the body of the called function. The sample code and disassembly The code outputs the power of two between 0 to 9 by colling a power() function: #include <stdio.h> int...


Timestamp and microseconds on Windows platforms

The datetime.datetime object can return the current date and time with a resolution up to microseconds which is true on *nix platforms but not completely true on Windows platforms. The problem On both platforms, and at least with Python 2.7, the minimum resolution reported by datetime is 1 microsecond: >>>...


For loops in Windows command line

I’m working on a script on Windows to automatically install Python packages listed in a text file like pip does. Because easy_install doesn’t support a requirements.txt as a packages’ list I made a small Batch file to simulate the pip’s behavior, and I discovered the for loop of Windows’s command...


Distribution of wealth in a society of equals

In this blog I usually write posts related to information technology, but I stumble upon this post on NoiseFromAmerika (an italian blog about politics and economics) and I was surprised about how a concept backed by the common sense it’s so wrong when it’s analysed with scientific method. I made...


Multiple prefixes match

I’m not using often string manipulation (I usually handle SQLAlchemy’s queries and sometime XML/XHTML manipulation with the lxml library) but sometimes I need to analyse some paths expressed with a dotted notation comparing every path with a predefined list of path prefixes. The obvious solution is to compare in a...


Disassembly C code for fun – Part 5: if statement

Time to disassembly the if..then conditional statement. I’ll say in this post will be not exciting like the previous ones (except when we’ll disassembly the optimised code) but the if..then block is one of the base statements of every language so it’s important to know how it’s translate to assembly...


Disassembly C code for fun – Part 4: floats and SSE2

Today we look at the disassembly of a functions involving floats and SSE2 instructions. As I stated in the first post Disassembly C code for fun: part 1 the C code is compiled for a x86-64 architecture which means the CPU has the SSE/SSE2 instructions sets by default. The code...


Count elements in an iterator

Counting elements in a list, tuple or dictionary in Python is trivial, count the elements in a iterator is not. Because of the nature of a generator it’s not possibile to know how many elements will be returned from the iterator, only one element is returned at time and the...


Disassembly C code for fun - Part 3: for loop

…And it’s already the third post about disassembly C code for fun. Today we disassembly a simple for..loop cycle Update: Don’t miss the next post Disassembly C code for fun – Part 4: floats and SSE2 Disassembly time First the C code: #include <stdio.h> int main() { for (int i...


Disassembly C code for fun: part 2

So, in the previous post Disassembly C code for fun: part 1 I didn’t used as example the well known “Hello World!” code, but for a good reason: I need to explain some basics of the disassembly process (prologue, epilogue, stack, etc.) before proceed any further. Anyway in this post...


Disassembly C code for fun: part 1

I’m currently reading The C Programming Language by Ritchie and Kernighan to learn the C language from ground zero. I already write sometimes C/C++ code but it’s always a good idea to learn a already known language from the chapter 1 specially if you learn it by practice skipping the...


Python 3 annotations and performances

Python 3 introduced the concept of parameter and return type annotation with PEP 3107. Other interpreted languages supports type annotations for example ActionScript3 and this improved the quality of the generated byte-code and the execution speed because the interpreter can output specialised code for the annotated type. But does is...


Build 64-bit Skia on Mac OS X

It takes me a while to discover how to do it, so I’ll leave this post as a note to myself and to anyone which will have the same need. By default Skia will be build as 32-bit static library under Mac OS X but for who needs to have...


Beware of keyword's default values

Look this class definition, do you see what’s is wrong and potentially generate big problems in you code? class Foo(object): def __init__(self, bars=[]): self.bars = bars <!–more–!> Before jumping straight to the solution lets play with some instances of that class: >>> foo1=Foo([1, 2, 3]) >>> foo2=Foo([3, 4, 5]) >>>...


Parallel data processing

Developing an application or script which process data is a straight-forward pattern: data is collected, processed and the result is provided to be stored in a file or database. Those three blocks (collect, process, store) forms a pipeline and in the old days this pipeline is implemented in a series...


Build PyQt4 into your virtualenv

Here I’ll show the steps to build and install PyQt4 inside a virtualenv. These instructions are related to Mac OS X and Linux systems only. SIP First build and install the SIP binaries: $ cd sip $ python configure.py --incdir=${VIRTUAL_ENV}/include $ make -j2 $ make install The ${VIRTUAL_ENV} environmental variable...


Object's instance key in SQLAlchemy

In SQLAlchemy you can query the database returning ORM objects instead of raw data from the SQL statement (if you don’t know about it this is a good starting point). Object can be fetched by the value of their primary key: session.query(<class>).get(<primary_key_value>) which returns an instance of class with the...


Use custom SQLite database with iOS

Core Data is the framework available on iOS to allow to store data on the device using a common API without worrying about the storage backend. It’s very powerful but if you are creating and managing the data from your app. However, if you are providing your custom SQLite database...


Use MacBook Air SuperDrive with a MacBook Pro

Apple inconsistency strikes again: if you have a MacBook Air USB SuperDrive you cannot use it with a normal MacBook Pro. Why, damn Apple, why? There’s no any technical reason except for marketing and profit I suppose. Anyway, googling around a bit I found a workaround to be able to...


Box2D physics simulation on Android

It was a rainy and cold afternoon and I had nothing to do except to play around with Box2D on my Android device. I will show you the another-ball-bouncing example using Box2D and Cocos2D. For this example you will need to download these libraries: Cocos2D for Android: you can donwload...


Global hotkey

Let start by explaining the goal: we want to send a custom signal when the user press a combination of keys. Sound pretty easy, your are already thinking to override the keyPressEvent() and keyReleaseEvent() virtual methods of your QMainWindow with your custom code. It’s a fair solution, but not the...


Build Google WebM library under Windows

Google WebM is the new lossy encoder for images (and videos) which promise a better compression than the old and well known JPEG format. The source code is available anche can be build on Linux, Mac OS X and Windows as well. Anyway, on Windows if you follow the instructions...


Open URLs in external browser by Javascript in WebKit

It’s very simple with the QtWebKit APIs to open an link with your default browser from a QWebView (you can find some solutions like this in StackOverflow about that) but that works well only with <a> elements. If you are opening an external browser by a window.open() JavaScript function you need a...


CTypes and Incomplete Types

Some days ago I was writing a DLL’s wrapper in ctypes and I came across of this typedef in the header file: typedef struct _FILEHandle *FILEHandle; To translate this definition into ctypes is pretty straight-forward (if you already know the solution :-) ). First of all, _FILEHandle is a forward...


Best compressible serialisation format

For my next project I need to serialise a list of classes using the technology which fits those requirements: ninimum code to serialise/unserialise minmum file size when compressed As you can see the speed for read/write the serialised data is not a requirement but the size of the compressed file...


Execute JavaScript code at the end of an Adobe Edge movie

Adobe Edge is the new product form Adobe which will aim to replicate the features of Flash using HTML5 technology. I’m using Adobe Edge to create an animated introduction for a small GWT project and I ended up with the need to execute code at the end of the movie....


Experiment Necessitas: Qt apps on Android

Some weeks ago I read about the Necessitas project on the Qt Labs Blog which let you able to run Qt application on the Android platform. It was very interesting and now I’m going to try it. My test environment is my Mac Book Pro with Snow Leopard. Unfortunately the...


Compile PySide under Mac OS X

How to setup and compile the latest source code of PySide under Mac OS X Prerequisites Before starting to download and compile, PySide sources you must have some things installed on your system: XCode Python for Mac OS X. In this how-to I will use the 64-bit version Qt toolkit...


Handling Win32 window handler in PySide

To pass a Win32 window handler from a PySide object to a [win32gui.GetDC()](http://docs.activestate.com/activepython/2.7/pywin32/win32gui__GetDC_meth.html) method is not so simple as with PyQt4 or wxPython. I wrote a small test for a C function wich renders a bitmap to a Windows DC and I made a PyQt QMainWindow to show the result....


SWIG tutorial for Mac OS X

In this post I’ll show an updated version of the SWIG tutorial to build a Python module on top of a C library which works under Mac OS X. My test environment: Mac OS X 10.6.6 Python 2.7.1 32bit SWIG 2.0.1 XCode 3.2.5 Here a copy&paste of the original tutorial...


Make tests Qt-friendly

Testing Qt code with the Python unittest module is simple as ever. Take a look to this unittest code: from PyQt4 import QtGui import sys import unittest class TestQtWindow( unittest.TestCase ): app = QtGui.QApplication( sys.argv ) def test_window1(self): window = QtGui.QMainWindow( None ) window.show() self.assertTrue( isinstance( window, QtGui.QMainWindow ) )...


Simple event dispatcher in Python

In this post I’ll show you a simple event dispatcher implementation in Python. Every big UI frameworks can play with events an listener without reinvent the wheel but sometimes you need a very simple way to dispatch and receive events in you application without involve in megabytes of an external...


Let Babel works with py2exe

Babel is a framework to translate date, times, numbers and currency in all the languages of the world. Unfortunately the package is not py2exe-friendly but with a little patch we can let Babel works when is embedded with py2exe. When you build a Windows executable with py2exe all goes fine...


Compile PyQt4 with Python 2.7 64-bit under Mac OS X

A couple fo days ago Python 2.7 was release as a first stable release. Looking at the Mac OS X ports I found the new official 64-bit release and I installed it. But now the Qt Python bindings stops working because they are compiled as 32-bit binaries. The procedure to...


Install PyQt4 in Snow Leopard

New country, new life style, new spoken language and new GUI toolkit ! After 5 years of wxPython toolkit now it’s the time to move to PyQt4. Let’s see how install this GUI toolkit under Mac OS X Snow Leopard. First, donwload th QT4 SDK from QT official site, from...