Skip to content

Commit

Permalink
Move __str__ to pprint_grid and add test for it (#394)
Browse files Browse the repository at this point in the history
  • Loading branch information
pseudo-rnd-thoughts authored Aug 14, 2023
1 parent 9dbdf61 commit 55beb58
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
6 changes: 5 additions & 1 deletion minigrid/minigrid_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,16 @@ def hash(self, size=16):
def steps_remaining(self):
return self.max_steps - self.step_count

def __str__(self):
def pprint_grid(self):
"""
Produce a pretty string of the environment's grid along with the agent.
A grid cell is represented by 2-character string, the first one for
the object and the second one for the color.
"""
if self.agent_pos is None or self.agent_dir is None or self.grid is None:
raise ValueError(
"The environment hasn't been `reset` therefore the `agent_pos`, `agent_dir` or `grid` are unknown."
)

# Map of object types to short string
OBJECT_TO_STR = {
Expand Down
22 changes: 22 additions & 0 deletions tests/test_envs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import pickle
import re
import warnings

import gymnasium as gym
Expand Down Expand Up @@ -327,3 +328,24 @@ def env_func():
env.reset()
env.step(env.action_space.sample())
env.close()


def test_pprint_grid(env_id="MiniGrid-Empty-8x8-v0"):
env = gym.make(env_id)

env_repr = str(env)
assert (
env_repr
== "<OrderEnforcing<PassiveEnvChecker<EmptyEnv<MiniGrid-Empty-8x8-v0>>>>"
)

with pytest.raises(
ValueError,
match=re.escape(
"The environment hasn't been `reset` therefore the `agent_pos`, `agent_dir` or `grid` are unknown."
),
):
env.unwrapped.pprint_grid()

env.reset()
assert isinstance(env.unwrapped.pprint_grid(), str)

0 comments on commit 55beb58

Please sign in to comment.