diff --git a/interfaces/cython/cantera/test/test_thermo.py b/interfaces/cython/cantera/test/test_thermo.py index ca7ee68f4..766c677ba 100644 --- a/interfaces/cython/cantera/test/test_thermo.py +++ b/interfaces/cython/cantera/test/test_thermo.py @@ -1,4 +1,5 @@ from os.path import join as pjoin +import pathlib import os import numpy as np import gc @@ -1048,6 +1049,14 @@ class TestSpecies(utilities.CanteraTest): self.assertEqual(species[1].composition, {'H': 1, 'O': 2}) self.assertNear(species[0].thermo.h(300), 100) + def test_listFromYaml_section(self): + species = ct.Species.listFromYaml( + pathlib.Path('../data/ideal-gas.yaml').read_text(), + 'species') + + self.assertEqual(species[0].name, 'O2') + self.assertEqual(species[1].composition, {'N': 1, 'O': 1}) + def test_listFromXml(self): p = os.path.dirname(__file__) with open(pjoin(p, '..', 'data', 'h2o2.xml')) as f: diff --git a/interfaces/cython/cantera/thermo.pyx b/interfaces/cython/cantera/thermo.pyx index cbbb96d8c..c36c65748 100644 --- a/interfaces/cython/cantera/thermo.pyx +++ b/interfaces/cython/cantera/thermo.pyx @@ -44,7 +44,11 @@ cdef class Species: containing the species defined in the GRI 3.0 mechanism:: S = ct.Species.listFromFile('gri30.yaml') - S = ct.Species.listFromYaml(open('path/to/gri30.yaml').read()) + + import pathlib + S = ct.Species.listFromYaml( + pathlib.Path('path/to/gri30.yaml').read_text(), + section='species') """ def __cinit__(self, *args, init=True, **kwargs): @@ -164,13 +168,18 @@ cdef class Species: return species @staticmethod - def listFromYaml(text): + def listFromYaml(text, section=None): """ Create a list of Species objects from all the species defined in a YAML - string. + string. If ``text`` is a YAML mapping, the ``section`` name of the list + to be read must be specified. If ``text`` is a YAML list, no ``section`` + name should be supplied. """ root = AnyMapFromYamlString(stringify(text)) - cxx_species = CxxGetSpecies(root[stringify("items")]) + + # ``items`` is the pseudo-key used to access a list when it is at the + # top level of a YAML document + cxx_species = CxxGetSpecies(root[stringify(section or "items")]) species = [] for a in cxx_species: b = Species(init=False)