Wikidata client library for Python¶
This package provides easy APIs to use Wikidata for Python.
>>> entity = client.get('Q494290')
>>> entity
<wikidata.entity.Entity Q494290 'Shin Jung-hyeon'>
>>> entity.description
m'South Korean rock guitarist and singer-songwriter'
wikidata
— Wikidata client library¶
wikidata.client
— Client session¶
-
class
wikidata.client.
Client
(base_url: str='https://www.wikidata.org/wiki/', opener: typing.Union=None, entity_type_guess: bool=True, repr_string: typing.Union=None) → None¶ Wikidata client session.
Parameters: - base_url (
str
) – The base url of the Wikidata.WIKIDATA_BASE_URL
is used by default. - opener (
urllib.request.OpenerDirector
) – The opener forurllib.request
. If omitted orNone
the default opener is used. - entity_type_guess (
bool
) – Whether to guesstype
ofEntity
from itsid
for less HTTP requests.True
by default.
New in version 0.2.0: The
entity_type_guess
option.-
entity_type_guess
= True¶ (
bool
) Whether to guesstype
ofEntity
from itsid
for less HTTP requests.New in version 0.2.0.
-
get
(entity_id: <function NewType.<locals>.new_type at 0x7f8bea049488>) → wikidata.entity.Entity¶ Get a Wikidata entity by its
EntityId
.
-
guess_entity_type
(entity_id: <function NewType.<locals>.new_type at 0x7f8bea049488>) → typing.Union¶ Guess
EntityType
from the givenEntityId
. It could returnNone
when it fails to guess.Note
It always fails to guess when
entity_type_guess
is configued toFalse
.Returns: The guessed EntityId
, orNone
if it fails to guess.Return type: Optional
[EntityType
]New in version 0.2.0.
- base_url (
wikidata.entity
— Wikidata entities¶
-
class
wikidata.entity.
Entity
(id: <function NewType.<locals>.new_type at 0x7f8bea049488>, client: 'Client') → None¶ Wikidata entity. Can be an item or a property. Its attrributes can be lazily loaded.
To get an entity use
Client.get()
method instead of the constructor ofEntity
.Note
Although it implements
Mapping
[EntityId
,object
], it actually is multidict. See alsogetlist()
method.Changed in version 0.2.0: Implemented
Mapping
[EntityId
,object
] protocol for easy access of statement values.Changed in version 0.2.0: Implemented
Hashable
protocol and==
/=
operators for equality test.-
getlist
(key: 'Entity') → typing.Sequence¶ Return all values associated to the given
key
property in sequence.Parameters: key ( Entity
) – The property entity.Returns: A sequence of all values associated to the given key
property. It can be empty if nothing is associated to the property.Return type: Sequence
[object
]
-
lists
() → typing.ItemsView¶ Similar to
items()
except the returning pairs have each list of values instead of each single value.Returns: The pairs of (key, values) where values is a sequence. Return type: ItemsView
[Entity
,Sequence
[object
]]
-
type
¶ (
EntityType
) The type of entity,item
orproperty
.New in version 0.2.0.
-
-
class
wikidata.entity.
EntityType
¶ The enumerated type which consists of two possible values:
New in version 0.2.0.
-
item
= <EntityType.item: 'item'>¶ (
EntityType
) Items areEntity
objects that are typically represented by Wikipage (at least in some Wikipedia languages). They can be viewed as “the thing that a Wikipage is about,” which could be an individual thing (the person Albert Einstein), a general class of things (the class of all Physicists), and any other concept that is the subject of some Wikipedia page (including things like History of Berlin).See also
- Items — Wikibase Data Model
- The data model of Wikibase describes the structure of the data that is handled in Wikibase.
-
property
= <EntityType.property: 'property'>¶ (
EntityType
) Properties areEntity
objects that describe a relationship between items (or otherEntity
objects) and values of the property. Typical properties are population (using numbers as values), binomial name (using strings as values), but also has father and author of (both using items as values).See also
- Properties — Wikibase Data Model
- The data model of Wikibase describes the structure of the data that is handled in Wikibase.
-
wikidata.multilingual
— Multilingual texts¶
-
wikidata.multilingual.
normalize_locale_code
(locale: typing.Union) → str¶ Determine the normalized locale code string.
>>> normalize_locale_code('ko-kr') 'ko_KR' >>> normalize_locale_code('zh_TW') 'zh_Hant_TW' >>> normalize_locale_code(Locale.parse('en_US')) 'en_US'
Changelog¶
Version 0.2.0¶
Released on February 19, 2017.
- Made
Entity
multidict. Now it satisfiesMapping
[Entity
,object
] protocol. - Added
Entity.type
property andEntityType
enum class to represent it. - Added
entity_type_guess
option andguess_entity_type()
method toClient
class. - Implemented
Hashable
protocol and==
/=
operators toEntity
for equality test.
Version 0.1.0¶
Initial version. Released on February 15, 2017.