Removed dangling print and added disable_print parameter (#137)

Co-authored-by: Mark Towers <mark.m.towers@gmail.com>
This commit is contained in:
Kevlyn Kadamala
2022-11-17 20:40:19 +00:00
committed by GitHub
parent f037fdea5a
commit 4621fd44dd
5 changed files with 29 additions and 11 deletions

View File

@@ -21,7 +21,7 @@ Environments can also be created through python imports.
## All registered environments
To find all the registered Gymnasium environments, use the `gymnasium.envs.registry.keys()`.
To find all the registered Gymnasium environments, use the `gymnasium.pprint_registry()`.
This will not include environments registered only in OpenAI Gym however can be loaded by `gymnasium.make`.
## Spec
@@ -29,3 +29,10 @@ This will not include environments registered only in OpenAI Gym however can be
```{eval-rst}
.. autofunction:: gymnasium.spec
```
## Pretty print registry
```{eval-rst}
.. autofunction:: gymnasium.pprint_registry
```

View File

@@ -60,6 +60,7 @@ napoleon_custom_sections = [("Returns", "params_style")]
# Autodoc
autoclass_content = "both"
autodoc_preserve_defaults = True
# -- Options for HTML output -------------------------------------------------

View File

@@ -1,4 +1,5 @@
sphinx
sphinx-autobuild
myst-parser
sphinx_gallery
git+https://github.com/Farama-Foundation/Celshast#egg=furo

View File

@@ -708,15 +708,23 @@ def pprint_registry(
_registry: dict = registry,
max_rows: int = 10,
exclude_namespaces: Optional[List[str]] = None,
) -> None:
"""List the environments currently supported."""
disable_print: bool = False,
) -> Optional[str]:
"""Pretty print the environments in the registry.
Args:
_registry: Environment registry to be printed.
max_rows: Number of rows per column.
exclude_namespaces: Exclude any namespaces from being printed.
disable_print: Whether to return a string of all the namespaces and environment IDs
instead of printing it to console.
"""
# Defaultdict to store environment names according to namespace.
namespace_envs = defaultdict(lambda: [])
max_justify = float("-inf")
for env in _registry.values():
namespace, _, _ = parse_env_id(env.id)
print(namespace, env.id, env.entry_point)
if namespace is None:
# Since namespace is currently none, use regex to obtain namespace from entrypoints.
env_entry_point = re.sub(r":\w+", "", env.entry_point)
@@ -756,4 +764,7 @@ def pprint_registry(
return_str += "\n"
return_str += "\n"
return return_str
if disable_print:
return return_str
else:
print(return_str, end="")

View File

@@ -10,20 +10,19 @@ def test_pprint_custom_registry():
"CartPole-v0": gym.envs.registry["CartPole-v0"],
"CartPole-v1": gym.envs.registry["CartPole-v1"],
}
out = gym.pprint_registry(a)
out = gym.pprint_registry(a, disable_print=True)
correct_out = """===== classic_control =====
CartPole-v0
CartPole-v1
"""
assert out == correct_out
def test_pprint_registry():
"""Testing the default registry, with no changes."""
out = gym.pprint_registry()
out = gym.pprint_registry(disable_print=True)
correct_out = """===== classic_control =====
Acrobot-v1
@@ -79,8 +78,7 @@ test/NoHumanOldAPI-v0
def test_pprint_registry_exclude_namespaces():
"""Testing the default registry, with no changes."""
out = gym.pprint_registry(
max_rows=20,
exclude_namespaces=["classic_control"],
max_rows=20, exclude_namespaces=["classic_control"], disable_print=True
)
correct_out = """===== box2d =====
@@ -134,7 +132,7 @@ def test_pprint_registry_no_entry_point():
"""Test registry if there is environment with no entry point."""
gym.register("NoNamespaceEnv", "no-entry-point")
out = gym.pprint_registry()
out = gym.pprint_registry(disable_print=True)
correct_out = """===== classic_control =====
Acrobot-v1