import pywikibot
import time

wikidata_site = pywikibot.Site('wikidata', 'wikidata')
repo = wikidata_site.data_repository()

wikipedia_site = pywikibot.Site('de', 'wikipedia')

rankToSet = 'deprecated' # normal preferred deprecated

prop = 'P227'
inputdata = { 
    'Q59127270' : '1108981429', 'Q50788961' : '1117790150', 'Q61053799' : '115011919', 'Q61054132' : '1082908460', 'Q16591585' : '103244158', 'Q62479297' : '101886586', 'Q2958857' : '102019746', 'Q62834247' : '112544053',
}
waitTime = 5
editSummary = 'set undifferentiated (type Tn) GND identifier "{}" to deprecated rank'

print('Found {} input elements'.format(len(inputdata)))

for i, item in enumerate(inputdata):
    Qitem = pywikibot.ItemPage(repo, item)
    Qitem.get()
    
    print()
    print('=== Item {} ({}/{}) ==='.format(item, i+1, len(inputdata)))
    if not Qitem.claims or prop not in Qitem.claims:
        print('* No claims for or no {} claims found; skip item'.format(prop))
        continue
        
    for claim in Qitem.claims[prop]:
        if claim.getTarget() != inputdata[item]:
            print('* Found differing value "{}" with rank "{}"'.format(claim.getTarget(), claim.getRank()))
            continue
        
        if claim.getRank() == rankToSet:
            print('* Rank to set "{}" already set for value "{}"'.format(rankToSet, claim.getTarget()))
            continue
        
        print('* Found value "{}" with rank "{}"'.format(claim.getTarget(), claim.getRank()))
        
        claim.changeRank(rankToSet, summary=editSummary.format(inputdata[item]))
        print('** Set {} --> {} --> "{}" to "{} rank"'.format(item, prop, claim.getTarget(), rankToSet))
        
        #if 'dewiki' in Qitem.sitelinks:
        #    wikiPage = pywikibot.Page(wikipedia_site, Qitem.sitelinks['dewiki'])
        #    wikiPage.touch(quiet=True)
        #    print('** Touched connected dewiki page "{}"'.format(Qitem.sitelinks['dewiki']))
        #    time.sleep(waitTime)
            
print()
print('All done, task finished')
Found 8 input elements

=== Item Q59127270 (1/8) ===
* Found value "1108981429" with rank "normal"
** Set Q59127270 --> P227 --> "1108981429" to "deprecated rank"
* Found differing value "1148894861" with rank "deprecated"

=== Item Q50788961 (2/8) ===
* Found value "1117790150" with rank "normal"
** Set Q50788961 --> P227 --> "1117790150" to "deprecated rank"
* Found differing value "124196578" with rank "deprecated"

=== Item Q61053799 (3/8) ===
* Found value "115011919" with rank "normal"
** Set Q61053799 --> P227 --> "115011919" to "deprecated rank"
* Found differing value "1068461152" with rank "deprecated"

=== Item Q61054132 (4/8) ===
* Found value "1082908460" with rank "normal"
** Set Q61054132 --> P227 --> "1082908460" to "deprecated rank"
* Found differing value "1051364892" with rank "deprecated"

=== Item Q16591585 (5/8) ===
* Found value "103244158" with rank "normal"
** Set Q16591585 --> P227 --> "103244158" to "deprecated rank"
* Found differing value "1089164971" with rank "normal"
* Found differing value "1087837278" with rank "deprecated"

=== Item Q62479297 (6/8) ===
* Found value "101886586" with rank "normal"
** Set Q62479297 --> P227 --> "101886586" to "deprecated rank"
* Found differing value "115724470X" with rank "normal"
* Found differing value "178709727" with rank "deprecated"

=== Item Q2958857 (7/8) ===
* Found value "102019746" with rank "normal"
** Set Q2958857 --> P227 --> "102019746" to "deprecated rank"

=== Item Q62834247 (8/8) ===
* Found value "112544053" with rank "normal"
** Set Q62834247 --> P227 --> "112544053" to "deprecated rank"

All done, task finished