Count elementwise matches for two NumPy arrays

Let’s say we have two integer NumPy arrays and want to count the number of elementwise matches. Here are our two arrays (NumPy imported as np): >>> a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> b = np.array([1, 2, 2, 2, 5, 6, 7, 9, 9]) To create a third (boolean) array that contains True for a match, and False otherwise, we can use the equality operator. >>> a == b array([ True, True, False, False, True, True, True, False, True]) Given that, counting the number of matches is as easy as: >>> np.count_nonzero(a==b) 6 (Another option would be to use the (slower) np.sum(a==b))

January 26, 2018

Basic understanding of IPv4 addresses

In a recent project I needed to anonymize IP addresses in tracking data. While masking a few bits from an IP address is not so interesting, it’s a good excuse to review some of the basics of how (IPv4) IP addresses work. What’s an IP address and how does it look like? To be able to route traffic through a network (e.g. your local network or the internet), we need a way to uniquely identify devices on that network, i.e. we need addresses. ...

January 22, 2018

VPN: Temporarily solve same subnet conflicts

I recently had the problem of needing to establish a connection to a server behind a VPN that was in the same subnet as the network I was connecting from. Every call I wanted to make to the server on the remote network wouldn’t go through as it was looking for the server on the local network. To eventually reach the server, I temporarily added a route to the routing tables, telling the client it should transmit traffic to the destination ip through the interface of the VPN connection. ...

September 27, 2017

Running Flask on macOS with mod_wsgi/wsgi-express

Flask is a (micro) web development Framework for Python. It is fairly simple to get started. All you need to do is to pip install Flask into your virtualenv, give the FLASK_APP environment variable your file and run flask run (described in detail in installation and quick start). This will launch the development server and you can instantly start hacking around. When you want to use your Apache webserver, however, you need to install and configure a WSGI module. When I first wanted to do this I tried to install mod_wsgi via brew (brew install mod_wsgi from the homebrew/apache tap), but quickly ran into some (apparently common) issues with the XCode toolchain. Then I discovered that there is a much easier way of installing mod_wsgi as a Python package. ...

August 5, 2017

SQL: Get the count of related records

Micro tutorial: Sometimes you need to show results from table A but also include the count of related records from table B. For example: how many items are in each purchase order, how many comments in each post, how many galaxies in each universe (wait, what?), … The following query shows one way of selecting the title and id from a table Posts, the count of the related records in a table Comments and then sorts descending by that count. fk_post is the foreign key of the post in the Comments table. ...

July 11, 2017