Skip to content

Instantly share code, notes, and snippets.

@victorkristof
Last active July 27, 2016 08:13
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 victorkristof/883896c94d66217d379476b0d85e13fb to your computer and use it in GitHub Desktop.
Save victorkristof/883896c94d66217d379476b0d85e13fb to your computer and use it in GitHub Desktop.
UK parliament ePetition explorer.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# ePetition\n",
"\n",
"A bit of exploration on the ePetition dataset."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Library to load JSON files\n",
"import json\n",
"# Library to fetch URLs\n",
"import urllib.request"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Load the data"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# The petitions.json file must be in the same directory as this notebook\n",
"with open('petitions.json', 'r') as f:\n",
" petitions = json.load(f)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Get the first petition in the list\n",
"\n",
"Each `petition` dictionary has a key `data` storing some information about the petition and a key `url` to fetch the json file about the given petition (contains more details). "
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Get the first petition\n",
"first_petition = petitions['data'][0]\n",
"# Extract the url\n",
"url = first_petition['links']['self']\n",
"# Retrieve the details about this petition\n",
"with urllib.request.urlopen(url) as response:\n",
" petition_json = response.read()\n",
"# Load the associated json\n",
"petition = json.loads(petition_json.decode('utf-8'))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We print some info about the petition."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"TITLE: EU Referendum Rules triggering a 2nd EU Referendum\n",
"DETAILS: We the undersigned call upon HM Government to implement a rule that if the remain or leave vote is less than 60% based a turnout less than 75% there should be another referendum.\n",
"AUTHOR: William Oliver Healey\n",
"SIGNATURES: 4140949\n",
"RESPONSE FROM THE GOVERNMENT: The EU Referendum Act received Royal Assent in December 2015. The Act was scrutinised and debated in Parliament during its passage and agreed by both the House of Commons and the House of Lords. The Act set out the terms under which the referendum would take place, including provisions for setting the date, franchise and the question that would appear on the ballot paper. The Act did not set a threshold for the result or for minimum turnout.\r\n",
"\r\n",
"As the Prime Minister made clear in his statement to the House of Commons on 27 June, the referendum was one of the biggest democratic exercises in British history with over 33 million people having their say. The Prime Minister and Government have been clear that this was a once in a generation vote and, as the Prime Minister has said, the decision must be respected. We must now prepare for the process to exit the EU and the Government is committed to ensuring the best possible outcome for the British people in the negotiations.\r\n",
"\r\n",
"Foreign and Commonwealth Office\n"
]
}
],
"source": [
"print(\"TITLE:\", petition['data']['attributes']['action'])\n",
"print(\"DETAILS:\", petition['data']['attributes']['background'])\n",
"print(\"AUTHOR:\", petition['data']['attributes']['creator_name'])\n",
"print(\"SIGNATURES:\", petition['data']['attributes']['signature_count'])\n",
"print(\"RESPONSE FROM THE GOVERNMENT:\", petition['data']['attributes']['government_response']['details'])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We print the top 15 countries that signed the petition. Glad to see Switzerland here =)."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"United Kingdom 3975250\n",
"France 28491\n",
"Spain 17877\n",
"Australia 17201\n",
"United States 17014\n",
"Germany 11208\n",
"Canada 6347\n",
"Gibraltar 5929\n",
"New Zealand 4952\n",
"Netherlands 4696\n",
"Ireland 4576\n",
"Italy 4026\n",
"Switzerland 3529\n",
"Belgium 3494\n",
"Hong Kong 3179\n"
]
}
],
"source": [
"signatures_by_country = petition['data']['attributes']['signatures_by_country']\n",
"for country in sorted(signatures_by_country, key=lambda k: k['signature_count'], reverse=True)[:15]:\n",
" print(country['name'], country['signature_count'])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### References\n",
"\n",
"Get the url to the next page of petitions data."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"https://petition.parliament.uk/petitions.json?page=2&state=all\n"
]
}
],
"source": [
"print(petitions['links']['next'])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Keys of the attributes in `petitions['data]`. "
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"['additional_details',\n",
" 'state',\n",
" 'rejected_at',\n",
" 'debate_threshold_reached_at',\n",
" 'rejection',\n",
" 'background',\n",
" 'updated_at',\n",
" 'signature_count',\n",
" 'creator_name',\n",
" 'created_at',\n",
" 'response_threshold_reached_at',\n",
" 'government_response',\n",
" 'scheduled_debate_date',\n",
" 'moderation_threshold_reached_at',\n",
" 'action',\n",
" 'debate',\n",
" 'closed_at',\n",
" 'open_at',\n",
" 'government_response_at',\n",
" 'debate_outcome_at']"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"['additional_details', 'state', 'rejected_at', 'debate_threshold_reached_at', 'rejection', 'background', 'updated_at', 'signature_count', 'creator_name', 'created_at', 'response_threshold_reached_at', 'government_response', 'scheduled_debate_date', 'moderation_threshold_reached_at', 'action', 'debate', 'closed_at', 'open_at', 'government_response_at', 'debate_outcome_at']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Keys of one petition. This is where the *real data* is."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"['additional_details',\n",
" 'state',\n",
" 'debate_threshold_reached_at',\n",
" 'rejection',\n",
" 'background',\n",
" 'government_response_at',\n",
" 'signature_count',\n",
" 'created_at',\n",
" 'scheduled_debate_date',\n",
" 'debate',\n",
" 'closed_at',\n",
" 'rejected_at',\n",
" 'government_response',\n",
" 'updated_at',\n",
" 'signatures_by_country',\n",
" 'creator_name',\n",
" 'action',\n",
" 'moderation_threshold_reached_at',\n",
" 'signatures_by_constituency',\n",
" 'open_at',\n",
" 'response_threshold_reached_at',\n",
" 'debate_outcome_at']"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"['additional_details', 'state', 'debate_threshold_reached_at', 'rejection', 'background', 'government_response_at', 'signature_count', 'created_at', 'scheduled_debate_date', 'debate', 'closed_at', 'rejected_at', 'government_response', 'updated_at', 'signatures_by_country', 'creator_name', 'action', 'moderation_threshold_reached_at', 'signatures_by_constituency', 'open_at', 'response_threshold_reached_at', 'debate_outcome_at']"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment