Skip to content

Instantly share code, notes, and snippets.

@rhysm94
Last active March 22, 2019 00:14
Show Gist options
  • Save rhysm94/82c9a21a2d71efe0f1a04414170f2cb4 to your computer and use it in GitHub Desktop.
Save rhysm94/82c9a21a2d71efe0f1a04414170f2cb4 to your computer and use it in GitHub Desktop.
import requests
import functools
url = 'https://petition.parliament.uk/petitions/241584.json'
json = requests.get(url).json()
signatures = json['data']['attributes']['signatures_by_country']
get_signature_count = lambda country: country['signature_count']
get_uk = lambda x: x['name'] == 'United Kingdom'
get_non_uk = lambda x: x['name'] != 'United Kingdom'
otherCountrySignatures = functools.reduce(lambda total, country: total + get_signature_count(country) if get_non_uk(country) else total, signatures, 0)
ukSignatures = get_signature_count(list(filter(get_uk, signatures))[0])
totalSignatures = json['data']['attributes']['signature_count']
print(f'Total Signatures: {totalSignatures}')
print(f'UK Signatures: {ukSignatures}')
print(f'Non-UK Signatures: {otherCountrySignatures}')
print(f'Non-UK as a percentage of UK: {otherCountrySignatures / totalSignatures * 100}%')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment