Pretty way out with pyamle

I have a python project where I would like to use YAML (pyYaml 3.11), especially because it is “beautiful” and convenient for users to edit in a text editor, if and when necessary. My problem, however, is that if I bring YAML into a python application (as I need) and edit the contents (as I need), then writing a new document is usually not as beautiful as what I am began.

The pyyaml ​​documentation is pretty poor - it doesn't even document the parameters of the dump function. I found http://dpinte.wordpress.com/2008/10/31/pyaml-dump-option/ . However, I still lack the necessary information. (I started looking at the source, but this does not seem the most attractive. If I do not get a solution here, then this is my only resource.)

I start with a document that looks like this:

  - color green:
      inputs:
         - port thing:
             widget-hint: filename
             widget-help: Select a filename
         - port target_path: 
             widget-hint: path
             value: 'thing' 
      outputs:
         - port value:
              widget-hint: string
      text: |
             I'm lost and I'm found
             and I'm hungry like the wolf.

After loading in python (yaml.safe_load (s)), I will try a couple of ways to reset it:

  >>> print yaml.dump (d3, default_flow_style = False, default_style = '')
 - color green:
     inputs:
     - port thing:
         widget-help: Select a filename
         widget-hint: filename
     - port target_path:
         value: thing
         widget-hint: path
     outputs:
     - port value:
         widget-hint: string
     text: 'I''m lost and I''m found

       and I''m hungry like the wolf.

       '' 
  >>> print yaml.dump (d3, default_flow_style = False, default_style = '|')
 - "color green":
     "inputs":
     - "port thing":
         "widget-help": | -
           Select a filename
         "widget-hint": | -
           filename
     - "port target_path":
         "value": | -
           thing
         "widget-hint": | -
           path
     "outputs":
     - "port value":
         "widget-hint": | -
           string
     "text": |
       I'm lost and I'm found
       and I'm hungry like the wolf.

Ideally, I would like the “short lines” not to use quotation marks, as in the first result. But I would like multiline lines to be written as blocks, as in the second result. I think, in principle, I'm trying to minimize the explosion of unnecessary quotes in a file, which, in my opinion, will make it much more annoying for editing in a text editor.

Does anyone have any experience?

+6
source share
2 answers

If you can use ruamel.yaml (disclaimer: I am the author of this extended version of PyYAML), you can do:

import ruamel.yaml yaml_str = """\ - color green : inputs : - port thing : widget-hint : filename widget-help : Select a filename - port target_path : widget-hint : path value : 'thing' outputs: - port value: widget-hint : string text : | I'm lost and I'm found and I'm hungry like the wolf. """ data = ruamel.yaml.round_trip_load(yaml_str) res = "" for line in ruamel.yaml.round_trip_dump(data, indent=5, block_seq_indent=3).splitlines(True): res += line[3:] print(res) 

You are getting:

 - color green: inputs: - port thing: widget-hint: filename widget-help: Select a filename - port target_path: widget-hint: path value: thing outputs: - port value: widget-hint: string text: | I'm lost and I'm found and I'm hungry like the wolf. 

Not exactly where you started (but after that it is stable both ways). In later versions of ruamel.yaml, you can set both the indentation and the relative indentation of the sequence - inside this indentation. The latter, however, also affects your top-level sequence, therefore, post-processing.

Important (for me) things that are saved: comments, bindings, match mappings and literal scalars (multi-line use | )

+5
source

Try pyaml beautiful printer. It gets closer, although it puts quotation marks around short lines with spaces in them:

 >>> print pyaml.dump(d3) - 'color green': inputs: - 'port thing': widget-help: 'Select a filename' widget-hint: filename - 'port target_path': value: thing widget-hint: path outputs: - 'port value': widget-hint: string text: | I'm lost and I'm found and I'm hungry like the wolf. 
+4
source

Source: https://habr.com/ru/post/971356/


All Articles