# -*- coding: utf-8  -*-

import pywikibot
import json
site = pywikibot.Site("wikidata", "wikidata")
repo = site.data_repository()

def mark_as_part_of_current_landtag(politican_item_id):
    item = pywikibot.ItemPage(repo, politican_item_id)
    claims = item.get()['claims']['P39']
    
    legislaturperiode = u'P2937'
    landtag_period = u"Q27949540" # 16th NRW

    # Q17781726 is: Mitglied des Lantags Nordrhein-Westfahlen
    landtag_membership = next(claim for claim in claims if claim.target.id == 'Q17781726')

    qualifier = pywikibot.Claim(repo, legislaturperiode)
    target = pywikibot.ItemPage(repo, landtag_period)
    qualifier.setTarget(target)    
    landtag_membership.addQualifier(qualifier)

    item.editEntity(summary='pyWikibot politican in Wahlperiode')
    
mark_as_part_of_current_landtag(u"Q1322650")
WARNING: API error modification-failed: The statement has already a qualifier with hash f0bd3825e7138883b70a876266e5a2e4babe7dc1
---------------------------------------------------------------------------
APIError                                  Traceback (most recent call last)
<ipython-input-1-d4aca7a5ceb3> in <module>()
     23     item.editEntity(summary='pyWikibot politican in Wahlperiode')
     24 
---> 25 mark_as_part_of_current_landtag(u"Q1322650")
     26 
     27 

<ipython-input-1-d4aca7a5ceb3> in mark_as_part_of_current_landtag(politican_item_id)
     19     target = pywikibot.ItemPage(repo, landtag_period)
     20     qualifier.setTarget(target)
---> 21     landtag_membership.addQualifier(qualifier)
     22 
     23     item.editEntity(summary='pyWikibot politican in Wahlperiode')

/srv/paws/pwb/pywikibot/page.py in addQualifier(self, qualifier, **kwargs)
   4734         @type qualifier: Claim
   4735         """
-> 4736         data = self.repo.editQualifier(self, qualifier, **kwargs)
   4737         qualifier.isQualifier = True
   4738         self.on_item.latest_revision_id = data['pageinfo']['lastrevid']

/srv/paws/pwb/pywikibot/site.py in callee(self, *args, **kwargs)
   1328             else:
   1329                 raise Exception("Not implemented")
-> 1330             return fn(self, *args, **kwargs)
   1331 
   1332         if not __debug__:

/srv/paws/pwb/pywikibot/site.py in editQualifier(self, claim, qualifier, new, bot, summary, baserevid)
   7493 
   7494         req = self._simple_request(**params)
-> 7495         data = req.submit()
   7496         return data
   7497 

/srv/paws/pwb/pywikibot/data/api.py in submit(self)
   2187                               % result)
   2188 
-> 2189                 raise APIError(**result['error'])
   2190             except TypeError:
   2191                 raise RuntimeError(result)

APIError: modification-failed: The statement has already a qualifier with hash f0bd3825e7138883b70a876266e5a2e4babe7dc1 [help:See https://www.wikidata.org/w/api.php for API usage. Subscribe to the mediawiki-api-announce mailing list at &lt;https://lists.wikimedia.org/mailman/listinfo/mediawiki-api-announce&gt; for notice of API deprecations and breaking changes.]