Skip to content

Commit

Permalink
Force older and newer yaml.dump to give the same output
Browse files Browse the repository at this point in the history
* pyyaml changed its default flow semantics in yaml/pyyaml#256
* We must override the default with the magic tribool value `None`
* Fixes 13541
  • Loading branch information
ggould-tri committed Jun 11, 2020
1 parent e55b6dd commit 8b72ed9
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions examples/acrobot/dev/acrobot_io.py
Expand Up @@ -2,6 +2,12 @@

import numpy as np

# This is a magic tribool value described at
# https://github.com/yaml/pyyaml/pull/256
# and must be set this way for post- and pre- pyyaml#256 yaml dumpers to give
# the same output.
_FLOW_STYLE = None


def load_scenario(*, filename=None, data=None, scenario_name=None):
"""Given a scenario `filename` xor `data`, and optionally `scenario_name`,
Expand Down Expand Up @@ -42,7 +48,9 @@ def save_scenario(*, scenario, scenario_name):
]
else:
scrubbed[key] = [float(x) for x in scenario[key]]
return yaml.dump({scenario_name: scrubbed}, Dumper=yaml.CDumper)
return yaml.dump({scenario_name: scrubbed},
Dumper=yaml.CDumper,
default_flow_style=_FLOW_STYLE)


def load_output(*, filename=None, data=None):
Expand All @@ -68,4 +76,5 @@ def load_output(*, filename=None, data=None):
def save_output(*, x_tape):
"""Given an acrobot output `x_tape`, returns a yaml-formatter str for it.
"""
return yaml.dump({"x_tape": x_tape.tolist()})
return yaml.dump({"x_tape": x_tape.tolist()},
default_flow_style=_FLOW_STYLE)

0 comments on commit 8b72ed9

Please sign in to comment.