Prevent long lines getting wrapped in ruamel.yaml
Asked Answered
A

1

19

I use the load_yaml_guess_indent(f, preserve_quotes=True) to read a YAML file, then modify it and write it back. I noticed long lines are getting wrapped when they are written back. (A line break is inserted after 80-85 characters.) Is there a parameter I can pass to stop this from happening?

Atheist answered 11/2, 2017 at 0:13 Comment(0)
C
21

These are the parameters you can hand in to round_trip_dump():

def round_trip_dump(data, stream=None, Dumper=RoundTripDumper,
                default_style=None, default_flow_style=None,
                canonical=None, indent=None, width=None,
                allow_unicode=None, line_break=None,
                encoding=enc, explicit_start=None, explicit_end=None,
                version=None, tags=None, block_seq_indent=None,
                top_level_colon_align=None, prefix_colon=None):

The one you are looking for is width

If you are using the new (ruamel.yaml >= 0.15 ) API, most of these parameters directly translate to attributes on the YAML instance you create, so in that case you would do:

yaml = ruamel.yaml.YAML()
yaml.width = 4096  # or some other big enough value to prevent line-wrap
yaml.dump(data, stream=your_stream)
Claptrap answered 11/2, 2017 at 7:33 Comment(3)
@KatrinaBrock DoneClaptrap
@JeffHykin That is because the line width (an integer) is compared to the value provided, and that value is not "cast" to an an integer. I.e. in Python 80 < float("Infinity") gives True but int(float("Infinity")) throws an overflow error.Claptrap
Is it possible to preserve existing line wrapping regardless of width? Fixed line width causes either wrapping or unwrapping if existing line wrappings are inconsistent, and I'd like to reduce whitespace noiseKana

© 2022 - 2024 — McMap. All rights reserved.