2828
2929# cython: language_level=3
3030
31+ import collections.abc
3132from collections import defaultdict, deque, OrderedDict
3233import copy
3334from itertools import chain
@@ -52,12 +53,14 @@ from ConfigSpace.forbidden import (
5253 AbstractForbiddenClause,
5354 AbstractForbiddenConjunction,
5455)
55- from typing import Union, List, Any, Dict, Iterable, Set, Tuple, Optional
56+ from typing import (
57+ Union, List, Any, Dict, Iterable, Set, Tuple, Optional, KeysView
58+ )
5659from ConfigSpace.exceptions import ForbiddenValueError
5760import ConfigSpace.c_util
5861
5962
60- class ConfigurationSpace (object ):
63+ class ConfigurationSpace (collections.abc.Mapping ):
6164
6265 # TODO add a method to add whole configuration spaces as a child "tree"
6366
@@ -744,6 +747,9 @@ class ConfigurationSpace(object):
744747 """
745748 return list(self._hyperparameters.keys())
746749
750+ def __getitem__(self , key: str ) -> Hyperparameter:
751+ return self.get_hyperparameter(key )
752+
747753 def get_hyperparameter(self , name: str ) -> Hyperparameter:
748754 """
749755 Gives the hyperparameter from the configuration space given its name.
@@ -1229,6 +1235,12 @@ class ConfigurationSpace(object):
12291235 """ Allows to iterate over the hyperparameter names in (hopefully?) the right order."""
12301236 return iter(self._hyperparameters.keys())
12311237
1238+ def keys(self ) -> KeysView[str]:
1239+ return self._hyperparameters.keys()
1240+
1241+ def __len__(self ) -> int:
1242+ return len(self._hyperparameters )
1243+
12321244 def sample_configuration(self , size: int = 1 ) -> Union['Configuration', List['Configuration']]:
12331245 """
12341246 Sample ``size`` configurations from the configuration space object.
@@ -1330,7 +1342,7 @@ class ConfigurationSpace(object):
13301342 self.random = np.random.RandomState(seed)
13311343
13321344
1333- class Configuration(object ):
1345+ class Configuration(collections.abc. Mapping ):
13341346 def __init__ (self , configuration_space: ConfigurationSpace ,
13351347 values: Union[None , Dict[str , Union[str , float , int]]] = None ,
13361348 vector: Union[None , np.ndarray] = None ,
@@ -1582,6 +1594,9 @@ class Configuration(object):
15821594 def __iter__ (self ) -> Iterable:
15831595 return iter(self.keys())
15841596
1597+ def __len__(self ) -> int:
1598+ return len(self.configuration_space )
1599+
15851600 def keys(self ) -> List[str]:
15861601 """
15871602 Cache the keys to speed up the process of retrieving the keys.
0 commit comments