checksit.cvs

Load and check against controlled vocabularies.

This module provides a class to load controlled vocabularies, either from JSON files or from URLs. It also provides a method to check a value against a controlled vocabulary.

Classes

Vocabs()

Load and check against controlled vocabularies.

class checksit.cvs.Vocabs[source]

Load and check against controlled vocabularies.

This class provides methods to load controlled vocabularies from JSON files or from URLs on GitHub or the CEDA Vocab Service. It also provides a method to check a value against a value or list of values from those controlled vocabularies.

_vocabs

A dictionary of controlled vocabularies, where the keys are the vocabulary IDs and the values are the vocabularies themselves.

check(vocab_lookup: str | List[str | int | float], value: Any, label: str = '', lookup: bool = True, spec_verb: bool = False)[source]

Checks value or values against value or values in vocabulary.

Checks whether a given value (or values) matches the value or values at a given location in a controlled vocabulary. Controlled vocabulary is a JSON file either within the vocabs directory, or at a given URL. For vocabulary files in the vocab directory, the vocab_lookup should start “__vocabs__:path/to/file:…” (NOTE without the “.json” extension), and vocabularies accessed by a URL should start “__URL__:www.website.com/vocab_file.json:…” (NOTE without the “https://” at the start, but with the “.json” extension). Each key within the vocabulary file that leads to the value(s) required should follow and be separated by a colon. vocab_lookup could also be a list of values to check directly against - in this case, set lookup to False. Returns list of error messages for values not found in vocabulary.

Parameters:
  • vocab_lookup – Vocabulary to use and path to value(s) in vocabulary.

  • value – Value(s) to check against vocabulary.

  • label – Text to prepend to error messages.

  • lookup – Find vocabulary from file or URL (True, default), or use vocab_lookup as the vocabulary to use (False).

  • spec_verb – Print information about vocab check.

Returns:

List of messages where value(s) can not be found in vocabulary.

lookup(vocab_lookup: str) Dict[str, Any] | List[str] | str | int | float[source]

Nested dictionary-style look-up for value(s) in a vocabulary.

Iterates through a vocabulary to find the value(s) to that are required for the check. The string “__all__” can be used once within the vocab_lookup. If “__all__” is the last key in the lookup, this will return a list of all the keys at that stage in the vocabulary file. If it comes before, e.g. “__all__:type”, then it will return the value of “type” from every dictionary at the “__all__” level in the vocabulary.

Parameters:

vocab_lookup – String that states which vocabulary to use and what value(s) within the vocabulary to use. Should be of format “path/to/vocab_id:keys:in:vocab:file”.

Returns:

Value, list of values, or dictionary of data from vocabulary.