Page MenuHomePhabricator

Allow to add more than one value for the same claim at the same time
Closed, InvalidPublic

Description

Hello, first of all sorry if it is not a bug of if this feature already exists. I am learning python and pywikibot so it could be my mistake. What I want to do is presented here.
In summary, this piece of code illustrates my point (I hope)

p_stated_in = 'P248'
article_qid = ['Q45846010', 'Q45851113']
source_map = {p_stated_in: ['item',   article_qid]}
create_source_claim(new_claim, source_map)

def create_source_claim(claim, source_map):
    source_claims = []
    for src_prop in source_map.keys():
        target_type, source_value = source_map[src_prop]
        print('target: {0} , source: {1}, prop: {2}').format(target_type, source_value,src_prop)
        source_claim = pywikibot.Claim(repo, src_prop, isReference=True)
        if target_type == 'item':
            for qid in source_value:
                source_page = pywikibot.ItemPage(repo, qid)
                source_claim.setTarget(source_page)
                source_claims.append(source_claim)
        else:
            source_claim.setTarget(source_value)
        source_claims.append(source_claim)
    claim.addSources(source_claims, bot=True)
    return True

But doing that P248 has only the "Q45851113" value; "Q45846010" is not taken into account. Could you tell me how to do that?
And once more excuse me if this is not the right place but I tried to get some help from Wikidata previously but nothing.

Event Timeline

Pamputt created this task.Dec 21 2017, 7:11 PM
Restricted Application added subscribers: pywikibot-bugs-list, Aklapper. · View Herald TranscriptDec 21 2017, 7:11 PM
Pamputt renamed this task from Enable to add more than one value for the same claim at one same time to Allow to add more than one value for the same claim at one same time.Dec 21 2017, 7:46 PM
Pamputt renamed this task from Allow to add more than one value for the same claim at one same time to Allow to add more than one value for the same claim at the same time.
matej_suchanek closed this task as Invalid.Jan 13 2018, 11:29 AM
matej_suchanek added a subscriber: matej_suchanek.

The problem is you are still working with the same pywikibot.Claim instance (source_claim). The resulting source_claims is just a list of the same object. You should create a new object for each source like:

p_stated_in = 'P248'
article_qid = ['Q45846010', 'Q45851113']
source_map = {p_stated_in: ['item',   article_qid]}
create_source_claim(new_claim, source_map)

def create_source_claim(claim, source_map):
    source_claims = []
    for src_prop in source_map.keys():
        target_type, source_values = source_map[src_prop]
        print('target: {0} , source: {1}, prop: {2}').format(target_type, source_value,src_prop)

        if target_type == 'item':
            for qid in source_values:
                source_claim = pywikibot.Claim(repo, src_prop, isReference=True)
                source_page = pywikibot.ItemPage(repo, qid)
                source_claim.setTarget(source_page)
                source_claims.append(source_claim)
        else:
            source_claim = pywikibot.Claim(repo, src_prop, isReference=True)
            source_claim.setTarget(source_value)
            source_claims.append(source_claim)
    claim.addSources(source_claims, bot=True)
    return True