Skip to content

Commit

Permalink
[ctml2yaml] Small code formatting improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanwweber committed Sep 8, 2019
1 parent ac1d37b commit bde88fe
Showing 1 changed file with 22 additions and 24 deletions.
46 changes: 22 additions & 24 deletions interfaces/cython/cantera/ctml2yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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"):
Expand All @@ -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
Expand Down Expand Up @@ -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",
Expand All @@ -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__":
Expand Down

0 comments on commit bde88fe

Please sign in to comment.