Skip to content

Utility modules

client.utilities wraps the small read modules of the Action API: wbavailablebadges, wbformatentities, wbformatvalue, wbparsevalue, wbsgetsuggestions, and wbcheckconstraints. Every method raises on an HTTP 200 error body instead of returning an empty answer: no-such-entity becomes NotFoundError, ratelimited becomes RateLimitError, and every other error envelope becomes APIError. The examples on this page use the client from the first block; later blocks reuse it.

import asyncio

from bibliofabric.wikidata import WikidataClient

client = WikidataClient()
badges = await client.utilities.available_badges()
print(len(badges), badges[:3])
13 ['Q17437798', 'Q17437796', 'Q17559452']

available_badges() takes no parameters and returns every badge id usable on site links.

Format entities as HTML

links = await client.utilities.format_entities(["Q42", "P31"])
print(sorted(links))
print(links["Q42"])
['P31', 'Q42']
<a title="Q42" href="https://www.wikidata.org/wiki/Q42" lang="mul">Douglas Adams</a>

format_entities() returns an HTML link per id, keyed by the id. The module accepts at most 50 ids per request, so the client splits longer lists into chunks and merges the answers into one dict.

Format and parse values

format_value() renders one datavalue as HTML. It takes the {"type": ..., "value": ...} shape; without the type key the service answers baddatavalue.

html = await client.utilities.format_value(
    {"type": "string", "value": "hello"}, "string"
)
print(html)
hello

parse_value() goes the other way. It parses serialized values against a datatype and returns ParsedValue records with the raw input, the parsed value, and the value type.

parsed = await client.utilities.parse_value(["Q42"], datatype="wikibase-item")
print(parsed[0].raw, parsed[0].type, parsed[0].value)
Q42 wikibase-entityid {'entity-type': 'item', 'numeric-id': 42, 'id': 'Q42'}

Get property suggestions

suggestions() wraps wbsgetsuggestions. The module answers an error unless entity or property_ids is given, so the client rejects a call without either before sending.

suggestions = await client.utilities.suggestions(property_ids=["P31"], limit=5)
for suggestion in suggestions[:3]:
    print(suggestion.id, suggestion.label, int(suggestion.rating))
P17 country 57189
P18 image 31084
P373 Commons category 28346

Each PropertySuggestion carries id, url, rating (a float weight), label, and description. The module caps the property_ids list at 50 per request, so the client splits longer lists into chunks and merges the suggestions in order.

Check constraint compliance

check_constraints() wraps wbcheckconstraints. It returns a report keyed by entity id, then property id, with CheckedClaim records per claim; each carries the claim id and a mainsnak whose results list the check status, the constraint, and a message.

report = await client.utilities.check_constraints(["Q42"])
for property_id, claims in report["Q42"].items():
    for claim in claims:
        for check in claim.mainsnak.results:
            print(property_id, check.status, check.constraint.type)
        break
    break
P3192 bad-parameters Q21502404

The module's entity parameter is id (entity and entityid answer the no-data error). It accepts at most 50 ids per request, so the client splits longer lists into chunks and merges the reports. Pass claim_ids=, constraint_ids=, or statuses= to narrow the check.