Skip to content

Commit 4bb7574

Browse files
committed
refactor(repr): Return repr as python object
1 parent e7284fe commit 4bb7574

File tree

1 file changed

+27
-25
lines changed

1 file changed

+27
-25
lines changed

Diff for: cellular_automata/wa_tor.py

+27-25
Original file line numberDiff line numberDiff line change
@@ -77,18 +77,18 @@ def reset_reproduction_time(self) -> None:
7777
def __repr__(self) -> str:
7878
"""
7979
>>> Entity(prey=True, coords=(1, 1))
80-
<entity_type=prey coords=(1, 1) remaining_reproduction_time=5>
81-
>>> Entity(prey=False, coords=(2, 1))
82-
<entity_type=predator coords=(2, 1) remaining_reproduction_time=20 energy=15>
80+
Entity(prey=True, coords=(1, 1), remaining_reproduction_time=5)
81+
>>> Entity(prey=False, coords=(2, 1)) # doctest: +NORMALIZE_WHITESPACE
82+
Entity(prey=False, coords=(2, 1),
83+
remaining_reproduction_time=20, energy_value=15)
8384
"""
8485
repr_ = (
85-
f"entity_type={'prey' if self.prey is True else 'predator'}"
86-
f" coords={self.coords}"
87-
f" remaining_reproduction_time={self.remaining_reproduction_time}"
86+
f"Entity(prey={self.prey}, coords={self.coords}, "
87+
f"remaining_reproduction_time={self.remaining_reproduction_time}"
8888
)
89-
if self.prey is False:
90-
repr_ += f" energy={self.energy_value}"
91-
return f"<{repr_}>"
89+
if self.energy_value is not None:
90+
repr_ += f", energy_value={self.energy_value}"
91+
return f"{repr_})"
9292

9393

9494
class WaTor:
@@ -230,8 +230,8 @@ def get_surrounding_prey(self, entity: Entity) -> list[Entity]:
230230
... [None, Entity(True, (2, 1)), None]])
231231
>>> wt.get_surrounding_prey(
232232
... Entity(False, (1, 1))) # doctest: +NORMALIZE_WHITESPACE
233-
[<entity_type=prey coords=(2, 1) remaining_reproduction_time=5>,
234-
<entity_type=prey coords=(0, 1) remaining_reproduction_time=5>]
233+
[Entity(prey=True, coords=(2, 1), remaining_reproduction_time=5),
234+
Entity(prey=True, coords=(0, 1), remaining_reproduction_time=5)]
235235
>>> wt.set_planet([[Entity(False, (0, 0))]])
236236
>>> wt.get_surrounding_prey(Entity(False, (0, 0)))
237237
[]
@@ -240,7 +240,7 @@ def get_surrounding_prey(self, entity: Entity) -> list[Entity]:
240240
... [None, Entity(False, (1, 1)), Entity(True, (2, 1))],
241241
... [None, None, None]])
242242
>>> wt.get_surrounding_prey(Entity(False, (1, 0)))
243-
[<entity_type=prey coords=(0, 0) remaining_reproduction_time=5>]
243+
[Entity(prey=True, coords=(0, 0), remaining_reproduction_time=5)]
244244
"""
245245
coords = entity.coords
246246
row, col = coords
@@ -302,25 +302,25 @@ def move_and_reproduce(
302302
>>> wt.set_planet(planet)
303303
>>> wt.move_and_reproduce(Entity(True, coords=(1, 1)), direction_orders=["N"])
304304
>>> wt.planet # doctest: +NORMALIZE_WHITESPACE
305-
[[None, <entity_type=prey coords=(0, 1) remaining_reproduction_time=4>, None],
305+
[[None, Entity(prey=True, coords=(0, 1), remaining_reproduction_time=4), None],
306306
[None, None, None],
307307
[None, None, None]]
308308
>>> wt.planet[0][0] = Entity(True, coords=(0, 0))
309309
>>> wt.planet[0][2] = None
310310
>>> wt.move_and_reproduce(Entity(True, coords=(0, 1)),
311311
... direction_orders=["N", "W", "E", "S"])
312312
>>> wt.planet # doctest: +NORMALIZE_WHITESPACE
313-
[[<entity_type=prey coords=(0, 0) remaining_reproduction_time=5>, None,
314-
<entity_type=prey coords=(0, 2) remaining_reproduction_time=4>],
313+
[[Entity(prey=True, coords=(0, 0), remaining_reproduction_time=5), None,
314+
Entity(prey=True, coords=(0, 2), remaining_reproduction_time=4)],
315315
[None, None, None],
316316
[None, None, None]]
317317
>>> wt.planet[0][1] = wt.planet[0][2]
318318
>>> wt.planet[0][2] = None
319319
>>> wt.move_and_reproduce(Entity(True, coords=(0, 1)),
320320
... direction_orders=["N", "W", "S", "E"])
321321
>>> wt.planet # doctest: +NORMALIZE_WHITESPACE
322-
[[<entity_type=prey coords=(0, 0) remaining_reproduction_time=5>, None, None],
323-
[None, <entity_type=prey coords=(1, 1) remaining_reproduction_time=4>, None],
322+
[[Entity(prey=True, coords=(0, 0), remaining_reproduction_time=5), None, None],
323+
[None, Entity(prey=True, coords=(1, 1), remaining_reproduction_time=4), None],
324324
[None, None, None]]
325325
326326
>>> wt = WaTor(WIDTH, HEIGHT)
@@ -330,8 +330,10 @@ def move_and_reproduce(
330330
>>> wt.move_and_reproduce(reproducable_entity,
331331
... direction_orders=["N", "W", "S", "E"])
332332
>>> wt.planet # doctest: +NORMALIZE_WHITESPACE
333-
[[<entity_type=predator coords=(0, 0) remaining_reproduction_time=20 energy=15>,
334-
<entity_type=predator coords=(0, 1) remaining_reproduction_time=20 energy=15>]]
333+
[[Entity(prey=False, coords=(0, 0),
334+
remaining_reproduction_time=20, energy_value=15),
335+
Entity(prey=False, coords=(0, 1), remaining_reproduction_time=20,
336+
energy_value=15)]]
335337
"""
336338
coords = entity.coords
337339
row, col = coords
@@ -400,8 +402,8 @@ def perform_prey_actions(
400402
>>> wt.perform_prey_actions(reproducable_entity,
401403
... direction_orders=["N", "W", "S", "E"])
402404
>>> wt.planet # doctest: +NORMALIZE_WHITESPACE
403-
[[<entity_type=prey coords=(0, 0) remaining_reproduction_time=5>,
404-
<entity_type=prey coords=(0, 1) remaining_reproduction_time=5>]]
405+
[[Entity(prey=True, coords=(0, 0), remaining_reproduction_time=5),
406+
Entity(prey=True, coords=(0, 1), remaining_reproduction_time=5)]]
405407
"""
406408
self.move_and_reproduce(entity, direction_orders)
407409

@@ -431,8 +433,8 @@ def perform_predator_actions(
431433
>>> wt.set_planet([[Entity(True, coords=(0, 0)), Entity(False, coords=(0, 1))]])
432434
>>> wt.perform_predator_actions(Entity(False, coords=(0, 1)), (0, 0), [])
433435
>>> wt.planet # doctest: +NORMALIZE_WHITESPACE
434-
[[<entity_type=predator coords=(0, 0)
435-
remaining_reproduction_time=20 energy=19>, None]]
436+
[[Entity(prey=False, coords=(0, 0),
437+
remaining_reproduction_time=20, energy_value=19), None]]
436438
"""
437439
assert entity.energy_value is not None # [type checking]
438440

@@ -576,9 +578,9 @@ def display_visually(wt: WaTor, iter_number: int, *, colour: bool = True) -> Non
576578

577579

578580
if __name__ == "__main__":
579-
# import doctest
581+
import doctest
580582

581-
# doctest.testmod()
583+
doctest.testmod()
582584

583585
wt = WaTor(WIDTH, HEIGHT)
584586
wt.time_passed = display_visually

0 commit comments

Comments
 (0)