Skip to main content

United Kingdom political party names, colors, and election data

Project description

uk-politics

United Kingdom political party names, colors, and election data.

Installation

This repository is available on PyPI, meaning it can be installed using:

pip install uk-politics

And used within python like so:

import uk_politics

Functionality

This module has three core aims:

  • Matching free-text entries to UK political party names
  • Searching historic election data
  • Matching party names to party colors

Free text names

Use uk_politics.find_party to get the official name of the closest-named party.

>>> uk_politics.find_party("Lib Dem")
'Liberal Democrats'
>>> uk_politics.find_party("SNP")
'Scottish National Party'
>>> uk_politics.find_party("Sinn Fein")
'Sinn Féin'

Sometimes the official name isn't the name used in everyday speech, or even on the parliament website, so we use these shorter names by default, with the option of the official names as well.

>>> uk_politics.find_party("Tory")
'Conservative Party'
>>> uk_politics.find_party("Tory", return_short_name=False)
'Conservative and Unionist Party'
>>> uk_politics.find_party("Tory")
'Conservative Party'
>>> uk_politics.find_party("Tory", return_short_name=False)
'Conservative and Unionist Party'

What about misspellings? The module will warn you the first time it encounters a name that it can't match to an existing nickname, and returns its best guess via fuzzy matching.

>>> uk_politics.find_party("Labuor Party")
Renaming 'labuor party' -> 'Labour Party'
'Labour Party' 

You can turn this fuzzy matching functionality off to get an exception instead of a warning.

>>> uk_politics.find_party("Labuor Party", allow_fuzzy=False)
[...] uk_politics.exceptions.PartyNameNotFound: No party with name 'labuor party' was found.

Finally we have created a party named "No vote" that exists just to catch the following sorts of situations:

>>> uk_politics.find_party("Didn't vote")
'No vote'

How does this work? We have compiled a list of nicknames we've observed across platforms, websites, and data providers, and then use fuzzy matching the rest of the way. The nicknames are stored in /src/uk_politcs/data/party_nicknames.csv so you can easily add to or alter them, and null values pass straight through as None .

Searching election data

The House of Commons has released historic election data which we have transformed and included as /src/data/GE_results.tsv , and created some tools to allow for filtering by region, country, date, etc.. If you want to see every seat and winning party in the most recent election just run:

# Returns a list of all 650 constituencies and winning parties
>>> uk_politics.elections.seats()
[...]
>>> uk_politics.elections.seats()[0]
(E14000833|NEWCASTLE UPON TYNE NORTH|Tyne and Wear|North East|England|68486, 'Labour Party')

This is a tuple of type (uk_politics.Location, str) , and as you can see the uk_politics.Location object knows the following properties of a constituency:

>>> newcastle_north = uk_politics.elections.seats()[0][0]
>>> newcastle_north.ons_id
'E14000833'
>>> newcastle_north.constituency
'NEWCASTLE UPON TYNE NORTH'
>>> newcastle_north.county
'Tyne and Wear'
>>> newcastle_north.region
'North East'
>>> newcastle_north.country
'England'
>>> newcastle_north.electorate
68486

If you want to see not just who won, but the vote tally for each party those are stored in uk_politics.elections.COUNTS . For example:

>>> import datetime
>>> election_2019 = datetime.date(2019,12,12)
>>> for count in uk_politics.elections.COUNTS:
...     if count.date == election_2019 and count.location.constituency == "DUNDEE EAST":
...         print(f"{count.party}: {count.votes}")
... 
Conservative and Unionist Party: 10986
Labour Party: 6045
Liberal Democrats: 3573
Scottish National Party: 24361
Other: 312

By default uk_politics.elections.seats assumes you want to know about the most recent election, but it has data on all elections back to 1997 (see uk_politics.elections.DATES for a list). You can pass a date as the first argument to uk_politics.elections.seats in order to grab that election data instead. Likewise the function uk_politics.elections.ran

(which grabs a list of all parties with recorded vote counts) can also be passed a date:

>>> uk_politics.elections.ran(datetime.date(2001, 6, 7))
['Conservative and Unionist Party', 'Democratic Unionist Party', 'Labour Party', 'Liberal Democrats', 'Other', 'Plaid Cymru - the Party of Wales', 'Scottish National Party', 'Sinn Féin', 'Social Democratic and Labour Party', 'Speaker', 'Ulster Unionist Party']

Note the Speaker is included as their own party, since they traditionally sever links to any previous affiliation.

You can also use the uk_politics.Location class to filter by any of the properties we listed for newcastle_north above.

>>> wales_filter = uk_politics.Location(country="Wales")
>>> uk_politics.elections.ran(location_filter=wales_filter)
['Conservative and Unionist Party', 'Green Party of England and Wales', 'Labour Party', 'Liberal Democrats', 'Other', 'Plaid Cymru - the Party of Wales', 'Reform UK']
>>> uk_politics.elections.seats(location_filter=wales_filter)
[(W07000065|PRESELI PEMBROKESHIRE|Dyfed|Wales|Wales|59606, 'Conservative and Unionist Party'), ...]

The Location class supports >= and <= comparison; set a property to None to have it act as a wildcard. These comparisons are only designed to work when at least one of the Location objects is fully populated with data.

>>> west_glamorgan = uk_politics.elections.COUNTS[0].location
>>> wales = uk_politics.Location(country="Wales")
>>> wales >= west_glamorgan
True
>>> west_glamorgan >= wales
False

This data is only as nuanced as the House Of Commons historic data, and so smaller parties may have been grouped under Other in some years but not others.

Finding the party color

uk-politics lets you grab a hex code straight from a party name (or nickname).

>>> uk_politics.color("Labour")
'#E4003B'
>>> uk_politics.color("Alliance")
'#F6CB2F'

Here's an example using the seaborn library:

[...]
>>> import seaborn as sns
>>> sns.countplot(y=vote_column, palette={party: uk_politics.color(party) for party in vote_column.unique()})
[...]

Development notes

Spelling: US American English

Linting: pylint, pydocstyle

Testing: pytest

Style guide: https://google.github.io/styleguide/pyguide.html

The code of the project is licensed under MIT license, with data provided under the licenses next to each data file.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

uk-politics-1.0.5.tar.gz (410.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

uk_politics-1.0.5-py3-none-any.whl (412.5 kB view details)

Uploaded Python 3

File details

Details for the file uk-politics-1.0.5.tar.gz.

File metadata

  • Download URL: uk-politics-1.0.5.tar.gz
  • Upload date:
  • Size: 410.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

File hashes

Hashes for uk-politics-1.0.5.tar.gz
Algorithm Hash digest
SHA256 a4b6fbf1a0200f5fc6780e078e579a81c6136617fdecb9e755a3c2a894eb5750
MD5 4dfffea87771ef2b5b3e8716d8d24c97
BLAKE2b-256 725f58952cd225085b82a878a5ef7b03184298fe0d8a41d2ec486921d09d7daa

See more details on using hashes here.

File details

Details for the file uk_politics-1.0.5-py3-none-any.whl.

File metadata

  • Download URL: uk_politics-1.0.5-py3-none-any.whl
  • Upload date:
  • Size: 412.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

File hashes

Hashes for uk_politics-1.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 56d935b1d0a63e730acb607458b098929740bc4e80554ee083bba4fddfe96ddc
MD5 1b007fbd262422e0fdfab33efe1de7c4
BLAKE2b-256 392b3a15ae8aa485b6f637c81b47664848b8175fed054938d68c2d452d12eb11

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page