In [1]:
import pywikibot
from pywikibot import pagegenerators as pg

site = pywikibot.Site('wikidata', 'wikidata')
repo = site.data_repository()
In [2]:
query = """
SELECT ?item ?itemLabel WHERE {
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
  ?item wdt:P31 wd:Q24649. 
  ?item wdt:P463 wd:Q60310569
}
LIMIT 100
"""
In [3]:
parties = list(pg.WikidataSPARQLPageGenerator(query, site=site))
In [4]:
member_of = "P463"
register = pywikibot.ItemPage(repo, "Q60310569")

def claims(item, id):
    content = item.get()
    claims = content["claims"]
    claim = claims[id]
    return claim

def target_claim(item, prop_id, target):
    for i, claim in enumerate(claims(item, prop_id)):
        if claim.getTarget() == target:
            return (i, claim)
In [5]:
no_value_end_date = pywikibot.Claim.fromJSON(site, {"mainsnak":{'snaktype': 'novalue', 'property': 'P582', 'hash': 'f148a727ee614bf2862cfc5c4730f50936c281de'}})
In [6]:
for party in parties:
    party_dict = party.get()
    print(party, party_dict["labels"]["en"])
    i, claim = target_claim(party, member_of, register)
    print("    ", i, claim)
    print("    ", "*", claim.qualifiers)
    for q in claim.qualifiers:
        print("           ", q)
    try:
        claim.addQualifier(no_value_end_date)
    except:
        print("skipping")
    party.save()
[[wikidata:Q1577483]] European Conservatives and Reformists Party
     0 Claim.fromJSON(DataSite("wikidata", "wikidata"), {'mainsnak': {'snaktype': 'value', 'property': 'P463', 'datatype': 'wikibase-item', 'datavalue': {'value': {'entity-type': 'item', 'numeric-id': 60310569}, 'type': 'wikibase-entityid'}}, 'type': 'statement', 'id': 'Q1577483$be3081a6-429f-9ef5-b255-8089e116fd70', 'rank': 'normal', 'qualifiers': {'P582': [{'snaktype': 'novalue', 'property': 'P582', 'hash': 'f148a727ee614bf2862cfc5c4730f50936c281de'}]}, 'qualifiers-order': ['P582']})
     * OrderedDict([('P582', [Claim.fromJSON(DataSite("wikidata", "wikidata"), {'snaktype': 'novalue', 'property': 'P582', 'hash': 'f148a727ee614bf2862cfc5c4730f50936c281de'})])])
            P582
Sleeping for 19.2 seconds, 2019-11-03 00:47:46
WARNING: API error modification-failed: The statement has already a qualifier with hash f148a727ee614bf2862cfc5c4730f50936c281de
skipping
Sleeping for 19.5 seconds, 2019-11-03 00:48:06
WARNING: API error wikibase-no-direct-editing: Direct editing is disabled in namespace
---------------------------------------------------------------------------
APIError                                  Traceback (most recent call last)
/srv/paws/pwb/pywikibot/page.py in handle(func, self, *args, **kwargs)
    106         try:
--> 107             func(self, *args, **kwargs)
    108         # TODO: other "expected" error types to catch?

/srv/paws/pwb/pywikibot/page.py in _save(self, summary, watch, minor, botflag, cc, quiet, **kwargs)
   1292         done = self.site.editpage(self, summary=summary, minor=minor,
-> 1293                                   watch=watch, bot=botflag, **kwargs)
   1294         if not done:

/srv/paws/pwb/pywikibot/site.py in callee(self, *args, **kwargs)
   1319                 raise Exception('Not implemented')
-> 1320             return fn(self, *args, **kwargs)
   1321 

/srv/paws/pwb/pywikibot/site.py in editpage(self, page, summary, minor, notminor, bot, recreate, createonly, nocreate, watch, **kwargs)
   5266                 try:
-> 5267                     result = req.submit()
   5268                     pywikibot.debug('editpage response: %s' % result,

/srv/paws/pwb/pywikibot/data/api.py in submit(self)
   2078 
-> 2079                 raise APIError(**result['error'])
   2080             except TypeError:

APIError: wikibase-no-direct-editing: Direct editing is disabled in namespace [help:See https://www.wikidata.org/w/api.php for API usage. Subscribe to the mediawiki-api-announce mailing list at <https://lists.wikimedia.org/mailman/listinfo/mediawiki-api-announce> for notice of API deprecations and breaking changes.]

During handling of the above exception, another exception occurred:

OtherPageSaveError                        Traceback (most recent call last)
<ipython-input-6-dfbe29f53c90> in <module>
     11     except:
     12         print("skipping")
---> 13     party.save()

/srv/paws/pwb/pywikibot/tools/__init__.py in wrapper(*__args, **__kw)
   1736                              cls, depth)
   1737                     del __kw[old_arg]
-> 1738             return obj(*__args, **__kw)
   1739 
   1740         if not __debug__:

/srv/paws/pwb/pywikibot/tools/__init__.py in wrapper(*__args, **__kw)
   1736                              cls, depth)
   1737                     del __kw[old_arg]
-> 1738             return obj(*__args, **__kw)
   1739 
   1740         if not __debug__:

/srv/paws/pwb/pywikibot/page.py in save(self, summary, watch, minor, botflag, force, asynchronous, callback, apply_cosmetic_changes, quiet, **kwargs)
   1280         self._save(summary=summary, watch=watch, minor=minor, botflag=botflag,
   1281                    asynchronous=asynchronous, callback=callback,
-> 1282                    cc=apply_cosmetic_changes, quiet=quiet, **kwargs)
   1283 
   1284     @allow_asynchronous

/srv/paws/pwb/pywikibot/page.py in wrapper(self, *args, **kwargs)
    126             pywikibot.async_request(handle, func, self, *args, **kwargs)
    127         else:
--> 128             handle(func, self, *args, **kwargs)
    129 
    130     manage_wrapping(wrapper, func)

/srv/paws/pwb/pywikibot/page.py in handle(func, self, *args, **kwargs)
    118                 if isinstance(err, pywikibot.PageSaveRelatedError):
    119                     raise err
--> 120                 raise pywikibot.OtherPageSaveError(self, err)
    121         if callback:
    122             callback(self, err)

OtherPageSaveError: Edit to page [[wikidata:Q1577483]] failed:
wikibase-no-direct-editing: Direct editing is disabled in namespace [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.]
In [ ]: