parse_value#

wikibaseintegrator.wbi_helpers.parse_value(values, datatype=None, prop_nr=None, options=None, validate=False, allow_anonymous=True, **kwargs)[source]#

Parse values written as text (e.g. ‘3 March 1850’, ‘1850s’, ‘12±0.5’) into data values, with the parsers of the Wikibase instance and the wbparsevalue API.

A value which can’t be parsed (or isn’t valid, with validate) doesn’t raise an exception: its result has an ‘error’ key, with the error in ‘messages’ (and the validation errors in ‘validation-errors’), so that all the values can be checked at once.

Parameters:
  • values (str | list[str]) – One value or a list of values to parse. They are sent in batches of 50, the limit of the API.

  • datatype (str | None, default: None) – The datatype of the values, e.g. ‘time’, ‘quantity’, ‘globe-coordinate’ or ‘wikibase-item’. Use either datatype or prop_nr.

  • prop_nr (str | None, default: None) – A property whose datatype is used (and whose constraints are checked by validate), e.g. ‘P569’. Use either datatype or prop_nr.

  • options (dict[str, Any] | None, default: None) – Options for the parser, e.g. {‘lang’: ‘fr’} to parse dates written in French, or {‘precision’: 9} for a time value.

  • validate (bool, default: False) – Also validate the parsed values against the datatype, e.g. that a URL has a scheme. A valid value has a ‘valid’ key.

  • allow_anonymous (bool, default: True) – Allow anonymous interaction with the MediaWiki API. ‘True’ by default.

  • kwargs (Any) – Extra parameters for mediawiki_api_call_helper()

Return type:

list[dict[str, Any]]

Returns:

One result per value, in the same order: a dictionary with the ‘raw’ value, and the parsed ‘value’ and its ‘type’ (the datavalue of a snak), or an ‘error’

Raises:

ValueError – if neither or both of datatype and prop_nr are given