Upload type_utils.py with huggingface_hub
Browse files- type_utils.py +31 -0
type_utils.py
CHANGED
|
@@ -841,3 +841,34 @@ def to_float_or_default(v, failure_default=0):
|
|
| 841 |
if failure_default is None:
|
| 842 |
raise e
|
| 843 |
return failure_default
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 841 |
if failure_default is None:
|
| 842 |
raise e
|
| 843 |
return failure_default
|
| 844 |
+
|
| 845 |
+
|
| 846 |
+
def verify_required_schema(
|
| 847 |
+
required_schema_dict: typing.Dict[str, str],
|
| 848 |
+
input_dict: typing.Dict[str, typing.Any],
|
| 849 |
+
) -> None:
|
| 850 |
+
"""Verifies if passed input_dict has all required fields, and they are of proper types according to required_schema_dict.
|
| 851 |
+
|
| 852 |
+
Parameters:
|
| 853 |
+
required_schema_dict (Dict[str, str]):
|
| 854 |
+
Schema where a key is name of a field and a value is a string
|
| 855 |
+
representing a type of its value.
|
| 856 |
+
input_dict (Dict[str, Any]):
|
| 857 |
+
Dict with input fields and their respective values.
|
| 858 |
+
"""
|
| 859 |
+
for field_name, data_type_string in required_schema_dict.items():
|
| 860 |
+
try:
|
| 861 |
+
value = input_dict[field_name]
|
| 862 |
+
except KeyError as e:
|
| 863 |
+
raise KeyError(
|
| 864 |
+
f"Unexpected field name: '{field_name}'. "
|
| 865 |
+
f"The available names: {list(input_dict.keys())}."
|
| 866 |
+
) from e
|
| 867 |
+
|
| 868 |
+
data_type = parse_type_string(data_type_string)
|
| 869 |
+
|
| 870 |
+
if not isoftype(value, data_type):
|
| 871 |
+
raise ValueError(
|
| 872 |
+
f"Passed value '{value}' of field '{field_name}' is not "
|
| 873 |
+
f"of required type: ({data_type_string})."
|
| 874 |
+
)
|