• The message at this year’s Tech for Good Summit was that as a society we face significant challenges, only by working together can we have an impact on those challenges.

    One of the day’s events, Volunteers Assemble, saw three big challenges identified by the Scottish Tech Army presented to small groups of volunteers. We had two and a half hours to develop an innovative solution.

    Although a short amount of time, it was surprising how much each team achieved. Our challenge was an educational web app to help teachers deliver computing lessons.

    I try to focus on a single lesson, something to take away for future projects. Here it was the value of timeboxing.

    A short sprint focussed team members, resulting in two prototypes for the client to choose. There are two advantages. If the team misunderstands the brief or client can’t convey the requirement, little time or resources are wasted. Secondly there are often so many ideas that teams can end up in decision paralysis.

    It is important roles are assigned and understood, but that comes with effective communication and anyone volunteering learns that skill quite quickly.

    That just leaves presenting. I’ve a lengthy career in the armed forces behind me, and confidence speaking in front of people is part of that. I realised some time ago that that’s not true of everyone.

  • Playing with Rainbow Hat I learned a few things about Python as a result I found out what a decorator is, the difference between args and kwargs and threads. I also learned that a lot of guides don’t understand either.

    If you can’t explain it simply, you don’t understand it well enough 1.


    Rainbow Hat documentation says, “use touch handlers either by passing in a function by name or using the method as a decorator”.

    Learning Python (Lutz, 2013) dedicates a chapter to decorators and sums it up well:

    In short, decorators provide a way to insert automatically run code at the end of function and class definition statements—at the end of a def for function decorators, and at the end of a class for class decorators 2.

    With similar notation to Java’s annotations:

    @decorator_function def function(arguments): ...

    Python is running one function through another and binding the result to the original function name.

    def function(arguments):
    function = decorator_function(function)

    For example, Python has a built-in function that returns a static method staticmethod(function). To make example_func static, we put:

    def example_func(arg)

    Which is rebound to:


    So now I know what a decorator is in Python, I used it for the buttons. What to use them for though? I figure that they should control speed of LED, sequence, or colour. That’s going to need a thread running as an event handler.

    A short digression on arguments

    What is a key-worded argument? Lots of documentation refers to *args and **kwargs but had no idea what it was. Arguments passed to functions are read left to right:

    function('Dougie', 42)

    But we can also use a key-value pair:

    function(name='Dougie', age=42)

    Apart from improving readability in the function call, default arguments can be assigned in the function definition:

    def function(name='Dougie', age=42)

    By convention these are referred to as arg and kwarg. That just leaves the *. Python lets you define functions that take any number of arguments, assembling them into a tuple. If you use key-value arguments, it assembles a dictionary.

    def function(**kwargs): {...}

    Now the clever(er) bit because if you do the same on the function call, Python unpacks the argument into individual arguments (*arg) or key-value pairs (**kwarg).


    Back to the main thread

    The Rainbow Hat has buttons, so I want to use these to control rainbow speed. This seems suited to a thread running an event handler. The syntax for the thread library (hopefully explaining the digression) is:

    thread.start_new_thread (function_name, (*args, **kwargs))

    Concurrency in Python is a post in its own right. The CPython interpreter bytecode isn’t fully thread safe. There are different interpretations of what that means so I’ll use the Open University definition:

    A class is considered thread-safe if its instances behave under concurrent method calls as if they were called sequentially 3).

    Python source code is compiled to bytecode which is run in a VM as machine code. To ensure only one thread executes bytecode at once the current thread has to hold a global lock (Global Interpreter Lock (GIL)).

    This means multiple processor cores aren’t being used. In this application it doesn’t matter because the interpreter emulates concurrency by routinely switching threads.

    1. Albert Einstein[]
    2. Learning Python (Lutz, 2013), pp1034 “Chapter 32: Advanced Class Topics”[]
    3. M362 Developing concurrent distributed systems (THE OU, 2008[]
  • I’ve been spending time with Python recently and am beginning to really like some of the language’s features. List comprehension creates a list by evaluating an expression on each item in each list, from left to right. It combines an expression and a loop:

    >>> [ord(letter) for letter in 'example']
    [101, 120, 97, 109, 112, 108, 101]

    Apply a condition :

    >>> [ord(letter) for letter in 'example' if ord(letter) < 112]
    [101, 97, 109, 108, 101]

    It’s useful for combining lists:

    >>> [(letter, number) for letter in 'ab' for number in '12']
    [('a', '1'), ('a', '2'), ('b', '1'), ('b', '2')]

    Pimoroni’s Rainbow Hat introduction has an example to cycle colours on the LED rainbow:

    for i in range(101):
        h = i / 100.0
        r, g, b = [int(c * 255) for c in colorsys.hsv_to_rgb(h, 1.0, 1.0)]
        rh.rainbow.set_all(r, g, b)

    This is a little hard to follow but we’ll break it down. Hue, Saturation and Value (HSV) represents colour using three values between 0.0 and 1.0 creating a colour “wheel” that is easy to cycle on the LED. Unfortunately, the LED combines red, green and blue. The Colorsys library can convert between the two.

    • The expression is int(c*255)
    • The loop is for c in colorsys.hsv_to_rgb(h, 1.0, 1.0)

    The h=i/100 is giving a range of values from 0.0 to 1.0 in 0.01 steps (we could use a list comprehension too [i/100 for i in range(101)]).

    So, let’s look at a snapshot, where h=1.3:

    >>> colorsys.hsv_to_rgb(0.13,1.0,1.0)
    (1.0, 0.78, 0.0)

    Which the list comprehension converts to:

    >>> [int(c*255) for c in colorsys.hsv_to_rgb(0.13, 1.0, 1.0)]
    [255, 198, 0]

    Giving us the RGB value needed.

  • In an earlier post, I used Threads but the thread module in Python when I should be using threading. The documentation for threading says it builds upon the thread module (renamed _thread):

    This module provides low-level primitives for working with multiple threads (also called light-weight processes or tasks) — multiple threads of control sharing their global data space. For synchronization, simple locks (also called mutexes or binary semaphores) are provided. The threading module provides an easier to use and higher-level threading API built on top of this module.

    That makes sense, so I should be using the threading module. So why is there a leading underscore on thread? The Style Guide (PEP 8) says it’s a weak internal use indication and that for example modules with an underscore are not imported when using an asterisk.

    There are two ways to use this module. Define a subclass and override the run method or to use the Thread class’s default run method. I’ll use the second method here.

    The Python 2.x code I was using looked like this:

    import thread
    thread.start_new_thread(scroll, (text,))

    The new code:

    import threading
    def scroll(text):
    text = 'Example text'
    thread = threading.Thread(target=scroll, args=(text,))

    This isn’t that different, but it is a lot more readable. The arguments are explicit and there’s no ambiguity.

    Fluent Python (Ramalho, 2015) suggests using concurrent.futures package with Python 3.x. ThreadPoolExecutor and ProcessPoolExecutor implement interfaces to submit threads or processes respectively for execution. I’ll expand on this in a future post as it is useful to submit multiple tasks and collect results – however that’s not what is needed in this example.

  • What’s a triangular number? It is the sequence found by summing all the natural numbers, for example the third number is $1+2+3=6$. Interestingly, it counts objects arranged as a triangle.

    Melchoir, CC BY-SA 3.0 https://creativecommons.org/licenses/by-sa/3.0, via Wikimedia Commons

    This also has closed form $T_n=\sum_{i=1}^{n}i=\frac{n(n+1)}{2}$.

    I started with a brute force approach – iterate through the triangular numbers and test if the number of divisors is greater than 500. I’m using the “numbers” package’s Sigma function to implement divisor function $\sigma x(n)=\sum{d n}d^x$ where $\sigma _0(n)$ gives the total number of factors for a given number. That requires a loop, which is going to dominate for large $n$, so $O(n^2)$.

    triangular.number <- function(number) {
    	number = (number * (number + 1)) / 2
    num.factors <- 0
    i <- 1
    while (num.factors < 500) {
    	num.factors <- (Sigma1, k = 0))
    	print(triangular.number(i)) i <- i + 1

    Not terribly efficient but it gets the correct answer. So how can we improve the algorithm? Reducing the number of times we repeat the loop would be a good place to start.

    Now, $a(n)$ and $b(n+1)$ are co-prime – the only positive integer that divides them both is 1. This has a useful property, that $lcm(a,b)=ab$. Thing is, I can’t see how to incorporate it…

    1. triangular.number(i[]
  • Managing resources usually involves three steps – allocating, using, and releasing. You’re familiar with the try... finally pattern but Python also provides context managers.

  • Check error logs to find which plugin is crashing the site, then check the site’s wp-config.php file to identify the database name and prefix.

    Using phpMyAdmin or similar, assuming “wp” is the prefix, locate wp_options table and edit the active_plugins row.

    The data starts a:X where X is the number of active plugins. Change the entry to a:0:{} to disable all plugins.

Got any book recommendations?