Skip to content

Commit

Permalink
refactor: upgrade tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Midnighter committed Sep 6, 2018
1 parent 318d0fa commit 37d33c8
Show file tree
Hide file tree
Showing 28 changed files with 2,045 additions and 1,940 deletions.
5 changes: 3 additions & 2 deletions cobra/io/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

from __future__ import absolute_import

from cobra.io.schemata import MODEL_SCHEMA
from cobra.io.dict import (model_from_dict, model_to_dict)
from cobra.io.json import (
to_json, from_json, load_json_model, save_json_model)
to_json, from_json, load_json_model, save_json_model, JSON_SPEC)
from cobra.io.yaml import (
to_yaml, from_yaml, load_yaml_model, save_yaml_model)
to_yaml, from_yaml, load_yaml_model, save_yaml_model, YAML_SPEC)
from cobra.io.sbml3 import read_sbml_model, write_sbml_model
from cobra.io.sbml import read_legacy_sbml
from cobra.io.sbml import write_cobra_model_to_sbml_file as \
Expand Down
63 changes: 35 additions & 28 deletions cobra/io/json.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
# -*- coding: utf-8 -*-

from __future__ import absolute_import
from importlib_resources import open_text

try:
import simplejson as json
except ImportError:
import json
import json

from six import string_types

import cobra.io.schemata
from cobra.io.dict import model_to_dict, model_from_dict

JSON_SPEC = "1"

JSON_SPEC = "1"

def to_json(model, sort=False, **kwargs):
JSON_FORMAT = {
True: {
"indent": 2,
"separators": (", ", ": "),
"sort_keys": True,
"allow_nan": False
},
False: {
"separators": (",", ":"),
"sort_keys": False,
"allow_nan": False
}
}


def to_json(model, sort=False, pretty=False, **kwargs):
"""
Return the model as a JSON document.
Expand All @@ -27,6 +40,10 @@ def to_json(model, sort=False, **kwargs):
sort : bool, optional
Whether to sort the metabolites, reactions, and genes or maintain the
order defined in the model.
pretty : bool, optional
Whether to format the JSON more compactly (default) or in a more
verbose but easier to read fashion. Can be partially overwritten by the
``kwargs``.
Returns
-------
Expand All @@ -37,10 +54,13 @@ def to_json(model, sort=False, **kwargs):
--------
save_json_model : Write directly to a file.
json.dumps : Base function.
"""
obj = model_to_dict(model, sort=sort)
obj[u"version"] = JSON_SPEC
return json.dumps(obj, allow_nan=False, **kwargs)
options = JSON_FORMAT[pretty]
options.update(kwargs)
return json.dumps(obj, **options)


def from_json(document):
Expand All @@ -60,6 +80,7 @@ def from_json(document):
See Also
--------
load_json_model : Load directly from a file.
"""
return model_from_dict(json.loads(document))

Expand Down Expand Up @@ -89,25 +110,18 @@ def save_json_model(model, filename, sort=False, pretty=False, **kwargs):
--------
to_json : Return a string representation.
json.dump : Base function.
"""
obj = model_to_dict(model, sort=sort)
obj[u"version"] = JSON_SPEC

if pretty:
dump_opts = {
"indent": 4, "separators": (",", ": "), "sort_keys": True,
"allow_nan": False}
else:
dump_opts = {
"indent": 0, "separators": (",", ":"), "sort_keys": False,
"allow_nan": False}
dump_opts.update(**kwargs)
options = JSON_FORMAT[pretty]
options.update(**kwargs)

if isinstance(filename, string_types):
with open(filename, "w") as file_handle:
json.dump(obj, file_handle, **dump_opts)
json.dump(obj, file_handle, **options)
else:
json.dump(obj, filename, **dump_opts)
json.dump(obj, filename, **options)


def load_json_model(filename):
Expand All @@ -128,17 +142,10 @@ def load_json_model(filename):
See Also
--------
from_json : Load from a string.
"""
if isinstance(filename, string_types):
with open(filename, "r") as file_handle:
return model_from_dict(json.load(file_handle))
else:
return model_from_dict(json.load(filename))


def init_json_schema():
""" Import the JSON schema for schema validation. """
with open_text("cobra.io.schemata", "json_schema.json",
encoding="utf-8") as file_handle:
json_schema = json.load(file_handle)
return json_schema
11 changes: 11 additions & 0 deletions cobra/io/schemata/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
# -*- coding: utf-8 -*-

"""Provide JSON schemata that can be used for validation."""

from __future__ import absolute_import

import json

from importlib_resources import open_text


with open_text("cobra.io.schemata", "model_schema.json",
encoding="utf-8") as file_handle:
MODEL_SCHEMA = json.load(file_handle)
File renamed without changes.
1 change: 1 addition & 0 deletions cobra/io/yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@


class MyYAML(YAML):

def dump(self, data, stream=None, **kwargs):
inefficient = False
if stream is None:
Expand Down
Binary file modified cobra/test/data/iJO1366.pickle
Binary file not shown.
Loading

0 comments on commit 37d33c8

Please sign in to comment.