WEBVTT 0 00:04.780 --> 00:10.530 Hello and welcome to this lecture, In this lecture we take a look at what YAML files are. 1 00:10.810 --> 00:16.090 If you are familiar with YAML already feel free to skip this section and head over to the next section 2 00:16.660 --> 00:18.860 if you have not worked with YAML in the past. 3 00:18.880 --> 00:25.590 I would highly recommend going through this because the rest of the Course depends entirely on YAML. 4 00:26.060 --> 00:32.210 If you have worked with other data structure formats like XML or JSON you should be able to easily 5 00:32.210 --> 00:33.340 pick it up. 6 00:33.440 --> 00:35.620 Don't worry if you haven't worked on any of this. 7 00:35.690 --> 00:40.590 You should still be able to easily pick it up going through all the coding exercises that accompany 8 00:40.590 --> 00:41.500 this course. 9 00:43.630 --> 00:46.510 A YAML file is used to represent data. 10 00:46.520 --> 00:54.140 This case configuration data here is a quick comparison of sample data in three different formats. 11 00:54.140 --> 01:01.490 The one on the left is XML where we display a list of servers and their information the same data is 12 01:01.490 --> 01:08.180 represented in Json format in the middle and finally in YAML format to the right. 13 01:08.180 --> 01:20.280 Take a minute to compare the three formats. 14 01:20.340 --> 01:23.110 Let's take a close look at YAML. 15 01:23.220 --> 01:28.210 If you take the data in its simplest form such as key value pair. 16 01:28.260 --> 01:32.600 This is how you would define it in YAML .Key and value 17 01:32.670 --> 01:34.430 separated by a colon. 18 01:34.680 --> 01:40.080 The keys are fruit, vegetable, liquid and meat and the values are. 19 01:40.080 --> 01:44.490 Apple, carrot, water and chicken. 20 01:44.600 --> 01:48.420 Remember you must have a space followed by a colon. 21 01:48.480 --> 01:50.930 Differentiating the key and the value. 22 01:51.210 --> 01:55.020 Let's take a look at how an array is represented. 23 01:55.020 --> 01:58.190 We would like to list some fruits and vegetables. 24 01:58.350 --> 02:02.420 We would say fruits followed by a colon on the next line. 25 02:02.460 --> 02:05.630 Enter each item with a dash in the front. 26 02:05.670 --> 02:09.730 The dash indicates that it's an element of an array. 27 02:10.840 --> 02:12.650 How about a dictionary. 28 02:12.670 --> 02:17.470 A dictionary is a set of properties grouped together under an item. 29 02:17.560 --> 02:22.350 Here we try to represent nutritional information of two fruits. 30 02:22.510 --> 02:27.580 The calories, fat and carbs are different for each fruit. 31 02:27.580 --> 02:30.690 Notice the blank space before each item. 32 02:31.060 --> 02:36.570 You must have equal number of blank spaces before the properties of a single item. 33 02:36.650 --> 02:39.410 So they are all aligned together. 34 02:43.560 --> 02:46.840 Let's take a closer look at spaces in YAML. 35 02:46.860 --> 02:51.870 Here we have a dictionary representing the nutrition information of banana. 36 02:51.930 --> 02:57.100 The total amount of calories fat and carbs are shown. 37 02:57.120 --> 03:04.250 Notice the number of spaces before each property that indicates these key value pairs fall within banana. 38 03:04.600 --> 03:13.210 But what if we had extra spaces for fat and carbs then they will fall under calories and thus become 39 03:13.210 --> 03:15.210 properties of calories. 40 03:15.280 --> 03:17.400 Which doesn't make any sense. 41 03:17.500 --> 03:23.020 This will result in a syntax error which will tell you that mapping values are not allowed here because 42 03:23.020 --> 03:27.450 calories already have a value set which is 105. 43 03:27.490 --> 03:30.570 You can either set a direct value or a hash map. 44 03:30.610 --> 03:32.180 You cannot have both. 45 03:32.500 --> 03:37.340 So the number of spaces before each property is key in YAML. 46 03:37.390 --> 03:42.580 You must ensure you're in the right form to represent your data correctly. 47 03:46.330 --> 03:52.480 Let's take it to another level you can have a list containing dictionaries containing list. 48 03:52.570 --> 03:58.590 In this case I have a list of fruits and the elements of the list are banana and grape. 49 03:58.840 --> 04:03.820 But each of these elements are further dictionaries containing nutrition information. 50 04:07.580 --> 04:15.490 A lot of students new to YAML have reached out to me asking when to use a dictionary or a list. 51 04:15.540 --> 04:18.740 So let me explain this a little bit better. 52 04:18.750 --> 04:26.190 First of all, it is important to understand that all of what we discussed so far such as XML, Jone or 53 04:26.190 --> 04:29.330 YAML are used to represent data. 54 04:29.580 --> 04:35.940 It could be data about an organization and all of its employees and their personal details or it could 55 04:35.940 --> 04:43.140 be data about a school and all of its students and their grades or it could be data about an automobile 56 04:43.140 --> 04:47.610 manufacturing company and all of its cars and its details. 57 04:47.670 --> 04:49.420 It could be anything. 58 04:49.770 --> 04:52.540 Let's take an example of a car. 59 04:52.770 --> 05:01.530 A car is a single object and it has properties such as color, model, transition and price, to store different 60 05:01.530 --> 05:05.030 information or properties of a single object. 61 05:05.070 --> 05:09.310 We use a dictionary in this simple dictionary. 62 05:09.400 --> 05:14.760 I have properties of the card defined in a key: value format. 63 05:14.760 --> 05:17.170 This need not be as simple as this. 64 05:17.250 --> 05:24.540 For example in case we need to split the model further into model, name and make either you could then 65 05:24.630 --> 05:28.690 represent this as a dictionary within another dictionary. 66 05:28.950 --> 05:37.530 In this case the single value of model is now replaced by a small dictionary with two properties name 67 05:37.620 --> 05:38.720 and year. 68 05:39.120 --> 05:42.990 So this is a dictionary within another dictionary. 69 05:45.170 --> 05:48.690 Lets say we would like to store the name of six cars. 70 05:48.920 --> 05:55.310 The names are formed by the color and the model of the car to store this, we would use a list or 71 05:55.310 --> 06:03.680 an array as it is multiple items of the same type of object since we are only storing the names we have 72 06:03.800 --> 06:06.600 a single list of strings. 73 06:08.160 --> 06:12.450 What if we would like to store all information about each car. 74 06:12.450 --> 06:20.370 Everything that we listed before such as the color model transition and price we will then modify the 75 06:20.370 --> 06:25.560 array from a list of strings to a list of dictionaries. 76 06:25.560 --> 06:32.410 So we expand each item in the array and replace the name with the dictionary we built earlier. 77 06:33.220 --> 06:41.600 This way we are able to represent all information about multiple cars in a single YAML file using a 78 06:41.650 --> 06:43.660 list of dictionaries. 79 06:50.670 --> 06:57.090 So that's the difference between a dictionary, list and list of dictionaries. 80 06:57.090 --> 07:01.860 I hope you understood the difference between the three and when to use each of these. 81 07:04.640 --> 07:06.620 Before we head over to exercises, 82 07:06.620 --> 07:09.430 Let's take a look at some key notes. 83 07:09.470 --> 07:14.580 Dictionary is an unordered collection whereas lists are ordered collection. 84 07:14.840 --> 07:16.310 So what does that mean. 85 07:16.520 --> 07:21.010 The two dictionaries that you see here have the same properties for banana. 86 07:21.290 --> 07:28.130 However you can see that the order of properties fat and carbs do not match in the first dictionary 87 07:28.160 --> 07:35.150 fat is defined before carbs and in the second dictionary carbs comes first followed by fat, but that 88 07:35.150 --> 07:36.600 doesn't really matter. 89 07:36.650 --> 07:41.920 The properties can be defined in any order but the two dictionaries will still be the same. 90 07:41.930 --> 07:50.360 As long as the values of each property match. This is not the same for lists or arrays. Arrays are ordered 91 07:50.360 --> 07:51.140 collection. 92 07:51.140 --> 07:59.360 So the order of items matter the tool list shown are not the same because apple and banana are in different 93 07:59.420 --> 08:00.480 positions. 94 08:00.800 --> 08:06.220 This is something to keep in mind while working with data structures. 95 08:06.230 --> 08:18.080 Also remember any line beginning with a hash is automatically ignored and considered as a comment. 96 08:18.100 --> 08:23.270 We are now ready to head over to the coding exercises and have fun playing with YAML files.