dahlia / wikidata

Wikidata client library for Python
https://pypi.org/project/Wikidata/
GNU General Public License v3.0
337 stars 31 forks source link

wikidata.datavalue.DatavalueError: 9: time precision other than 11 or 14 is unsupported #6

Closed az0 closed 4 years ago

az0 commented 6 years ago

This date of birth is just the year 1950 instead of the full date. How would I get the partial data 1950 without this exception?

>>> from wikidata.client import Client                                              
>>> client = Client()  # doctest: +SKIP                                             
>>> prop_dob = client.get('P569')                                                      
>>> entity = client.get('Q4794599')
>>> entity[prop_dob]

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/z/.local/lib/python3.5/site-packages/wikidata/entity.py", line 160, in __getitem__
    result = self.getlist(key)
  File "/home/z/.local/lib/python3.5/site-packages/wikidata/entity.py", line 191, in getlist
    for snak in (claim['mainsnak'] for claim in claims)]
  File "/home/z/.local/lib/python3.5/site-packages/wikidata/entity.py", line 191, in <listcomp>
    for snak in (claim['mainsnak'] for claim in claims)]
  File "/home/z/.local/lib/python3.5/site-packages/wikidata/client.py", line 178, in decode_datavalue
    return decode(self, datatype, datavalue)
  File "/home/z/.local/lib/python3.5/site-packages/wikidata/datavalue.py", line 127, in __call__
    return method(client, datavalue)
  File "/home/z/.local/lib/python3.5/site-packages/wikidata/datavalue.py", line 210, in time
    datavalue
wikidata.datavalue.DatavalueError: 9: time precision other than 11 or 14 is unsupported: {'type': 'time', 'value': {'precision': 9, 'timezone': 0, 'after': 0, 'before': 0, 'time': '+1950-01-01T00:00:00Z', 'calendarmodel': 'http://www.wikidata.org/entity/Q1985727'}}
dahlia commented 6 years ago

You can always access to its underlying data through attributes property (though it's undocumented), e.g.:

entity.attributes['claims'][prop_dob.id]
az0 commented 6 years ago

OK, I see the year 1950 here, and the precison=9 means year. Thank you

entity.attributes['claims'][prop_dob.id][0]['mainsnak']['datavalue']['value']['time']

nelson-liu commented 4 years ago

This should be closed with https://github.com/dahlia/wikidata/pull/26