#!/usr/bin/env python # coding: utf-8 # # UK Parliament ODataService # # 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. # # 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). # In[2]: #!pip3 install git+https://github.com/tuomur/python-odata.git # In[3]: from odata import ODataService url = 'http://services.odata.org/V4/Northwind/Northwind.svc/' Service = ODataService(url, reflect_entities=True) Supplier = Service.entities['Supplier'] query = Service.query(Supplier) query = query.limit(2) query = query.order_by(Supplier.CompanyName.asc()) for supplier in query: print('Company:', supplier.CompanyName) for product in supplier.Products: print('- Product:', product.ProductName) # In[50]: url='https://api.parliament.uk/Staging/odata/' Service = ODataService(url, reflect_entities=True) # In[191]: ', '.join(sorted(list(Service.entities.keys()))) # In[159]: def get_attributes(entity): return [i for i in dir(entity) if not i.startswith('_')] # In[185]: House = Service.entities['House'] # In[186]: get_attributes(House) # In[187]: query = Service.query(House) query = query.limit(5) for q in query: print(q.HouseName) # In[211]: get_attributes(b) # In[216]: query = Service.query(House) query = query.limit(5) for q in query: print(q.HouseName,'\n') for b in q.HouseHasFormalBody[:3]: print(b.FormalBodyName) print(b.FormalBodyStartDate) print('\n') # In[201]: Member = Service.entities['Member'] get_attributes(Member) # In[236]: query = Service.query(Member) query = query.limit(5) for q in query: print(q.PersonGivenName,q.PersonFamilyName ) for i in (q.MemberHasParliamentaryIncumbency): print(i.ParliamentaryIncumbencyStartDate) # In[ ]: