diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/argaze.test/GazeFeatures.py | 14 | ||||
-rw-r--r-- | src/argaze/GazeFeatures.py | 11 |
2 files changed, 21 insertions, 4 deletions
diff --git a/src/argaze.test/GazeFeatures.py b/src/argaze.test/GazeFeatures.py index 5a3c2d9..dd0717c 100644 --- a/src/argaze.test/GazeFeatures.py +++ b/src/argaze.test/GazeFeatures.py @@ -117,6 +117,7 @@ class TestTimeStampedGazePositionsClass(unittest.TestCase): ts_gaze_positions = GazeFeatures.TimeStampedGazePositions() ts_gaze_positions[0] = GazeFeatures.GazePosition() ts_gaze_positions[1] = GazeFeatures.UnvalidGazePosition() + ts_gaze_positions[2] = {"value": (0, 0), "accuracy": 0.} # Check GazePosition is correctly stored and accessible as a GazePosition self.assertIsInstance(ts_gaze_positions[0], GazeFeatures.GazePosition) @@ -126,13 +127,22 @@ class TestTimeStampedGazePositionsClass(unittest.TestCase): self.assertIsInstance(ts_gaze_positions[1], GazeFeatures.UnvalidGazePosition) self.assertEqual(ts_gaze_positions[1].valid, False) + # Check dict with "value" and "accuracy" keys is correctly stored and accessible as a GazePosition + self.assertIsInstance(ts_gaze_positions[2], GazeFeatures.GazePosition) + self.assertEqual(ts_gaze_positions[2].valid, True) + # Check that bad data type insertion fails with self.assertRaises(AssertionError): - ts_gaze_positions[2] = "This string is not a gaze position value." + ts_gaze_positions[3] = "This string is not a gaze position value." + + # Check that dict with bad keys insertion fails + with self.assertRaises(AssertionError): + + ts_gaze_positions[4] = {"bad_key": (0, 0), "accuracy": 0.} # Check final lenght - self.assertEqual(len(ts_gaze_positions), 2) + self.assertEqual(len(ts_gaze_positions), 3) def test___repr__(self): """Test inherited string representation.""" diff --git a/src/argaze/GazeFeatures.py b/src/argaze/GazeFeatures.py index 33c10db..f7e8c01 100644 --- a/src/argaze/GazeFeatures.py +++ b/src/argaze/GazeFeatures.py @@ -73,8 +73,15 @@ class UnvalidGazePosition(GazePosition): class TimeStampedGazePositions(DataStructures.TimeStampedBuffer): """Define timestamped buffer to store gaze positions.""" - def __setitem__(self, key, value: GazePosition): - """Force value to be GazePosition.""" + def __setitem__(self, key, value: GazePosition|dict): + """Force GazePosition storage.""" + + # Convert dict into GazePosition + if type(value) == dict: + + assert(set(["value", "accuracy"]).issubset(value.keys())) + + value = GazePosition(value["value"], accuracy=value["accuracy"]) assert(type(value) == GazePosition or type(value) == UnvalidGazePosition) |