@@ -44,35 +44,39 @@ class MmifObject(object):
44
44
45
45
1. _unnamed_attributes:
46
46
Only can be either None or an empty dictionary. If it's set to None,
47
- it means the class won't take any ``Additional Attributes`` in the JSON
48
- schema sense. If it's a dict, users can throw any k-v pairs to the
49
- class, EXCEPT for the reserved two key names.
47
+ it means the class won't take any ``Additional Attributes`` in the
48
+ JSON schema sense. If it's an empty dict, users can throw any k-v
49
+ pairs to the class, as long as the key is not a "reserved" name,
50
+ and those additional attributes will be stored in this dict while
51
+ in memory.
50
52
2. _attribute_classes:
51
53
This is a dict from a key name to a specific python class to use for
52
54
deserialize the value. Note that a key name in this dict does NOT
53
55
have to be a *named* attribute, but is recommended to be one.
54
56
3. _required_attributes:
55
- This is a simple list of names of attributes that are required in the object.
56
- When serialize, an object will skip its *empty* (e.g. zero-length, or None)
57
- attributes unless they are in this list. Otherwise, the serialized JSON
58
- string would have empty representations (e.g. ``""``, ``[]``).
57
+ This is a simple list of names of attributes that are required in
58
+ the object. When serialize, an object will skip its *empty* (e.g.
59
+ zero-length, or None) attributes unless they are in this list.
60
+ Otherwise, the serialized JSON string would have empty
61
+ representations (e.g. ``""``, ``[]``).
59
62
4. _exclude_from_diff:
60
- This is a simple list of names of attributes that should be excluded from
61
- the diff calculation in ``__eq__``.
63
+ This is a simple list of names of attributes that should be excluded
64
+ from the diff calculation in ``__eq__``.
62
65
63
66
# TODO (krim @ 8/17/20): this dict is however, a duplicate with the type hints in the class definition.
64
- Maybe there is a better way to utilize type hints (e.g. getting them as a programmatically), but for now
65
- developers should be careful to add types to hints as well as to this dict.
67
+ Maybe there is a better way to utilize type hints (e.g. getting them
68
+ as a programmatically), but for now developers should be careful to
69
+ add types to hints as well as to this dict.
66
70
67
71
Also note that those special attributes MUST be set in the __init__()
68
72
before calling super method, otherwise deserialization will not work.
69
73
70
74
And also, a subclass that has one or more *named* attributes, it must
71
- set those attributes in the __init__() before calling super method. When
72
- serializing a MmifObject, all *empty* attributes will be ignored, so for
73
- optional named attributes, you must leave the values empty (len == 0), but
74
- NOT None. Any None-valued named attributes will cause issues with current
75
- implementation.
75
+ set those attributes in the __init__() before calling super method.
76
+ When serializing a MmifObject, all *empty* attributes will be ignored,
77
+ so for optional named attributes, you must leave the values empty
78
+ (len == 0), but NOT None. Any None-valued named attributes will cause
79
+ issues with current implementation.
76
80
77
81
:param mmif_obj: JSON string or `dict` to initialize an object.
78
82
If not given, an empty object will be initialized, sometimes with
@@ -272,8 +276,8 @@ def __len__(self) -> int:
272
276
"""
273
277
Returns number of attributes that are not *empty*.
274
278
"""
275
- return sum ([named in self and not self .is_empty (self [named ]) for named in self ._named_attributes ()]) \
276
- + (len (self ._unnamed_attributes ) if self ._unnamed_attributes else 0 )
279
+ return ( sum ([named in self and not self .is_empty (self [named ]) for named in self ._named_attributes ()])
280
+ + (len (self ._unnamed_attributes ) if self ._unnamed_attributes else 0 ) )
277
281
278
282
def __setitem__ (self , key , value ) -> None :
279
283
if key in self .reserved_names :
0 commit comments