import pywikibot as pwb
from pywikibot import pagegenerators as pg
site = pwb.Site("wikidata", "wikidata") # or pwb.Site("test", "wikidata") for test.wikidata.org
repo = site.data_repository()
pwb.config.noisysleep = 10
print(pwb);
<module 'pywikibot' from '/srv/paws/pwb/pywikibot/__init__.py'>
item = pwb.ItemPage(repo, "Q441005");
print(item.get()["labels"]["en"]);
print(item.get()["claims"]);
print(dir(item.get()["claims"]["P1889"][0]))
print(item.get()["claims"]["P1889"][0].getTarget());
claim=item.get()["claims"]["P1889"][0];
#print(claim.has_qualifier("",""));
for key in claim.qualifiers.keys():
  print(key);
print(claim.qualifiers["P1013"][0].getTarget());
Amar
{'P31': [<pywikibot.page.Claim object at 0x7f15ed8f7358>], 'P1889': [<pywikibot.page.Claim object at 0x7f15ed8e84e0>, <pywikibot.page.Claim object at 0x7f15ed8e8438>]}
['SNAK_TYPES', 'TARGET_CONVERTER', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_formatDataValue', '_formatValue', '_type', 'addQualifier', 'addSource', 'addSources', 'changeRank', 'changeSnakType', 'changeTarget', 'fromJSON', 'getID', 'getRank', 'getSnakType', 'getSources', 'getTarget', 'getType', 'has_qualifier', 'hash', 'id', 'isQualifier', 'isReference', 'on_item', 'qualifierFromJSON', 'qualifiers', 'rank', 'referenceFromJSON', 'removeQualifier', 'removeQualifiers', 'removeSource', 'removeSources', 'repo', 'setRank', 'setSnakType', 'setTarget', 'snak', 'snaktype', 'sources', 'target', 'target_equals', 'toJSON', 'type', 'types', 'value_types']
[[wikidata:Q19793951]]
P1013
[[wikidata:Q23765057]]
def item(name):
    return pwb.ItemPage(repo, name);

def getClaim(claims, prop, target):
    tgtClaim = None;
    for claim in claims.get(prop,[]):
        if claim.getTarget() == target:
            tgtClaim = claim;
            break;
    return tgtClaim;

def ensure(item, prop, target):
    tgtClaim = getClaim(item.get()["claims"], prop, target);
    if tgtClaim is None:
        print("adding");
        tgtClaim=pwb.Claim(repo, prop);
        tgtClaim.setTarget(target);
        item.addClaim(tgtClaim);
    print(tgtClaim);
    return tgtClaim;

def qualifier(claim, prop, target):
    tgtQ = getClaim(claim.qualifiers, prop, target);
    if tgtQ is None:
        print("adding qualifier");
        tgtClaim=pwb.Claim(repo, prop);
        tgtClaim.setTarget(target);
        claim.addQualifier(tgtClaim);
    return tgtQ;

wikiDisPage = item("Q4167410");
wdcfam = item("Q27924673");
wdcfn = item("Q23765057");
familyName = item("Q101352");
givenName = item("Q202444");
different = "P1889";
criterion = "P1013";

def fixName(person):
    print(person);
    print(person.get()["labels"].get("en","<no value>"));
    for fnc in person.get()["claims"].get("P734",[]):
        theDis = fnc.getTarget();
        nameString = theDis.get()["labels"]["en"];
        print(nameString);
        if getClaim(theDis.get()["claims"], "P31", wikiDisPage) is None:
            print("Name is no disambig-page");
            return
        print("Name is disambig-page");
        theFN = None;
        for cl in theDis.get()["claims"].get(different,[]):
            if getClaim(cl.qualifiers, criterion, wdcfam) is None:
                continue;
            fn = cl.getTarget();
            if getClaim(fn.get()["claims"], "P31", familyName) is None:
                print("Name "+fn.title()+" is no family name");
                continue;
            if theFN is not None:
                print("Error, multiple FNs: "+fn);
                return;
            theFN = fn;
            print("Found: "+fn.title())
        if theFN is None:
            print("No suitable name found, creating");
            new_item = pwb.ItemPage(repo);
            new_item.editLabels({"en":nameString});
            new_item.editDescriptions({"en":"family name"});
            theFN=new_item;
            ensure(theFN, "P31", familyName);
            qualifier(ensure(theFN, different, theDis), criterion, wdcfam);
            qualifier(ensure(theDis, different, theFN), criterion, wdcfam);
        if theFN.get()["labels"]["en"] != theDis.get()["labels"]["en"]:
            print("Error, name has different label.");
            return;
        print("ok, ready")
        fnc.changeTarget(theFN);
#for n in "Q18436215 Q4059737 Q4738576 Q20116347 Q21754159 Q18015431".split(" "):
#    fixName(item(n));
cyrillic = item("Q8209");
latin = item("Q8229");
greek = item("Q8216");

theDis = item("Q453842");
theFN = item("Q28114624");

def createName(latin, original, lang, nameType, writingSystem, disambig):
    new_item = pwb.ItemPage(repo);
    #new_item=item("Q28916482");
    if latin is None:
        new_item.editLabels({"en":original});
        print(new_item);
        new_item.editDescriptions({"en":nameType["desc"]});
    else:
        new_item.editLabels({"en":latin, lang:original});
        print(new_item);
        new_item.editAliases({"en":[original]});
        new_item.editDescriptions({"en":nameType["desc"]+" ("+original+")"});
    theFN=new_item;
    #theFN=item("Q28914610");
    ensure(theFN, "P31", nameType["class"]);
    ensure(theFN, "P1705", pwb.WbMonolingualText(original, "mul"));
    ensure(theFN, "P282", writingSystem);
    qualifier(ensure(theFN, different, disambig), criterion, nameType["crit"]);
    qualifier(ensure(disambig, different, theFN), criterion, nameType["crit"]);
    return theFN;
#Шрётер, Алексей Иванович
#{"class":givenName, "crit":wdcfn, "desc":"give name"}
#createName("Moskal", "Москаль","uk", {"class":familyName, "desc":"family name", "crit":wdcfam}, cyrillic, item("Q1949284"));

ingram = item("Q16871009");
#for q in ["Q17145019", "Q7174831", "Q3928918", "Q28823484", "Q726459", "Q1750696", "Q7349098", "Q7358388", "Q7372988", "Q7407681", "Q112849", "Q7529636", "Q376467", "Q1674513"]:
#    qualifier(ensure(item(q), "P734", ingram), "P143", item("Q328"));
st = ensure(item("Q17145019"), "P734", ingram);
def fixImported(claim, prop, target):
    tgtQ = getClaim(claim.qualifiers, prop, target);
    if tgtQ is not None:
        claim.removeQualifier(tgtQ);
        claim.addSource(tgtQ);
    else:
        print("Error, strange situation");
#print(st.sources[0]);
fixImported(st, "P143", item("Q328"));
#ensure(theFN, "P31", familyName);
#qualifier(ensure(theFN, different, theDis), criterion, wdcfam);
#qualifier(ensure(theDis, different, theFN), criterion, wdcfam);
<pywikibot.page.Claim object at 0x7f92c1271978>
Error, strange situation
"a db".split(" ")
['a', 'db']
disambig = pwb.ItemPage(repo, "Q25548024");
fam = pwb.ItemPage(repo,"Q28913395");
print(disambig.get()["claims"]["P31"][0].getTarget() == pwb.ItemPage(repo, "Q4167410"))
claim = pwb.Claim(repo, "P1889");
claim.setTarget(disambig);
fam.addClaim(claim);
True