@@ -253,7 +253,11 @@ class WikidataClaimValue:
253253 @classmethod
254254 def from_raw (cls , claim , value , qualifiers , lazylabel ):
255255 if value .get ('value' ) is None :
256- parsed_value = None
256+ return cls (
257+ claim = claim ,
258+ value = None ,
259+ qualifiers = []
260+ )
257261 elif value .get ('type' ) == 'wikibase-entityid' :
258262 parsed_value = WikidataEntity (
259263 id = value ['value' ]['id' ],
@@ -306,14 +310,20 @@ def from_raw(cls, claim, value, qualifiers, lazylabel):
306310 )
307311
308312 def __str__ (self ):
313+ if not self :
314+ return ''
315+
309316 string = str (self .value )
310- attributes = [str (q ) for q in self .qualifiers if str ( q ) != '' ]
317+ attributes = [str (q ) for q in self .qualifiers if q ]
311318 if len (attributes ) > 0 :
312319 string += f" ({ ', ' .join (attributes )} )"
313320 return string
314321
322+ def __bool__ (self ):
323+ return (self .value is not None ) and str (self .value ) != ''
324+
315325 def to_json (self ):
316- if self . value is None :
326+ if not self :
317327 return None
318328
319329 value = self .value .to_json ()
@@ -324,10 +334,15 @@ def to_json(self):
324334 'label' : value ['label' ]
325335 }
326336
337+ qualifiers = [q .to_json () for q in self .qualifiers if q ]
338+ if len (qualifiers ) == 0 :
339+ return {
340+ "value" : value
341+ }
342+
327343 return {
328344 "value" : value ,
329- "qualifiers" : [q .to_json () for q in self .qualifiers \
330- if q is not None ]
345+ "qualifiers" : [q .to_json () for q in self .qualifiers if q ]
331346 }
332347
333348
@@ -339,7 +354,7 @@ class WikidataClaim:
339354 datatype : str
340355
341356 @classmethod
342- def from_raw (cls , subject , property , claim , lazylabel ):
357+ def from_raw (cls , subject , property , claim , lazylabel , external_ids = True ):
343358 if not claim :
344359 return cls (
345360 subject = subject ,
@@ -348,6 +363,16 @@ def from_raw(cls, subject, property, claim, lazylabel):
348363 datatype = 'empty'
349364 )
350365
366+ datatype = claim [0 ].get ('mainsnak' , claim [0 ])\
367+ .get ('datatype' , {})
368+ if not external_ids and datatype == 'external-id' :
369+ return cls (
370+ subject = subject ,
371+ property = property ,
372+ values = [],
373+ datatype = datatype
374+ )
375+
351376 rank_preferred_found = False
352377 for i in range (len (claim )):
353378 claim [i ]['datavalue' ] = claim [i ].get ('mainsnak' , claim [i ])\
@@ -369,8 +394,6 @@ def from_raw(cls, subject, property, claim, lazylabel):
369394 claim [i ]['include' ] = rank_normal_condition or \
370395 rank_preferred_condition
371396
372- datatype = claim [0 ].get ('mainsnak' , claim [0 ])\
373- .get ('datatype' , {})
374397 values = [
375398 WikidataClaimValue .from_raw (
376399 claim = None ,
@@ -394,28 +417,30 @@ def from_raw(cls, subject, property, claim, lazylabel):
394417
395418
396419 def __str__ (self ):
397- if str ( self . property . label ) == '' :
420+ if not self :
398421 return ''
399422
400- if len (self .values ) == 0 :
401- values = "no value"
402- else :
403- values = [str (val ) for val in self .values if str (val ) != '' ]
404- values = ", " .join (values )
405-
406- if values == '' :
423+ if not str (self .property .label ):
407424 return ''
408425
426+ values = [str (v ) for v in self .values if v ]
427+ values = ", " .join (values )
428+
409429 return f"{ str (self .property .label )} : { values } "
410430
431+ def __bool__ (self ):
432+ return (self .property is not None ) and \
433+ str (self .property ) != '' and \
434+ (len (self .values ) > 0 ) and \
435+ any (bool (val ) for val in self .values )
436+
411437 def to_json (self ):
412438 property = self .property .to_json ()
413439 return {
414440 "PID" : property ['QID' ],
415441 "property_label" : property ['label' ],
416442 "datatype" : self .datatype ,
417- "values" : [v .to_json () for v in self .values \
418- if v is not None ]
443+ "values" : [v .to_json () for v in self .values if v ]
419444 }
420445
421446
@@ -429,7 +454,7 @@ class WikidataEntity:
429454 claims : list [WikidataClaim ]
430455
431456 @classmethod
432- def from_id (cls , id : str , lang : str = 'en' ):
457+ def from_id (cls , id : str , lang : str = 'en' , external_ids : bool = True ):
433458 entity_dict = get_wikidata_entities_by_ids (id )
434459 if id not in entity_dict :
435460 raise ValueError (f"ID not found." )
@@ -459,7 +484,8 @@ def from_id(cls, id: str, lang: str = 'en'):
459484 claims = []
460485 ),
461486 claim = claim ,
462- lazylabel = lazylabel
487+ lazylabel = lazylabel ,
488+ external_ids = external_ids
463489 ) for pid , claim in entity_dict .get ('claims' , {}).items ()
464490 ]
465491
@@ -492,7 +518,7 @@ def __str__(self):
492518 if self .aliases :
493519 string += f", also known as { ', ' .join (map (str , self .aliases ))} "
494520
495- attributes = [str (c ) for c in self .claims if str ( c ) != '' ]
521+ attributes = [str (c ) for c in self .claims if c ]
496522 if len (attributes ) > 0 :
497523 attributes = "\n - " .join (attributes )
498524 string += f". Attributes:\n - { attributes } "
@@ -501,12 +527,16 @@ def __str__(self):
501527
502528 return string
503529
530+ def __bool__ (self ):
531+ return (self .id is not None ) and \
532+ (self .label is not None ) and \
533+ (str (self .label ) != '' )
534+
504535 def to_json (self ):
505536 return {
506537 'QID' : self .id ,
507538 'label' : str (self .label ),
508539 'description' : self .description ,
509540 'aliases' : self .aliases ,
510- 'claims' : [c .to_json () for c in self .claims \
511- if c is not None ]
541+ 'claims' : [c .to_json () for c in self .claims if c ]
512542 }
0 commit comments