Skip to content

Instantly share code, notes, and snippets.

@stuartlangridge
Created January 31, 2018 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stuartlangridge/28414e8c45dd8379eb0c069c6aba33b3 to your computer and use it in GitHub Desktop.
Save stuartlangridge/28414e8c45dd8379eb0c069c6aba33b3 to your computer and use it in GitHub Desktop.
A simple script to count UK MPs, since Parliament began, by first name, so I could post https://twitter.com/sil/status/958750576900296706
#!/usr/bin/env python3
"""Daft script to count MPs by first name.
Expects to find people.json from https://github.com/mysociety/parlparse/tree/master/members
Run as "python3 mpnames.py Keir" or similar.
Written to answer Jess Phillips' question at https://twitter.com/sil/status/958750576900296706
"""
import json, pprint, sys
fp = open("people.json", encoding="utf-8")
data = json.loads(fp.read())
fp.close()
memberships_by_person_id = {}
for m in data["memberships"]:
p = m.get("person_id")
if not p: continue
if m.get("organization_id") == "house-of-lords": continue
if not memberships_by_person_id.get(p): memberships_by_person_id[p] = []
memberships_by_person_id[p].append(m)
by_name = {}
for p in data["persons"]:
names = []
m = memberships_by_person_id.get(p["id"])
if not m: continue
if p.get("other_names"):
for on in p["other_names"]:
if on.get("given_name"):
names.append(on["given_name"])
for n in names:
if not by_name.get(n): by_name[n] = []
by_name[n].append({"name": [x for x in p.get("other_names") if x.get("note") == "Main"], "person": p, "memberships": m})
counter = sorted([(x, len(by_name[x])) for x in by_name.keys()], key=lambda a:a[1], reverse=True)
print("Ten most popular names")
for f in counter[:10]:
print("%s (%s)" % f)
print()
print("MPs with the name '%s'" % (sys.argv[1],))
matches = by_name[sys.argv[1]]
for m in matches:
print(("%s %s %s" % (m["name"][0].get("honorific_prefix", ""), m["name"][0].get("given_name", ""), m["name"][0].get("family_name", ""))).strip())
print("(%s MP%s)" % (len(matches), "" if len(matches) == 1 else "s"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment