def _override_gym_spaces_dict_constructor()

in pybulletX/utils/space_dict.py [0:0]


def _override_gym_spaces_dict_constructor():
    """
    Override the existing the constructor of class gym.spaces.Dict.
    The only difference between this and the original __init__ is that space
    in spaces.values() can not only be gym.Space but also any kind of dict-like
    object that conforms to collections.abc.Mapping
    """

    def __init__(self, spaces=None, **spaces_kwargs):
        assert (spaces is None) or (
            not spaces_kwargs
        ), "Use either Dict(spaces=dict(...)) or Dict(foo=x, bar=z)"
        if spaces is None:
            spaces = spaces_kwargs
        if isinstance(spaces, dict) and not isinstance(spaces, collections.OrderedDict):
            spaces = collections.OrderedDict(sorted(list(spaces.items())))
        if isinstance(spaces, list):
            spaces = collections.OrderedDict(spaces)
        self.spaces = spaces
        for key in spaces.keys():
            if isinstance(spaces[key], collections.abc.Mapping):
                spaces[key] = SpaceDict(**spaces[key])
            assert isinstance(
                spaces[key], gym.Space
            ), "Values of the dict should be instances of gym.Space"
        super(gym.spaces.Dict, self).__init__(
            None, None
        )  # None for shape and dtype, since it'll require special handling

    setattr(gym.spaces.Dict, "__init__", __init__)