-2

I have been using ''' for block comments in yaml. Like:

''' This is a comment ''' 

I have noticed that this approach isn't one of the answers to the How do you block comment in yaml question. Is there a reason why not to do this (other than terrible multiline string formating glitches in VIM)? Does it get loaded into memory or something else that could be problematic?

3 Answers 3

1

YAML comments are started with # separated from other tokens with whitespace and terminate at the end of line

If you do:

''' This is a comment ''' 

You specify a scalar node, that starts and ends with one (1) single quote. That is because in single quoted style scalar nodes, you can insert a single quote by escaping it with a single quote. Since YAML does line unwrapping the above loads as the string ' This is a comment ' (the string including the quotes).

However if you insert that as comment after a scalar node like 42 as in:

answer: 42 ''' This is a comment ''' 

You still have valid YAML, but this will load e.g. in Python as a dict with a key answer and an associated value of 42 ''' This is a comment '''. A string, which would probably give you some error if you expected the integer value 42.

Sign up to request clarification or add additional context in comments.

Comments

0

Based on the spec, use # only: http://yaml.org/spec/1.2/spec.html#comment/

As to why? Short of 'Because they said so' I would guess that some of the readability of YAML is lost with multiline comments.

You're use of ''' is the standard for Python docstrings.

1 Comment

PEP 257 explicity states: For consistency, always use """triple double quotes""" around docstrings, so the use of ''' is not the standard for Python docstrings.
0

You can run the dehash script on your yaml and tell it to leave c-preprocessor (cpp) directives and then run the dehashed file through the c preprocessor. That lets you use all cpp directives including #if 0 ... #endif and/or /* ... */ to do block comments.

dehash can be downloaded from github with this command:

git clone [email protected]:maartenwrs/dehash 

After the above command before changing directory, it can be temporarily added to your path with:

export PATH="`pwd`/dehash:$PATH" 

After running dehash on your file and piping the output through cpp and saving the output of that in a new file, it can be used in place of the original file. The new file will not have the block comments as they will be removed by cpp. For example:

dehash -c -o - my.yaml | cpp -P > my2.yaml myToolThatReadsYaml my2.yaml 

For those that use make, there is a template Makefile and example here: https://github.com/maartenwrs/espmake. Although it is tailored to esphome yaml files, it is relevant to most yaml projects.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.