Skip to content

Commit

Permalink
Simplify getter/setter code and add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
bgyori committed Nov 12, 2019
1 parent 66d8da7 commit fade96b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
3 changes: 2 additions & 1 deletion indra/statements/evidence.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ def __init__(self, source_api=None, source_id=None, pmid=None, text=None,
self.stmt_tag = None

def __setstate__(self, state):
state = state[1]
# With a slots-based object, state is a tuple, otherwise it's a dict
state = state[1] if isinstance(state, tuple) else state
for slot in self.__slots__:
setattr(self, slot, state.get(slot, None))

Expand Down
30 changes: 19 additions & 11 deletions indra/statements/statements.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,16 +267,11 @@ def __init__(self, evidence=None, supports=None, supported_by=None):

@property
def evidence(self):
evjsgz = gzip.decompress(self._evidence)
evjs = json.loads(evjsgz.decode('utf-8'))
evs = [Evidence._from_json(e) for e in evjs]
# Decompress, decode, and then deserialize each Evidence from JSON
evs = [Evidence._from_json(e) for e in
json.loads(gzip.decompress(self._evidence).decode('utf-8'))]
return evs

def add_evidence(self, ev):
evs = self.evidence
evs.append(ev)
self.evidence = evs

@evidence.setter
def evidence(self, evidence):
if evidence is None:
Expand All @@ -288,9 +283,22 @@ def evidence(self, evidence):
else:
raise ValueError('evidence must be an Evidence object, a list '
'(of Evidence objects), or None.')
evjs = json.dumps([e.to_json() for e in evs])
evjsgz = gzip.compress(evjs.encode('utf-8'))
self._evidence = evjsgz
self._evidence = \
gzip.compress(json.dumps([e.to_json()
for e in evs]).encode('utf-8'))

def add_evidence(self, ev):
"""Extend the Statement's evidence list with a new Evidence.
Parameters
----------
ev : indra.statements.Evidence
An Evidence object to be added to the Statement's list of
evidences.
"""
evs = self.evidence
evs.append(ev)
self.evidence = evs

def matches_key(self):
raise NotImplementedError("Method must be implemented in child class.")
Expand Down

0 comments on commit fade96b

Please sign in to comment.