I need to compare current and candidate values for a property, keep what's equal, remove what's different and add absent ones. I wrote the following function but it looks way more complicated than it should be:
def check_and_update_claim(new_claims, item_page, ignore_refs):
prop = new_claims[0].getID()
if prop in item_page.get()["claims"].keys():
for current_claim in item_page.claims[prop]:
different = []
same = []
for new_claim in new_claims:
if not new_claim.same_as(current_claim, ignore_refs=ignore_refs):
different.append(True)
else:
different.append(False)
same.append(new_claim)
if all(different):
with warnings.catch_warnings():
warnings.simplefilter("ignore")
item_page.removeClaims(current_claim, summary=u"Removing to update value")
if same:
for new_claim in same:
new_claims.remove(new_claim)
for new_claim in new_claims:
if new_claim.getTarget():
item_page.addClaim(new_claim, summary="Adding or updating value")
Is there a better way of doing this? I can't create sets of Claim
objects, which I guess would facilitate checking for unique values and differences between current and candidates.