def get_or_bust(data_dict, keys): '''Try and get values from dictionary and if they are not there raise a validation error. data_dict: a dictionary keys: either a single string key in which case will return a single value, or a iterable which will return a tuple for unpacking purposes. e.g single_value = get_or_bust(data_dict, 'a_key') value_1, value_2 = get_or_bust(data_dict, ['key1', 'key2']) ''' if isinstance(keys, basestring): keys = [keys] import ckan.logic.schema as schema schema = schema.create_schema_for_required_keys(keys) data_dict, errors = _validate(data_dict, schema) if errors: raise ValidationError(errors) # preserve original key order values = [data_dict[key] for key in keys] if len(values) == 1: return values[0] return tuple(values)
def get_or_bust( data_dict: dict[str, Any], keys: Union[str, Iterable[str]]) -> Union[Any, tuple[Any, ...]]: '''Return the value(s) from the given data_dict for the given key(s). Usage:: single_value = get_or_bust(data_dict, 'a_key') value_1, value_2 = get_or_bust(data_dict, ['key1', 'key2']) :param data_dict: the dictionary to return the values from :type data_dict: dictionary :param keys: the key(s) for the value(s) to return :type keys: either a string or a list :returns: a single value from the dict if a single key was given, or a tuple of values if a list of keys was given :raises: :py:exc:`ckan.logic.ValidationError` if one of the given keys is not in the given dictionary ''' if isinstance(keys, str): keys = [keys] from ckan.logic.schema import create_schema_for_required_keys schema = create_schema_for_required_keys(keys) data_dict, errors = _validate(data_dict, schema) if errors: raise ValidationError(errors) # preserve original key order values = [data_dict[key] for key in keys] if len(values) == 1: return values[0] return tuple(values)
def get_or_bust(data_dict, keys): '''Return the value(s) from the given data_dict for the given key(s). Usage:: single_value = get_or_bust(data_dict, 'a_key') value_1, value_2 = get_or_bust(data_dict, ['key1', 'key2']) :param data_dict: the dictionary to return the values from :type data_dict: dictionary :param keys: the key(s) for the value(s) to return :type keys: either a string or a list :returns: a single value from the dict if a single key was given, or a tuple of values if a list of keys was given :raises: :py:exc:`ckan.logic.ValidationError` if one of the given keys is not in the given dictionary ''' if isinstance(keys, basestring): keys = [keys] import ckan.logic.schema as schema schema = schema.create_schema_for_required_keys(keys) data_dict, errors = _validate(data_dict, schema) if errors: raise ValidationError(errors) # preserve original key order values = [data_dict[key] for key in keys] if len(values) == 1: return values[0] return tuple(values)