Advanced YAML Syntax
00:10 Remember early on when I put an asterisk beside the readability aspect of YAML? Well, this is where you’ll be learning some features that, if used, may impact that readability. That’s not to say you shouldn’t use them, but that your file will go from a mostly self-explanatory hierarchy of key-value pairs to one requiring the reader to know the more esoteric YAML features.
00:43 You reference an anchored segment using an alias. Aliases use the name you created with the anchor, but an asterisk instead. For you C programmers in the audience, yep, they use pointer semantics. I’ll open the REPL and show you an example.
The top window contains a YAML file. In the first child chunk here, I have declared an anchor named
push-up by placing an ampersand before the name. I then do that again for the second sequence of muscles, naming it
squat, and then again for the third group—you get the idea. Let me scroll down here.
The same goes for
wednesday as well. As you might have guessed by my warning at the top of the lesson, I’m not sure how I feel about this feature. On one hand, it reduces typing and possibly errors by removing repetition. On the other hand, it feels like you’ve gone from an understandable text format to a programming language. A strong use of this kind of feature is in DevOps configuration.
02:12 You often end up with a fair amount of repetition. If you’re configuring multiple S3 buckets, being able to inherit from your base configuration means each of the buckets will be consistently set up, so there are places where you might want to take advantage of this.
02:31 YAML allows you to anchor and alias within the same node, making a recursive structure. In theory, this can be a powerful mechanism. In practice, a lot of parsers don’t deal with it well, and it can blow things up. Again, back to the “moving from text to code” sort of argument I made before, you should be careful with this.
In addition to aliases, you can reuse attributes through a merge. Merges are a different way of reusing anchored content. Instead of the asterisk on its own, you use a
<< (double-less-than). An alias puts the whole chunk as a value in your doc, whereas a merge allows the reuse of key-value pairs as a set.
The YAML file in the top window here contains both aliases and merges. The
person hashes have been marked with anchors. Inside of the
student hash, I use an alias for the person, meaning the value that goes with the name is the entire chunk of data.
04:15 YAML supports multiline text. Indentation is what indicates continuation. This means that a newline in your YAML isn’t necessarily a new line in your data, so to insert an actual new line, you use a blank line.
Alternatively, you can use the pipe operator (
|) to do something like a verbatim block in your text. This can be useful for embedding scripts or other content where the new lines are significant.
This happens a lot in DevOps stuff and might be why YAML is so popular in that space. Doing the same thing in JSON could be painful. Let me scroll down a bit. The last value here is the
> symbol changes how continuation works. Now either a blank line or another indent will be treated as a new line. I’m not sure why this one exists, seeing as you can do the same with a blank line, but it does. Let’s see the resulting Python. Import … reading it …
Let me just scroll back up to the top here and do the same for the code. When you look at the
multi-line key in the Python, you’ll notice that it has one newline character inside of it that’s caused by the blank line in the multiline YAML.
07:06 The generator and the publisher are two different tools, so part of the process is copying and pasting the YAML from one to the other. I’d say about a third of the time, I miss a space when I grab the doc to copy, and that results in bad YAML being pasted.
So far you’ve been using the
safe_load() function to parse YAML. This is actually a shortcut. PyYAML has multiple ways of loading and writing YAML, and in the next lesson, I’ll show you the differences.
Become a Member to join the conversation.