In [1]:
import pywikibot
import time
from pywikibot import pagegenerators

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

inputdata = []
dataset_query = """SELECT DISTINCT ?item WHERE {
  ?item p:P1087 [ pq:P585 ?date; wikibase:rank ?rank ]; wdt:P570 ?dod .
  FILTER(?rank != wikibase:DeprecatedRank) .
  FILTER(?date >= ?dod) .
}"""
for itemkey in pagegenerators.WikidataSPARQLPageGenerator(dataset_query, site=wikidata_site):
    inputdata.append(itemkey.title())

for i, item in enumerate(inputdata):
    #Qitem = pywikibot.ItemPage(repo, item)
    #Qitem.get()
    
    while True:
        try:
            Qitem = pywikibot.ItemPage(repo, item)
            Qitem.get()
            break
        except pywikibot.exceptions.MaxlagTimeoutError as e:
            print(e)
            time.sleep(10)    
    
    print()
    print('=== Item {} ({}/{}) ==='.format(item, i+1, len(inputdata)))
    if not Qitem.claims:
        print('* No claims found; skip item')
        continue
    
    if 'P570' not in Qitem.claims:
        print('* No date of death found; skip item')
        continue
        
    if len(Qitem.claims['P570'])>1:
        print('* More than one date of death found; skip item')
        continue
        
    dod = Qitem.claims['P570'][0].getTarget()
    if dod.precision < 9:
        print('* Date of death precision less than "year"; skip item')
        continue
    
    if 'P1087' not in Qitem.claims:
        print('* No Elo numbers found; skip item')
        continue
        
    for claim in Qitem.claims['P1087']:
        if not claim.qualifiers or 'P585' not in claim.qualifiers:
            print('* Found claim (value {}) without P585 qualifier; skip claim'.format(claim.getTarget().amount))
            continue
        
        if claim.qualifiers['P585'][0].getTarget().precision < 10:
            print('* Elo point in time precision less than "month"; skip claim')
            continue
        
        if claim.getRank() == 'deprecated':
            #print('* Claim is already deprecated; skip claim')
            continue
            
        if dod.year < claim.qualifiers['P585'][0].getTarget().year or (dod.precision>=10 and dod.year == claim.qualifiers['P585'][0].getTarget().year and dod.month < claim.qualifiers['P585'][0].getTarget().month):
            claim.changeRank('deprecated', summary='deprecate [[Property:P1087]] claim for point of time after subject’s date of death')
            print('* Set {} --> P1087 --> "{}" --> P585 --> "{:04d}-{:02d}" to "deprecated rank" (dod: {:04d}-{:02d}-{:02d}, precision {})'.format(item, claim.getTarget().amount, claim.qualifiers['P585'][0].getTarget().year, claim.qualifiers['P585'][0].getTarget().month, dod.year, dod.month, dod.day, dod.precision))

print()
print('All done, task finished')
=== Item Q27579925 (1/66) ===

=== Item Q2272619 (2/66) ===
* Set Q2272619 --> P1087 --> "2288" --> P585 --> "2019-11" to "deprecated rank" (dod: 2007-04-02, precision 11)
* Set Q2272619 --> P1087 --> "2288" --> P585 --> "2019-04" to "deprecated rank" (dod: 2007-04-02, precision 11)

=== Item Q71292638 (3/66) ===

=== Item Q27527477 (4/66) ===

=== Item Q4251345 (5/66) ===
* Set Q4251345 --> P1087 --> "2251" --> P585 --> "2019-11" to "deprecated rank" (dod: 2017-02-19, precision 11)

=== Item Q24042575 (6/66) ===
* Set Q24042575 --> P1087 --> "2064" --> P585 --> "2019-11" to "deprecated rank" (dod: 2015-06-26, precision 11)

=== Item Q7846368 (7/66) ===
* Set Q7846368 --> P1087 --> "2180" --> P585 --> "2019-11" to "deprecated rank" (dod: 2010-08-02, precision 11)

=== Item Q16678543 (8/66) ===
* Set Q16678543 --> P1087 --> "2220" --> P585 --> "2019-11" to "deprecated rank" (dod: 2014-06-27, precision 11)

=== Item Q4320456 (9/66) ===

=== Item Q15116820 (10/66) ===

=== Item Q22964571 (11/66) ===
* Set Q22964571 --> P1087 --> "2302" --> P585 --> "2019-11" to "deprecated rank" (dod: 2014-10-06, precision 11)

=== Item Q25553882 (12/66) ===
* Set Q25553882 --> P1087 --> "2325" --> P585 --> "2019-11" to "deprecated rank" (dod: 1998-01-02, precision 11)

=== Item Q75879 (13/66) ===
* Set Q75879 --> P1087 --> "2290" --> P585 --> "2019-04" to "deprecated rank" (dod: 2010-05-12, precision 11)

=== Item Q20005310 (14/66) ===
* Set Q20005310 --> P1087 --> "2291" --> P585 --> "2019-11" to "deprecated rank" (dod: 2013-11-09, precision 11)
* Set Q20005310 --> P1087 --> "2291" --> P585 --> "2019-04" to "deprecated rank" (dod: 2013-11-09, precision 11)

=== Item Q24844895 (15/66) ===

=== Item Q2090109 (16/66) ===
* Set Q2090109 --> P1087 --> "2330" --> P585 --> "2019-11" to "deprecated rank" (dod: 2013-08-21, precision 11)

=== Item Q18407901 (17/66) ===
* Set Q18407901 --> P1087 --> "2290" --> P585 --> "2019-11" to "deprecated rank" (dod: 2010-12-08, precision 11)

=== Item Q2650747 (18/66) ===

=== Item Q6109690 (19/66) ===
* Set Q6109690 --> P1087 --> "2355" --> P585 --> "2019-11" to "deprecated rank" (dod: 2018-05-25, precision 11)

=== Item Q4477631 (20/66) ===
* Set Q4477631 --> P1087 --> "2157" --> P585 --> "2019-11" to "deprecated rank" (dod: 2010-08-30, precision 11)

=== Item Q27533972 (21/66) ===

=== Item Q3561381 (22/66) ===
* Set Q3561381 --> P1087 --> "2175" --> P585 --> "2019-11" to "deprecated rank" (dod: 2013-01-19, precision 11)

=== Item Q4212847 (23/66) ===

=== Item Q27523580 (24/66) ===

=== Item Q4259216 (25/66) ===
* Set Q4259216 --> P1087 --> "2350" --> P585 --> "2019-11" to "deprecated rank" (dod: 2013-00-00, precision 9)

=== Item Q16945472 (26/66) ===
* Set Q16945472 --> P1087 --> "2153" --> P585 --> "2019-11" to "deprecated rank" (dod: 2006-08-29, precision 11)

=== Item Q12280154 (27/66) ===
* Set Q12280154 --> P1087 --> "2285" --> P585 --> "2019-11" to "deprecated rank" (dod: 2012-07-08, precision 11)

=== Item Q27531654 (28/66) ===
* Set Q27531654 --> P1087 --> "2314" --> P585 --> "2019-11" to "deprecated rank" (dod: 2010-08-25, precision 11)

=== Item Q11711661 (29/66) ===

=== Item Q1729954 (30/66) ===
* Set Q1729954 --> P1087 --> "2358" --> P585 --> "2019-11" to "deprecated rank" (dod: 2019-08-01, precision 11)

=== Item Q4496452 (31/66) ===

=== Item Q110945 (32/66) ===
* Set Q110945 --> P1087 --> "2294" --> P585 --> "2019-11" to "deprecated rank" (dod: 2006-01-14, precision 11)

=== Item Q1821987 (33/66) ===
* Set Q1821987 --> P1087 --> "2176" --> P585 --> "2019-11" to "deprecated rank" (dod: 2015-08-12, precision 11)

=== Item Q15310325 (34/66) ===
* Set Q15310325 --> P1087 --> "2350" --> P585 --> "2019-11" to "deprecated rank" (dod: 2004-00-00, precision 9)

=== Item Q4252801 (35/66) ===

=== Item Q27530171 (36/66) ===
* Set Q27530171 --> P1087 --> "2466" --> P585 --> "2019-11" to "deprecated rank" (dod: 2012-01-18, precision 11)

=== Item Q260100 (37/66) ===
* Set Q260100 --> P1087 --> "2437" --> P585 --> "2019-11" to "deprecated rank" (dod: 2010-12-17, precision 11)

=== Item Q1298537 (38/66) ===
* Set Q1298537 --> P1087 --> "2199" --> P585 --> "2019-04" to "deprecated rank" (dod: 2005-07-04, precision 11)

=== Item Q7278420 (39/66) ===
* Set Q7278420 --> P1087 --> "2336" --> P585 --> "2019-11" to "deprecated rank" (dod: 2009-02-04, precision 11)

=== Item Q27524625 (40/66) ===
* Set Q27524625 --> P1087 --> "2387" --> P585 --> "2019-11" to "deprecated rank" (dod: 2014-08-19, precision 11)

=== Item Q27527409 (41/66) ===

=== Item Q1961009 (42/66) ===
* Set Q1961009 --> P1087 --> "2382" --> P585 --> "2019-11" to "deprecated rank" (dod: 2016-09-19, precision 11)

=== Item Q24958404 (43/66) ===
* Set Q24958404 --> P1087 --> "2030" --> P585 --> "2019-11" to "deprecated rank" (dod: 2016-07-30, precision 11)

=== Item Q15445343 (44/66) ===
* Set Q15445343 --> P1087 --> "2077" --> P585 --> "2019-11" to "deprecated rank" (dod: 2016-03-12, precision 11)

=== Item Q11698838 (45/66) ===
* Set Q11698838 --> P1087 --> "2287" --> P585 --> "2019-11" to "deprecated rank" (dod: 2016-12-00, precision 10)

=== Item Q27527774 (46/66) ===
* Set Q27527774 --> P1087 --> "2424" --> P585 --> "2019-11" to "deprecated rank" (dod: 2014-01-17, precision 11)

=== Item Q98881 (47/66) ===
* Set Q98881 --> P1087 --> "2389" --> P585 --> "2019-11" to "deprecated rank" (dod: 2017-12-16, precision 11)

=== Item Q4421791 (48/66) ===

=== Item Q4526117 (49/66) ===
* Set Q4526117 --> P1087 --> "2281" --> P585 --> "2019-11" to "deprecated rank" (dod: 2007-09-12, precision 11)

=== Item Q4191878 (50/66) ===

=== Item Q126621 (51/66) ===
* Set Q126621 --> P1087 --> "2002" --> P585 --> "2019-11" to "deprecated rank" (dod: 2016-01-29, precision 11)

=== Item Q5557979 (52/66) ===
* Set Q5557979 --> P1087 --> "1820" --> P585 --> "2019-11" to "deprecated rank" (dod: 2018-08-13, precision 11)

=== Item Q12630999 (53/66) ===

=== Item Q358808 (54/66) ===

=== Item Q18613644 (55/66) ===
* Set Q18613644 --> P1087 --> "2224" --> P585 --> "2019-11" to "deprecated rank" (dod: 2019-10-05, precision 11)

=== Item Q25268277 (56/66) ===
* Set Q25268277 --> P1087 --> "2178" --> P585 --> "2019-11" to "deprecated rank" (dod: 2016-06-29, precision 11)

=== Item Q17426778 (57/66) ===
* Set Q17426778 --> P1087 --> "2158" --> P585 --> "2019-11" to "deprecated rank" (dod: 2010-06-29, precision 11)

=== Item Q4346051 (58/66) ===

=== Item Q788941 (59/66) ===
* Set Q788941 --> P1087 --> "2200" --> P585 --> "2019-11" to "deprecated rank" (dod: 2019-10-11, precision 11)

=== Item Q7177751 (60/66) ===
* More than one date of death found; skip item

=== Item Q2069351 (61/66) ===

=== Item Q9388077 (62/66) ===
* Set Q9388077 --> P1087 --> "2348" --> P585 --> "2019-11" to "deprecated rank" (dod: 2018-05-26, precision 11)
* Set Q9388077 --> P1087 --> "2348" --> P585 --> "2019-04" to "deprecated rank" (dod: 2018-05-26, precision 11)

=== Item Q2643184 (63/66) ===
* Set Q2643184 --> P1087 --> "2351" --> P585 --> "2019-11" to "deprecated rank" (dod: 2019-05-05, precision 11)

=== Item Q119383 (64/66) ===
* Set Q119383 --> P1087 --> "2190" --> P585 --> "2019-11" to "deprecated rank" (dod: 2010-07-18, precision 11)
* Set Q119383 --> P1087 --> "2190" --> P585 --> "2019-04" to "deprecated rank" (dod: 2010-07-18, precision 11)

=== Item Q18243395 (65/66) ===

=== Item Q1321538 (66/66) ===
* Set Q1321538 --> P1087 --> "2280" --> P585 --> "2019-11" to "deprecated rank" (dod: 2016-02-28, precision 11)

All done, task finished
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]: