garmentiq.utils.validate_garment_class_dict

Validating a garment class dictionary.

 1"""Validating a garment class dictionary."""
 2def validate_garment_class_dict(class_dict: dict) -> bool:
 3    """
 4    Validates the structure and content of a garment class dictionary.
 5
 6    Ensures that the dictionary adheres to the expected format, including the presence
 7    of required keys ("num_predefined_points", "index_range", "instruction") and
 8    correct data types and logical consistency for their values.
 9
10    Args:
11        class_dict (dict): The dictionary to be validated.
12
13    Returns:
14        bool: True if the dictionary is valid, False otherwise.
15    """
16    required_keys = {"num_predefined_points", "index_range", "instruction"}
17
18    if not isinstance(class_dict, dict):
19        return False
20
21    for class_name, class_info in class_dict.items():
22        if not isinstance(class_name, str):
23            return False
24        if not isinstance(class_info, dict):
25            return False
26        if not required_keys.issubset(class_info.keys()):
27            return False
28
29        num_points = class_info["num_predefined_points"]
30        index_range = class_info["index_range"]
31        instruction = class_info["instruction"]
32
33        # Check types
34        if not isinstance(num_points, int):
35            return False
36        if (
37            not isinstance(index_range, tuple)
38            or len(index_range) != 2
39            or not all(isinstance(i, int) for i in index_range)
40        ):
41            return False
42
43        # Check logical consistency
44        if index_range[1] - index_range[0] != num_points:
45            return False
46
47        # Validate instruction field (must be a string ending with .json)
48        if not (isinstance(instruction, str) and instruction.endswith(".json")):
49            return False
50
51    return True
def validate_garment_class_dict(class_dict: dict) -> bool:
 3def validate_garment_class_dict(class_dict: dict) -> bool:
 4    """
 5    Validates the structure and content of a garment class dictionary.
 6
 7    Ensures that the dictionary adheres to the expected format, including the presence
 8    of required keys ("num_predefined_points", "index_range", "instruction") and
 9    correct data types and logical consistency for their values.
10
11    Args:
12        class_dict (dict): The dictionary to be validated.
13
14    Returns:
15        bool: True if the dictionary is valid, False otherwise.
16    """
17    required_keys = {"num_predefined_points", "index_range", "instruction"}
18
19    if not isinstance(class_dict, dict):
20        return False
21
22    for class_name, class_info in class_dict.items():
23        if not isinstance(class_name, str):
24            return False
25        if not isinstance(class_info, dict):
26            return False
27        if not required_keys.issubset(class_info.keys()):
28            return False
29
30        num_points = class_info["num_predefined_points"]
31        index_range = class_info["index_range"]
32        instruction = class_info["instruction"]
33
34        # Check types
35        if not isinstance(num_points, int):
36            return False
37        if (
38            not isinstance(index_range, tuple)
39            or len(index_range) != 2
40            or not all(isinstance(i, int) for i in index_range)
41        ):
42            return False
43
44        # Check logical consistency
45        if index_range[1] - index_range[0] != num_points:
46            return False
47
48        # Validate instruction field (must be a string ending with .json)
49        if not (isinstance(instruction, str) and instruction.endswith(".json")):
50            return False
51
52    return True

Validates the structure and content of a garment class dictionary.

Ensures that the dictionary adheres to the expected format, including the presence of required keys ("num_predefined_points", "index_range", "instruction") and correct data types and logical consistency for their values.

Arguments:
  • class_dict (dict): The dictionary to be validated.
Returns:

bool: True if the dictionary is valid, False otherwise.