Created
January 24, 2018 17:43
-
-
Save psychemedia/d41abc807c753df2908fe0019674731c to your computer and use it in GitHub Desktop.
First attempt PDS Odata
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "# UK Parliament ODataService\n", | |
| "\n", | |
| "A PDS blogpost today - [Accessing semantic data with OData web interface](https://pds.blog.parliament.uk/2018/01/24/accessing-semantic-data-with-odata-web-interface/)- announced a new RESTful JSON web API to the PDS OData service under OData v4.\n", | |
| "\n", | |
| "I've dabbled with this before in OData2 land ([Calling an OData Service From Python – UK Parliament Members Data Platform](https://blog.ouseful.info/2016/03/23/calling-an-odata-service-from-python-uk-parliament-members-data-platform/)) but I think the package that I used then has rotted a bit, so here's another attempt, this time using the `odata` package from [tuomur/python-odata](https://github.com/tuomur/python-odata)." | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 2, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "#!pip3 install git+https://github.com/tuomur/python-odata.git" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 3, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "Company: Aux joyeux ecclésiastiques\n", | |
| "- Product: Côte de Blaye\n", | |
| "- Product: Chartreuse verte\n", | |
| "Company: Bigfoot Breweries\n", | |
| "- Product: Sasquatch Ale\n", | |
| "- Product: Steeleye Stout\n", | |
| "- Product: Laughing Lumberjack Lager\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "from odata import ODataService\n", | |
| "url = 'http://services.odata.org/V4/Northwind/Northwind.svc/'\n", | |
| "Service = ODataService(url, reflect_entities=True)\n", | |
| "Supplier = Service.entities['Supplier']\n", | |
| "\n", | |
| "query = Service.query(Supplier)\n", | |
| "query = query.limit(2)\n", | |
| "query = query.order_by(Supplier.CompanyName.asc())\n", | |
| "\n", | |
| "for supplier in query:\n", | |
| " print('Company:', supplier.CompanyName)\n", | |
| "\n", | |
| " for product in supplier.Products:\n", | |
| " print('- Product:', product.ProductName)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 50, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "url='https://api.parliament.uk/Staging/odata/'\n", | |
| "Service = ODataService(url, reflect_entities=True)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 191, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'AlternateMembership, Approval, ApprovedEPetition, Candidacy, CandidacyResult, Candidate, Concept, ConceptualisedThing, ConstituencyArea, ConstituencyGroup, ContactPoint, ContactableThing, Country, Debate, DeceasedPerson, DodsPerson, DodsThing, EPetition, Election, ElectionType, ElectoralIncumbency, Electorate, ExOfficioMembership, ExternalThing, FormalBody, FormalBodyMembership, Gender, GenderIdentity, GeographicalThing, GovRegisterCountry, GovRegisterTerritory, GovRegisterThing, GovernmentDepartment, GovernmentIncumbency, GovernmentPerson, GovernmentPosition, GovernmentResponse, House, HouseSeat, HouseSeatType, Image, ImageSubject, Incumbency, LocatedSignatureCount, Logo, LogoImage, Member, MemberImage, MnisConstituencyGroup, MnisContactPoint, MnisElectionType, MnisFormalBody, MnisFormalBodyMembership, MnisGender, MnisGovernmentDepartment, MnisGovernmentIncumbency, MnisGovernmentPosition, MnisHouseSeatType, MnisOppositionIncumbency, MnisOppositionPosition, MnisParty, MnisPartyMembership, MnisPerson, MnisSeatIncumbency, MnisThing, Moderation, NamedThing, OnsConstituencyGroup, OnsThing, OppositionIncumbency, OppositionPerson, OppositionPosition, ParliamentPeriod, ParliamentaryIncumbency, Party, PartyMember, PartyMembership, PastConstituencyGroup, PastFormalBody, PastFormalBodyMembership, PastIncumbency, PastParliamentPeriod, PastParliamentaryIncumbency, PastPartyMembership, PastThing, Person, PersonImage, PersonWebLink, PimsPerson, PimsThing, Place, Position, PostalAddress, RejectedEPetition, Rejection, RejectionCode, SeatIncumbency, SkosConcept, SubjectTaggedThing, TemporalThing, Territory, ThingWithLogo, Threshold, ThresholdAttainment, UkgapEPetition, UkgapThing, WebLink, WebLinkedThing, WikidataParliamentPeriod, WikidataThing'" | |
| ] | |
| }, | |
| "execution_count": 191, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "', '.join(sorted(list(Service.entities.keys())))" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 159, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "def get_attributes(entity):\n", | |
| " return [i for i in dir(entity) if not i.startswith('_')]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 185, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "House = Service.entities['House']" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 186, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "['HouseHasFormalBody', 'HouseHasHouseSeat', 'HouseName', 'Id']" | |
| ] | |
| }, | |
| "execution_count": 186, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "get_attributes(House)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 187, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "House of Commons\n", | |
| "House of Lords\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "query = Service.query(House)\n", | |
| "query = query.limit(5)\n", | |
| "\n", | |
| "for q in query:\n", | |
| " print(q.HouseName)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 211, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "['FormalBodyHasFormalBodyMembership',\n", | |
| " 'FormalBodyHasHouse',\n", | |
| " 'FormalBodyName',\n", | |
| " 'FormalBodyStartDate',\n", | |
| " 'Id']" | |
| ] | |
| }, | |
| "execution_count": 211, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "get_attributes(b)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 216, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "House of Commons \n", | |
| "\n", | |
| "Culture, Media and Sport Committee\n", | |
| "1997-07-28 00:00:00+00:00\n", | |
| "Public Service\n", | |
| "1995-01-01 00:00:00+00:00\n", | |
| "Members' Expenses Committee\n", | |
| "2011-07-18 00:00:00+00:00\n", | |
| "\n", | |
| "\n", | |
| "House of Lords \n", | |
| "\n", | |
| "Procedure Committee (Lords)\n", | |
| "1974-11-12 00:00:00+00:00\n", | |
| "Relations between Central and Local Government, Committee on\n", | |
| "1995-11-27 00:00:00+00:00\n", | |
| "BBC Charter Review Committee\n", | |
| "2005-03-01 00:00:00+00:00\n", | |
| "\n", | |
| "\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "query = Service.query(House)\n", | |
| "query = query.limit(5)\n", | |
| "\n", | |
| "for q in query:\n", | |
| " print(q.HouseName,'\\n')\n", | |
| " for b in q.HouseHasFormalBody[:3]:\n", | |
| " print(b.FormalBodyName)\n", | |
| " print(b.FormalBodyStartDate)\n", | |
| " print('\\n')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 201, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "['ContactableThingHasContactPoint',\n", | |
| " 'Id',\n", | |
| " 'MemberHasAlternateMembership',\n", | |
| " 'MemberHasExOfficioMembership',\n", | |
| " 'MemberHasMemberImage',\n", | |
| " 'MemberHasParliamentaryIncumbency',\n", | |
| " 'PersonDateOfBirth',\n", | |
| " 'PersonFamilyName',\n", | |
| " 'PersonGivenName',\n", | |
| " 'PersonHasContactPoint',\n", | |
| " 'PersonHasFormalBodyMembership',\n", | |
| " 'PersonHasGenderIdentity',\n", | |
| " 'PersonHasPersonImage',\n", | |
| " 'PersonHasPersonWebLink',\n", | |
| " 'PersonOtherNames']" | |
| ] | |
| }, | |
| "execution_count": 201, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "Member = Service.entities['Member']\n", | |
| "get_attributes(Member)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 236, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "Dawn Primarolo\n", | |
| "1987-06-11 00:00:00+00:00\n", | |
| "2001-06-07 00:00:00+00:00\n", | |
| "1997-05-01 00:00:00+00:00\n", | |
| "2010-05-06 00:00:00+00:00\n", | |
| "1992-04-09 00:00:00+00:00\n", | |
| "2005-05-05 00:00:00+00:00\n", | |
| "2015-10-26 00:00:00+00:00\n", | |
| "Lyn Brown\n", | |
| "2017-06-08 00:00:00+00:00\n", | |
| "2015-05-07 00:00:00+00:00\n", | |
| "2005-05-05 00:00:00+00:00\n", | |
| "2010-05-06 00:00:00+00:00\n", | |
| "Margaret Hodge\n", | |
| "2015-05-07 00:00:00+00:00\n", | |
| "2005-05-05 00:00:00+00:00\n", | |
| "2001-06-07 00:00:00+00:00\n", | |
| "1997-05-01 00:00:00+00:00\n", | |
| "2010-05-06 00:00:00+00:00\n", | |
| "1994-06-09 00:00:00+00:00\n", | |
| "2017-06-08 00:00:00+00:00\n", | |
| "Sandip Verma\n", | |
| "2006-06-02 00:00:00+00:00\n", | |
| "Jennifer Randerson\n", | |
| "2011-01-27 00:00:00+00:00\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "query = Service.query(Member)\n", | |
| "query = query.limit(5)\n", | |
| "\n", | |
| "for q in query:\n", | |
| " print(q.PersonGivenName,q.PersonFamilyName )\n", | |
| " for i in (q.MemberHasParliamentaryIncumbency):\n", | |
| " print(i.ParliamentaryIncumbencyStartDate)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [] | |
| } | |
| ], | |
| "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.6.4" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 2 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment