Last week a colleague asked me for help on a figure he wanted to plot using
pyplot. He had a couple of line plots and an intersecting line and wanted to
show the histogram of the intersection points on both the X and Y axis.
I found a neat way of acomplishing such a plot using a
Here is the code, using a regular scatter plot as the central element, but as
every part of the figure is filled separately you can swap any of them out.
You have to make sure though that the histogram plots and the main plot have
the same X and Y axes respectively.
I remove the axes from the main plot (set_axis_off()) because they looked
is used to remove the gaps between the subplots. You can tweak width_ratios
and height_ratios to change how much space is allocated to the histograms
and the main plot.
On client project I have to do a search in a structure of tables and return
results including some child objects. A classical instance of the N+1 query
problem, if I was using an ORM. I decided not to use the ORM for the feature
because it will be one of the hottest paths of the application and I wanted
more control over the queries (rather complex logic with LIKE & sorting).
But the filtering/sorting is not the topic today, so I will leave it out in
For illustration, let’s assume the following schema:
Without any further thinking I would do a query like this and a bit of code
that constructs parent objects with their respective children.
Just before I started writing the logic to pull apart the result and put it
into it’s object structure, I thought, “It would be nice to let the database
put together all the children of one parent into an array.”
I already knew
which aggregates all values into an array. After some reading I discovered
which takes a sequence of keys and values and creates a JSON object from it.
So my final query looked like this:
When the query is executed with sqlalchemy, children in the resulting rows
is already correctly typed as a list of dictionaries.
The explain analyze output for both queries (on a trivially small test set)
shows that the aggregated version is 50-100% slower (150-200μs vs. 250-350μs),
but I guess that will rarely be a real problem because - at least in my case
the execution time of my query is dominated by filtering/sorting the parent
The bare except statement is something I often see in the main loop of a
script that should run indefinitely. Pylint will
warn you about this
problem, but in my opinion we should start to treat this as an error.
Consider the following code snippet:
This will work nicely to keep your program running whatever happends inside you
risky_business function. But let’s try to stop your program by pressing
We caught the KeyboardInterrupt exception and just carried on. But we catch
more which we should not. Let’s try to exit the program from the business
function, maybe because the user asked for it or we got signaled.
$ python a.py
('Problems?', (<type 'exceptions.SystemExit'>, SystemExit(0,), <traceback object at 0x7f49b7ab5e18>))
('Problems?', (<type 'exceptions.SystemExit'>, SystemExit(0,), <traceback object at 0x7f49b7ab5ea8>))
We can fix our problem by restricting the kind of exception that we want to
When we press CTRL-C now, the program exits with the right exception.
$ python a.py
^CTraceback (most recent call last):
File "a.py", line 10, in <module>
File "a.py", line 6, in risky_business
Pylint will still warn you
about this line, because catching Exception is still very broad and you
should only do this when you have a good reason, e.g. the reason mentioned
above (to keep a service running even if it could not handle a certain input or
So next time you see a bare except statement in your code, take a moment to
reconsider if you really want to catch SystemExit, KeyboardInterrupt (and
GeneratorExit which I did not mention above). If that is the case, you might
want to make it explicit so that the next person reading the code does not have
to check again.
tl;dr: Puzzle had to be solved. Did it in Haskell. Had fun. Code is here :-)
Last week one of my colleagues, Marco,
brought a so-called Minotauros cube to work and left it on the counter near the
coffee machine. You might call it an obvious attack on the productivity of the
At least for me it is hard to tolerate to be unable to solve a puzzle.
Unfortunately, it is a quite tricky one. Here is a picture; there is only one
way of putting the parts back into a cubic shape.
While I am sure that there are solutions and algorithms to solve this kind of
puzzle, I wanted to think of something myself. Thus, I abstained from searching
and started to think about a way to solve it. I immediately decided to build
the solution in Haskell. I have done quite a bit of programming in Haskell in my
first year of university but not much recently. But at
Polyconf 2015 I got some inspiration to do more
functional programming again (I am playing with elm
(talk at Polyconf) at the
moment, more on that in another post).
It took me some time and fruitless tries experiments until I had a good idea
how to structure the solution of the problem. I decided to use a backtracking
algorithm which tries to find dead ends as quickly as possible.
01: function solve (shapes, solution)
02: shape, rest = head, tail of shapes
03: for shape' in all rotations of shape
04: for shape'' in all possible positions of shape'
05: solution' := solution ++ shape''
06: if solution' is valid
07: solution'' := solve (res, solution')
08: if solution'' is valid
09: return solution''
10: return invalid solution
11: solve(all shapes, empty solution)
During the implementation I decided to change the algorithm slightly: It now
starts out with a full solution (a shape in which all the coordinates are
“occupied”) and consequently “subtracts” instead of “adds” in line 05.
Another problem was the data structure to represent shapes. My first try
featured the following type using Data.Array:
It is a quite obvious choice for somebody who lives most of the day in the
imperative world, but I also noticed quite quickly that the Array type is
a little cumbersome to work with.
I then reconsidered what a truly functional way of representing spatial data
would be. I came up with the following:
I planned to use this as a three-dimensional doubly linked list. I thought it
might be nice to traverse this using pattern matching. While Haskell’s
laziness enables us to do so I
quickly noticed that using this kind of structure is too much for my mind.
I finally resorted to something easier:
As soon as I had made up my mind about the data structure it took me a couple
of evenings to create all the necessary functions. The full implementation can
be found in this gist.
This experiment and also my recent experience with elm has motivated me to do
more strongly typed functional programming again. The power of those compilers
is sometimes just incredible. ;-)