Skip to content

Commit

Permalink
Allow the YAML library to choose the flow style automatically. This r…
Browse files Browse the repository at this point in the history
…esults in more human-friendly outputs.
  • Loading branch information
pavel-kirienko committed Mar 7, 2021
1 parent ea268e9 commit e084088
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 10 deletions.
9 changes: 2 additions & 7 deletions yakut/param/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,7 @@ def _unittest_formatter() -> None:
obj = {
2345: {
"abc": {
"def": [
123,
456,
],
"def": [123, 456],
},
"ghi": 789,
}
Expand All @@ -101,9 +98,7 @@ def _unittest_formatter() -> None:
== """---
2345:
abc:
def:
- 123
- 456
def: [123, 456]
ghi: 789
"""
)
Expand Down
7 changes: 4 additions & 3 deletions yakut/yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@
class YAMLDumper:
"""
YAML generation facade.
Natively represents decimal.Decimal as floats in the output.
"""

def __init__(self, explicit_start: bool = False):
# We need to use the roundtrip representer to retain ordering of mappings, which is important for usability.
self._impl = ruamel.yaml.YAML(typ="rt")
# noinspection PyTypeHints
self._impl.explicit_start = explicit_start
self._impl.default_flow_style = False
self._impl.default_flow_style = None # Choose between block/inline automatically
self._impl.width = 2 ** 31 # Unlimited width

def dump(self, data: typing.Any, stream: typing.TextIO) -> None:
self._impl.dump(data, stream)
Expand All @@ -38,7 +40,6 @@ def dumps(self, data: typing.Any) -> str:
class YAMLLoader:
"""
YAML parsing facade.
Natively represents decimal.Decimal as floats in the output.
"""

def __init__(self) -> None:
Expand Down Expand Up @@ -85,7 +86,7 @@ def _unittest_yaml() -> None:
abc: -.inf
def:
- .nan
- qaz: 789.0
- {qaz: 789.0}
"""
)
assert YAMLLoader().load(ref) == {
Expand Down

0 comments on commit e084088

Please sign in to comment.