@@ 53-109 (lines=57) @@ | ||
50 | return self._search_space.items() |
|
51 | ||
52 | ||
53 | class SearchConfig(SearchSpaceDictLike): |
|
54 | ||
55 | @calculate_properties |
|
56 | def __init__(self, **param_space): |
|
57 | super().__init__(**param_space) |
|
58 | ||
59 | for key, value in param_space.items(): |
|
60 | setattr(self, key, value) |
|
61 | ||
62 | @calculate_properties |
|
63 | def __setitem__(self, key, value): |
|
64 | SearchSpaceDictLike.__setitem__(self, key, value) |
|
65 | ||
66 | @calculate_properties |
|
67 | def __delitem__(self, key): |
|
68 | SearchSpaceDictLike.__delitem__(self, key) |
|
69 | ||
70 | def print(self, indent=2, max_list_length=5): |
|
71 | """ |
|
72 | Prints the dictionary in a readable format. |
|
73 | ||
74 | Args: |
|
75 | indent (int): The number of spaces to indent nested structures. |
|
76 | max_list_length (int): The maximum number of items to display from long lists. |
|
77 | """ |
|
78 | ||
79 | def format_value(value, level=0): |
|
80 | prefix = " " * (level * indent) |
|
81 | if isinstance(value, list): |
|
82 | if len(value) > max_list_length: |
|
83 | # Truncate long lists for readability |
|
84 | result = "[\n" |
|
85 | result += "".join( |
|
86 | f"{prefix}{' ' * indent}{repr(item)},\n" |
|
87 | for item in value[:max_list_length] |
|
88 | ) |
|
89 | result += f"{prefix}{' ' * indent}... ({len(value) - max_list_length} more items)\n" |
|
90 | result += f"{prefix}]" |
|
91 | else: |
|
92 | result = "[\n" |
|
93 | result += "".join( |
|
94 | f"{prefix}{' ' * indent}{repr(item)},\n" |
|
95 | for item in value |
|
96 | ) |
|
97 | result += f"{prefix}]" |
|
98 | elif isinstance(value, dict): |
|
99 | # Format nested dictionaries |
|
100 | result = "{\n" |
|
101 | for k, v in value.items(): |
|
102 | result += f"{prefix}{' ' * indent}{repr(k)}: {format_value(v, level + 1)},\n" |
|
103 | result += f"{prefix}}}" |
|
104 | else: |
|
105 | result = repr(value) |
|
106 | return result |
|
107 | ||
108 | for key, value in self.items(): |
|
109 | print(f"{key}: {format_value(value)}") |
|
110 |
@@ 51-107 (lines=57) @@ | ||
48 | return self._search_space.items() |
|
49 | ||
50 | ||
51 | class SearchSpace(SearchSpaceDictLike): |
|
52 | ||
53 | @calculate_properties |
|
54 | def __init__(self, **param_space): |
|
55 | super().__init__(**param_space) |
|
56 | ||
57 | for key, value in param_space.items(): |
|
58 | setattr(self, key, value) |
|
59 | ||
60 | @calculate_properties |
|
61 | def __setitem__(self, key, value): |
|
62 | SearchSpaceDictLike.__setitem__(self, key, value) |
|
63 | ||
64 | @calculate_properties |
|
65 | def __delitem__(self, key): |
|
66 | SearchSpaceDictLike.__delitem__(self, key) |
|
67 | ||
68 | def print(self, indent=2, max_list_length=5): |
|
69 | """ |
|
70 | Prints the dictionary in a readable format. |
|
71 | ||
72 | Args: |
|
73 | indent (int): The number of spaces to indent nested structures. |
|
74 | max_list_length (int): The maximum number of items to display from long lists. |
|
75 | """ |
|
76 | ||
77 | def format_value(value, level=0): |
|
78 | prefix = " " * (level * indent) |
|
79 | if isinstance(value, list): |
|
80 | if len(value) > max_list_length: |
|
81 | # Truncate long lists for readability |
|
82 | result = "[\n" |
|
83 | result += "".join( |
|
84 | f"{prefix}{' ' * indent}{repr(item)},\n" |
|
85 | for item in value[:max_list_length] |
|
86 | ) |
|
87 | result += f"{prefix}{' ' * indent}... ({len(value) - max_list_length} more items)\n" |
|
88 | result += f"{prefix}]" |
|
89 | else: |
|
90 | result = "[\n" |
|
91 | result += "".join( |
|
92 | f"{prefix}{' ' * indent}{repr(item)},\n" |
|
93 | for item in value |
|
94 | ) |
|
95 | result += f"{prefix}]" |
|
96 | elif isinstance(value, dict): |
|
97 | # Format nested dictionaries |
|
98 | result = "{\n" |
|
99 | for k, v in value.items(): |
|
100 | result += f"{prefix}{' ' * indent}{repr(k)}: {format_value(v, level + 1)},\n" |
|
101 | result += f"{prefix}}}" |
|
102 | else: |
|
103 | result = repr(value) |
|
104 | return result |
|
105 | ||
106 | for key, value in self.items(): |
|
107 | print(f"{key}: {format_value(value)}") |
|
108 |