py_ez_wikidata API Documentation
ezwd_cmd
Created on 2026-06-26
ezwd command line interface
@author: wf
EzWdCmd
Bases: BaseCmd
command line interface for easy Wikidata access: search Wikidata, inspect a named property mapping and create/update a Wikidata item from a record using that mapping.
Source code in ez_wikidata/ezwd_cmd.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 | |
__init__()
constructor
Source code in ez_wikidata/ezwd_cmd.py
30 31 32 33 34 | |
add_arguments(parser)
add the ezwd specific arguments
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parser(ArgumentParser)
|
the parser to add arguments to |
required |
Source code in ez_wikidata/ezwd_cmd.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | |
create(name, record_path, lang, write, baseurl=None, strict=False, rdf_format=None)
create (or dry-run) a Wikidata item from the given record file using the named property mapping
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name(str)
|
the mapping name (e.g. scholar) |
required | |
record_path(str)
|
path to the YAML/JSON record file |
required | |
lang(str)
|
the language to use |
required | |
write(bool)
|
if True actually write to Wikidata |
required | |
baseurl(str)
|
the wikibase baseurl to write to (defaults to production Wikidata) |
required | |
strict(bool)
|
if True set exit code 1 when the result has problems |
required | |
rdf_format(str)
|
if given also print the built item as RDF |
required |
Source code in ez_wikidata/ezwd_cmd.py
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | |
create_from_record(mappings, record, lang, write, baseurl=None, strict=False, rdf_format=None)
create (or dry-run) a Wikidata item from the given record using the given property mappings
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mappings(PropertyMappings)
|
the property mappings |
required | |
record(dict)
|
the record |
required | |
lang(str)
|
the language to use |
required | |
write(bool)
|
if True actually write to Wikidata |
required | |
baseurl(str)
|
the wikibase baseurl to write to |
required | |
strict(bool)
|
if True set exit code 1 when the result has problems |
required | |
rdf_format(str)
|
if given also print the built item as RDF |
required |
Source code in ez_wikidata/ezwd_cmd.py
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 | |
example(name, lang, strict=False, rdf_format=None)
print the worked example embedded in the named mapping and run it as dry-run - see issue #19
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name(str)
|
the mapping name (e.g. paper) |
required | |
lang(str)
|
the language to use |
required | |
strict(bool)
|
if True set exit code 1 when the result has problems |
required | |
rdf_format(str)
|
if given also print the built item as RDF |
required |
Source code in ez_wikidata/ezwd_cmd.py
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | |
handle_args(args)
handle the parsed arguments
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args(Namespace)
|
the parsed arguments |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the arguments were handled |
Source code in ez_wikidata/ezwd_cmd.py
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | |
list_mapping(name)
list the columns of the named property mapping
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name(str)
|
the mapping name (e.g. scholar) |
required |
Source code in ez_wikidata/ezwd_cmd.py
155 156 157 158 159 160 161 162 163 164 165 | |
load_record(record_path)
load a record from a YAML or JSON file
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
record_path(str)
|
path to the record file |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
the loaded record |
Source code in ez_wikidata/ezwd_cmd.py
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | |
search(search_for, lang, limit)
search Wikidata and print candidate q-id / label / description rows
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
search_for(str)
|
the text to search for |
required | |
lang(str)
|
the language to use |
required | |
limit(int)
|
the maximum number of results |
required |
Source code in ez_wikidata/ezwd_cmd.py
142 143 144 145 146 147 148 149 150 151 152 153 | |
main(argv=None)
main entry point for the ezwd command line tool
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
argv(list)
|
the command line arguments (defaults to sys.argv) |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
the exit code |
Source code in ez_wikidata/ezwd_cmd.py
283 284 285 286 287 288 289 290 291 292 293 294 295 | |
trulytabular
Created on 2022-04-14
@author: wf
TrulyTabular
Bases: object
truly tabular SPARQL/RDF analysis
checks "how tabular" a query based on a list of properties of an itemclass is
Source code in ez_wikidata/trulytabular.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 | |
__init__(itemQid, propertyLabels=[], propertyIds=[], search_predicate='wdt:P31', where=None, endpointConf=None, lang='en', debug=False)
Constructor
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
itemQid(str)
|
wikidata id of the type to analyze |
required | |
propertyLabels(list)
|
a list of labels of properties to be considered |
required | |
propertyIds(list)
|
a list of ids of properties to be considered |
required | |
search_predicate(str)
|
the search predicate to use e.g. instanceof / subclass of |
required | |
where(str)
|
extra where clause for instance selection (if any) |
required | |
endpoint(str)
|
the url of the SPARQL endpoint to be used |
required |
Source code in ez_wikidata/trulytabular.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | |
__str__()
Returns:
| Name | Type | Description |
|---|---|---|
str |
my text representation |
Source code in ez_wikidata/trulytabular.py
81 82 83 84 85 86 | |
addStatsColWithPercent(m, col, value, total)
add a statistics Column Args: m(dict): col(str): name of the column value: value total: total value
Source code in ez_wikidata/trulytabular.py
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 | |
asText(long=True)
returns my content as a text representation
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
long(bool)
|
True if a long format including url is wished |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
a text representation of my content |
Source code in ez_wikidata/trulytabular.py
112 113 114 115 116 117 118 119 120 121 122 123 | |
count()
get my count
Source code in ez_wikidata/trulytabular.py
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | |
genPropertyStatistics()
generate the property Statistics
Returns:
| Name | Type | Description |
|---|---|---|
generator |
a generator of statistic dict rows |
Source code in ez_wikidata/trulytabular.py
436 437 438 439 440 441 442 443 444 445 446 | |
genWdPropertyStatistic(wdProperty, itemCount, withQuery=True)
generate a property Statistics Row for the given wikidata Property
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
wdProperty(WikidataProperty)
|
the property to get the statistics for |
required | |
itemCount(int)
|
the total number of items to check |
required | |
withQuery(bool)
|
if true include the sparql query |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
a statistics row |
Source code in ez_wikidata/trulytabular.py
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 | |
generateSparqlQuery(genMap, listSeparator='⇹', naive=True, lang='en')
generate a SPARQL Query
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
genMap(dict)
|
a dictionary of generation items aggregates/ignores/labels |
required | |
listSeparator(str)
|
the symbole to use as a list separator for GROUP_CONCAT |
required | |
naive(bool)
|
if True - generate a naive straight forward SPARQL query if False generate a proper truly tabular aggregate query |
required | |
lang(str)
|
the language to generate for |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
the generated SPARQL Query |
Source code in ez_wikidata/trulytabular.py
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | |
getPropertyStatistics()
get the property Statistics
Source code in ez_wikidata/trulytabular.py
448 449 450 451 452 453 454 455 456 457 | |
getQueryManager(lang='sparql', name='trulytabular', debug=False)
classmethod
get the query manager for the given language and fileName
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lang(str)
|
the language of the queries to extract |
required | |
name(str)
|
the name of the manager containing the query specifications |
required | |
debug(bool)
|
if True set debugging on |
required |
Source code in ez_wikidata/trulytabular.py
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | |
mostFrequentPropertiesQuery(whereClause=None, minCount=0)
get the most frequently used properties
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
whereClause(str)
|
an extra WhereClause to use |
required |
Source code in ez_wikidata/trulytabular.py
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 | |
noneTabular(wdProperty)
get the none tabular result for the given Wikidata property
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
wdProperty(WikidataProperty)
|
the Wikidata property |
required |
Source code in ez_wikidata/trulytabular.py
366 367 368 369 370 371 372 373 374 375 376 377 | |
noneTabularQuery(wdProperty, asFrequency=True)
get the none tabular entries for the given property
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
wdProperty(WikidataProperty)
|
the property to analyze |
required | |
asFrequency(bool)
|
if true do a frequency analysis |
required |
Source code in ez_wikidata/trulytabular.py
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 | |
version
Created on 2024-03-01
@author: wf
Version
dataclass
Bases: object
Version handling for easy wikidata access
Source code in ez_wikidata/version.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | |
wbquery
Created on 2022-04-30
@author: wf
WikibaseQuery
Bases: object
a Query for Wikibase
Source code in ez_wikidata/wbquery.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 | |
__init__(entity, wpm=None, debug=False)
Constructor
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entity(str)
|
the entity this query represents |
required | |
debug(bool)
|
if True switch on debugging |
required |
Source code in ez_wikidata/wbquery.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | |
addPropertyFromDescriptionRow(row)
add a property from the given row
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
row(dict)
|
the row to add |
required |
Source code in ez_wikidata/wbquery.py
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | |
asSparql(filterClause=None, orderClause=None, pk=None, lang='en')
get the sparqlQuery for this query optionally applying a filterClause
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filterClause(str)
|
a filter to be applied (if any) |
required | |
orderClause(str)
|
an orderClause to be applied (if any) |
required | |
pk(str)
|
primaryKey (if any) |
required | |
lang(str)
|
the language to be used for labels |
required |
Source code in ez_wikidata/wbquery.py
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 | |
getColumnTypeAndVarname(propName)
get a signature tuple consisting of columnName, propertType and SPARQL variable Name for the given property Name
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
propName(str)
|
the name of the property |
required |
Raises:
| Type | Description |
|---|---|
Exception
|
if property name is not known |
Returns:
| Type | Description |
|---|---|
(str, str, str)
|
column,propType,varName tupel |
Source code in ez_wikidata/wbquery.py
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | |
getValuesClause(values, propVarname='short_name', propType='text', lang=None, ignoreEmpty=True, wbPrefix='http://www.wikidata.org/entity/')
create a SPARQL Values clause
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values(list)
|
the list of values to create values for |
required | |
propVarname(str)
|
the property variable name to assign the values for |
required | |
propType
|
str
|
|
'text'
|
lang
|
str
|
language of labels to query |
None
|
ignoreEmpty(bool)
|
ignore empty values if True |
required | |
wbPrefix(str)
|
a wikibase/wikidata prefix to be removed for items values |
required |
Returns: str: the SPARQL values clause
Source code in ez_wikidata/wbquery.py
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | |
get_item_mapping()
Get the mapping that describes the wikidata entity item
Source code in ez_wikidata/wbquery.py
50 51 52 53 54 | |
get_property_mappings()
Get the property mappings as PropertyMapping list
Returns:
| Type | Description |
|---|---|
List[PropertyMapping]
|
List[PropertyMapping]: list of PropertyMappings |
Source code in ez_wikidata/wbquery.py
40 41 42 43 44 45 46 47 48 | |
inFilter(values, propName='short_name', lang='en')
create a SPARQL IN filter clause
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values(list)
|
the list of values to filter for |
required | |
propName(str)
|
the property name to filter with |
required | |
lang(str)
|
the language to apply |
required |
Source code in ez_wikidata/wbquery.py
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | |
ofEntityMap(entity, entityMap)
classmethod
create a WikibaseQuery for the given entity and entityMap
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entity(str)
|
the entity name |
required | |
entityMap(dict)
|
the entity property descriptions |
required |
Returns: WikibaseQuery
Source code in ez_wikidata/wbquery.py
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 | |
ofMapRows(entityMapRows, debug=False)
classmethod
create a dict of wikibaseQueries from the given entityMap list of dicts
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entityMapRows(list)
|
a list of dict with row descriptions |
required | |
debug(bool)
|
if True switch on debugging |
required |
Source code in ez_wikidata/wbquery.py
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 | |
wdproperty
Created on 2024-03-02
@author: wf
PropertyMapping
Represents a single column Wikidata property mapping.
Attributes:
| Name | Type | Description |
|---|---|---|
column |
Optional[str]
|
The column name in the data source; if None, the value is directly used. |
propertyName |
str
|
The human-readable name of the property. |
propertyId |
str
|
The Wikidata property ID (e.g., "P31"). |
propertyType |
str
|
The type of the property as a string; converted to an enum in post-init. |
qualifierOf |
Optional[str]
|
Specifies if the property is a qualifier of another property. |
valueLookupType |
Optional[Any]
|
The type (instance of/P31) of the property value for lookup if the value is not already a QID. |
value |
Optional[Any]
|
The default value to set for the property. |
varname |
Optional[str]
|
An optional variable name for internal use. |
listIndexBase |
Optional[int]
|
for a qualifier mapping without column: the qualifier value is the index of the qualified value in its list counted from this base, e.g. 1 for series ordinal (P1545), which uses one-based numbering - see issue #15 |
property_type_enum |
WdDatatype
|
The enum representation of the property type, initialized based on propertyType. |
The post_init method ensures the propertyType is correctly interpreted and stored as both a string and an enum.
Source code in ez_wikidata/wdproperty.py
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 | |
__post_init__()
Convert propertyType from string to WdDatatype enum if necessary
Source code in ez_wikidata/wdproperty.py
505 506 507 508 509 510 511 512 513 514 515 516 517 518 | |
from_record(wpm, record)
classmethod
initialize PropertyMapping from the given record Args: wpm(WikidataPropertyManager): to be used for type lookup record(Dict): property mapping information
Returns:
| Type | Description |
|---|---|
PropertyMapping
|
PropertyMapping |
Source code in ez_wikidata/wdproperty.py
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 | |
getDefaultItemPropertyMapping()
classmethod
get the defaultItemPropertyMapping
Source code in ez_wikidata/wdproperty.py
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 | |
get_item_mapping(property_mappings)
classmethod
get the property mapping that is used for the default "item" primary key if no property is defined use the default "item" mapping
Source code in ez_wikidata/wdproperty.py
653 654 655 656 657 658 659 660 661 662 663 664 665 | |
get_legacy_mapping()
classmethod
Returns the Mapping from old prop map keys to the new once
Source code in ez_wikidata/wdproperty.py
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 | |
get_qualifier_lookup(properties)
classmethod
Get a lookup for a property and all its qualifier
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
properties
|
List[PropertyMapping]
|
property mappings to generate the lookup from |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, List[PropertyMapping]]
|
dict as property qualifier lookup |
Source code in ez_wikidata/wdproperty.py
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 | |
is_item_itself()
Check if the property_type is an item
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the property mapping links to the existing item |
Source code in ez_wikidata/wdproperty.py
617 618 619 620 621 622 623 624 625 | |
is_qualifier()
Returns true if the property mapping describes a qualifier
Source code in ez_wikidata/wdproperty.py
594 595 596 597 598 599 | |
to_record()
convert property mapping to its dict representation
Source code in ez_wikidata/wdproperty.py
584 585 586 587 588 589 590 591 592 | |
PropertyMappings
A collection of Wikidata property mappings, with metadata.
Source code in ez_wikidata/wdproperty.py
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 | |
get_example()
get the worked example embedded in the header of the mapping file as 🌐🕸 annotation: a yaml block keyed by the id of the item that was created with this mapping and holding its record - see issue #19
Returns:
| Type | Description |
|---|---|
Optional[str]
|
Tuple[Optional[str], Optional[dict]]: the item id and the record, |
Optional[dict]
|
(None, None) if the mapping has no example |
Source code in ez_wikidata/wdproperty.py
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 | |
of_name(name)
classmethod
load the bundled PropertyMappings resource of the given name (e.g. "scholar" -> resources/scholar_props.yaml), mirroring the existing sb_props.yaml convention.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name(str)
|
the use-case name (resource is |
required |
Returns:
| Name | Type | Description |
|---|---|---|
PropertyMappings |
PropertyMappings
|
the loaded mapping set |
Source code in ez_wikidata/wdproperty.py
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 | |
resource_path(name)
classmethod
get the path of the bundled mapping resource of the given name
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name(str)
|
the use-case name (resource is |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
the path of the resource file |
Source code in ez_wikidata/wdproperty.py
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 | |
Variable
Variable e.g. name handling
Source code in ez_wikidata/wdproperty.py
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | |
validVarName(varStr)
classmethod
convert the given potential variable name string to a valid variable name
see https://stackoverflow.com/a/3305731/1497139
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
varStr(str)
|
the string to convert |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
a valid variable name |
Source code in ez_wikidata/wdproperty.py
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | |
WdDatatype
Bases: Enum
Supported Wikidata datatypes, sorted by frequency and including special cases.
Source code in ez_wikidata/wdproperty.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | |
from_wb_type_name(wb_type_name)
classmethod
convert a wikibase type name to a WdDatatype
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
wb_type_name(str)
|
the string name of the wikibase type (with or without wikibase ontology prefix) |
required |
Source code in ez_wikidata/wdproperty.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | |
get_by_wikibase(property_type)
classmethod
Get WdDatatype by the corresponding wikibase datatype Args: property_type: wikibase name of the type
Returns:
| Type | Description |
|---|---|
Union[WdDatatype, None]
|
WdDatatype |
Source code in ez_wikidata/wdproperty.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | |
WikidataProperty
Represents a Wikidata Property.
Source code in ez_wikidata/wdproperty.py
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | |
__post_init__()
creates and modify calculated fields
Source code in ez_wikidata/wdproperty.py
170 171 172 173 174 175 176 177 178 179 180 181 182 | |
getPredicate()
get me as a Predicate
Source code in ez_wikidata/wdproperty.py
184 185 186 187 188 189 190 | |
WikidataPropertyManager
handle Wikidata Properties
Source code in ez_wikidata/wdproperty.py
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 | |
__init__(endpoint_url='https://qlever.dev/api/wikidata', langs=['en', 'zh', 'hi', 'de', 'fr', 'ar', 'es', 'bn', 'ru'], with_load=True, profile=True, debug=False)
initialize the lookups
Source code in ez_wikidata/wdproperty.py
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 | |
get_instance(endpoint_url='https://qlever.dev/api/wikidata')
classmethod
initialize the wikidata property manager
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
endpoint_url(str)
|
the SPARQL endpoint to query if there is no cache available |
required | |
lang(str)
|
the languages to query propery labels and descriptions for |
required |
Source code in ez_wikidata/wdproperty.py
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 | |
get_mappings_for_records(prop_mapping_records)
convert given list of property mapping records to list of PropertyMappings Args: prop_mapping_records: records to convert
Returns:
| Type | Description |
|---|---|
List[PropertyMapping]
|
property mappings |
Source code in ez_wikidata/wdproperty.py
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 | |
get_properties_by_ids(ids, lang='en')
Get properties by their IDs for a specific language.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ids
|
List[str]
|
List of property IDs to search for. |
required |
lang(str)
|
the language |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, Optional[WikidataProperty]]
|
A dictionary of {property ID: WikidataProperty or None} for found and not found properties. |
Source code in ez_wikidata/wdproperty.py
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 | |
get_properties_by_labels(labels, lang='en')
Get properties by their labels for a specific language.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labels
|
List[str]
|
List of property labels to search for. |
required |
lang
|
str
|
the language to match with |
'en'
|
Returns: A dictionary of {label: WikidataProperty} for found properties.
Source code in ez_wikidata/wdproperty.py
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 | |
get_property_by_id(property_id)
lookup a WikidataProperty for the given property_id
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
property_id(str)
|
a property ID e.g. "P6375" |
required |
Source code in ez_wikidata/wdproperty.py
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 | |
get_query_for_langs(langs=None)
Get the SPARQL query for the given list of langs.
Source code in ez_wikidata/wdproperty.py
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 | |
init_props()
initialize my property structures
Source code in ez_wikidata/wdproperty.py
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 | |
load()
load the properties
Source code in ez_wikidata/wdproperty.py
297 298 299 300 301 302 303 304 305 306 307 308 309 310 | |
load_from_sparql()
get my list of dicts from sparql
Source code in ez_wikidata/wdproperty.py
231 232 233 234 235 236 237 238 239 240 | |
load_from_sql()
load from SQL
Source code in ez_wikidata/wdproperty.py
263 264 265 266 267 268 269 270 271 272 | |
prepare_store()
prepare storing by adding id and pid and avoiding duplicates
Source code in ez_wikidata/wdproperty.py
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | |
store()
store my list of dicts
Source code in ez_wikidata/wdproperty.py
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 | |
with_user_agent(sparql)
set the Wikimedia-policy User-Agent on the given SPARQL accessor
Source code in ez_wikidata/wdproperty.py
32 33 34 35 | |
wdsearch
Created on 2022-07-24
@author: wf
WikidataSearch
Bases: object
Wikidata Search API wrapper
Interactive (per-keyclick) search: it must fail fast and is deliberately NOT rate limited - throttling/blocking belongs on the batch SPARQL path, not on search-as-you-type.
Source code in ez_wikidata/wdsearch.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | |
__init__(language='en', timeout=2.0)
Constructor
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
language(str)
|
the language to use e.g. en/fr |
required | |
timeout(float)
|
maximum time to wait for result - kept short so interactive search fails fast rather than blocking the UI |
required |
Source code in ez_wikidata/wdsearch.py
26 27 28 29 30 31 32 33 34 35 36 37 | |
getProperties()
get the Wikidata Properties
Source code in ez_wikidata/wdsearch.py
130 131 132 133 134 135 136 137 138 | |
get_user_agent()
classmethod
Construct a Wikimedia-policy-compliant User-Agent string identifying this project with a contact URL. see https://meta.wikimedia.org/wiki/User-Agent_policy
Source code in ez_wikidata/wdsearch.py
39 40 41 42 43 44 45 46 47 | |
make_wikidata_request(apisearch)
Make a fail-fast HTTP request to the Wikidata API with a policy User-Agent
Source code in ez_wikidata/wdsearch.py
84 85 86 87 88 89 90 | |
search(searchFor, limit=9)
Search for Wikidata entities.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
searchFor(str)
|
the string to search for |
required | |
limit(int)
|
the maximum amount of results to search for |
required |
Returns:
| Type | Description |
|---|---|
List[dict]
|
List[dict]: list of search result dictionaries, or error pseudo-result on failure |
Source code in ez_wikidata/wdsearch.py
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | |
searchOptions(searchFor, limit=9)
Search and return a list of qid, itemLabel, description tuples.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
searchFor
|
str
|
the string to search for. |
required |
limit
|
int
|
the maximum amount of results to return. |
9
|
Returns:
| Type | Description |
|---|---|
List[Tuple[str, str, str]]
|
List[Tuple[str, str, str]]: |
List[Tuple[str, str, str]]
|
A list of tuples containing |
List[Tuple[str, str, str]]
|
qid, itemLabel, and description. |
Source code in ez_wikidata/wdsearch.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | |
wikidata
Created on 2022-04-18
@author: wf
TimeoutHTTPAdapter
Bases: HTTPAdapter
HTTPAdapter with a default timeout for requests that do not set one
Source code in ez_wikidata/wikidata.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | |
__init__(timeout=(10, 60), **kwargs)
constructor
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timeout
|
tuple
|
the default (connect, read) timeout in seconds |
(10, 60)
|
Source code in ez_wikidata/wikidata.py
60 61 62 63 64 65 66 67 68 | |
send(request, **kwargs)
send the request with the default timeout if none is given
Source code in ez_wikidata/wikidata.py
70 71 72 73 74 75 76 77 | |
UrlReference
Bases: Reference
Reference consisting of reference URL (P854) retrieved (P813)
Source code in ez_wikidata/wikidata.py
1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 | |
__init__(url, date=None)
constructor Args: url: reference URL date: retrieved at
Source code in ez_wikidata/wikidata.py
1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 | |
Wikidata
wikidata access
see http://learningwikibase.com/data-import/
Source code in ez_wikidata/wikidata.py
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 | |
query_sparql
property
lazily created rate-limited SPARQL accessor for read queries against the configured endpoint (avoids HTTP 429/403 from Wikidata)
wbi
property
writable
WikibaseIntegrator
__init__(baseurl=None, wpm=None, debug=False, endpointConf=None)
Constructor
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
baseurl(str)
|
the baseurl of the wikibase to use |
required | |
debug(bool)
|
if True output debug information |
required | |
wpm(WikidataPropertymanager)
|
|
required | |
endpointConf(Endpoint)
|
the SPARQL endpoint to use for read queries (defaults to the Wikidata Query Service) |
required |
Source code in ez_wikidata/wikidata.py
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | |
addDict(row, mapDict, itemId=None, lang='en', write=False, ignoreErrors=False)
add the given row mapping with the given map Dict
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
row(dict)
|
the data row to add |
required | |
mapDict(dict)
|
the mapping dictionary to use |
required | |
itemId
|
Union[str, None]
|
wikidata id of the item the data should be added to. If None a new item is created unless item id is provided in the record |
None
|
lang(str)
|
the language for lookups |
required | |
write(bool)
|
if True do actually write |
required | |
ignoreErrors(bool)
|
if True ignore errors |
required |
Returns:
| Name | Type | Description |
|---|---|---|
WikiDataResult |
WikidataResult
|
the result of the operation |
Source code in ez_wikidata/wikidata.py
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 | |
add_record(record, property_mappings, item_id=None, lang='en', write=False, ignore_errors=False, summary=None, reference=None)
add the given row mapping with the given map Dict
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
record(dict)
|
the data row to add |
required | |
property_mappings(list)
|
the mapping dictionary to use |
required | |
item_id
|
Union[str, None]
|
wikidata id of the item the data should be added to. If None a new item is created unless item id is provided in the record |
None
|
lang(str)
|
the language for lookups |
required | |
write(bool)
|
if True do actually write |
required | |
ignore_errors(bool)
|
if True ignore errors |
required | |
summary
|
str
|
summary of the item edits |
None
|
reference
|
Reference
|
reference to add to all claims |
None
|
Returns:
| Type | Description |
|---|---|
(qId, errors)
|
the wikidata item create (if any) and a dict of errors |
Source code in ez_wikidata/wikidata.py
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 | |
as_rdf(result, rdf_format='turtle', lang='en')
serialize the item of the given result as RDF
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result(WikidataResult)
|
the result whose item to serialize |
required | |
rdf_format(str)
|
an rdflib serialization format |
required | |
lang(str)
|
the language of label and description |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
the serialized graph, empty if the result has no item |
Source code in ez_wikidata/wikidata.py
776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 | |
check_result(result, property_mappings, record)
check the given result against the record and the mappings
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result(WikidataResult)
|
the result to check |
required | |
property_mappings(list)
|
the mappings that were applied |
required | |
record(dict)
|
the record that was mapped |
required |
Returns:
| Name | Type | Description |
|---|---|---|
list |
List[str]
|
one problem message per mapped record value without a claim, |
List[str]
|
per error of the result and per record column without mapping |
Source code in ez_wikidata/wikidata.py
796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 | |
convert_to_claim(value, pm)
Convert the given value to a corresponding wikidata statement Args: value: value of the statement pm: information about the property statement ot generate
Raises:
| Type | Description |
|---|---|
Exception
|
if property datatype is unknown or not supported |
Returns:
| Type | Description |
|---|---|
Union[BaseDataType, None]
|
BaseDataType |
Source code in ez_wikidata/wikidata.py
960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 | |
explain_result(result, lang='en')
explain the given result in human readable form
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result(WikidataResult)
|
the result to explain |
required | |
lang(str)
|
the language to use for label and description |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
one line for the label, the description and each claim |
Source code in ez_wikidata/wikidata.py
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 | |
getCredentials()
get my credentials https://test.wikidata.org/wiki/Property:P370
from the wd npm command line tool
Throws
Exception: if no credentials are available for the baseurl
Returns:
| Type | Description |
|---|---|
(str, str)
|
(username, password) of the account assigned to the baseurl |
Source code in ez_wikidata/wikidata.py
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 | |
getItemByName(itemName, itemType, lang='en')
get an item by Name ToDo: Needs to be reworked as always WDQS is used as endpoint even if a different one is defined Args: itemName(str): the item to look for itemType(str): the type of the item lang(str): the language of the itemName
Source code in ez_wikidata/wikidata.py
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 | |
get_datatype_of_property(property_id)
Get the datatype of the given property Args: property_id: id of the property e.g. P31 or 31
Returns:
| Type | Description |
|---|---|
Union[str, None]
|
datatype of the property of None if no datatype is defined |
Source code in ez_wikidata/wikidata.py
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 | |
get_date_claim(date, prop_nr)
staticmethod
Get the data statement for the given date and property id Args: date: date value prop_nr: id of the property
Returns:
| Type | Description |
|---|---|
Claim
|
statement of the given property number with the given value |
Source code in ez_wikidata/wikidata.py
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 | |
get_item(item_id)
read the given item without maxlag - maxlag is meant for edits, a read must not wait for the replication lag of Wikidata - see issue #18; writes keep the maxlag default of WikibaseIntegrator
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item_id
|
str
|
the id of the item to read |
required |
Returns:
| Name | Type | Description |
|---|---|---|
ItemEntity |
ItemEntity
|
the item |
Source code in ez_wikidata/wikidata.py
476 477 478 479 480 481 482 483 484 485 486 487 488 489 | |
get_item_label(item_id, lang=None)
Get the label for the given item id Args: item_id: id of the item lang: label language to return. Default is "en"
Returns:
| Name | Type | Description |
|---|---|---|
str |
Union[str, None]
|
label of the item |
None |
Union[str, None]
|
If the label can not be determined or the item_id is None or can not be found |
Source code in ez_wikidata/wikidata.py
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 | |
get_or_create_item(item_id)
Get or create the requested wikidata item Args: item_id: item to retrieve if None create a new item
Source code in ez_wikidata/wikidata.py
925 926 927 928 929 930 931 932 933 934 935 | |
get_prop_value(record, pm, lang)
Retrieve the property value from the record and prepare the value if necessary Args: record: record containing the property data pm: property mapping lang: language to use
Returns:
| Type | Description |
|---|---|
Any
|
value of the property from the record |
Source code in ez_wikidata/wikidata.py
937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 | |
get_record(item_id, property_mappings, include_label=True, include_description=True, label_for_qids=False)
Get the properties form the given item Args: item_id: id of the item to get the data from property_mappings: list of property values to extract include_label: include_description: label_for_qids: If True fetch the label for a linked Qid Returns: dict with the property values
Source code in ez_wikidata/wikidata.py
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 | |
get_wddatatype_of_property(property_id)
Get the datatype of the given property Args: property_id: id of the property e.g. P31 or 31
Returns:
| Type | Description |
|---|---|
WdDatatype
|
WdDatatype of the property of None if no datatype is defined |
Source code in ez_wikidata/wikidata.py
1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 | |
get_wikidata_item(qid_or_label, item_type_qid=None)
Get WikidataItem for given label or Qid
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
qid_or_label
|
str
|
label or Qid of a item |
required |
Returns:
| Type | Description |
|---|---|
Optional[WikidataItem]
|
WikidataItem |
Source code in ez_wikidata/wikidata.py
1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 | |
is_wikidata_item_id(value)
staticmethod
Returns true if the given value is a wikidata item id
Source code in ez_wikidata/wikidata.py
1033 1034 1035 1036 1037 1038 | |
is_wikidata_property_id(value)
staticmethod
Returns true if the given value is a wikidata property id
Source code in ez_wikidata/wikidata.py
1040 1041 1042 1043 1044 1045 | |
item_to_graph(item, subject_id='Qnew', lang='en')
convert the given item to an RDF graph in the Wikidata model: wd: for the item, p:/ps: for the statements, pq: for the qualifiers and wdt: for the truthy values - see issue #8
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item(ItemEntity)
|
the item to convert, e.g. the built item of a dry run |
required | |
subject_id(str)
|
the id to use for an item without id |
required | |
lang(str)
|
the language of label and description |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Graph |
Graph
|
the RDF graph |
Source code in ez_wikidata/wikidata.py
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 | |
loginWithCredentials(user=None, pwd=None)
login using the given credentials or credentials retrieved via self.getCredentials
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user(str)
|
the username |
required | |
pwd(str)
|
the password |
required |
Source code in ez_wikidata/wikidata.py
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 | |
logout()
log the user out again
Source code in ez_wikidata/wikidata.py
270 271 272 273 274 275 276 | |
mount_retry(session, timeout=(10, 60))
staticmethod
mount an urllib3 Retry on the given session honouring Retry-After on 429 / 5xx responses - WikibaseIntegrator reads via POST; every request gets a default timeout so that a server that does not answer can not block the caller indefinitely - see issue #16
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
Session
|
the requests session to configure |
required |
timeout
|
tuple
|
the default (connect, read) timeout in seconds |
(10, 60)
|
Returns:
| Name | Type | Description |
|---|---|---|
Session |
Session
|
the configured session |
Source code in ez_wikidata/wikidata.py
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | |
normalize_records(record, prop_map)
Normalize given record by converting Qids to WikidataItem objects (lookup label) and find out Qid if label given based on the given prop_map
Source code in ez_wikidata/wikidata.py
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 | |
sanitize_label(label, limit=None, postfix=None)
staticmethod
sanitize given label by ensuring it is not too long Args: label: label to sanitize limit: max length of the label
Returns:
| Type | Description |
|---|---|
str
|
sanitized label |
Source code in ez_wikidata/wikidata.py
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 | |
WikidataItem
dataclass
Source code in ez_wikidata/wikidata.py
1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 | |
__eq__(other)
WikidataItems are equal if the qid is equal
Source code in ez_wikidata/wikidata.py
1175 1176 1177 1178 1179 1180 1181 1182 | |
__post_init__()
handle the construction
Source code in ez_wikidata/wikidata.py
1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 | |
asText(long=True, wrapAt=0)
returns my content as a text representation
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
long(bool)
|
True if a long format including url is wished |
required | |
wrapAt(int)
|
wrap long lines at the given width (if >0) |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
a text representation of my content |
Source code in ez_wikidata/wikidata.py
1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 | |
getItemsByLabel(sparql, itemLabel, lang='en', debug=False)
classmethod
get a Wikidata items by the given label
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sparql(SPARQL)
|
the SPARQL endpoint to use |
required | |
itemLabel(str)
|
the label of the items |
required | |
lang(str)
|
the language of the label |
required | |
debug(bool)
|
if True show debugging information |
required |
Returns:
| Type | Description |
|---|---|
list
|
a list of potential items |
Source code in ez_wikidata/wikidata.py
1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 | |
getLabelAndDescription(sparql, itemId, lang='en', debug=False)
classmethod
get the label for the given item and language
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
itemId(str)
|
the wikidata Q/P id |
required | |
lang(str)
|
the language of the label |
required | |
debug(bool)
|
if True output debug information |
required |
Returns:
| Type | Description |
|---|---|
(str, str)
|
the label and description as a tuple |
Source code in ez_wikidata/wikidata.py
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 | |
WikidataResult
dataclass
a class for handling a wikidata result
Source code in ez_wikidata/wikidata.py
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | |
pretty_item_json
property
Returns a pretty-printed JSON string of the item.