Skip to content

Commit

Permalink
Merge pull request #605 from opencobra/fix-json
Browse files Browse the repository at this point in the history
Fix json output error reporting
  • Loading branch information
Midnighter committed Jan 30, 2019
2 parents fd54034 + 44ded58 commit 915541c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
2 changes: 2 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ History

Next Release
------------
* Improve logging output when there is a problem with serializing a result to
JSON.
* Fix some test cases that got broken by cobrapy's new boundary identification.

0.9.2 (2019-01-28)
Expand Down
4 changes: 3 additions & 1 deletion memote/suite/tests/test_thermodynamics.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ def test_find_candidate_irreversible_reactions(model):
rev_index, incomplete, problematic, unbalanced = \
thermo.find_thermodynamic_reversibility_index(met_rxns)
ann["data"] = (
[(r.id, i) for r, i in rev_index],
# The reversibility index can be infinite so we convert it to a JSON
# compatible string.
[(r.id, str(i)) for r, i in rev_index],
get_ids(incomplete),
get_ids(problematic),
get_ids(unbalanced)
Expand Down
14 changes: 10 additions & 4 deletions memote/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from textwrap import TextWrapper

from future.utils import raise_with_traceback
from numpy import isfinite
from numpydoc.docscrape import NumpyDocString
from six import string_types
from depinfo import print_dependencies
Expand Down Expand Up @@ -186,13 +187,15 @@ def log_json_incompatible_types(obj):
while len(keys_to_explore) > 0:
key = keys_to_explore.pop()
if not isinstance(key, str):
LOGGER.debug(type(key))
LOGGER.info(type(key))
value = obj[key]
if isinstance(value, dict):
LOGGER.debug("%s:", key)
LOGGER.info("%s:", key)
log_json_incompatible_types(value)
elif not isinstance(value, JSON_TYPES):
LOGGER.debug("%s: %s", key, type(value))
LOGGER.info("%s: %s", key, type(value))
elif isinstance(value, (int, float)) and not isfinite(value):
LOGGER.info("%s: %f", key, value)


def extended_summary(func):
Expand Down Expand Up @@ -240,7 +243,10 @@ def jsonify(obj, pretty=False):
separators=(",", ":"), ensure_ascii=False)
try:
return json.dumps(obj, **params)
except TypeError as error:
except (TypeError, ValueError) as error:
LOGGER.critical(
"The memote result structure is incompatible with the JSON "
"standard.")
log_json_incompatible_types(obj)
raise_with_traceback(error)

Expand Down

0 comments on commit 915541c

Please sign in to comment.