Python Project Tutorial | Google IT Automation with Python Certificate

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this lesson is a part of the google it automation with python certificate providing you with in-demand python git and it automation skills to advance your career in it get access to practice exercises quizzes discussion forums job search help and more on coursera and you can earn your official certificate visit grow.google itcert to enroll in the full learning experience today [Music] imagine that you're an i.t specialist working in a medium-sized company your manager wants to create a daily report that tracks the use of machines specifically she wants to know which users are currently connected to which machines it's your job to create the report in your company there's a system that collects every event that happens on the machines on the network among the many events collected it records each time a user logs in or out of a computer with this information we want to write a script that generates a report of which users are logged in to which machines at that time before we jump into solving that problem we need to know what information we'll use as input and what information we'll have as output we can work this out by looking at the rest of the system where our script will live in our report scenario the input is a list of events each event is an instance of the event class an event class contains the date when the event happened the name of the machine where it happened the user involved and the event type in this scenario we care about the login and log out event type all right that's good to know but we need to know exact names of the attributes otherwise we won't be able to access them the attributes are called date user machine and type the event types are strings and the ones we care about are login and logout with that we should have enough information about the input of our script our script will receive a list of event objects and we'll access the events attributes we'll then use that information to know if a user is currently logged into a machine or not let's talk about the output we want to generate a report that lists all the machine names and for each machine lists the users that are currently logged in we then want this information printed on the screen we've been tasked with generating a report and we can decide exactly how we want that report to look one option would be to print the name of the machine at the beginning of the line and then list the current users on separate lines and indented to the right or we could print the machine name followed by a colon and then the user names separated by commas all in the same line and we can probably come up with something even more fancy when formatting a report it's easy to get caught up in the making it look good part i've fallen into that trap but what really matters is how well the script solves the problem so it's better to first focus on making the program work you can always spend time making the report look nice later let's keep it simple for now and we'll go with the approach of printing the machine name followed by all the current users separated by commas okay we now have a pretty good idea of what we need to do we've identified our problem statement which is we need to process a list of event objects using their date type machine and user attributes to generate a report that lists all users currently logged into the machines we're off to a great start the next step we're going to do is some research to work out how to best actually do this [Music] okay so we have a problem statement which helps us understand the problem and focus our approach we know we have to input a list of event objects and evaluate these objects attributes to output a report of all the users currently logged into a machine now it's time for step two the research we're going to consider all the tools we have available to help us solve the problem to find out which users are currently logged into machines we need to check when they logged in and when they logged out if a user logged into a machine and then logged out they're no longer logged into it but if they didn't log out yet they're still logged in i know we're stating the obvious here but in programming it is super important to be clear on the parameters also knowing this tells us that to solve this correctly it's vital that we process the events in chronological order if they're not we can get the log out event before the corresponding login event and our code may do unpredictable things and no one likes unpredictable code so how do we sort lists in python we'll need to do some research type sort lists in python into your favorite search engine and you'll get a bunch of results that mention the lists sort method and the sorted function the difference between these two options is that the sort method modifies the list it's executed on while the sorted function returns a new list that's been sorted apart from that they work the exact same way let's check out this difference in action first we'll create a list of numbers and call the sort method to sort the list you can see here that the elements of the list have been sorted let's try a different example now using the sorted function we'll create a list of names then we'll print the output of the sorted function let's print the original list again to check that it didn't change so you can see that the original list wasn't modified the sorted function returned a new sorted list but the original was left untouched nice we now know how to sort things in python for this problem it's fine to modify the original list so we'll use the sort method but wait see how both these options sorted the list alphabetically that's the default approach python takes but what if we wanted to organize our lists by a different criteria again if we take a look at the documentation we found online we'll see that the sort method can take a couple of parameters one of these parameters is called key and it lets us use a function as the sorting key let's try this out on our list of names instead of sorting alphabetically we could sort by the length of each string do you remember which function we can use to do that yep we can pass the len function as the key all right we now know how to order elements of a list based on the return value of a function in our report scenario we know that our elements will be instances of the event class and we want to order by date which is an attribute of the event class one way we could do this is to write a function called get event date which returns the date stored in the event object we could also create this as a method in the event class if we had access to modifying the class but since we're working with a bigger system that generates these events we'll assume that we can't just add a method to the class so we'll create our own function instead how does this sound is it all making sense remember that there are various paths we could take to solve this problem but some are better than others so it's important to understand why we chose the options we did feel free to take some time on your own to explore the possibilities and understand what we're doing [Music] okay you're doing great with this so far we've already defined our problem statement and then researched options to figure out what tools we have available and which are best for the job now it's time to plan our approach so we know that our input will be a list of events and we'll sort them by time each event in that list will include a machine name a username and tell us whether the event is a login or a logout we want our script to keep track of users as they log in and out of machines so how can we do this let's think about what we'll do for each event and see if we can figure out the best strategy when we process an event we'll see that someone interacted with a machine if it's a login we want to add it to the group of users logged into that machine if it's a logout we want to remove it from the group of users locked into the machine in this scenario it makes sense to use a set to store the current users adding new users at login time and removing them at logout time great but if the current users of a given machine are stored in a set how do we know which set corresponds to the machine we're looking for the easiest way to know this is to store this information in a dictionary we'll use the name of the machine as the key and the current users of that machine as the value so for each event we process we'll first check in the dictionary to see if the machine is already there we need to check this because it could be the first time we're processing an event for that machine if it's not there we'll create a new entry if it is we'll update the existing entry with the action corresponding to the event which means we either add the user if the event is a login or remove the user if it's a logout once we're done processing the events we'll want to print a report of the information we generated this is a completely separate task so it should be a separate function this function will receive the dictionary we generated and print the report it's important to have separate functions to process the data and to print the data to the screen this is because if we want to modify how the report is printed we know we only need to change the function in charge of printing or if we find a bug in how we're processing the data we only need to change the processing function it would also allow us to use the same data processing function to generate a different kind of report like generating a pdf file for example yay we know what we need to do how we need to do it and how we'll structure our code now we can get into the meaty stuff actually writing the code [Music] we've come a long way to get here so let's quickly rattle off what we know so far we know that we need to process the events to generate a report we know how to sort the list of events chronologically we know that we'll store the data in a dictionary of sets which we'll use to keep track of who's logged in where and that we'll have a function that generates the dictionary and a separate one that prints the dictionary okay i think that's everything know what that means we're finally ready to write our code here we go let's start by defining the helper function that we'll use to sort the list we'll use the simple function as the parameter to the sort function to sort the list now we're ready to start coding our processing function which we will call current users the first step is to define the function inside the function we'll first sort our events by using the sort method and passing the function we just created as the key now before we start iterating through our list of events we need to create the dictionary where we will store the names and users of a machine now we're ready to iterate through our list of events next we want to check if the machine affected by this event is in the dictionary if it's not we'll add it with an empty set as the value now for the login events we want to add the user to the list and for the logout events we want to remove users from the list to do this we're going to use the add and remove methods which add and remove elements from a set once we're done iterating through the list of events the dictionary will contain all machines we've seen as keys with a set containing the current users of the machines as the values this function returns the dictionary we'll handle printing in a different function nice we now have the dictionary ready and we need to print it for that we'll create a new function called generate report in our report we want to iterate over the keys and values in the dictionary to do that we'll use the method items that returns both the key and the value for each pair in the dictionary now before we print anything we want to ensure that we don't print any machines where nobody is currently logged in this could happen if a user logged in and then logged out to avoid that we tell the computer only to print when the set of users has more than zero elements now we said earlier that we want to print the machine name followed by the users logged into the machine separated by commas let's generate the string of logged in users for that machine using the method join and now we can generate the string we want using the format method yay we've written all the functions we need to tackle our problem [Music] we're almost done solving our problem we've written the code that solves our problem statement after following our research and plan we're now going to put all of our code in a jupiter notebook execute it and see what happens this is what our code currently looks like as we wrote it in the previous video to check that our code is doing everything it's supposed to do we need an event class for this scenario we'll use the very simple event class okay we have an event class that has a constructor and sets the necessary attributes using this constructor will create some events and add them to a list okay we've got a bunch of events they're currently unsorted they affect a few machines and include some users we'll feed these events into our function and see what happens everything is now ready to go drum roll please great our code correctly created a dictionary with the machine names as keys there's one empty set and two sets with one value let's now try generating the report success our report correctly skipped the one machine that had an empty set that's great [Music] you've made it through the first course but your journey with python is just heating up in our next course my friend and colleague roger is going to be your instructor as you learn how python interacts with the operating system you'll build on all the skills you learned here and your programming is going to get a little more sophisticated hey roger so what can learners expect from the next course hey christine and hello to all the learners out there i'm super excited about the next course we're going to cover how to set up your own developer environment in python and in no time you'll start feeling comfortable using code to interact with the system we'll also manipulate files and processes running on the os and dive into regex which is a super powerful tool for processing text files you're even going to write a script that might be similar to a task you'd be assigned at your job but personally my favorite part of the whole course is definitely where we talk about the linux os and it's not because it's the primary os i use in my job linux opens up a whole world of customization and configuration and i find it really interesting we've got a lot of powerful and fun concepts coming up so don't miss out i'll see you over in the next course thanks roger and thank you every one of you for tuning in and hanging with me in this course i'm not crying you're crying bye for now congratulations on finishing this lesson in the google it automation with python certificate access the full experience including job search help and get the official certificate by clicking the icon or the link in the description watch the next lesson in the course by clicking here and subscribe to our channel for more lessons from upcoming google career certificates
Info
Channel: Google Career Certificates
Views: 5,257
Rating: 4.9763312 out of 5
Keywords: Grow with Google, Career Change, Tech jobs, Google Career Certificate, Google Career Certificates, Job skills, Coursera, Certification, Google, professional certificates, training program, professional certificate program, Google IT Automation Certificate, Google IT Automation with Python Certificate, Google IT Automation with Python Professional Certificate, Python for beginners, Python, Python tutorial, Python certification, coding, Python script, scripting, syntax
Id: kbLDcrmga0o
Channel Id: undefined
Length: 20min 17sec (1217 seconds)
Published: Tue Mar 30 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.