diff --git a/interfaces/cython/cantera/ctml2yaml.py b/interfaces/cython/cantera/ctml2yaml.py index 94b1d30eb68..316b367725c 100644 --- a/interfaces/cython/cantera/ctml2yaml.py +++ b/interfaces/cython/cantera/ctml2yaml.py @@ -9,6 +9,8 @@ import xml.etree.ElementTree as etree from email.utils import formatdate +from typing import Any + try: import ruamel_yaml as yaml except ImportError: @@ -31,25 +33,23 @@ def FlowList(*args, **kwargs): # Improved float formatting requires Numpy >= 1.14 -if hasattr(np, "format_float_positional"): - - def float2string(data): - if data == 0: - return "0.0" - elif 0.01 <= abs(data) < 10000: - return np.format_float_positional(data, trim="0") - else: - return np.format_float_scientific(data, trim="0") +HAS_FMT_FLT_POS = hasattr(np, "format_float_positional") -else: - - def float2string(data): +def float2string(data): + if not HAS_FMT_FLT_POS: return repr(data) + if data == 0: + return "0.0" + elif 0.01 <= abs(data) < 10000: + return np.format_float_positional(data, trim="0") + else: + return np.format_float_scientific(data, trim="0") + def represent_float(self, data): - # type: (Any) -> Any + # type: (Any, Any) -> Any if data != data: value = ".nan" elif data == self.inf_value: @@ -648,6 +648,7 @@ def convert(inpfile, outfile): for phase_node in ctml_tree.iterfind("phase"): this_phase = Phase(phase_node) phases.append(this_phase) + reaction_filter = phase_node.find("./reactionArray/include") if reaction_filter is not None: if reaction_filter.get("min") != reaction_filter.get("max"): @@ -662,7 +663,6 @@ def convert(inpfile, outfile): # Species species_data = [] for species in ctml_tree.find("speciesData").iterfind("species"): - species_data.append(Species(species)) # Reactions @@ -694,12 +694,10 @@ def convert(inpfile, outfile): output_species = BlockMap({"species": species_data}) output_species.yaml_set_comment_before_after_key("species", before="\n") - yaml_obj = yaml.YAML() - yaml_obj.register_class(Phase) - yaml_obj.register_class(Species) - yaml_obj.register_class(SpeciesThermo) - yaml_obj.register_class(SpeciesTransport) - yaml_obj.register_class(Reaction) + emitter = yaml.YAML() + for cl in [Phase, Species, SpeciesThermo, SpeciesTransport, Reaction]: + emitter.register_class(cl) + metadata = BlockMap( { "generator": "ctml2yaml", @@ -710,10 +708,10 @@ def convert(inpfile, outfile): if inpfile is not None: metadata["input-files"] = FlowList([str(inpfile)]) with Path(outfile).open("w") as output_file: - yaml_obj.dump(metadata, output_file) - yaml_obj.dump(output_phases, output_file) - yaml_obj.dump(output_species, output_file) - yaml_obj.dump(output_reactions, output_file) + emitter.dump(metadata, output_file) + emitter.dump(output_phases, output_file) + emitter.dump(output_species, output_file) + emitter.dump(output_reactions, output_file) if __name__ == "__main__":