Skip to content

Commit

Permalink
[CI] Replace deprecated yaml.safe_load
Browse files Browse the repository at this point in the history
  • Loading branch information
ischoegl committed May 31, 2021
1 parent 01bf417 commit 6abc56c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 27 deletions.
21 changes: 5 additions & 16 deletions interfaces/cython/cantera/test/test_composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@
from collections import OrderedDict
import warnings

try:
import ruamel_yaml as yaml
except ImportError:
from ruamel import yaml


import cantera as ct
from cantera.composite import _h5py, _pandas
from . import utilities
Expand All @@ -23,8 +17,7 @@ class TestModels(utilities.CanteraTest):
def setUpClass(cls):
utilities.CanteraTest.setUpClass()
cls.yml_file = pjoin(cls.test_data_dir, "thermo-models.yaml")
with open(cls.yml_file, 'rt', encoding="utf-8") as stream:
cls.yml = yaml.safe_load(stream)
cls.yml = utilities.load_yaml(cls.yml_file)

def test_load_thermo_models(self):
for ph in self.yml['phases']:
Expand Down Expand Up @@ -478,8 +471,7 @@ def test_yaml_simple(self):
gas.equilibrate('HP')
gas.TP = 1500, ct.one_atm
gas.write_yaml('h2o2-generated.yaml')
with open('h2o2-generated.yaml', 'r') as infile:
generated = yaml.safe_load(infile)
generated = utilities.load_yaml('h2o2-generated.yaml')
for key in ('generator', 'date', 'phases', 'species', 'reactions'):
self.assertIn(key, generated)
self.assertEqual(generated['phases'][0]['transport'], 'mixture-averaged')
Expand All @@ -503,10 +495,8 @@ def test_yaml_outunits(self):
gas.TP = 1500, ct.one_atm
units = {'length': 'cm', 'quantity': 'mol', 'energy': 'cal'}
gas.write_yaml('h2o2-generated.yaml', units=units)
with open('h2o2-generated.yaml') as infile:
generated = yaml.safe_load(infile)
with open(pjoin(self.cantera_data, "h2o2.yaml")) as infile:
original = yaml.safe_load(infile)
generated = utilities.load_yaml('h2o2-generated.yaml')
original = utilities.load_yaml(pjoin(self.cantera_data, "h2o2.yaml"))
self.assertEqual(generated['units'], units)

for r1, r2 in zip(original['reactions'], generated['reactions']):
Expand All @@ -529,8 +519,7 @@ def test_yaml_surface(self):
surf.coverages = np.ones(surf.n_species)
surf.write_yaml('ptcombust-generated.yaml')

with open('ptcombust-generated.yaml') as infile:
generated = yaml.safe_load(infile)
generated = utilities.load_yaml('ptcombust-generated.yaml')
for key in ('phases', 'species', 'gas-reactions', 'Pt_surf-reactions'):
self.assertIn(key, generated)
self.assertEqual(len(generated['gas-reactions']), gas.n_reactions)
Expand Down
15 changes: 4 additions & 11 deletions interfaces/cython/cantera/test/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@
import cantera as ct
from cantera import ck2cti, ck2yaml, cti2yaml, ctml2yaml

try:
import ruamel_yaml as yaml
except ImportError:
from ruamel import yaml


class converterTestCommon:
def convert(self, inputFile, thermo=None, transport=None,
Expand Down Expand Up @@ -479,25 +474,23 @@ def test_extra(self):
extra='extra.yaml')

output = pjoin(self.test_work_dir, 'gri30_extra' + self.ext)
with open(output, 'rt', encoding="utf-8") as stream:
yml = yaml.safe_load(stream)
yml = utilities.load_yaml(output)

desc = yml['description'].split('\n')[-1]
self.assertEqual(desc, 'This is an alternative description.')
for key in ['foo', 'bar']:
self.assertIn(key, yml.keys())
# This test tests it can convert the SRI parameters when D or E equal to 0

def test_sri_zero(self):
self.convert('sri_convert_test.txt')
output = pjoin(self.test_work_dir, 'sri_convert_test' + self.ext)
with open(output, 'r') as f:
mech = yaml.safe_load(f)
mech = utilities.load_yaml(output)
D = mech['reactions'][0]['SRI']['D']
E = mech['reactions'][0]['SRI']['E']
self.assertEqual(D, 0)
self.assertEqual(E, 0)

def test_duplicate_reactions(self):
# Running a test this way instead of using the convertMech function
# tests the behavior of the ck2yaml.main function and the mechanism
Expand Down
14 changes: 14 additions & 0 deletions interfaces/cython/cantera/test/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,24 @@
import tempfile
import unittest
import errno

try:
import ruamel_yaml as yaml
except ImportError:
from ruamel import yaml

import cantera

slow_test = unittest.skipIf(os.environ.get("CT_SKIP_SLOW", "0") == "1", "slow test")


def load_yaml(yml_file, typ='safe'):
# service function for loading of YAML from file. default loader is safe
yaml_ = yaml.YAML(typ=typ)
with open(yml_file, 'rt', encoding="utf-8") as stream:
return yaml_.load(stream)


class CanteraTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
Expand Down

0 comments on commit 6abc56c

Please sign in to comment.