Skip to content

Commit

Permalink
rename views.get_last method for better clarification
Browse files Browse the repository at this point in the history
  • Loading branch information
keighrim committed Jul 12, 2024
1 parent 79f43e1 commit 2543352
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
15 changes: 13 additions & 2 deletions mmif/serialize/mmif.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,24 @@ def append(self, value: View, overwrite=False) -> None:
"""
super()._append_with_key(value.id, value, overwrite)

def get_last(self) -> Optional[View]:
def get_last_contentful_view(self) -> Optional[View]:
"""
Returns the last view appended to the list.
Returns the last view that is contentful, i.e., has no error or warning .
"""
for view in reversed(self._items.values()):
if 'error' not in view.metadata and 'warning' not in view.metadata:
return view

def get_last_view(self) -> Optional[View]:
"""
Returns the last view appended.
"""
if self._items:
return self._items[list(self._items.keys())[-1]]

def get_last(self) -> Optional[View]:
warnings.warn('get_last() is deprecated, use get_last_contentful_view() instead.', DeprecationWarning)
return self.get_last_contentful_view()


class Mmif(MmifObject):
Expand Down
22 changes: 11 additions & 11 deletions tests/test_serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -1118,11 +1118,11 @@ def test_document_added_properties(self):
# then converted to an `Annotation` annotation during serialization at `Mmif`-level
mmif.add_document(doc1)
## no Annotation before serialization
self.assertEqual(0, len(list(mmif.views.get_last().get_annotations(AnnotationTypes.Annotation, author='me'))))
self.assertEqual(0, len(list(mmif.views.get_last_contentful_view().get_annotations(AnnotationTypes.Annotation, author='me'))))
## after serialization, the `Annotation` annotation should be added to the last view
## note that we didn't add any views, so the last view before and after the serialization are the same
mmif_roundtrip = Mmif(mmif.serialize())
self.assertTrue(next(mmif_roundtrip.views.get_last().get_annotations(AnnotationTypes.Annotation, author='me')))
self.assertTrue(next(mmif_roundtrip.views.get_last_contentful_view().get_annotations(AnnotationTypes.Annotation, author='me')))
# finally, when deserialized back to a Mmif instance, the `Annotation` props should be added
# as a property of the document
doc1_mmif_roundtrip = mmif_roundtrip.get_document_by_id('doc1')
Expand All @@ -1135,7 +1135,7 @@ def test_document_adding_duplicate_properties(self):
mmif.add_document(doc1)
did = 'doc1'
# last view before serialization rounds
r0_vid = mmif.views.get_last().id
r0_vid = mmif.views.get_last_contentful_view().id
doc1.at_type = DocumentTypes.TextDocument
doc1.id = did
doc1.location = 'aScheme:///data/doc1.txt'
Expand Down Expand Up @@ -1165,7 +1165,7 @@ def test_document_adding_duplicate_properties(self):
self.assertEqual(2, len(doc1._props_pending))
mmif_roundtrip2 = Mmif(mmif_roundtrip1.serialize())
## but not serialized
self.assertEqual(0, len(list(mmif_roundtrip2.views.get_last().get_annotations(AnnotationTypes.Annotation))))
self.assertEqual(0, len(list(mmif_roundtrip2.views.get_last_contentful_view().get_annotations(AnnotationTypes.Annotation))))

# adding non-duplicate value should be serialized into a new Annotation object
# even when there is a duplicate key in a previous view
Expand Down Expand Up @@ -1211,7 +1211,7 @@ def test_document_added_properties_with_manual_capital_annotation(self):
doc1.add_property('author', 'me')
mmif_roundtrip = Mmif(mmif.serialize())
## should be only one Annotation object, from manual call of `new_annotation`
self.assertEqual(1, len(list(mmif_roundtrip.views.get_last().get_annotations(AnnotationTypes.Annotation))))
self.assertEqual(1, len(list(mmif_roundtrip.views.get_last_contentful_view().get_annotations(AnnotationTypes.Annotation))))

# test with duplicate property added by a downstream app
mmif_roundtrip = Mmif(mmif.serialize())
Expand All @@ -1223,9 +1223,9 @@ def test_document_added_properties_with_manual_capital_annotation(self):
# author=me is already in the input MMIF
doc1_prime.add_property('author', 'me')
mmif_roundtrip2 = Mmif(mmif_roundtrip.serialize())
self.assertEqual(1, len(list(mmif.views.get_last().get_annotations(AnnotationTypes.Annotation))))
self.assertEqual(1, len(list(mmif.views.get_last_contentful_view().get_annotations(AnnotationTypes.Annotation))))
# none should be added
self.assertEqual(0, len(list(mmif_roundtrip2.views.get_last().get_annotations(AnnotationTypes.Annotation))))
self.assertEqual(0, len(list(mmif_roundtrip2.views.get_last_contentful_view().get_annotations(AnnotationTypes.Annotation))))

# test with same key, different value
mmif_roundtrip = Mmif(mmif.serialize())
Expand All @@ -1238,13 +1238,13 @@ def test_document_added_properties_with_manual_capital_annotation(self):
doc1_prime.add_property('author', 'you')
mmif_roundtrip2 = Mmif(mmif_roundtrip.serialize())
# the Annotation in the previous view should be preserved
self.assertEqual(1, len(list(mmif.views.get_last().get_annotations(AnnotationTypes.Annotation))))
self.assertEqual(1, len(list(mmif.views.get_last_contentful_view().get_annotations(AnnotationTypes.Annotation))))
# but a new one should be added
self.assertEqual(1, len(list(mmif_roundtrip2.views.get_last().get_annotations(AnnotationTypes.Annotation))))
self.assertEqual(1, len(list(mmif_roundtrip2.views.get_last_contentful_view().get_annotations(AnnotationTypes.Annotation))))

self.assertEqual('me', list(mmif.views.get_last().get_annotations(AnnotationTypes.Annotation))[0].get_property(
self.assertEqual('me', list(mmif.views.get_last_contentful_view().get_annotations(AnnotationTypes.Annotation))[0].get_property(
'author'))
self.assertEqual('you', list(mmif_roundtrip2.views.get_last().get_annotations(AnnotationTypes.Annotation))[
self.assertEqual('you', list(mmif_roundtrip2.views.get_last_contentful_view().get_annotations(AnnotationTypes.Annotation))[
0].get_property('author'))

def test_capital_annotation_generation_viewfinder(self):
Expand Down

0 comments on commit 2543352

Please sign in to comment.