Fun with True and False in Python

TIL: In Python, True = 1 and False = 0

>>> var0 = 0
>>> var1 = 1
>>> var0 == True
>>> var0 == False
>>> var1 == True
>>> var1 == False

The number 2, however, is neither True nor False. But it is truthy.

>>> var2 = 2
>>> var2 == True
>>> var2 == False
>>> bool(var2)

Which has the following interesting side effects when testing conditions

>>> if (var1 == True): print("***  var1 is true ***")
... else: print("*** var1 is not true ***")
***  var1 is true ***

>>> if (var2 == True): print("*** var2 is true ***")
... else: print("*** var2 is not true ***")
*** var2 is not true ***

>>> if (bool(var2)): print("*** var2 is truthy ***")
... else: print("*** var2 is not truthy ***")
*** var2 is truthy ***

Admittedly, this is a bit artificial because in reality if you wanted to test for the existence of var2 you’d just do:

>>> if var2: print("*** var2 is truthy ***")
... else: print("*** var2 is not truthy ***")
*** var2 is truthy ***

But I did enjoy discovering this little nugget as it means you can easily count up the number of Trues in a list like so:

>>> sum([True,False,True,True,False,False,True])

Management and product development lessons from the 1950’s

2671775In 1955, Elihu Katz and Paul Lazarsfeld published “Personal Influence“. This studied how small-group dynamics moderate or influence mass media messaging. For example how people decide who to vote for, which brand of lipstick to use, or which movie to go and watch.

Reading this in 2018 it’s striking to see how much is still valid. I’m not posting this to provide tremendous new insights. Any insights here are over 60 years old. Apparently, human behaviour doesn’t change very much over the generations.

How people choose their leaders

In order to become a leader, one must share prevailing opinions and attitudes. (p52)

They cite a 1952 study on children in a day nursery in which kids with “leadership qualities” were separated from the other children who were then placed into groups of 3-6. These new groups created their own “traditions” (e.g. who sits where, group jargon, division of who plays with what objects). The original leaders were then re-introduced:

In every case, when an old leader attempted to assert authority which went contrary to a newly established “tradition” of the group, the group did not respond. Some of the leaders, as a matter of fact, never returned to power. Others, who were successful, did achieve leadership once more but only after they had completely identified with the new “tradition” and participated in it themselves. (p52)

Or another 1952 study amongst a religious interest group, a political group, a medical fraternity and a medical sorority:

[T]hose who had been chosen as leaders were much more accurate in judging group opinion … But this was so only on the matters which were relevant to the group’s interest – medicine for the medical group, politics for the political group, etc. It seems reasonable to conclude … that leaders of groups like this are chosen, in part at least, because of recognized qualities of ‘sensitivity’ to other members of the group. (p102)

A succinct argument as to why people who want to become leaders need to first spend time listening.

Group participation improves take-up

Here’s are some more 1952 studies that the authors cite:

  1. A study in a maternity hospital in which “some mothers were given individual instruction .. and others were formed into groups of six and guided in a discussion which culminated in a [group] ‘decision’ [to follow the instruction.” The participants in the group dicussion adhered “much more closely” to the child-care programme. (pp74-75).
  2. A study comparing a lecture approach vs a group discussion on “the nutritional and patriotic justifications for the wartime food campaign to buy and serve ‘unpopular’ cuts of meat. 3% of those involved in the lecture followed the desired course of action, vs 32% of those in the group discussion.

Worth bearing in mind in the next meeting you host, or the next corporate communication you send out.

How small groups construct their reality

So many things in the world are inaccessible to direct empirical observation that individuals must continually rely on each other for making sense out of things. (p55)

Apparently 1952 was a bumper year for social sciences. Here is another 1952 study in which individuals were asked to decide how far and in which direction a point of light was moving. The catch was that the point of light was static. The study found that:

  1. When people were shown the light individually, they would make their own judgment of how it was moving. When they were later put into small groups of 2 or 3, “[e]ach of the subjects based his first few estimates on his previously established standard, but confronted, this time, with the dissenting judgments of the others each gave way somewhat until a new, group standard became established.”
  2. If a group session came first, the group would achieve a consensus of how the light was moving, and each individual would adopt the group’s consensus as their own position.

The way reality is generated by social groups is something to bear in mind during user research activities.

How the make-up of a group affects quality of communication

You guessed it, it’s another 1952 study that found that:

  1. Rank in the group affects how people communicate. Specifically: “[P]-erson-to-person messaged are directed at the more popular group members and thus may be said to move upward in the hierarchy, while communication from one person to several others tends to flow down” (p89).
  2. As groups get larger (from 3 to 8) “more and more communication is directed to one member of the group, thus reducing the relative amount of interchange among all members with each other. At the same time the recipient of this increased attention begins to direct more and more of his remarks to the group as a whole, and proportionately less to specific individuals.” (pp89-90)

I’m sure these two findings will ring very true of many meetings you’ve been in. I suspect that the person who becomes the centralising leader in these communications might not even realise the role they are playing. Reading this makes me more keen to try out the kind of silent meetings approach they use at Square.



Using local settings in a Scrapy project

TL;DR Seeing a pattern used in one framework can help you address similar problems in a different framework.

I was working on a scrapy project that would save pages into a local database. We wanted to be able to test it using a test database, just as you would with Rails.

Wec could see how to configure a database connection string in the config file, but this didn’t help switch between development and test database

Stackoverflow wasn’t much help, so we ended up rolling our own:

  1. Edit the file so it would read from additional settings files depending on a SCRAPY_ENV environment variable
  2. Move all the settings files to a separate config directory (and change scrapy.cfg so it knew where to look
  3. Edit .gitignore so that local files wouldn’t get committed to the repo (and then added some .sample files)

Git repo is here.

The magic happens at the end of

from importlib import import_module
from scrapy.utils.log import configure_logging
import logging
import os

if SCRAPY_ENV == None:
    raise ValueError("Must set SCRAPY_ENV environment var")
logger = logging.getLogger(__name__)
configure_logging({'LOG_FORMAT': '%(levelname)s: %(message)s'})

# Load if file exists; incorporate any names started with an
# uppercase letter into globals()
def load_extra_settings(fname):
    if not os.path.isfile("config/" % fname):
        logger.warning("Couldn't find %s, skipping" % fname)
    mdl=import_module("config.%s" % fname)
    names = [x for x in mdl.__dict__ if x[0].isupper()]
    globals().update({k: getattr(mdl,k) for k in names})

load_extra_settings("secrets_%s" % SCRAPY_ENV)
load_extra_settings("settings_%s" % SCRAPY_ENV)

It feels a bit hacky, but it does the job, so I would love to learn a more pythonic way to address this issue.

Re-learning regexes to help with NLP

TL;DR Don’t forget about boring old-school solutions when you’re working with shiny new tech approaches. Dusty old tech might still have a part to play.

I’ve been experimenting with entity recognition. The use case is to identify company names in text. I’ve been using the fab service from as my gold standard of what should be achievable.

Overall it is pretty impressive. Consider the following phrases:

Takeda is a gerbil

Dandelion recognises that this phrase is about a gerbil.

Takeda eats barley from Shire

Dandelion recognises that this is about barley

Takeda buys goods from Shire

A subtle change of words means this sentence is probably about a company called Takeda and a company called Shire.

Very cool.

But what about this sentence:

Polish media and telecoms group Cyfrowy Polsat (CPS.WA) said late Thursday it agreed to buy a controlling stake in sports content producer and distributor Eleven Sports Network Sp.z o.o. (ESN) for 38 million euros ($44.48 million).

Still pretty impressive but it has made one big fumble. It thinks that CPS stands for Canon, whereas in reality CPS.WA is the stock ticker for Cyfrowy Polsat.

Is there an alternative approach?

In this sort of document, company names are often abbreviated, or referenced by their stock ticker. When they are abbreviated they use the same kind of convention. Sounds like a job for a regex.

In case you think that something as old-school as regexes can’t possibly have a role to play with cutting edge code, bear in mind they are heavily used in NLP, so Dandelion must be using them somewhere under the covers. Or have a look at Scikit-learn’s CountVectorizer which uses a very simply regex for the token-pattern that it uses for splitting up text into different terms.

I can’t remember the last time I used a regex in anger. (See also this great stackoverflow question and answer on the topic). I don’t like just copy pasting from stack overflow so I broke the task down into a few steps to make sure I fully understood what was going on. The iterations I went through are below (paste this into a python console to see the results).

sent = "Polish media and telecoms group Cyfrowy Polsat (CPS.WA) said late Thursday it agreed to buy a controlling stake in sports content producer and distributor Eleven Sports Network Sp.z o.o. (ESN) for 38 million euros ($44.48 million)."
import regex as re

To go through the iterations one by one with what I re-learnt at each stage.

>>> re.findall(r"\(.+\)",sent)
['(CPS.WA) said late Thursday it agreed to buy a controlling stake in sports content producer and distributor Eleven Sports Network Sp.z o.o. (ESN) for 38 million euros ($44.48 million)']

Find one or more characters inside a bracket. Brackets have special significance so need to be escaped. This simple regex finds the longest match (it’s “greedy”) so we need to change it to a “lazy” search.

>>> re.findall(r"\(.+?\)",sent)
['(CPS.WA)', '(ESN)', '($44.48 million)']

Better, but the only real initials are combinations of a capital and a dot, so specify that:

>>> re.findall(r"\([A-Z.]+?\)",sent)
['(CPS.WA)', '(ESN)']

Next I need to find the text before the initials. This would be a Capital letter followed by one or more other characters followed by a space:

>>> re.findall(r"[A-Z]\w+\s\([A-Z.]+?\)",sent)
['Polsat (CPS.WA)']

Getting somewhere, but really we need multiple words before the initials. So put brackets around the part of the regex that includes a capitalised word and match this one or more times

>>> re.findall(r"([A-Z]\w+\s)+\([A-Z.]+?\)",sent)
['Polsat ']

WTF? Makes no sense. Except it does: The brackets create a capture group. This changes findall’s behaviour. findall now returns the results of the capture group rather than the overall match. See below how using the captures() method returns the whole match.

['Cyfrowy Polsat (CPS.WA)']

Solution is to turn this new group that we created into a non-capturing group using ?:

>>> re.findall(r"(?:[A-Z]\w+\s)+\([A-Z.]+?\)",sent)
['Cyfrowy Polsat (CPS.WA)', '(ESN)']

A bit better, but it would be nice now if we can separate out the name from the abbreviation, so we return two capture groups in the regex:

>>> re.findall(r"([A-Z]\w+\s[A-Z](?:[\w.\s]+))\s\(([A-Z.]+?)\)",sent)
[('Cyfrowy Polsat', 'CPS.WA'), ('', 'ESN')]

At this point the regex is only looking for capitalised words before the abbreviation. Eleven Sports Network has some lower case terms in the name, so the regex needs a bit more tuning. The following example does the job in this particular case. It looks for a capitalised word then a space, a capital letter and then some other text until it gets to what looks like an abbreviation in brackets:

>>> re.findall(r"([A-Z]\w+\s[A-Z](?:[\w.\s]+))\s\(([A-Z.]+)\)",sent)
[('Cyfrowy Polsat', 'CPS.WA'), ('Eleven Sports Network Sp.z o.o.', 'ESN')]

You can see this regex in action on the fab site. Let’s break this down:

(                           )\s\((       )\)
 [A-Z]\w+\s[A-Z]                  [A-Z.]+

  • Capturing Group 1
    1. [A-Z] a capital letter
    2. \w+ one or more letters (to complete a word)
    3. \s a space (either to start another word, or just before the abbreviation)
    4. [A-Z] then a capital letter, to start a 2nd capitalised word
      1. (?:[\w.\s]+) a non-capturing group of one or more letters or full stops (periods) or spaces.
  • \s\( a space and then an open bracket
  • Capturing Group 2:
    1. [A-Z.]+? one or more capital letters or full stops.
  • \) Close bracket

This regex does the job but it didn’t take long for it to become incomprehensible. If ever there were a use case for copious unit tests it’s using regexes.

Also, it doesn’t generalise well. It won’t identify “Acme & Bob Corp (ABC)” or “ABC and Partners Ltd (ABC)” or “X.Y.Z. Property Co (XYZ)” or “ABC Enterprises, Inc. (‘ABC’)” properly. Writing one regex to handle all of these types of string would quickly become very brittle and hard to understand. In reality I would end up using a serious of regexes rather than trying to code one regex to rule the all.

Nevertheless I hope it’s clear how a boring old piece of tech can help plug gaps in the cool new stuff. It’s never too late to (re-)learn the old ways. And it’s ok to put your pride aside and go back to basics.

How Ofsted failed my children

(A “Back To School” personal posting, not the usual stuff I write about here, but still important and somehow relevant to tech businesses)

Three of my kids were in a Rudolf Steiner school. Our plan had been to do Steiner education for primary and then mainstream education for secondary. The oldest child has made this transition successfully, but plans for the younger two were recently thrown into disarray.

The school has now closed following about 18 months of pressure from Ofsted and the Department for Education (DfE). As far as I can tell, the closure is as a direct result of this pressure. Ofsted were unhappy with how the school operated. Whatever issues Ofsted might have found, forcing the school into a position where it had to close was absolutely not in the best interests of the children. Ofsted has failed the children who have had their education disrupted so abruptly.

But there’s more than this. Ofsted have failed to recognise all the things that this school system is good at – things that are becoming more and more important in our changing world. Some examples:

  • The value of spending lots of time outdoors, and not just walking around tarmac playgrounds
  • How to introduce foreign languages at a young age
  • How to build a real community of children, teachers and parents
  • How to delay the age at which kids start getting online
  • The importance of joy in learning
  • How too much progress monitoring detracts from delivering real education

The fact of the matter is that the world that our children will inherit is changing. Automation is transforming the nature of the jobs market. The sorts of skills that people will need in the jobs market in 10 or 20 years are going to be very different to when knowing how to file a tax return guaranteed someone a decent wage.

There is no accident that many people who work in the tech industry and can see close-up the changes coming our way choose this kind of education.

Obviously the Steiner system in general, and this school in particular, don’t have all the answers and won’t get everything right. You’d have to be quite an extremist to take that position (I’m sure there are some such extremists around). In my view, the healthy thing to be doing would be to learn what the school does well rather than just trying to shut it down for what you don’t like about it.  Instead the DfE has thrown the baby out with the bathwater.

Disclosure: I was part of the governing team of the school up until the point that Ofsted got involved. I haven’t had any involvement since the school started working through the issues that Ofsted raised and write this just as an ex-parent.

How long will this feature take?

Be clear on someone’s definition of done when trying to communicate what it will take to build a feature.

Planning software development timescales is hard. As an industry we have moved away from the detailed Gantt charts and their illusion of total clarity and control. Basecamp have recently been talking about the joys of using Hill Charts to better communicate project statuses. The folk at ProdPad have been championing, for a long time, the idea of a flexible, transparent roadmap instead of committing to timelines.

That’s all well and good if you are preaching to the choir. But if you are working in a startup and the CEO needs to make a build or buy decision for a piece of software, you need to make sure that you have some way of weighing up the likely costs and efforts of any new feature you commit to build. It’s not good enough to just prioritise requests and drop them in the backlog.

The excellent Programmer Time Translation Table is a surprisingly accurate way of interpreting developer time estimates. My own rule of thumb is similar to Anders’ project manager. I usually triple anything a developer tells me because you build everything at least 3 times: once based on the developer’s interpretation of the requirements; once to convert that into what the product owner wanted; and once into what the end users will use. But even these approaches only look at things from the developer’s point of view, based on a developer’s “definition of done”. The overall puzzle can be much bigger than that.

For example, the startup CEO who is trying to figure out if we should invest in Feature X probably has a much longer range “definition of done” than “when can we release a beta version”. For example: “When will this make an impact on my revenues” or “when will this improve my user churn rates”. Part of the CTO job is to help make that decision from the business point of view in addition to what seems to be interesting from a tech angle.

For example, consider these two answers to the same question “When will the feature be done?”.

  1. The dev team is working on it in the current iteration, assuming testing goes well it will be released in 2 weeks.
  2. The current set of requirements is currently on target to release in 2 weeks. We will then need to do some monitoring over the next month or two so that we can iron out any issues that we spot in production and build any high priority enhancements that the users need. After that we will need to keep working on enhancements, customer feedback and scalability/security improvements so probably should expect to dedicate X effort on an ongoing basis over the next year.

Two examples from my experience:

A B2B system used primarily by internal staff. It took us about 6 weeks to release the first version from initial brainstorming on it. Then it took about another two months to get the first person to use it live. Within 2 years it was contributing 20% of our revenues, and people couldn’t live without it.

An end user feature that we felt would differentiate us from the competition. This was pretty technically involved so the backend work kept someone busy for a couple months. After some user testing we realised that the UI was going to need some imaginative work to get right. Eventually it got released. Two months after release the take-up was pretty unimpressive. But 5 years later that feature was fully embedded and it seems that everyone is using it.

What is the right “definition of done” for both of these projects? Depends on who is asking. It’s as well to be clear on what definition they are using before you answer. The right answer might be in the range of months or years, not hours or weeks.

Memory and Mining in Ethereum – a look in Geth internals

There are a few comments on my guide to getting started with Ethereum private networks about mining taking ages to run, or apparently not running at all. The consensus seems to be that you need to be on a 64 bit OS and using plenty of memory. In this post I’ll compare mining in a private network with 1Gb, 2Gb and 4Gb virtual machine configurations. This was an academic exercise I went through for my own interest, but hopefully will be of some interest to people who enjoy understanding a bit more about how the code works.

Here are the steps, collected from various docs, that I went through on Ubuntu. The developer’s guide was my starting point.

Install Go:

sudo add-apt-repository ppa:gophers/archive
sudo apt-get update
sudo apt-get install golang-1.9


mkdir -p ~/go; echo "export GOPATH=$HOME/go" >> ~/.bashrc
echo "export PATH=$PATH:$HOME/go/bin:/usr/local/go/bin" >> ~/.bashrc
source ~/.bashrc

A few more commands so Go can be found in the GOPATH

mkdir $GOPATH/bin
sudo ln -s /usr/lib/go-1.9/bin/go $GOPATH/bin/go

Clone geth repo and build geth:

git clone $GOPATH/src/
cd $GOPATH/src/
go install -v ./cmd/geth

You’ve probably got two geths installed now, a system-wide one and the one that you just installed, as below.

ethuser@eth-host:~$ geth version | grep '^Version:'
Version: 1.7.3-stable
ethuser@eth-host:~$ $GOPATH/bin/geth version | grep '^Version:'
Version: 1.8.0-unstable

Before we do anything else, let’s just double check that you can run the newly installed geth. Assuming you used the guide in the previous post:

cd ~
$GOPATH/bin/geth --datadir node1 --networkid 98765 console

Add some logging to geth. See this commit which provides additional logging during the mining process:

Apply these changes to your local version, or if you prefer you can clone the branch in my fork that has these changes already in.

git clone -b extra_logging --depth 1 $GOPATH/src/

Rebuild your local version using go install -v ./cmd/geth and now you’ll get some more logging when you mine in your private network. See below for some examples with extra logging with 4Gb, 2Gb and 1Gb virtual machine configurations:


> miner.start(1);admin.sleepBlocks(1);miner.stop()
INFO [01-26|09:37:47] Updated mining threads threads=1
INFO [01-26|09:37:47] Transaction pool price threshold updated price=18000000000
INFO [01-26|09:37:47] Starting mining operation 
INFO [01-26|09:37:47] Commit new mining work number=34 txs=0 uncles=0 elapsed=125.978µs
INFO [01-26|09:37:47] Started ethash search for new nonces miner=0 seed=7377726478179259701
INFO [01-26|09:38:05] Still mining miner=0 attempts=1000 duration=18s
INFO [01-26|09:38:06] Still mining miner=0 attempts=2000 duration=19s
INFO [01-26|09:38:06] Still mining miner=0 attempts=4000 duration=19s
INFO [01-26|09:38:06] Still mining miner=0 attempts=8000 duration=19s
INFO [01-26|09:38:07] Still mining miner=0 attempts=16000 duration=20s
INFO [01-26|09:38:08] Still mining miner=0 attempts=32000 duration=21s
INFO [01-26|09:38:09] Finished mining miner=0 attempts=42332 duration=22s
INFO [01-26|09:38:09] Successfully sealed new block number=34 hash=c53c2f…45286c
INFO [01-26|09:38:09] 🔨 mined potential block number=34 hash=c53c2f…45286c

2 Gb

> miner.start(1);admin.sleepBlocks(1);miner.stop()
INFO [01-26|10:32:19] Updated mining threads threads=1
INFO [01-26|10:32:19] Transaction pool price threshold updated price=18000000000
INFO [01-26|10:32:19] Starting mining operation 
INFO [01-26|10:32:19] Commit new mining work number=37 txs=0 uncles=0 elapsed=129.734µs
INFO [01-26|10:32:19] Started ethash search for new nonces miner=0 seed=4187772785547710899
INFO [01-26|10:32:40] Still mining miner=0 attempts=1000 duration=21s
INFO [01-26|10:32:46] Still mining miner=0 attempts=2000 duration=27s
INFO [01-26|10:32:49] Still mining miner=0 attempts=4000 duration=30s
INFO [01-26|10:32:49] Still mining miner=0 attempts=8000 duration=30s
INFO [01-26|10:32:50] Still mining miner=0 attempts=16000 duration=31s
INFO [01-26|10:32:51] Still mining miner=0 attempts=32000 duration=32s
INFO [01-26|10:32:53] Finished mining miner=0 attempts=57306 duration=34s
INFO [01-26|10:32:53] Successfully sealed new block number=37 hash=068096…c31777
INFO [01-26|10:32:53] 🔨 mined potential block number=37 hash=068096…c31777


I cancelled after over half an hour…

> miner.start(1);admin.sleepBlocks(1);miner.stop()
INFO [01-26|09:51:58] Updated mining threads threads=1
INFO [01-26|09:51:58] Transaction pool price threshold updated price=18000000000
INFO [01-26|09:51:58] Starting mining operation 
INFO [01-26|09:51:58] Commit new mining work number=37 txs=0 uncles=0 elapsed=243.956µs
INFO [01-26|09:51:58] Started ethash search for new nonces miner=0 seed=1420748052411692923
INFO [01-26|09:53:11] Still mining miner=0 attempts=1000 duration=1m13s
INFO [01-26|09:54:23] Still mining miner=0 attempts=2000 duration=2m25s
INFO [01-26|09:56:52] Still mining miner=0 attempts=4000 duration=4m54s
INFO [01-26|10:01:53] Still mining miner=0 attempts=8000 duration=9m55s
INFO [01-26|10:11:49] Still mining miner=0 attempts=16000 duration=19m51s
INFO [01-26|10:30:27] Still mining miner=0 attempts=32000 duration=38m29s