Fix mmif.serialize
pythonic behavior
#304
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR aims to fix #295 which pointed out that dict-like (and list-like) classes in
mmif.serialize
package need to hold python'sdict
(andlist
) properties.Overview of classes in
mmif.serialize
MmifObject
: a base class for MMIF objects that are ''dict-like''DataList
: a base class for MMIF data fields that are ''list-like''DataDict
: a base class for MMIF data fields that are ''dict-like''Then, the following classes are defined and categorized into either ''dict-like''
or ''list-like'' child classes:
dict-like:
-
Mmif
: a class for MMIF objects-
MmifMetaData
: a class for MMIF metadata-
View
: a class for MMIF view-
ViewMetaData
: a class for MMIF view metadata-
ErrorDict
: a class for a specific view that contains error-
ContainsDict
: a class forView
's 'contains' field-
Annotation
&Document
: a class for MMIF annotation and document-
AnnotationProperties
&DocumentProperties
: a class for MMIF annotation properties-
Text
: a class forDocument
's text fieldlist-like:
-
DocumentsList
: a class for a list ofDocument
objects-
ViewsList
: a class for a list ofView
objects-
AnnotationsList
: a class for a list ofAnnotation
objectsBy doing so, any field of a ''dict-like'' class should be accessed if and
only if by either bracket
[key]
or.get(key)
. For a ''list-like'' class, theelements are ordered and accessed by index, for example,
[idx]
.Note
As I discussed with @keighrim, we intentionally leave a "backdoor" method
get_item
for the abstract classDataList
in order for achieving O(1) access to data (for example, aView
or anAnnotation
) by itslong_id
.What's new
Any subclass of
MmifObject
(i.e. dict-like class) has only two ways to access data via keys:dict_like_obj[key]
dict_like_obj.get(key)
Any subclass of
DataList
(i.e. list-like) has below changes:list_like_obj[index]
list_like_obj[index] = new_value raises TypeError
.get
to.get_item