Skip to content

Commit 37d33c8

Browse files
committed
refactor: upgrade tests
1 parent 318d0fa commit 37d33c8

28 files changed

+2045
-1940
lines changed

cobra/io/__init__.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
from __future__ import absolute_import
44

5+
from cobra.io.schemata import MODEL_SCHEMA
56
from cobra.io.dict import (model_from_dict, model_to_dict)
67
from cobra.io.json import (
7-
to_json, from_json, load_json_model, save_json_model)
8+
to_json, from_json, load_json_model, save_json_model, JSON_SPEC)
89
from cobra.io.yaml import (
9-
to_yaml, from_yaml, load_yaml_model, save_yaml_model)
10+
to_yaml, from_yaml, load_yaml_model, save_yaml_model, YAML_SPEC)
1011
from cobra.io.sbml3 import read_sbml_model, write_sbml_model
1112
from cobra.io.sbml import read_legacy_sbml
1213
from cobra.io.sbml import write_cobra_model_to_sbml_file as \

cobra/io/json.py

+35-28
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,33 @@
11
# -*- coding: utf-8 -*-
22

33
from __future__ import absolute_import
4-
from importlib_resources import open_text
54

6-
try:
7-
import simplejson as json
8-
except ImportError:
9-
import json
5+
import json
6+
107
from six import string_types
118

9+
import cobra.io.schemata
1210
from cobra.io.dict import model_to_dict, model_from_dict
1311

14-
JSON_SPEC = "1"
1512

13+
JSON_SPEC = "1"
1614

17-
def to_json(model, sort=False, **kwargs):
15+
JSON_FORMAT = {
16+
True: {
17+
"indent": 2,
18+
"separators": (", ", ": "),
19+
"sort_keys": True,
20+
"allow_nan": False
21+
},
22+
False: {
23+
"separators": (",", ":"),
24+
"sort_keys": False,
25+
"allow_nan": False
26+
}
27+
}
28+
29+
30+
def to_json(model, sort=False, pretty=False, **kwargs):
1831
"""
1932
Return the model as a JSON document.
2033
@@ -27,6 +40,10 @@ def to_json(model, sort=False, **kwargs):
2740
sort : bool, optional
2841
Whether to sort the metabolites, reactions, and genes or maintain the
2942
order defined in the model.
43+
pretty : bool, optional
44+
Whether to format the JSON more compactly (default) or in a more
45+
verbose but easier to read fashion. Can be partially overwritten by the
46+
``kwargs``.
3047
3148
Returns
3249
-------
@@ -37,10 +54,13 @@ def to_json(model, sort=False, **kwargs):
3754
--------
3855
save_json_model : Write directly to a file.
3956
json.dumps : Base function.
57+
4058
"""
4159
obj = model_to_dict(model, sort=sort)
4260
obj[u"version"] = JSON_SPEC
43-
return json.dumps(obj, allow_nan=False, **kwargs)
61+
options = JSON_FORMAT[pretty]
62+
options.update(kwargs)
63+
return json.dumps(obj, **options)
4464

4565

4666
def from_json(document):
@@ -60,6 +80,7 @@ def from_json(document):
6080
See Also
6181
--------
6282
load_json_model : Load directly from a file.
83+
6384
"""
6485
return model_from_dict(json.loads(document))
6586

@@ -89,25 +110,18 @@ def save_json_model(model, filename, sort=False, pretty=False, **kwargs):
89110
--------
90111
to_json : Return a string representation.
91112
json.dump : Base function.
113+
92114
"""
93115
obj = model_to_dict(model, sort=sort)
94116
obj[u"version"] = JSON_SPEC
95-
96-
if pretty:
97-
dump_opts = {
98-
"indent": 4, "separators": (",", ": "), "sort_keys": True,
99-
"allow_nan": False}
100-
else:
101-
dump_opts = {
102-
"indent": 0, "separators": (",", ":"), "sort_keys": False,
103-
"allow_nan": False}
104-
dump_opts.update(**kwargs)
117+
options = JSON_FORMAT[pretty]
118+
options.update(**kwargs)
105119

106120
if isinstance(filename, string_types):
107121
with open(filename, "w") as file_handle:
108-
json.dump(obj, file_handle, **dump_opts)
122+
json.dump(obj, file_handle, **options)
109123
else:
110-
json.dump(obj, filename, **dump_opts)
124+
json.dump(obj, filename, **options)
111125

112126

113127
def load_json_model(filename):
@@ -128,17 +142,10 @@ def load_json_model(filename):
128142
See Also
129143
--------
130144
from_json : Load from a string.
145+
131146
"""
132147
if isinstance(filename, string_types):
133148
with open(filename, "r") as file_handle:
134149
return model_from_dict(json.load(file_handle))
135150
else:
136151
return model_from_dict(json.load(filename))
137-
138-
139-
def init_json_schema():
140-
""" Import the JSON schema for schema validation. """
141-
with open_text("cobra.io.schemata", "json_schema.json",
142-
encoding="utf-8") as file_handle:
143-
json_schema = json.load(file_handle)
144-
return json_schema

cobra/io/schemata/__init__.py

+11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
11
# -*- coding: utf-8 -*-
22

3+
"""Provide JSON schemata that can be used for validation."""
4+
35
from __future__ import absolute_import
6+
7+
import json
8+
9+
from importlib_resources import open_text
10+
11+
12+
with open_text("cobra.io.schemata", "model_schema.json",
13+
encoding="utf-8") as file_handle:
14+
MODEL_SCHEMA = json.load(file_handle)
File renamed without changes.

cobra/io/yaml.py

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515

1616
class MyYAML(YAML):
17+
1718
def dump(self, data, stream=None, **kwargs):
1819
inefficient = False
1920
if stream is None:

cobra/test/data/iJO1366.pickle

3 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)