def reduce_claims(query_claims):
    """
    returns claims as reduced dict {P: [Q's or values]}
        P = property
        Q = item
    """
    claims = collections.defaultdict(list)

    for claim, entities in query_claims.items():

        for ent in entities:

            try:
                snak = ent.get('mainsnak')
                snaktype = snak.get('snaktype')
                value = snak.get('datavalue').get('value')
            except AttributeError:
                claims[claim] = []

            try:
                if snaktype != 'value':
                    val = snaktype
                elif value.get('id'):
                    val = value.get('id')
                elif value.get('text'):
                    val = value.get('text')
                elif value.get('time'):
                    val = value.get('time')
                else:
                    val = value
            except AttributeError:
                val = value

            if not val or not [x for x in val if x]:
                raise ValueError("%s %s" % (claim, ent))

            claims[claim].append(val)

    return dict(claims)
def reduce_claims ( query_claims ) : claims = collections . defaultdict ( list ) for claim , entities in query_claims . items ( ) : for ent in entities : try : snak = ent . get ( 'mainsnak' ) snaktype = snak . get ( 'snaktype' ) value = snak . get ( 'datavalue' ) . get ( 'value' ) except AttributeError : claims [ claim ] = [ ] try : if snaktype != 'value' : val = snaktype elif value . get ( 'id' ) : val = value . get ( 'id' ) elif value . get ( 'text' ) : val = value . get ( 'text' ) elif value . get ( 'time' ) : val = value . get ( 'time' ) else : val = value except AttributeError : val = value if not val or not [ x for x in val if x ] : raise ValueError ( "%s %s" % ( claim , ent ) ) claims [ claim ] . append ( val ) return dict ( claims )
returns claims as reduced dict {P: [Q's or values]}
        P = property
        Q = item