Working with YAML Files in Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is going on guys welcome back in this video today we're going to learn how to process and create yaml files in Python which is a very important skill for data scientists and especially if you want to do something with devops you should be very comfortable working with yaml files so let us get right into it [Music] alright so yaml is essentially a human readable data serialization language which is oftentimes used for configuration files and oftentimes in the context of devops it also has some features that other languages like Json do not offer and we're going to talk about those in this video today we're also going to learn how to process and create yaml files using Python and for this we're going to need to install an external python package called piyaml which we can do by opening up the command line and typing pip install pyaml and let's go ahead and create a simple yaml file just so to see what the syntax looks like we're going to click on new file and we're going to call this example dot yaml so the file type is either dot yaml or dot yml it's the same file type you can see both are recognized by pycharm we have this yamo logo or yaml icon here and we can now provide simple key value pairs we can have lists we can have dictionaries we can have multi-line strings and we can have some more advanced stuff that we're going to talk about later on in the video but the basic idea is you have a key you have a value specified like this or you have my list here with some items item one item two item three or something um or you have some stuff like uh my dict and then you have key value pairs here q1 value one key two value two or you can have some multi-line string for example multi-line Str which is started by a pipe symbol here and then I can say hello world this is a multi-line string and of course we can also have more than two lines so this is n lines as many lines as we want and we can now easily just go into Python and load this file into our python script using the pi pyaml package and we're going to just do this here for the example once then we're going to use a different file we're going to build a simple number guessing game we're going to import a configuration from the yaml file and then we're going to also look at the more advanced features so let's get started by just importing yaml so even though we install piyaml we're going to import yaml and the basic idea is that we say with open example yaml in Reading modes as F we're going to just say that the data or the config or whatever is going to be F dot reads um we're actually sorry this is different we're going to say yaml dot load and we're going to load from the file directly so we're not going to call F dot read and then parse the string we're going to say yaml dot load or we can also say yaml dot Safe Load from the file here um then we can just specify the file stream and then we can print the content so if I run this you can see here this was turned into a python dictionary we have the key the value then we have a list with the individual items we have a dictionary with a key value Pairs and we have this uh one multi-line string here with the respective line breaks so this is the essence of what we're going to do today now we're going to use the example of a number guessing game to specify a config then we're going to go into some more advanced stuff um but I'm going to show you how to do that here so we're going to delete this file now and we're going to create where's the delete there you go I'm going to delete this file we're going to create a new file and we're going to call this game config.yaml and here we can specify a simple game config something like the range of the numbers the minimum number is going to be 1 and the maximum number is going to be I don't know 100 or something then we can also specify the option of the guesses how many guesses does the player have let's say we have five by default um and then we can also say uh we want to have a moat and the mode can either be a single player or a multiplayer let's go and say single is going to be in this configuration and based on this file now we can just change the workings of the number guessing game so we can implement this game by just importing random by importing yaml of course and then we can just say with open game config yaml in reading mode SF we can say that the config is going to be yaml dot Safe Load f so then we have the config file and then we can get the individual field so we can say range minimum is going to be config and then from the config we want to get the range and from the range we want to get the minimum we're going to copy that and we're going to also load the maximum and then also the guesses allowed which is going to be config guesses and I think also whether we have a multiplayer or a single player so we're going to say the mode is going to be config mode and depending on whether this is a single player game or a multiplayer game we're going to either generate a random number or we're going to have player 2 provide the number we're going to say also solved equals false this is going to be for the game and the basic idea is we're going to say if the mode is single then we're going to generate the numbers so the random uh we're actually let's call it correct underscore number is going to be a random Dot randint and the range is going to be the range Min and range Max so what we loaded from the config and otherwise if the mode is multiplayer and we're just going to assume that there are no other values so if it's not single it's going to be multiplayer we're not going to check for invalid configuration or something or actually let's do that let's just go ahead and say alif mode equals multi then we're going to do something and otherwise we're going to say uh print invalid config and then exit and here we're going to say that the correct number is going to be specified by the user so it's not going to be randomly generated but since we're going to play on the same machine we don't want to just use the random input not just a random a clear text input we want to have the get pass input so that the player one cannot see what player 2 put in so we're going to say import get pass and we're going to see here that this is going to be int from getpass dot get passed the prompt is going to be player two please enter the number to guess um and then this is going to be the correct number so after this we're going to just have the simple game loop we're going to say 4i in range and we're going to have the number of guesses allowed here so four number for I in range guesses allowed we're going to say the guess of the user is going to be int off input enter your guess and then we're going to say if the guess is equal to the correct number we're going to print correct you need it and then I actually I plus 1 tries we're going to make this an F string here then alif if the guess is less than the correct number we're just going to print to low and otherwise we're going to print too high and again we're just going to assume that the user will enter a valid number if we enter ABC the program is going to crash but we're not going to handle all the exceptions here now because that's not to focus off the video um and then we're going to say if not solved in the end and if it is soft we're going to say solve equals true and break and if not solved after the loop we're going to say you lost the number was and we're going to just say here correct number there you go so what's the problem here um can be undefined actually it cannot be undefined because we would exit the script here so it actually should be defined every time so let's see what happens if I run this now from the single player configuration I can enter a guess let's say 90 too low oh actually this is interesting 99 is too high 96. yeah 96 was correct okay this was lucky because the number was quite high and I already started with 90. uh but you can see we had now four tries if I enter one one one one one one five times uh you can see too low and it already um terminated because I only have five tries but I can go into the config and say now okay I want to have 10 guesses and I want to have the range one to one thousand so I can start now I can say 500 you can see too high 250 too high 150 too low 200 too low 225 too low 240 and so on and so forth oh this was lucky again uh you need six tries but you can see we can change the config and to test this uh the multiplayer we actually have to use something else than pycharm because we have this in another video already pycharm is not able to handle the get pass input here so I can change this to multiplayer but I have to run it from terminal so I'm going to go to the python directory um okay actually this is not the correct one we need to go into this one here um and then we're going to start the main py file and here now I can say Okay I want the number to be 80 for example we can see 80 is not displayed on the screen I told you it was 80 but it is not visible on the screen so now I can say 70 I can say 90 I can say 81 I can say 79 and I can say 80 and then you can see it works so this is how you load a basic config from the yaml files now let me briefly show you how we can write the ammo files and then I want to show you an advanced feature which is quite impressive or quite useful so to write yaml files it's not too complicated you just let me just open up a new python script here writer.py all you have to do is you have to say import yaml again you have to say with open sumfile.yaml in writing mode SF and then we can say um yaml dot dump and you would need to have some data so let's create a dictionary let's say data equals and we can say here name equals mic then we have H is 25 and then we have maybe something like languages python C Java and then maybe address is going to be a dictionary with City let's see New York City then maybe zip code I don't know what the zip code of New York City is or what a valid zip code would be so I'm just going to say one two three four and then country Maybe us then you could also add a street or something that is our data so we can now just say yaml.dump this data uh into F and we're going to say that the default flow style is going to be false then I can run this here and you can see we have some file yaml with the respective information inside of the yaml file so that's quite simple now I want to show you a more advanced feature which is quite interesting and this is something that definitely is not supported in Json for example and this is you can reference individual instances of the yaml configuration so if you have a yaml file with certain key value pairs or certain objects you could say you can access those um or you can reference those objects in the yaml file so for example I can create a new file here let's call this complex Dot yaml and let's say that we have an address for example and we're going to say now this is and address so we're going to create this reference here and we're going to say that I don't know we have some streets let's call this uh my fancy Street 15. and then we're going to say we have a City My City and we're going to have a state my state whatever uh this is now an address object which we can reference in different other objects so I can say for example I have a person one and a person one will have a name and the name will be John for example and we're going to have an H and John will be 35 and then we're going to say that John has an address and the address of John is star address so this is like pointers we have the reference here we have this star operator to reference this object here and this basically means that now the address of person one is this and we can do the same thing with person two so I can actually say here person two will be bomb and bump will be 65 and he will have the same address and now we can just create um another python script complex.py for example and we can just load this so we can say import yaml and then with open complex let me just zoom in here complex yaml or was it yml yes in reading mode SF we're going to say data equals yaml Safe Load F and then we can just print the data and you will see that we now have the address actually inside of the individual uh person objects here so we have person one which is the key then we have the value which is this dictionary and you can see that from the reference which is just star address which references this address here we got the full address key value pair in our person two and person one key value pairs so this is a feature that Json for example does not offer and um yeah yaml is essentially quite useful oftentimes it's used for configuration files as I already mentioned for devops um and this is definitely something that you should know about you should be comfortable working with yaml files now last but not least I just want to show you a quick example of a yaml file that is used for example for the language configuration this is from the LG Hub so for the camera that I'm recording with this is a language file so you have these key value pairs where you have um the identifier and then the text in this case the in English text and then you have also the same for German and Spanish and stuff like that this would be one example one real life example of a yaml file that is used with an actual or for an actual software so that's it for today's video I hope you enjoyed it and hope you learned something if so let me know by hitting a like button and leaving a comment in the comment section down below and of course don't forget to subscribe to this Channel and hit the notification Bell to not miss a single future video for free other than that thank you much for watching see you next video and bye [Music] foreign [Music]
Info
Channel: NeuralNine
Views: 31,515
Rating: undefined out of 5
Keywords: python, yaml, pyyaml, yml, python yaml, python yml, python read yml, python process yml, python write yml, devops, dev ops, python devops, python dev ops, python yaml dev ops, config file
Id: nFl6EXfcvLI
Channel Id: undefined
Length: 16min 46sec (1006 seconds)
Published: Wed Dec 21 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.