Get breakdown of UK and non-UK votes for a Parliamentary Petition



Published: 2019-03-21 13:46:11 +0000
Categories: Python,

Language

Python

Description

For various reasons today it's been interesting to pull out what proportion of signatures on a given Parliamentary petition (i.e. one on https://petition.parliament.uk) came from outside of Great Britain.

Petitions are open to British Citizens, including those living overseas, so it's expected that there will be some signatures from other countries, without there being any foul play, this python3 script just helps assess the proportion.

Snippet

#!/usr/bin/env python3
# -*- coding: UTF-8
#
# Usage: petition_count.py [petition url]
#
#

import json
import urllib3
import sys

# get the URL from the command line
url="{}.json".format(sys.argv[1])

# Set up the urllib3 pool and fetch the JSON
http = urllib3.PoolManager()
response = http.request('GET',url)
p=json.loads(response.data.decode('utf8'))
response.release_conn()

total=p["data"]["attributes"]["signature_count"] 

# Iterate over signature countries and count anything not in GB
x=0
y={}
for country in p["data"]["attributes"]["signatures_by_country"]:
  if country["code"] not in ['GB','GI']:
        x=x+country["signature_count"]
        y[country["name"]] = country["signature_count"]

print("Total Signatures: {}".format(total))
print("Non-GB: {}".format(x))

# Print a breakdown of count by non GB country
for c,n in y.items():
   print('{}: {}'.format(c,n))

Usage Example

python3 petitions_count.py https://petition.parliament.uk/petitions/241584

Requires

  • Python 3
  • URLLib3

Video Example

License

BSD-3-Clause

Keywords

python3, petition, count, country, distribution,

Latest Posts


Copyright © 2019 Ben Tasker | Sitemap | Privacy Policy
Available at snippets.bentasker.co.uk and snippets.6zdgh5a5e6zpchdz.onion