MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / _validate_dict

Function _validate_dict

dynamic_programming/viterbi.py:340–371  ·  view source on GitHub ↗

>>> _validate_dict({"b": 0.5}, "mock_name", float) >>> _validate_dict("invalid", "mock_name", float) Traceback (most recent call last): ... ValueError: mock_name must be a dict >>> _validate_dict({"a": 8}, "mock_name", dict) Traceback (most recent call last):

(
    _object: Any, var_name: str, value_type: type, nested: bool = False
)

Source from the content-addressed store, hash-verified

338
339
340def _validate_dict(
341 _object: Any, var_name: str, value_type: type, nested: bool = False
342) -> None:
343 """
344 >>> _validate_dict({"b": 0.5}, "mock_name", float)
345 >>> _validate_dict("invalid", "mock_name", float)
346 Traceback (most recent call last):
347 ...
348 ValueError: mock_name must be a dict
349 >>> _validate_dict({"a": 8}, "mock_name", dict)
350 Traceback (most recent call last):
351 ...
352 ValueError: mock_name all values must be dict
353 >>> _validate_dict({2: 0.5}, "mock_name",float, True)
354 Traceback (most recent call last):
355 ...
356 ValueError: mock_name all keys must be strings
357 >>> _validate_dict({"b": 4}, "mock_name", float,True)
358 Traceback (most recent call last):
359 ...
360 ValueError: mock_name nested dictionary all values must be float
361 """
362 if not isinstance(_object, dict):
363 msg = f"{var_name} must be a dict"
364 raise ValueError(msg)
365 if not all(isinstance(x, str) for x in _object):
366 msg = f"{var_name} all keys must be strings"
367 raise ValueError(msg)
368 if not all(isinstance(x, value_type) for x in _object.values()):
369 nested_text = "nested dictionary " if nested else ""
370 msg = f"{var_name} {nested_text}all values must be {value_type.__name__}"
371 raise ValueError(msg)
372
373
374if __name__ == "__main__":

Callers 2

_validate_dictsFunction · 0.85
_validate_nested_dictFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected