Page MenuHomePhabricator

Cannot load item page. upperBound KeyError
Closed, DuplicatePublic

Description

I cannot load some item pages from wikidata.

Sample script:

#!/usr/bin/python

import pywikibot

site = pywikibot.Site()
repo = site.data_repository()

item = pywikibot.ItemPage(repo, 'Q626') # Volga river

item.get()

Error Message:

$  python pwb.py volga.py
Traceback (most recent call last):
  File "/data/project/chobot/src/pywikibot-core.new/pwb.py", line 255, in <module>
    if not main():
  File "/data/project/chobot/src/pywikibot-core.new/pwb.py", line 249, in main
    run_python_file(filename, [filename] + args, argvu, file_package)
  File "/data/project/chobot/src/pywikibot-core.new/pwb.py", line 121, in run_python_file
    main_mod.__dict__)
  File "volga.py", line 11, in <module>
    item.get()
  File "/mnt/nfs/labstore-secondary-tools-project/chobot/src/pywikibot-core.new/pywikibot/page.py", line 3931, in get
    data = super(ItemPage, self).get(force, *args, **kwargs)
  File "/mnt/nfs/labstore-secondary-tools-project/chobot/src/pywikibot-core.new/pywikibot/page.py", line 3544, in get
    c = Claim.fromJSON(self.repo, claim)
  File "/mnt/nfs/labstore-secondary-tools-project/chobot/src/pywikibot-core.new/pywikibot/page.py", line 4356, in fromJSON
    claim.type, lambda value, site: value)(value, site)
  File "/mnt/nfs/labstore-secondary-tools-project/chobot/src/pywikibot-core.new/pywikibot/page.py", line 4299, in <lambda>
    'quantity': lambda value, site: pywikibot.WbQuantity.fromWikibase(value),
  File "/mnt/nfs/labstore-secondary-tools-project/chobot/src/pywikibot-core.new/pywikibot/__init__.py", line 675, in fromWikibase
    upperBound = cls._todecimal(wb['upperBound'])
KeyError: u'upperBound'
<type 'exceptions.KeyError'>
CRITICAL: Closing network session.

Pywikibot version:

$ python pwb.py version
$ pwb version
Pywikibot: [https] r-pywikibot-core.git (2dcd409, g7611, 2016/11/18, 00:35:38, n/a)
Release version: 3.0-dev
requests version: 2.2.1
  cacerts: /etc/ssl/certs/ca-certificates.crt
    certificate test: ok
Python: 2.7.6 (default, Jun 22 2015, 17:58:13) 
[GCC 4.8.2]

Event Timeline

Hi @ChongDae, thanks for taking the time to report this!

Is this an issue in Pywikibot, or which project should be associated to this task?

Hi @ChongDae, first of all thank You for your bug report, that's pretty well described issue. In future if You will find another, don't hesitate - just raport it here and attach to Pywikibot project (as @Aklapper did).

And the second issue here is that I'm closing this one as a duplicate of T150210 and adding You to subscribers of the other one. There's already patch in code review that resolve this isssue waiting for successful review.

Hi @ChongDae, first of all thank You for your bug report, that's pretty well described issue. In future if You will find another, don't hesitate - just raport it here and attach to Pywikibot project (as @Aklapper did).

And the second issue here is that I'm closing this one as a duplicate of T150210 and adding You to subscribers of the other one. There's already patch in code review that resolve this isssue waiting for successful review.

I have installed the latest version of Pywikibot and sometimes I get the same error. It happens only with some pages. For instance running:

1  #test.py
2  import pywikibot
3  site = pywikibot.Site("it", "wikipedia")
4  page = pywikibot.Page(site, "Albert Einstein")
5  item = pywikibot.ItemPage.fromPage(page)
6  page = pywikibot.Page(site, "Jefferson Airplane")
7  item = pywikibot.ItemPage.fromPage(page)
8  page = pywikibot.Page(site, "Marocco")
9  item = pywikibot.ItemPage.fromPage(page)

I get the following:

Traceback (most recent call last):
  File "test.py", line 9, in <module>
    item = pywikibot.ItemPage.fromPage(page)
  File "C:\Users\Lorenzo\pywikibot\pywikibot\page.py", line 3556, in fromPage
    if not lazy_load and not i.exists():
  File "C:\Users\Lorenzo\pywikibot\pywikibot\page.py", line 3158, in exists
    self.get()
  File "C:\Users\Lorenzo\pywikibot\pywikibot\page.py", line 3576, in get
    c = Claim.fromJSON(self.repo, claim)
  File "C:\Users\Lorenzo\pywikibot\pywikibot\page.py", line 3995, in fromJSON
    claim.type, lambda value, site: value)(value, site)
  File "C:\Users\Lorenzo\pywikibot\pywikibot\page.py", line 3942, in <lambda>
    'quantity': lambda value, site: pywikibot.WbQuantity.fromWikibase(value),
  File "C:\Users\Lorenzo\pywikibot\pywikibot\__init__.py", line 512, in fromWikibase
    upperBound = eval(wb['upperBound'])
KeyError: u'upperBound'
<type 'exceptions.KeyError'>
CRITICAL: Waiting for 1 network thread(s) to finish. Press ctrl-c to abort

(note the error is only at line 9)
What should I do?

PWB version:

Pywikibot: pywikibot/__init__.py (c4ccfae, -1 (unknown), 2017/03/02, 02:02:52, n/a)
Release version: 2.0rc5
httplib2 version: 0.9.1
  cacerts: C:\Users\Lorenzo\pywikibot\externals\httplib2\python2\httplib2\cacerts.txt
    certificate test: ok
Python: 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)]
  unicode test: ok

@Horcrux92, installing latest Pywikibot version (git master from gerrit or the github mirror) should fix this issue for you.

@Horcrux92, installing latest Pywikibot version (git master from gerrit or the github mirror) should fix this issue for you.

Great, it works! Thank you.

I had the same problem with help of @Ladsgroup. Now it works.
At pywikibot/__init__.py: please, someone, replace these codes. it will solve the bug.

def fromWikibase(cls, wb):
    """
    Create a WbQuanity from the JSON data given by the Wikibase API.

    @param wb: Wikibase JSON
    @type wb: dict
    @rtype: pywikibot.WbQuanity
    """
    amount = cls._todecimal(wb['amount'])
    upperBound = cls._todecimal(wb['upperBound'])
    lowerBound = cls._todecimal(wb['lowerBound'])
    error = (upperBound - amount, amount - lowerBound)
    return cls(amount, wb['unit'], error)

with

def fromWikibase(cls, wb):
    """
    Create a WbQuanity from the JSON data given by the Wikibase API.

    @param wb: Wikibase JSON
    @type wb: dict
    @rtype: pywikibot.WbQuanity
    """
    amount = cls._todecimal(wb['amount'])
    if 'upperBound' in wb:
        upperBound = cls._todecimal(wb['upperBound'])
    else:
        upperBound = None

    if 'lowerBound' in wb:
        lowerBound = cls._todecimal(wb['lowerBound'])
    else:
        lowerBound = None
    if upperBound is None or lowerBound is None:
        error = 0
    else:
        error = (upperBound - amount, amount - lowerBound)
    return cls(amount, wb['unit'], error)