From 224b62b33771333696d85d478e0ddbff91f60480 Mon Sep 17 00:00:00 2001 From: Lindy Date: Fri, 29 Nov 2024 19:44:49 -0500 Subject: [PATCH 1/2] Improve extend method in LimitedHistory to enforce memory depth - Updated the extend method in the LimitedHistory class to properly truncate oldest entries when the combined history exceeds the defined memory depth. - Added a corresponding unit test in axelrod/tests/unit/test_history.py to validate this behavior. - Formatted all committed files using black for consistent code style. --- axelrod/history.py | 10 +++++++++- axelrod/tests/unit/test_history.py | 11 +++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/axelrod/history.py b/axelrod/history.py index 12114399b..0e914da77 100644 --- a/axelrod/history.py +++ b/axelrod/history.py @@ -117,8 +117,8 @@ def __init__(self, memory_depth, plays=None, coplays=None): memory_depth, int: length of history to retain """ - super().__init__(plays=plays, coplays=coplays) self.memory_depth = memory_depth + super().__init__(plays=plays, coplays=coplays) def flip_plays(self): """Creates a flipped plays history for use with DualTransformer.""" @@ -138,3 +138,11 @@ def append(self, play, coplay): first_play, first_coplay = self._plays.pop(0), self._coplays.pop(0) self._actions[first_play] -= 1 self._state_distribution[(first_play, first_coplay)] -= 1 + + def extend(self, new_plays, new_coplays): + """A function that emulates list.extend, respecting the stated memory depth.""" + self._plays.extend(new_plays) + self._coplays.extend(new_coplays) + if len(self._plays) > self.memory_depth: + self._plays = self._plays[-self.memory_depth :] + self._coplays = self._coplays[-self.memory_depth :] diff --git a/axelrod/tests/unit/test_history.py b/axelrod/tests/unit/test_history.py index 7c3fa6ef7..4907a2c2f 100644 --- a/axelrod/tests/unit/test_history.py +++ b/axelrod/tests/unit/test_history.py @@ -123,3 +123,14 @@ def test_memory_depth(self): h.state_distribution, Counter({(D, D): 1, (C, D): 1, (D, C): 1, (C, C): 0}), ) + + def test_extend(self): + h1 = LimitedHistory(3, plays=[C, C, D], coplays=[C, C, C]) + self.assertEqual(list(h1), [C, C, D]) + h1.extend([C, C], [D, D]) + self.assertEqual(list(h1), [D, C, C]) + h1.extend([D, C], [D, D]) + self.assertEqual(list(h1), [C, D, C]) + h1.memory_depth = 4 + h1.extend([D, C], [D, D]) + self.assertEqual(list(h1), [D, C, D, C]) From ea7fd974eb3830d85fc703c6eca3096854f15ca6 Mon Sep 17 00:00:00 2001 From: Lindy Date: Wed, 4 Dec 2024 10:36:10 -0500 Subject: [PATCH 2/2] Ignore workflow failures by adding suppress_health_check - add suppress_health_check to test_equality function in test_game.py to prevent workflow failure in Github remote. - Update HealthCheck module import order in test_game.py --- axelrod/tests/unit/test_game.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/axelrod/tests/unit/test_game.py b/axelrod/tests/unit/test_game.py index 083431e22..e124c9e18 100644 --- a/axelrod/tests/unit/test_game.py +++ b/axelrod/tests/unit/test_game.py @@ -1,7 +1,7 @@ import unittest import numpy as np -from hypothesis import given, settings +from hypothesis import HealthCheck, given, settings from hypothesis.extra.numpy import array_shapes, arrays from hypothesis.strategies import integers @@ -123,7 +123,7 @@ def test_random_repr(self, asymgame): self.assertEqual(expected_repr, str(asymgame)) @given(asymgame1=asymmetric_games(), asymgame2=asymmetric_games()) - @settings(max_examples=5) + @settings(max_examples=5, suppress_health_check=(HealthCheck.too_slow,)) def test_equality(self, asymgame1, asymgame2): """Tests equality of AsymmetricGames based on their matrices.""" self.assertFalse(asymgame1 == "foo")