lctn_utils

The data.lctn_utils module provides functions for querying locations.

Functions

Classes

wikirepo.data.lctn_utils.lctn_to_qid_dict()[source]

Queries a dictionary that links a location’s name to its WikiData QID.

wikirepo.data.lctn_utils.qid_to_lctn_dict()[source]

Queries a dictionary that links a location’s name to its WikiData QID.

Notes

Keys are QIDs, and values are dictionaries of QID labels and their locational level.

This could also be rewritten using Q3624078: sovereign state.

wikirepo.data.lctn_utils.incl_lctn_lbls(lctn_lvls=False)[source]

Queries the included location labels.

Parameters:
lctn_lvlsstr or list (contains strs)

The level(s) of location to be queried.

Returns:
incl_lctnslist (contains strs)

The Wikidata labels corresponding to the provided location level(s).

wikirepo.data.lctn_utils.incl_lctn_ids()[source]

Queries the included location ids.

wikirepo.data.lctn_utils.lctn_lbl_to_qid(locations)[source]

Returns the Wikidata QID for given location(s).

Parameters:
locationsstr or list (contains strs)

The label(s) of location(s) to be converted.

Returns:
wd_qidslist (contains strs)

The Wikidata QIDs corresponding to the provided location label(s).

wikirepo.data.lctn_utils.depth_to_col_name(depth)[source]

Derives the proper name of the column for locations given a depth.

wikirepo.data.lctn_utils.depth_to_qid_cols(depth)[source]

Derives the proper name of the column for qids for depth based assignment.

wikirepo.data.lctn_utils.depth_to_qid_col_name(depth)[source]

Derives the proper name of the column for qids for depth based assignment.

wikirepo.data.lctn_utils.depth_to_qid_cols(depth)[source]

Derives the proper name of the column for qids for depth based assignment.

wikirepo.data.lctn_utils.find_qid_get_depth(lctns_dict, c=0)[source]

Finds all QIDs and gets their depths.

wikirepo.data.lctn_utils.get_qids_at_depth(lctns_dict, depth=None)[source]

Finds all QIDs at a given depth of a LocationsDict.

wikirepo.data.lctn_utils.iter_set_dict(dictionary, key, sub_key, value)[source]

Iterates until a key is found, and then sets the value (potentially given a sub_key).

wikirepo.data.lctn_utils.gen_lctns_dict(ents_dict=None, locations=None, depth=0, sub_lctns=True, timespan=None, interval=None, verbose=True)[source]

Generates a dictionary of locations indexed by QIDs.

Parameters:
ents_dictwd_utils.EntitiesDictoptional (default=None)

A dictionary with keys being Wikidata QIDs and values being their entities.

locationsstr or list (contains strs)optional (default=None)

The name of a location or list of location names.

depthint (default=0, no sub_locations)

The depth from the given lbls or qids that data should go.

Note: this uses ‘P150’ (contains administrative territorial entity).

sub_lctnsstr or list (contains strs)optional (default=None)

sub_locations to subset by or not subset by adding ‘~’ as the first character.

timespantwo element tuple or listcontains datetime.date or tuple (default=None: (date.today(), date.today()))

A tuple or list that defines the start and end dates to be queried.

Note 1: if True, then the full timespan from 1-1-1 to the current day will be queried.

Note 2: passing a single entry will query for that date only.

intervalstr

The time interval over which queries will be made.

Note 1: see data.time_utils for options.

Note 2: if None, then only the most recent data will be queried.

verbosebool (default=True)

Whether to show a tqdm progress bar for the creation of the dictionary.

Potential later arguments:
multicorebool or int (default=False)

Whether to make use of multiple processes and threads, and how many to use.

Note: True uses all available cores.

Returns:
subs_dictdict

A dictionary of the given qids as keys and dictionaries of their subsidiaries by ‘P150’ as items.

Notes

‘P150’ (contains administrative territorial entity) is used to subset.

wikirepo.data.lctn_utils.derive_depth(a_dict, depth=0)[source]

Derives the depth of a LocationsDict.

wikirepo.data.lctn_utils.merge_lctn_dicts(ld1, ld2)[source]

Merges two location dictionaries conditionally on them having the same depth.

class wikirepo.data.lctn_utils.LocationsDict(*args, **kwargs)[source]

A dictionary for storing WikiData locations.

Notes

Keywords are QIDs, and values are dictionaries of depth, interval, and timespan specific information.

key_lbls_list()[source]

Provides a list of all location labels in the LocationsDict.

get_depth()[source]

The depth of the LocationsDict.

iter_key_items(kv)[source]

The items within a key in LocationsDict.

iter_set(key, sub_key, value)[source]

Finds and sets a key in LocationsDict.

get_qids_at_depth(depth=None)[source]

Finds all QIDs at a given depth of a LocationsDict.

key_lbls_at_depth(ents_dict, depth)[source]

Provides a list of key labels at a given depth.

_print()[source]

Prints the full LocationsDict.