Topic on User talk:Matěj Suchánek

Tozibb (talkcontribs)

Hi Matěj Suchánek,

thanks for your reply to me question concerning changeRank() on the project page. I would like to ask you if you could give me the code you used to change the rank? Either link to some repo, https://pastebin.com/ or posting it here/my talk page/project page would be helpful.

Thank you!

Matěj Suchánek (talkcontribs)
import pywikibot
repo = pywikibot.Site('wikidata', 'wikidata')
sandbox = pywikibot.ItemPage(repo, 'Q4115189')
claim = pywikibot.Claim(repo, 'P369')
claim.setTarget(pywikibot.ItemPage(repo, 'Q1'))
sandbox.addClaim(claim)
claim.changeRank('preferred')
Tozibb (talkcontribs)

It works now, thank you! I had to change the order of my calls though, which makes me wonder... My original attempt was to first add the 'point in time" source then set the claim to 'preferred' - which failed for some reason.

item.addClaim(alexa_ranking_claim, summary=u'Updating Alexa ranking')#1st
retrieved_source = _get_point_in_time_source()
alexa_ranking_claim.addSources([retrieved_source], summary=u'Adding retrieve date.')#2nd
alexa_ranking_claim.changeRank('preferred')#3rd

When changing the the execution order to set the rank to 'preferred' and then adding the sources the call works fine, i.e. no error and both the new ranking as well as the point in time source are taken in consideration

item.addClaim(alexa_ranking_claim, summary=u'Updating Alexa ranking')#1st
alexa_ranking_claim.changeRank('preferred')#3rd
retrieved_source = _get_point_in_time_source()
alexa_ranking_claim.addSources([retrieved_source], summary=u'Adding retrieve date.')2nd

Anyway - thanks again for your help! --Tozibb (talk) 18:56, 16 October 2017 (UTC)

Matěj Suchánek (talkcontribs)

This is interesting. I tried your code and found the problem.

When you construct the source inside _get_point_in_time_source(), you are probably not setting isReference=True via constructor (I did niether while testing). This is not problem when attaching it to a claim as a reference (ie. addSources). However, now the rank can only be changed by serializing the whole claim. When this happens, your reference gets serialized as well. The parent claim "thinks" it's a reference but the reference disagress. So the script fails.