virgesmith / UKCensusAPI

UK Census Data queries and downloads from python or R
MIT License
33 stars 16 forks source link

Issues with Py Module - no attribute 'get_metadata' #44

Closed psychemedia closed 5 years ago

psychemedia commented 5 years ago

Running:

import ukcensusapi.Nomisweb as census_api

census_api.get_metadata('NS-SeC by ethnic group by sex by age')

gives:

 AttributeError: module 'ukcensusapi.Nomisweb' has no attribute 'get_metadata'

and checking census_api methods gives:

dir(census_api)

['HTTPError',
 'Nomisweb',
 'OrderedDict',
 'Path',
 'URLError',
 '__builtins__',
 '__cached__',
 '__doc__',
 '__file__',
 '__loader__',
 '__name__',
 '__package__',
 '__spec__',
 '_get_api_key',
 '_shorten',
 'hashlib',
 'json',
 'os',
 'pd',
 'request',
 'timeout',
 'urlencode',
 'utils',
 'warnings']

The required method is available from ukcensusapi.Nomisweb.Nomisweb:

dir(census_api.Nomisweb)

['GeoCodeLookup',
 'Timeout',
 'URL',
 '_Nomisweb__cache_lad_codes',
 '_Nomisweb__fetch_json',
 '__class__',
 '__delattr__',
 '__dict__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__gt__',
 '__hash__',
 '__init__',
 '__le__',
 '__lt__',
 '__module__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '__weakref__',
 'contextify',
 'get_data',
 'get_geo_codes',
 'get_lad_codes',
 'get_metadata',
 'get_url',
 'load_metadata',
 'write_metadata']

but then the self element is not respected:

census_api.Nomisweb.get_metadata(table_name='NS-SeC by ethnic group by sex by age')

TypeError: get_metadata() missing 1 required positional argument: 'self' 

You can see the errors if you run the code via a Jupyter Python3 notebook on MyBinder (it may take some time to load...):

https://mybinder.org/v2/gh/virgesmith/UKCensusAPI/master

virgesmith commented 5 years ago

its because you haven't initialised Nomisweb, its a stateful object that requires initialisation. Do:

>>> import ukcensusapi.Nomisweb as census_api
>>> api = census_api.Nomisweb("/tmp")
>>> print(api.get_metadata("KS101EW"))
{'nomis_table': 'NM_144_1', 'description': 'KS101EW - Usual resident population', 'fields': {'GEOGRAPHY': {2092957703: 'England and Wales', 2092957699: 'England', 2092957700: 'Wales'}, 'RURAL_URBAN': {0: 'Total', 100: 'Urban (total)', 2: 'Urban major conurbation', 3: 'Urban minor conurbation', 4: 'Urban city and town', 1: 'Urban city and town in a sparse setting', 101: 'Rural (total)', 8: 'Rural town and fringe', 5: 'Rural town and fringe in a sparse setting', 9: 'Rural village', 6: 'Rural village in a sparse setting', 10: 'Rural hamlet and isolated dwellings', 7: 'Rural hamlet and isolated dwellings in a sparse setting'}, 'CELL': {0: 'All usual residents', 1: 'Males', 2: 'Females', 3: 'Lives in a household', 4: 'Lives in a communal establishment', 5: 'Schoolchild or full-time student aged 4 and over at their non term-time address', 6: 'Area (Hectares)', 7: 'Density (number of persons per hectare)'}, 'MEASURES': {20100: 'value', 20301: 'percent'}, 'FREQ': {'A': 'Annually'}}, 'geographies': {'TYPE265': 'NHS area teams', 'TYPE266': 'clinical commissioning groups', 'TYPE267': 'built-up areas including subdivisions', 'TYPE269': 'built-up areas', 'TYPE273': 'national assembly for wales electoral regions 2010', 'TYPE274': 'postcode areas', 'TYPE275': 'postcode districts', 'TYPE276': 'postcode sectors', 'TYPE277': 'national assembly for wales constituencies 2010', 'TYPE279': 'parishes 2011', 'TYPE282': '2011 local health boards', 'TYPE283': '2011 primary care trusts', 'TYPE284': '2011 strategic health authorities', 'TYPE295': '2011 wards', 'TYPE297': '2011 super output areas - middle layer', 'TYPE298': '2011 super output areas - lower layer', 'TYPE299': '2011 output areas', 'TYPE459': 'local enterprise partnerships (as of April 2017)', 'TYPE460': 'parliamentary constituencies 2010', 'TYPE462': 'former metropolitan counties', 'TYPE463': 'local authorities: county / unitary (prior to April 2015)', 'TYPE464': 'local authorities: district / unitary (prior to April 2015)', 'TYPE480': 'regions', 'TYPE499': 'countries'}}

Asking for api.get_metadata("NS-SeC by ethnic group by sex by age") won't work anyway because thats not a valid table name.