Create multi-dimensional arrays in pure Python: The Correct Way

The problem Yesterday I wasted about half an hour chasing a bug because I was mindlessly generating a 5x5 matrix with the list repeat syntax, aka list * integer in pure Python: n = 5 array = [[0] * n] * n When I tried to flip some specific bits in this matrix, I observed that entire columns got flipped, instead of just one element: array[0][0] = 1 print(array) # Output: [[1, 0, 0, 0, 0], # [1, 0, 0, 0, 0], # [1, 0, 0, 0, 0], # [1, 0, 0, 0, 0], # [1, 0, 0, 0, 0]] In retrospect, the behaviour makes sense, but in the middle of a much larger program, it was a bit hard to find....

September 5, 2021 · 6 min · Lim H.

Introducing Kedro Hooks

This is an article two colleagues and I wrote on QuantumBlack’s Medium blog introducing a new feature on Kedro, our open-source data pipelining framework.

June 10, 2020 · 1 min · Lim H., Kiyohito Kunii, Jo Stichbury

Dataclass field metadata: Pythonic equivalence of Go struct tag

1. What is Golang’s struct tag Recently I have been writing some Go and there is a particular language feature in Go that I really like: struct tag. Basically, a struct tag is just an arbitrary string literal that you can attach to a struct, which later on you can decode and turn into something useful via reflection. If that still sounds too cryptic, consider this example: type MyAwesomeGoServiceConfig struct { Host string `envconfig:"SERVICE_HOST" another_random_tag:"amazing"` Port string `envconfig:"SERVICE_PORT" this_is_seriously_cool:"agree"` } The code above, apart from sounding uncharacteristically upbeat, is 100% valid Golang:...

May 18, 2019 · 4 min · Lim H.

Parser Combinator in Haskell vs. Regex in Python

This isn’t meant to be a comprehensive comparison between the two styles of parsing, because parser combinator is certainly more powerful than plain regular expression, athough I have seen regular expressions that pretend to do just as much. Speaking of which, there is a recent story on Hacker News about an adventure in regex land which I highly recommend. The bad news is parser combinator isn’t part of the standard library in Python....

February 15, 2016 · 2 min · Lim H.