Hello and welcome to this lecture, In this lecture we take a look at what YAML files are.
If you are familiar with YAML already feel free to skip this section and head over to the next section
if you have not worked with YAML in the past.
I would highly recommend going through this because the rest of the Course depends entirely on YAML.
If you have worked with other data structure formats like XML or JSON you should be able to easily
pick it up.
Don't worry if you haven't worked on any of this.
You should still be able to easily pick it up going through all the coding exercises that accompany
this course.
A YAML file is used to represent data.
This case configuration data here is a quick comparison of sample data in three different formats.
The one on the left is XML where we display a list of servers and their information the same data is
represented in Json format in the middle and finally in YAML format to the right.
Take a minute to compare the three formats.
Let's take a close look at YAML.
If you take the data in its simplest form such as key value pair.
This is how you would define it in YAML .Key and value
separated by a colon.
The keys are fruit, vegetable, liquid and meat and the values are.
Apple, carrot, water and chicken.
Remember you must have a space followed by a colon.
Differentiating the key and the value.
Let's take a look at how an array is represented.
We would like to list some fruits and vegetables.
We would say fruits followed by a colon on the next line.
Enter each item with a dash in the front.
The dash indicates that it's an element of an array.
How about a dictionary.
A dictionary is a set of properties grouped together under an item.
Here we try to represent nutritional information of two fruits.
The calories, fat and carbs are different for each fruit.
Notice the blank space before each item.
You must have equal number of blank spaces before the properties of a single item.
So they are all aligned together.
Let's take a closer look at spaces in YAML.
Here we have a dictionary representing the nutrition information of banana.
The total amount of calories fat and carbs are shown.
Notice the number of spaces before each property that indicates these key value pairs fall within banana.
But what if we had extra spaces for fat and carbs then they will fall under calories and thus become
properties of calories.
Which doesn't make any sense.
This will result in a syntax error which will tell you that mapping values are not allowed here because
calories already have a value set which is 105.
You can either set a direct value or a hash map.
You cannot have both.
So the number of spaces before each property is key in YAML.
You must ensure you're in the right form to represent your data correctly.
Let's take it to another level you can have a list containing dictionaries containing list.
In this case I have a list of fruits and the elements of the list are banana and grape.
But each of these elements are further dictionaries containing nutrition information.
A lot of students new to YAML have reached out to me asking when to use a dictionary or a list.
So let me explain this a little bit better.
First of all, it is important to understand that all of what we discussed so far such as XML, Jone or
YAML are used to represent data.
It could be data about an organization and all of its employees and their personal details or it could
be data about a school and all of its students and their grades or it could be data about an automobile
manufacturing company and all of its cars and its details.
It could be anything.
Let's take an example of a car.
A car is a single object and it has properties such as color, model, transition and price, to store different
information or properties of a single object.
We use a dictionary in this simple dictionary.
I have properties of the card defined in a key: value format.
This need not be as simple as this.
For example in case we need to split the model further into model, name and make either you could then
represent this as a dictionary within another dictionary.
In this case the single value of model is now replaced by a small dictionary with two properties name
and year.
So this is a dictionary within another dictionary.
Lets say we would like to store the name of six cars.
The names are formed by the color and the model of the car to store this, we would use a list or
an array as it is multiple items of the same type of object since we are only storing the names we have
a single list of strings.
What if we would like to store all information about each car.
Everything that we listed before such as the color model transition and price we will then modify the
array from a list of strings to a list of dictionaries.
So we expand each item in the array and replace the name with the dictionary we built earlier.
This way we are able to represent all information about multiple cars in a single YAML file using a
list of dictionaries.
So that's the difference between a dictionary, list and list of dictionaries.
I hope you understood the difference between the three and when to use each of these.
Before we head over to exercises,
Let's take a look at some key notes.
Dictionary is an unordered collection whereas lists are ordered collection.
So what does that mean.
The two dictionaries that you see here have the same properties for banana.
However you can see that the order of properties fat and carbs do not match in the first dictionary
fat is defined before carbs and in the second dictionary carbs comes first followed by fat, but that
doesn't really matter.
The properties can be defined in any order but the two dictionaries will still be the same.
As long as the values of each property match. This is not the same for lists or arrays. Arrays are ordered
collection.
So the order of items matter the tool list shown are not the same because apple and banana are in different
positions.
This is something to keep in mind while working with data structures.
Also remember any line beginning with a hash is automatically ignored and considered as a comment.
We are now ready to head over to the coding exercises and have fun playing with YAML files.