From 4621fd44dd9f916b1875a8acc7d57895ceb90944 Mon Sep 17 00:00:00 2001 From: Kevlyn Kadamala <43489154+kad99kev@users.noreply.github.com> Date: Thu, 17 Nov 2022 20:40:19 +0000 Subject: [PATCH] Removed dangling print and added disable_print parameter (#137) Co-authored-by: Mark Towers --- docs/api/registry.md | 9 ++++++++- docs/conf.py | 1 + docs/requirements.txt | 1 + gymnasium/envs/registration.py | 19 +++++++++++++++---- tests/envs/test_pprint_registry.py | 10 ++++------ 5 files changed, 29 insertions(+), 11 deletions(-) diff --git a/docs/api/registry.md b/docs/api/registry.md index b0f39941b..4bfab84c1 100644 --- a/docs/api/registry.md +++ b/docs/api/registry.md @@ -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 +``` \ No newline at end of file diff --git a/docs/conf.py b/docs/conf.py index 75b191e5a..1c561e9d0 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -60,6 +60,7 @@ napoleon_custom_sections = [("Returns", "params_style")] # Autodoc autoclass_content = "both" +autodoc_preserve_defaults = True # -- Options for HTML output ------------------------------------------------- diff --git a/docs/requirements.txt b/docs/requirements.txt index b4080e375..d28aec2ad 100644 --- a/docs/requirements.txt +++ b/docs/requirements.txt @@ -1,4 +1,5 @@ sphinx +sphinx-autobuild myst-parser sphinx_gallery git+https://github.com/Farama-Foundation/Celshast#egg=furo diff --git a/gymnasium/envs/registration.py b/gymnasium/envs/registration.py index c74f6be96..aa323d533 100644 --- a/gymnasium/envs/registration.py +++ b/gymnasium/envs/registration.py @@ -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="") diff --git a/tests/envs/test_pprint_registry.py b/tests/envs/test_pprint_registry.py index 872ea4a4f..c2badb3ad 100644 --- a/tests/envs/test_pprint_registry.py +++ b/tests/envs/test_pprint_registry.py @@ -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