diff --git a/ctwrap/handler.py b/ctwrap/handler.py index b4e188e..4ebb266 100644 --- a/ctwrap/handler.py +++ b/ctwrap/handler.py @@ -60,11 +60,7 @@ from multiprocessing import queues as mpq from multiprocessing import synchronize as mps import queue # imported for using queue.Empty exception - -try: - import ruamel_yaml as yaml -except ImportError: - from ruamel import yaml +from ruamel.yaml import YAML # ctwrap specific import from .parser import _parse, _write, Parser @@ -169,7 +165,8 @@ def from_yaml(cls, yaml_file: str, "".format(yaml_file)) with open(full_name) as stream: - content = yaml.load(stream, Loader=yaml.SafeLoader) + yaml = YAML(typ="safe") + content = yaml.load(stream) output = content.get('output', {}) diff --git a/ctwrap/output.py b/ctwrap/output.py index 459ccdf..4b88ab1 100644 --- a/ctwrap/output.py +++ b/ctwrap/output.py @@ -14,15 +14,14 @@ from typing import Dict, List, Any, Optional, Union -import pkg_resources +import importlib # avoid explicit dependence on cantera -try: - pkg_resources.get_distribution('cantera') -except pkg_resources.DistributionNotFound: +ct_spec = importlib.util.find_spec("cantera") +if ct_spec is None: ct = ImportError('Method requires a working cantera installation.') else: - import cantera as ct + ct = importlib.import_module("cantera") class Output: @@ -236,7 +235,7 @@ def save(self, data, entry, variation=None, mode=None, errored=False): df = pd.read_csv(fname) else: df = pd.DataFrame(columns=row.keys()) - df = df.append(row, ignore_index=True) + df = pd.concat([df, row.to_frame().T], ignore_index=True) df.to_csv(fname, index=False) def dir(self): diff --git a/ctwrap/parser.py b/ctwrap/parser.py index 993fda5..259fe75 100644 --- a/ctwrap/parser.py +++ b/ctwrap/parser.py @@ -65,14 +65,10 @@ from typing import Optional, Dict, Any, Tuple, KeysView, Generator, Union from copy import deepcopy from pint import UnitRegistry +from ruamel.yaml import YAML import warnings import re -try: - import ruamel_yaml as yaml -except ImportError: - from ruamel import yaml - __all__ = ['Parser'] @@ -253,12 +249,13 @@ def from_yaml( elif path is not None: fname = Path(path) / fname + yaml = YAML(typ="safe") try: - _ = fname.is_file() # will raise error + _ = fname.is_file() # will raise error with open(fname) as stream: - out = yaml.load(stream, Loader=yaml.SafeLoader) + out = yaml.load(stream) except OSError: - out = yaml.load(yml, Loader=yaml.SafeLoader) + out = yaml.load(yml) if keys is None: return cls(out) @@ -267,6 +264,7 @@ def from_yaml( def to_yaml(self): """Convert Parser content to YAML string""" + yaml = YAML(typ="safe") return yaml.dump(self.raw, Dumper=yaml.SafeDumper) def get(self, key, default=None): diff --git a/environment.yml b/environment.yml index c67fe07..c659baa 100644 --- a/environment.yml +++ b/environment.yml @@ -9,8 +9,8 @@ dependencies: - numpy - h5py - pint - - ruamel.yaml - - pandas + - ruamel.yaml>=0.17.0 + - pandas>=1.4 - cantera - setuptools - pytest diff --git a/tests/test_output.py b/tests/test_output.py index a788c7a..716a5ed 100644 --- a/tests/test_output.py +++ b/tests/test_output.py @@ -7,11 +7,7 @@ import unittest from pathlib import Path import h5py - -try: - import ruamel_yaml as yaml -except ImportError: - from ruamel import yaml +from ruamel.yaml import YAML import warnings # add exception as pywintypes imports a deprecated module @@ -21,15 +17,14 @@ # pylint: disable=no-member import ctwrap.output as cwo -import pkg_resources +import importlib # avoid explicit dependence on cantera -try: - pkg_resources.get_distribution('cantera') -except pkg_resources.DistributionNotFound: +ct_spec = importlib.util.find_spec("cantera") +if ct_spec is None: ct = ImportError('Method requires a working cantera installation.') else: - import cantera as ct + ct = importlib.import_module("cantera") PWD = Path(__file__).parents[0] @@ -46,7 +41,8 @@ class TestOutput(unittest.TestCase): @classmethod def setUpClass(cls): with open(EXAMPLES / cls._yaml) as stream: - cls._config = yaml.load(stream, Loader=yaml.SafeLoader) + yaml = YAML(typ='safe') + cls._config = yaml.load(stream) out = cls._config.get('output') if out: diff --git a/tests/test_parser.py b/tests/test_parser.py index 09c8d4b..a476a5f 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -5,12 +5,8 @@ import unittest from pathlib import Path -import pint.quantity as pq - -try: - import ruamel_yaml as yaml -except ImportError: - from ruamel import yaml +import pint as pq +from ruamel.yaml import YAML import warnings # add exception as pywintypes imports a deprecated module @@ -202,7 +198,8 @@ class TestYAML(unittest.TestCase): def test_minimal(self): with open(EXAMPLES / 'minimal.yaml') as stream: - defaults = yaml.load(stream, Loader=yaml.SafeLoader) + yaml = YAML(typ='safe') + defaults = yaml.load(stream) p = cw.Parser.from_yaml('minimal.yaml', path=EXAMPLES) self.assertEqual(len(p), len(defaults)) self.assertEqual(p.keys(), defaults.keys()) @@ -216,7 +213,8 @@ def test_minimal(self): def test_ignition(self): with open(EXAMPLES / 'ignition.yaml') as stream: - yml = yaml.load(stream, Loader=yaml.SafeLoader) + yaml = YAML(typ='safe') + yml = yaml.load(stream) initial = cw.Parser(yml['defaults']['initial']) self.assertIsInstance(initial.T, pq.Quantity) self.assertIsInstance(initial['fuel'], str) diff --git a/tests/test_strategy.py b/tests/test_strategy.py index 763c8b7..6634a24 100644 --- a/tests/test_strategy.py +++ b/tests/test_strategy.py @@ -5,12 +5,6 @@ import unittest from pathlib import Path -import pint.quantity as pq - -try: - import ruamel_yaml as yaml -except ImportError: - from ruamel import yaml import warnings # add exception as pywintypes imports a deprecated module diff --git a/tests/test_wrapper.py b/tests/test_wrapper.py index bfe25a7..22c6839 100644 --- a/tests/test_wrapper.py +++ b/tests/test_wrapper.py @@ -6,14 +6,9 @@ import unittest from pathlib import Path import subprocess -import pint.quantity as pq import importlib import h5py - -try: - import ruamel_yaml as yaml -except ImportError: - from ruamel import yaml +from ruamel.yaml import YAML import warnings # add exception as pywintypes imports a deprecated module @@ -54,7 +49,8 @@ class TestWrap(unittest.TestCase): def setUp(self): with open(EXAMPLES / self._yaml) as stream: - self.config = yaml.load(stream, Loader=yaml.SafeLoader) + yaml = YAML(typ='safe') + self.config = yaml.load(stream) self.sim = cw.Simulation.from_module(self._module) self.sh = cw.SimulationHandler.from_yaml(self._yaml, strategy=self._strategy, database=EXAMPLES) @@ -247,7 +243,8 @@ class TestInvalid(TestWrap): def setUp(self): with open(EXAMPLES / self._yaml) as stream: - self.config = yaml.load(stream, Loader=yaml.SafeLoader) + yaml = YAML(typ='safe') + self.config = yaml.load(stream) self.sim = cw.Simulation.from_module(self._module) self.sh = cw.SimulationHandler.from_dict(self._dict)