From the course: Practical GitHub Actions
Python to process YAML with Codespaces - GitHub Tutorial
From the course: Practical GitHub Actions
Python to process YAML with Codespaces
- [Teacher] So it's time to start processing our YAML file. Now, to do that, I could clone this repo locally and work on it on my machine, but that would mean I have to make sure that I have Python installed and that I also installed something called pip to handle modules plus the module that I'm going to be using called the PyYAML module. But thankfully, GitHub has this great feature called codespaces that allows you to create a virtual environment with a fantastic editor. So we're going to use that. I'm going to go here to the Code button, and under Codespaces, I'll choose Create codespace on main. And this will procure an environment that is already going to have Python. It's already going to have pip installed, as well as the PyYAML module. So this is fantastic. It's going to save us a ton of time. And you don't have to worry about installing things on your local machine. All right, so it looks like my environment is finished loading, so my codespace is ready to go. What I'm going to do is create a file here called feed.py. This will be a Python document. And I'm going to go ahead and hide the terminal if it's showing, for right now. And what I can do here is go ahead and start by importing the package that I'm going to use called yaml, which is already installed with the codespace. And then I'm going to import another module called the ElementTree module. Import xml.etree. ElementTree here. And I'm going to give it a name of xml_tree, right? So that's going to let me use XML and also understand YAML. So I'm going to go ahead and open my feed.yaml document and I'll go ahead and read it and I'm going to call it file, all right? So here I'm going to load that document into a variable called yaml_data and I'll use the yaml command from the module and we'll make sure we'll use safe_load and we'll load up the file. safe_load is just a function that verifies that the file is going to be loading up correctly. All right, now I need to create the RSS element. So if you remember from the feed sample, if you scroll all the way up, you have a few tags that you need. First is this XML tag. We're going to be doing that later on. It's going to write that out automatically for us. Notice that this is a single tag and it has this weird question marks. It's the only one like that. But then we have this rss element. We begin it right here, and we close it at the end. And then there is a bunch of sub-elements. And so for right now, we're going to make the simplest element we can with just sort of a single item in it, just so that you get used to how this sort of module for reading and understanding XML and YAML work. All right, so rss_element equals xml_tree. So we're creating an XML tree. And in the element, we're going to create an rss tag. Now, the way that you write these is by normally either writing the data inside a tag like this. So the data here is Hiking Treks. This is actually a simple one. And then the more complex ones have attributes like this. They may notice that sometimes you'll see the word itunes mentioned a lot. iTunes is an Apple music player that pioneered podcasts, and that's why a lot of these tags will also have this itunes prefix. So sometimes it'll say things like description, but other times it'll say like itunes:author. And that's because this is an RSS feed, which is normally used for news, but the version or the subversion of this RSS feed is a podcast, which has these additional tags and mostly itunes tags. So it looks a little funky, but it works great. So what we're going to need is basically all of the attributes and values that are in this sample RSS. I don't want to have to type all that stuff in. So we'll go back here and we'll go ahead and paste it down here. And basically we need the attributes and the values and we're going to convert these equal signs to colons. So sort of like a JSON-type format if you're from a JavaScript background. We're going to convert these to single quotes and make sure that these also have single quotes. So sort of the name and value pairs need to look like this. And so here we're going to take this rss tag that we're making, and because we want there to be a bunch of attributes inside that, then we are going to move all these in here. And we'll indent these. All right, so now we're going to create an element for the channel. So again, if we refer back to the feed, and it's a good idea to sort of have this side by side while you're building this, inside this RSS tag, there is a channel tag. The channel tag has most of the information for the channel. So we need to go ahead and create that. So this will be channel_element equals xml_tree SubElement. So we're creating an element inside the XML tree, right? And this is going to be an rss. Actually, it's going to go inside the rss_element, and it's going to just be called channel. So it's not going to have any subdata in it. It's just going to create sort of the channel tag. Now, let's go ahead and xml_tree SubElement and we'll add a new channel_element. So inside the channel tag, right, we're going to create a title tag. And inside that title tag, we're going to add some text yaml and we'll read it from the YAML file. And all that data from the YAML file is now in an object called yaml_data. And then it'll go and retrieve the title. So if you remember the feed that YAML has this like title field. It's read it all into that variable called yaml_data and we can access it just like that. All right, so now it's time to output these things. So we'll create a variable called output_tree and this will be an xml_tree, right? ElementTree again. rss_element. So basically, the element that we have been building is going to be fed now into this output tree so that we can go ahead and send it out to a separate file. All right. And finally, we do the writing. So we'll say output_tree.write and we'll call this file podcast.xml. We'll do an encoding of UTF-8. It's just sort of an output format for the text file. And then we'll say xml_declaration equals True. So that would be the XML weird tag at the very top of this file right here. So it'll generate that for us automatically. And that should be it for kind of the basics of this file. Let's go ahead and pull up the terminal and I'm going to execute this file with python feed.py and let's see. So it looks like I made a mistake here. rss_element is not defined. And it looks like I need an equal sign here instead of a minus sign, so let's do that. Let's clear out the console. And we'll issue that python feed.py command. Now, it executed correctly and nothing happened, and you should see an additional podcast.xml file. You could see that it has that weird XML tag. It has an RSS feed, and if you follow it along, you can see all of the attributes that we placed in there, and inside that, we have a single element with the title of our podcast. So that's a lot for right now. We'll continue this in the next video.