JSON and API management in Python | Arkadiusz Włodarczyk | Skillshare

JSON and API management in Python

Arkadiusz Włodarczyk, Professional teacher

Play Speed
  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x
23 Lessons (2h 20m)
    • 1. What is JSON? Why do we use it? What problems does JSON solve?

      5:55
    • 2. Creating and saving data from Python in JSON format to file and String

      9:55
    • 3. Loading data from JSON file to Python

      3:59
    • 4. Pretty Printer - loading/saving pretty and sorted JSON

      7:38
    • 5. What is pip and PyPi? Installing and playing with external package - request

      11:29
    • 6. Loading data from server from JSON file - preface to next EXERCISE

      10:16
    • 7. Processing JSON data | extracting top values from list of dictionaries

      18:28
    • 8. Refactoring - making our code easier to maintain from previous lecture

      14:09
    • 9. SOLUTION 1: Retrieving a few users from server at the same time

      7:06
    • 10. SOLUTION 2: Retrieving a few users from server at the same time

      4:53
    • 11. SOLUTION 3: Retrieving a few users from server at the same time

      12:05
    • 12. defaultdict supplying missing values of dictionary when accessing empty element

      6:03
    • 13. What is public API? Where to find them? stackoverflow API

      12:50
    • 14. Getting JSON from stackoverflow.com -> getting top questions from last week

      10:17
    • 15. Opening websites from Python code - webbrowser module

      4:35
    • 16. Getting records from last week - datetime module

      10:15
    • 17. EXERCISE: Cat facts

      6:23
    • 18. EXERCISE: random CAT images by breed

      2:39
    • 19. Authorization - log in using API key - getting holiday calendar

      12:14
    • 20. Authorization through header - settings

      6:23
    • 21. EXERCISE: CAT system - introduction

      12:37
    • 22. POST - adding a cat to favourite list

      6:57
    • 23. DELETE - removing a cat from favourite list

      15:25

About This Class

Learn how to use JSON and API in Python in a fast and easy way!

Main features covered in the course:

  • What is JSON?
  • Why do we use it?
  • What problem does JSON solve?
  • Why is JSON so widely used?
  • Why is it a great solution for transferring data between different languages?
  • How to save Python data into JSON format to the file and to String?
  • How to load data from JSON file or string and change it into Python type?
  • Making the JSON format pretty while printing it out.
  • What is pip and how to use
  • What is PyPi
  • How to install external package like request
  • What does request is used for, and the basic usage of that package
  • How to connect to an external server that returns a JSON file
  • How to download the content of this file?
  • How to handle a possible exception
  • How to test and play with JSON data?
  • You will learn how to process data on practical example. How to make easy to maintain code.
  • How to refactor the code?
  • What is the API?
  • Getting in JSON format questions from stackoverflow.com
  • Processing and opening links directly from Python script found in last lecture
  • How to get questions from last week without the need of putting date manually
  • Exercise regarding cat facts :-)
  • Getting 3 random cat images in webbrowser of specific breed.
  • What is API key?
  • How to use it?
  • Why it is used on websites?
  • Practical example of usage of API key on websites with holidays

Transcripts

1. What is JSON? Why do we use it? What problems does JSON solve?: Hello, everybody. Today you learn what ISS Jason, Why do we use? What problem does Jason Self? Why is Jason so widely used? And why is the great solution for transferring data between different languages? Okay, so imagine for a second did the unit to process data using fightin that comes directly from database. And after processing these data, it looks like this in our pipe and language. Okay, so before it was in database, now it is here. Processed data inside AARP Iten language. Next, you want to pass this entire data toe? Another programming language, like, for example, javascript. Why? So the programmer who programs in javascript can show these data in web browsers for the users off his website. Okay, theoretically, you could write to the file the date I like this, which is shown here in a text for so you could just right do a fight character by character , right? Right. This entire data into a fight corrected by corrupt. But there is a problem because not necessarily in other programming languages. These data can be described in this way. What do I mean? Well, for example, in other languages, they might not have for example, topples right. So there is no a data type like this. For example, they do not type none, but they type in their language new or they use the small case here, not the upper case. So there may be might be different differences, but it really depends on the language, right? And that's why we cannot just save these data character by character into a fight and then read it inside. For example, Jarvis, People in other programming language. Okay, Fortunately, we have a data form called Jason. Okay, so Jason can be used in every programming language because Jason has certain rules for saving data followed by all programmers. And it's cool because you don't need to wear them because, well, they are there are built in methods Inp, Iten or other programming languages, which will do all the work for you. Okay, just one method and everything is gonna be transferred from this day. Data type two, the Jason Day that I, or from Jason two fighter or another programming language. So it's really cool. It's powerful. There is just This is just like a bit win data type that is used by other programming languages right. So with the help of one function, you can change your day that I've been biting, directed to Jason. And someone in another language will read Jason using Onley. One method he operates in his own language. You operate in your own land. Good. It's perfect, isn't it? You just don't need toe a learned another programming language in order to I understand how the other programmer works with feed his data, right. And to understand the power of the solution, imagine, for for a second that this common data tie former like just Jason doesn't exist at all. What would you need to do? You would, um, other than the thing that we told about before, right? Right in character by character. Well, you could, for example, right everything to if I in the four months that would be made up by you, for example, you could separate data with comas and subsequent records with enters or some other conditional interruptible special markings. OK, so you could, you know, think about for example, attributes or tags like in html. So you know something like this inside, um, inside our fight. But unfortunately, the person loading your file for example in JavaScript would have to create from complete zero from the bagging as special coat on Lee to support your your your new the invented data for mother game. So for every programmer, there would be probably a different idea to solve this type of problem. Imagine the chaos that it would bring to our programming world. Fortunately, we don't need to worry about it because we have something. Will this called Jason? Okay, Jason. So something what is between other languages? In the next lesson, I will show you how to write the following type. So this one directly into Jason and then you just don't need to care was gonna happen to this because the person that was gonna program Java script in C plus plus another for another programming language can just translate this into his language gels using Gwan met . That's why this is so powerful and coal to use the building. That lesson. Thank you very much. 2. Creating and saving data from Python in JSON format to file and String: Hello, everybody. Today you learn how to change the data format from fightin into the data for month in Jason . In order to use Jason, all you need to do is type import, Jason. And it's all Yes, because Jason is the stand up library. Uh, so you're going to do is type Jason important. Jason, right. Do you have got here a description off the methods today? You in then dumps and dump Matt. Dump means dropping something. It means throwing away, teaching something. Okay, so when we use the terms method, we really mean that we are dumping. You are dropping. This data were throwing the way, and we are changing it into the Jason. So this method saves data inside and this very important string. That's why here is as as for string for months, Jason. Okay. And, well, how to use it? It's very simple. You just need to type Jason dams, and then your time the data. You want to change the movie? Okay. Of course, we need to run the model because you just important. Chasen's Jason damps and movie. And as you can see, we have just changed this in today's, but it's not formatted as well, right? And not Is that instead off? True with the upper case, we have good here. Now they're small case, right? We also change the topple into some kind off array. Okay, so everything works fine, but are a few problems, right? You have probable notice that we have got here a weird thing. What's that? Well, you know, Polish characters. Like for example, this one is outside off asking table with this escape table. This is this thing. So this is some kind of table off base characters. Basic characters. Andi, for example 65 associated to upper case A 104 is associated toe age. Right? So this is a table off basic characters. But will characters from Poland, Ahrens Ah, here. Not all of them. Right from Germany, also from China also right. So that's why we have something like this. But if we want toe use these characters, we can do it. We can do it by changing the bowling. Ensured the asking to false because by default it is said to true as deacons. So when we send their movie and then we do something like that. Insure asking So we changed the name argument to the falls then, as you can see now, we do not have here this thing. But we have got really with the character with Okay, Andi and I think that you should always do it this way. So we have got here a string notice because we have got the apostrophe here, right? And which also means that we can really in vogue it this way so we can do something like this is Jason dumps and, for example, encoded, moved. Write something negative. And when we run our module, we have got it saved inside of a rebel. And now we could just easily send it directly to server, for example, looking like this. And somebody there could just really as a string right. And he could change it into his own, tied in his programming language, like, for example, inside javascript. That's good. But also, you have got the possibility to save it into the fire. And then no. Then you wouldn't send toe another server a string, but you'll send entire fire, right? So how to do it? You are using the Jason Damp Method 40. So we are writing Jason Damp. Dan, we need to type the data that we want toe, uh, changing too, Jason. So in our case, it's moving. And here we have got FB we just for which stands for five pointer. So we need to open the fight. Right? So we've are using the weaves in text. So the file with because automatic loving and it's called that five, for example, sample Jason. So we'll use the extension. Jason, you don't need to add an extension here, but I think it's good idea toe extension so everybody can see instantly. That is Jason five. And then you need to open it in the w mode, which is the writing bone because you want to ride into the fire. We don't want to read from a broken toe right to it. And I think it's good idea to set encoding in tow ut f eight. So everything inside is gonna be properly interpreted, right? All characters, and then we're gonna save it. It's the 505 and now we can just type it here, fire in going to make end, and here on and then as the last argument, we're gonna send the named argument, insure asking who falls So we don't have problem with the asking characters, right? And, well, we can now save it Random module. And about what happened, As you can see in the folder where we have our pipe and five, the sample. Ah, because the sample Jason Fire has just been created. We can now open it with the note but on. And as you can see, we have got it inside one line. Now it's not formatted, but, hey, it's working fine, because we change the uppercase into smaller case, right? We change the top of into the area and someone So we have got now a Jason formatted five that we can send toe server or two, some kind of javascript school screed on another programming language. And somebody can read it with its because he can just use one method to load it right, That that's very cool. And, well, you conform on this if you want to read it because the to be honest, the chasing format is an easy to read for month if you want to analyze data, right. So we imagined that you go to these data from somebody, right and you need to analyze it first before you gonna process it inside your program and how to do it. Well, look, no thought. Pass plastic unfollowed in the Internet. If you type total plus plus has good plug ins. And when you go to the plug ins, add mean you can type here. J s still Andi even type jails. Still, I have already installed it. You can just creek Andreas stole like that and Creek and stuff after you restart the program in the plaque is you have good now here, Jr Still, and you can just type ah Creek on J As far as you can see, it's formatted now and it's residue. You can easily see the title. It's like there's the release year. It's like this, right? The credits are for director like the director, which is very cool. It's easy to read, write, and what's more. Here you have good something like, for example, Jason viewer. So when you open it, you can jump inside the Or, as you can see, it's highlighting the line that I click here and you can jump in our fight and analyze. It is of course, right now we have got only one record. If there were more, things like that are really useful. So imagine that you are retrieving the data like this from somebody. You then analyze it like this using for example, milk plus plus plus. And because you have analyzed you now know how toe process this data later, right? You can load it like we learn how toe like we'll do in the next lecture and then you can process it with this. So to sum up what we have achieved in this lecture, we have learned how to change the type off our own tiding heighten into the Jason former. We have learned how to save it into the string, using the dams method which has got ask if that stands for string. And we have also learned how to save the Jason format into the fire. Right. And we have also ensured that asking characters are not gonna be presented inside the file or inside a string. We have also said the proper and Kolding So remember about the Sphinx here And what should we do after dead, right? To be honest, nothing. You don't need to worry about it anymore. Whoa! You don't need to worry what will happen with the data, because this data is gonna be sent to somebody, and that guy's gonna process it on his own. You don't need to communicate with him. You don't need to tell him how you have invented this former because he knows Jason and was more he just into use one method to load it into his own programming language. And he can use his own programming language toe process it right. That's really cool. That's only that. That's and thank you for 3. Loading data from JSON file to Python: Hello, everybody. Today we gonna load Jason String did is sent to our program from somewhere, right? We don't know from where exactly we don't care because it's always the same, right? Jackson for money is always the same. So imagine that this variable here and coded retrieved moving is really a variable that retrieved, which got the data that is here from, for example, a server. Okay, so it came to us from server from another server. We retweet it and we save it here. And now, if you want to process this data, we need to change it into the date of the locals like this, right? Because otherwise we won't be able to process it because there are things inside Jason for but that doesn't work in pipe. And in order to do it, we just need to use one line, Jason, that Loht as for string, and then we send it into this function argument. And we can, for example, saved this decoded movie. We can now just run our models and do something like this. Doesn't concede. It is now in Biden former, but you can see here is none. So it's gonna work fine. we didn't keep the topple right. But don't worry about it. Because if you want to have trouble here now, you can change this. We're using, for example, the dictionary expression. You can change this into the top, right? You can do whatever you want with this data time because it is now in Titan. Oh, you can just do anything with it, because topple. Just make sure that it's not gonna be changed, right? You are processing the Zeta, and you know that you shouldn't, for example, change it so you don't need to do it, adult. The most important thing is that we can now process this data. We don't care where it came from. From what language? It just works. What about Eve? You wanted to load Jason from define? Well, you're gonna have to first opened if I So, with open, let's use defy that we had from from the previous lecture. So sample, Jason. So sample that, Jason. And now we need to open in the in the in the remote. But read Moody's default mode need to set the including to the proper one and save the despite. And now we just need to you Do you do something like that, Jason, That loud and the five point they're here and the result of it is take the coded movie. So when we around, our module result is read the same as the coded movie, right? So that's how easy it is to read it as re cool, isn't it? Just one line and we can take the data from other languages and use it in our language. So Jason is really, very often news. Is it to read It is it will use its Christmas baby. Okay, as an exercise, try to do the same things that I did in the last two movies. Lectures. Okay, that's all in the election. And there's always if you have any questions, feel free to 4. Pretty Printer - loading/saving pretty and sorted JSON: hell over bother. You have probably noticed that the data that we bring from Jason is looking really bad in our show and it's car internalized because of it. Well, have one record now, but imagine there are about 30 of them. It would be hard to tell which one is which. Right? So it's a very good idea if we called. Print it as pretty as possible and how to do. There are two ways to do The 1st 1 is to use the damps. Thanks. Look, when you adhere another argument the argument called indent which tells you how big space should be between this rebels inside and how it should be formatted we will put here for you and not is that now when we're on our module on do something like that, we have got no back such an added here and some spaces. Right? So now when you do print and coated movie, it is formatted Great, isn't it? It looks good. You can see analyze thes we've got any hassle. What's more, you called sort kiss Here, look, you can do something like sort bye kiss and we can change from false which staff fault Default to true. And now, when we were on our module, you will not This that the keys are sorted. Alphabetic Carry right Actors Budget, ABC Here we have t r w a. Right, So there's no sort it So it's easy now to read. Okay. So, actors, okay, they are lying dead barred, written here. Try to analyze these the wrong, but I won't do it. I'm looking for about it. Where is budget? Oh, it's here. Okay, now you understand? What? Why? It's good idea, toe sort it. Why? It's good idea to make some Indians and make it look good here. Right. Okay, so we solved one problem. But how? Toe Save it into the fire. Not this way, but the proper way. Like here. What you do leave the same way. So you do something like that? No, We added it here around the module and you will notice that it's for murder. But you need to remember that there is a problem about four month and get here. This fight is gonna be a bit bigger, right? Because there are no white spaces at birth here. And Baxter Tran. So, um, it's not always a good idea. They re depends. Would you want to achieve right? If you want toe, make it the good And you know that you're gonna read from here, then you can do it right. Otherwise, you don't need to care about it. Because if somebody really needs to change it can use J A stool and Jason View where he can use the Firefox firehouse has a built in thing that can show you Jason looking good, even if it doesn't look good at start. So Oh, maybe even these another good idea, sort case is also only useful when you are going toe, read the content. Okay? Eso That's how you do it for saving. But what will happen if we try to open the fight? Look, when he opened the fire and we want to read from it. So we have got the result here in Jason right now. When we were on our module, we notice that we have got also not formatted Jason how to change it into formatted one. Well, in order to do it, we can preened and sent into Jason dumps again. The result. Okay. And then in and you need to ensure asking is false And while you can, for example, served the keys So it's a pretty long lying in order to do something like that, right? What's more, we had two first law didn't change it. Um, I don't like this line. We could, you know, create a function for it. We could also use the module, PP print. So how does BP print work? You can decide like that Import PP print, which stands for pretty print. So we're gonna bring this print is pretty and we use it like that PP print and we just send their a j some around the module. How of course you know the two years between to use it this way because this is the name of order. And now the function name around the module. This conceit it's also formatted on This is standard library, so you don't need to install it. Uh, it's already installed and you can read about it here, try to do a few other things. Look, you have examples I could talk about it, but to be honest, you need to start learning libraries at this the easy one on your own. These are not so useful libraries, right? They They are just additional things that you can learn and they might be useful, but it's not so important. So you can easily try to do it on your own. Okay, Take your time. Read what is written here and try to do the same stuff that was done here. And to be honest, if you are learning things don't copy everything from here. Right, But but to everything. Line by night. So we are importing the people bring. Then you can copy this line and for example, tothis and try to see what is under the stuff. So print the stuff, see what happened using this line right? Then next do days and try to see what is in pp and then ah, do the printing like they did here. And well, try to not use the England equal four right, And brings just like that to see the difference between the pretty printer with India and and we've outing them. Right That way you will learn you contain the variable here to from four to for example, 2010. See for yourself. The difference is that's how you learn. Okay, so this is your exercise exercise. Where you gonna read this content from here and try to, you know, play with it. And remember, you don't need to learn by heart. You don't need to remember all these functions forever. You don't make notes from it. Because to be honest, I don't remember most of these things here. I just know that they exist. And when I need them, I'm just coming back here and I am relearning it very, very far. Okay, It was just impossible, toe. You know, when you go to pipe and standard Ribery to remember all the staff from here height and remember that that's just names off modules and inside of it, there is even more content behind the It's a good idea toe know that something exists, but it doesn't mean that you need to remember it by heart. They're telling that lesson. Thank you very much. 5. What is pip and PyPi? Installing and playing with external package - request: Hello, my friend. Today you will end wore this, people and how to use it. Or this pie pie. And how to install an external package. Like for example, request. What does request is used for? And the basic usage of the package. So let's start from the people. People is a package installer. Okay, is an acronym for people installs packages and well, you need it for external packages. Why? Because let's see when we try to import something with this cult requests. Okay, this is an external module external package. When I ran this module, you will notice that we have got an arrow. No module named requests. We have gotten a role like this because we have not installed request. OK? So far, we've been using their basic the standard models and they worked find because they are already installed. But if we are going to use something that is not installed by default, we need to use the people OK, which is a package installer. But first we need to somehow find something like requests. Right? How to do it? Well, you should go to the pie pie. That organ, which is the pie turn right park it disappear. It's him, Index. So it is index, which means it is some kind off list off all the packages that you can install in. All right, then So you can search for projects here, like, for example, requests. And why do I look for requests? Because in future, I want to show you something which can request so it which can ask for the content of website. And the cool thing about Pi Tony's that somebody has already implemented. Which means that he called it the http protocol. So, http protocol right. You are probably wondering now, what does protocol mean? Well, protocol means that there are rules critical is rule set of rules that should be followed. Http protocols. It means that there are rules regarding connecting to the web site. Right? If you want to receive something crowded website, you need to follow the protocol, Http. And you will need to relearn entire protocal in order to do something so simple. Yeah, but fortunately for us, somebody has already programmed it. Right? That's why you are looking for the external library. So yeah, we wrote requests inside the search for the by pie website and we found something like this . We've got free libraries here. We will only focus on the one that doesn't have a number after the request name. So not records. Still not request free. This is the newest one always. Okay, so let's click on it. And now we have got a description off the request. What does it do? Some simple usage. What's more, you can go to the project link, or you can go to the documentation here and read lots off stuff about this. But to be honest, I don't even know everything from here because it's not like you need toe no end. Remember for four Hever everything that is here. U s a programmer need to know that something exists in the library like this. So it's a good idea to know, Read what this thing can do and only focus on things that you need. Okay, That's why for the next lectures, we learn only a few things from here. And that will be enough for the users that we want. And what use it do we want? Well, we want toe connect to a website that has good Jason data and we want toe get this Jason data and process it to make it look different to make it usable for us. Right? We've talked about the later for now, as you can see this thing sample off one off the many packages that are available on the pipe. I So this is a request, right? We found it. But how to install it? That's it. That's very important, I think, to do into insulin here, you have got in for how to do it so we can copied. Oh, you can just remember that you need to type tape install and then the name off the package you want to install. So we in orderto insulate. You need to open common line, prompt our power shell in windows. So we click here on the search button and we type a type cmd or we type in power show. Okay, That's for example, open power ship and, ah, when we are here Ah, we I can type people installed all we can just paste. Would we couple occupied from the pipe? I can't her. And as you can see, we just installed the request. You might be wondering How can we use the peep if we haven't installed it right. It is a package manager. It's some kind of program, that tool. Well, the newest versions of part and have it already installed. So if you're using the new reservation, you don't need to worry about it. If you are using all their version, just update the bite inversion, and you will have it already installed. So we've just installed a package external packages school, right? We can use it Now. Look, we can come back to our car and now wouldn't run our module. As you can see, it's working fine. We don't have a narrower like this anymore, so we can use it and how to use it. Well, you can read the tutorial, right? Or you we can No, you can watch this video toe. Learn that when you type something like that. Request that get request means in English, Ask for so ask to get and you just type that you are L. So, for example, let's connect to the Web site is called get cup dot com. Okay, so we want to get these website and will that save it to, for example, response. And now, when we were on our module. We have got inside and response an object that has a value 200. What does the 200 mean? It is a status off the website. It means that everything will find we got a proper response. We can now use it. So we can, for example, type response that text. And this thing is now a squeeze text that when we click well, we want quickly because we probably make our unresponsive. But this would be entire website. Okay, We don't want to open it here and we probably don't need every information from there, right? We want to process information from websites like this. How toe process information like that we learned in future elections. But for now, you know that in order to get and tired website which isn't to die this one line of code you don't need to know anything about http Protocol, right? Almost. You just need to know that 200 means that everything went fine and well, if you type something like these, even notice that the response will be a bit different it wo it found it. That's called. That means that somebody created really website like this. Let's do something like this. Maybe this time. So response. Okay, now we didn't find anything. Uh, that was unlocking. But as you can see, 404 means that nothing was found. Right. So two things to remember you can use it. The status coat you can type. Sounds like response that started, Scout. And as you can see, we have a 404. I like to give you an exercise. Now, I would like you to write a function that will open website provided as argument. Okay, So, like these and I would like you to invoke dysfunction for some kind of list off website. So you have got a list off websites looking and give There's puts under some content like website that exits and website the dozen. Next. Next, I want you to create at least off websites that did not open properly. So the start to scout was 400 form right. And I want you to save all that websites into That's fine. We want all these not open hanging websites inside a fire, right? No, not open. Not the one that are open. Imagine for a second that this least of websites came from your balls and he told you You know what? I want you to look at this 1000 websites and you need to open them and tell me which one worked and which one didn't. OK, because I know that I don't want to spend my time on it. You should do it. And you're like, Oh my God! I need to do so many things now Writing to open age off that websites manually. No, you don't. Because you Nope Iten, right. So you can save all websites that are not opening and save all the all websites that are opening dead opening into another fight. That's a kook, right? I really like python because you can do so many things. Ah, when you understand programming and you know, for example, who on external library right Like that and not is that with Children Only a few things from this library in next later lectures were focused on these library and I will show you really practical things regarding Jason the during the lesson. Thank you very much. 6. Loading data from server from JSON file - preface to next EXERCISE: I had all my friend in this lesson that you learned how to connect to an external server that returns adjacent fire help. You don't know the content of this fight how to handle a possible exception and how to set and play with Jason Data. Suppose for a second that you want to create a program that we count. We'd user finishes the biggest amount of tasks from their to do list. OK, after finding a guy like that, you want to give him the prize in the form of a cookie with the inscription Master off discipline. Let's assume that the data will be provided in the form off Jason from the server. Okay. And the data will look like this. There will be the idea off task. Right? And what does idea means? Idea is identification. Er it is something that can identify unique things in our case, a specific task. So when somebody we'd i d number one, he knows exactly the number one is a task about. For example, writing a letter to accustom Number two is about something. Girls free some about something else. And so the numbers say, is exactly what it is about the idea of task idea of user which identify the user and well there task content, right? Something like that On the information If something was completed or not. So building right through a fault Theoretically, you could create this program just like that. We've all done a data, right? You could provide the Tal ater you could just process daytime functions on assumed that everything will work fine. However, we've all testing whether everything works Fine. It's hard, right? We need Teoh. Really Be sure that everything is gonna be fine. Otherwise, when somebody provide you with data, you can find some arrows then and he tend You will need to write a program that we worked fine in a fast manner arrived. You won't have so much time so you can create yourself sample replacement data or you can take advantage off ready made, most common data situation. And there is a website that is called Jason Place Colder. You really like it because when you go here to, um, resources, you were not that Jason place called There comes with a set off six common resources. So six common things that happens when you're trying toe write a program you can play with them, all of them for free in the vehicle used This toe exercise because they think that you're gonna learn now are things that are really useful. DIsa practice. Finally, something really practical and well here are, For example, posts write their posts on forums with some comments. The album's with some photos to duelist tasks with the users that are doing this to do list . So let's go, for example, to two DUIs link and you can see we have got Now here's something Jason Place guarded that type ico dot com two DUIs And now we have got on external server. We've Jason fly it. Right. Um, there's a Jason type off five. As you can see, the false is with the starting with the full small letter so we can connect to it easy, right? You can just type here I this link and we are already connected to it. We just got it into our response. Next we can run our module, and I responded that text and says, You can see here gonna hear squeeze text, which is Jason. So we can really think about it as a string off Jason. It means that, yes, we can import Jason and then do something that Jason that loads. We've as here, which is for us when she's forced ink on. Then when we open parent is is we can just type your response, that text and we can save it for two, for example, sound like tasks I didn't want to run. Our module you're not is that we have got here now something like squeeze text again because it's very big. Ah, least right now. But we can, you know, access. For example, the first element that's Cassie here is user i D. And when we go here, you will notice that it's the same guy. One wound electors out about him and full as you can see, there is a small difference because false here starts with the upper case, which is good because these apartment type right now, So we change the chase and inter piping type. That's a very cool, very fast, very easy on. We can now analyze process thes date, right, because isn't at this so we can do something like, for example, let's take the first time lemons you can allies on internal elements. But we talk about processing this data in future Lecture. Right now that's focus on oh Pang this website. Look, when I type you, for example another reps website like, for example, this one And if there is no Jason there than this is not a good idea because we have problems, right? Because Jason loads changes, Jason, not something else into the Piketon types. So how to do it the other way? Well, to be honest, it is very easy because the requests package has got something like and method just so response that Jason will give you the same result. Like the both things when you're on our model, it's called negative this place for a second. You will notice that when I do something I did, the result is the same, but duris beat different thing. What happened? Well, look, no one. I do something like something wrong. Example. We're gonna come back to something that doesn't exist, right? And we're on our module. Well, maybe not something that doesn't exist. I think the idea first once I think that exists, but it's not adjacent because these things should be also, you know, um, establish You should also try toe open on the things that exist. But this is not the way we are not talking about this right now in the sector. These a problem. But I request we want to focus on Jason. So when we are trying to open get harp which is not giving us a Jason result, we can now do something that try to do this thing. Otherwise, if there is an exception that looks like this All right, then we can, for example, say that the content is not Jason. Right? So now when we were on our module, we notice that we have gotten the information to content is not James. But if we go to a proper place like this, as you can see, everything went fine. And now I will tell you something new. When you type your house, then this is a place where you should put a coat if everything went fine, OK? So as here in the tri except Syntex Ah statement when we run our module as quicksand like these that all the instructions after it are gonna be executed if everything went fine in trying case Okay, so that we can print here, for example, tasks off zero. Right, So we can process the process data here easy. And we process data in next lecture. Okay, So, to sum up, we learned in this election how to connect to extend all summer with Jason, and we found a place holder for our data. I'm gonna even create here your own placeholders. You can read about it in the guide and uploads to the get help your own times because they are likely right now, right? Goes there where data like these, or you can just play with what is already there. Right toe exercise made polls with Commons, and there was many processing things with this. But, you know, it's just for you for practicing. Purposes came. So we learned how to connect. Exemplify Donald the content of the five days, the way we know how to handle an exception that disconnected to Jason. We have also learned that when you type else into try except block that everything that is after else is gonna be executed when everything went fine in the tri book. So it's very useful. Andi. Well, that's owned a lesson. In the next lecture. We will learn how to process data like that. Right? We go The idea of tax idea of user We want toe give the person with the most completed amount of tasks the cookie monster of this city. Thank you very much. 7. Processing JSON data | extracting top values from list of dictionaries: head over. But in the last lecture, we requested Jason data from this website. Now it is in our program. We have get it inside a response. We also change it using the response Jason into the pie Tonton type off fire So we can now process this data and this lecture. We want to process it like this. So whenever the user i d is here with the completed equal to true So this record doesn't interest us. So these one doesn't interest us. There's no there's not. But this one Yeah, because completed is equal to true here. Right? So this is the guy who completed that us. It's the guy who might get the cookie, right? No off course. We need to complete the most mind off task. Right? So we need to count how many truth or car appear many first for the user. I d. By, for example, one or, for example, to right wing to count How many truths are for each guy. So we want to save each occurrence appearance. Manifestation off a guy with i d. Task, when completed, is equal to true in a former life is in a dictionary where the use. Their idea is here. And here is the amount off appearance of True for the person like this. Okay, so where should we do is I think you should do it. He right in the after the statement Because that's when we are sure that everything went find we retrieved the Jason response and we save it into the tasks. Okay, so this is the place where we should do How can we do it? What is tasks right now? This at least of dictionaries. So we can use. For example, they look forward to go through each entry inside the tasks for a second in order to remind ourselves how it works that just print out entry, right? Run module. And as you can see, we've got lots of entries, 200 off entries. And now we need to choose the ones that here have the completed equal toe fault toe. Not false, but true. Right? So how to do it? Well, in order to access anything from the dictionary what we what? We need to do it inside a entries addiction, right? So we actus entry and then we actors world we actus that key completed and we want to check if this completed is equal to Drew. So if it is equal to two, it means that we got I got a guy who completed a specific tough and the specific task where we can because, for example, print the title of it. But we don't care about title, right? We want toe. Add it somewhere. I want to count it count, common the time something was don't and we want to somehow save it to that. A specific idea off user that completed this task. Let's assume that I don't know how it works, so well, that's, for example, print what is inside right now. Entry off, for example, Use their i d on the march. As you can see now, we've got lots of ideas, right? And two of is that we got here ideas off guys that had completed a task. And well, we should count somehow how maney off these guys are here. How to do it. Well, I think it would be good idea to create a dictionary like we did here, right? So we should do something like task frequency by user and could create a simple, empty dictionary frequency and it will show the frequency by user. Right. Harman Times he completed the task. So maybe let's go to complete it tasks frequency by user Because we want to complete a task like we do here and now we're gonna access it like this. Completed tasks, frequency by user and we gonna actus like this. So look here we are accessing right now Completely task frequents by user and we create or a sign the user i d that is here on the left side. Right? And we want to assign toe hit the number that is here. Let's start. We don't have anything here, so well, we can do something like Plus, they call one. It means that for each time the truth is gonna be mad. So each time, our program, we work like this, huh? This guy? No, it's not. It's false. Here is false. This false. Oh, here is true. So we've got user I d one and we gonna create a dictionary completed task frequency by user off one and assigned to it to the thing that was there. Plus one. Right, So pass one. So it's equal toe want now Falls, falls falls true. So plus one so before was one. So now it's Do falls through free for five. Well, 678 nine. Ah, Dan 11. Okay, And now what? Here is another guy. He's a user I d off to know. So falls we are not doing anything about here is through. So we gonna now create as second entry for completed task requested by user because we're gonna use a different Kino. And now we go now, do the same thing for the guy who has i d to write. So here is through here is false. False. And he was he ready our program. He's gonna again out blast one plaza one plus one and so on. And so that's how it's gonna work, right? And, well, let's see if it's going around. As you can see, it's not going on. Why? Because a dictionary by default is empty. It doesn't have any values. In order to create in orderto art somethingto dictionary, you do not use these operator use the operator a sign that that's it. Problem, right, Because in order to create a key with a value first, we need to use equals sign not plus equal sign because we are like, right now trying toe at plus one to something that already existed by the The truth is, it didn't exist. So in first case, when there is nothing, we should handle the key error. So how to do it? Well, let's try toe involved these. But if there is exception when key arrow happens, we want to create this key. So do something. 90. So just for the first occurrence off True for the completed task for each user, we want to first create a user off that we have I d like this and assigned to it one because this is the first occurrence, right? Appearance manifestation off the guy with the completed tough. And then in the next rounds, this exception will no drama because, well, this will not give any arrow. It will not throw an exception. Right? And now when we run our module okay, works lying. That's to suffer like this. As you can see, we have got what we wanted. We have got now a dictionary off case where the key is user I d idea off user. So we should maybe change it to you. Heidi user and the value is the amount off appearances, currencies off completed tasks. So this is the amount of tusk complete by the user. As you can see, here is the biggest number. And here is the because number. So we've got two winners down. We need to give to cookies. We're gonna be poor. So we need to somehow now choose thes guy and the's going right. But when we are looking, get these. You can instantly see they are the winners right now. Imagine that there are, like, 1000 of records here. You would need to look through them. Of course, we have got a computer that can choose for us, but in order to do it, we need to create a cold. Did you do it? Okay, so let's creating our code that we choose a guy like this. So we need to get users with top amount off completed tasks. Let's for a second, do something like that. Access completed task frequency by users and access it through the function items. As you can see now we have got here a list off topples and we can actively soft apples easy using that Look for if you don't remember the stopping because this was a long time ago. Please go back, uh, to the section where we talked about so we can now do something like for each key body, right, Because he's a kid is a value, But what we could do stuff like for each key value. But we can name the ski and value. So for each user, I d. So this is a user i d. And for each number off completed tasks, a number off completed tasks. As you can see now, it's easier to read right, because for key value doesn't tell her support. Its exact Exactly. He when you're looking at court is easier to read for each user. I d for each guy like the inside completed task frequented by user I Thames. And now we can process data, right? Right now, the loop. We go through each off this record like this and we have inside user I d. This inside number of completed tasks. This the question is how to choose from all of these guys these guy, because this user I d has the biggest amount of completed tough and this guy, the first thing that we need to do. We need to somehow count going to somehow choose the guys with the biggest volume how to do it the easy way. Hm. I think we should use the function that is already inside. Pytorch. When we do something like this, we'll get all values. Now we need to get the guy What we need to see somehow the biggest volume from here. And we can do it by these way. So we're gonna use the box function. And now, Duke, we get the maximum value. So 12 is the maximum value swing to choose the guys that has 12 as the value. So there are two guys we need to remember doing to choose two guys because there are two. If there were more than two where we need to choose more than two, we need to make our program universal. So we can does young, do something, like, get the maximum and then get died. It going to choose more than one guy how to do this. But the first thing we need to simplify our our think we need to just choose the guy that has got the value equal to 12. So ive number off completed tasks is equal to maximum off this. Right? So it's ones 12. Then we have got the guy that we are looking for, right? So the user ideas the guy that we are looking for looking printed to see if it worked Fine . Um um, we made a mistake somewhere here. Yeah, we're missing. The parent is here, Ron DiMaggio. As you can see, we chose the guys with our i d five and 10 so we can not save them someone, right? I think that in order to make it easier to read, we should just We should create a variable here temporary variable that will, you know, hauled it. The result from here. And also we know exactly what does it return, right? Because when you're looking at these, where we need to think, what does it do right now? We know because just involved. But you know, you would come back to this cold later. Like for example, in two weeks. We know, remember with that. But if you save the result from this into, for example, marks amount off completed task. You instantly see that this thing returns this right And you can now compare it to these and what we need toe Save these users. I d some So I think that we should create, at least for example, users I d with marks completed, amount off tasks and let's make it empty. And now, let's just I spent It's like this, um it doesnt war because I appended marks amount of completely task. We should've went into this. Sorry. Run, DiMaggio. And as you can see now, um, right. Something doesn't work. We've got on the 10. We should have 10 and five. Okay, I made a mistake. And now I will tell you how I the blocked Well, I could, you know, record this lesson from start. But you wouldn't learn how I make this mistake. And you wouldn't know that program and like me with even experienced big mystics, right? You would think that when you're making a mistake, you are alone, right? You're just learning. No, that's not true. When you are even experienced programmer, you make mistakes like this and they happen mostly because they called like this. Doesn't Google so good? I will show you assume how to change it into the call that also looks good, the functions and so on. But first we need to solve the mystery. Drag this the first thing when I, uh, soldiers. I was like, Who and why doesn't it work? And I really like debugging this way. Let's see if this thing involved really two times something, this print and, for example, let's type you're a Let's run the module. That's type Okay, It really involved two times. Who knows Nazief, the user i d is really here. Printed properly, as you can see, hates to its five attempt. Why doesn't work. Oh, so we should probably look at there place where we did this right when we created user I d with marks completed amount of towels. The problem is that we created inside the loop. We should create it outside of look, right 19. Otherwise, we are really emptying were really assigning Each time we around this slope, we are assigning nothing into this at least, and that's why he didn't work. Now when you don't know a module, it works fine. So in this election, you also lend how I d bag my coat in a fast way with the small nieces exacted. And really, they happen a lot. Even if your experience Don't worry if something is not working instantly for you, right, this is still totally normal. Okay, so we did it with how it works. Fine. We can also, you know, pretty film, right? Look into something like these. After you leave the flu before you can toe print the users deaths shoot, get cookie off. Master off discipline, uh, and example like this and we can run our module. Yeah, gold. Now, we should have somehow extract the users that the user id's 5 10 and get their name or email and, you know, send the cookies of cars. Are we stepping out the lecture? Because it got a bit long. And in the next lecture are we show you It's a very important lecture, because it we think that you in London next lecture will help you avoid arrow like I did here. Okay? Because you will not have just so many coat appearing good miles. And it will be harder to make a mistake that the during the lesson. Thank you very much. 8. Refactoring - making our code easier to maintain from previous lecture: Hello, everybody. It's time to re factor the code from the previous lecture. But what does it mean? Any factor are cardio? Well, it means that we want to change the code that we did, so it doesn't change its behavior. What doesn't behavior mean? Well, to think that the code does Why do it done right? Why do thinks like this if we know it would work the same. Well, you are doing it so it's easier to maintain the cold like this in the long ground, okay? And it's really important. And to be honest, the programmer is a person who programs thinks pretty fast most time, but in order to make the things easy to maintain Oh, yeah, this takes time. You want to make a get kout, then you need to re factory it really lots of time. And, well, what's is here to re factor to change, So the behavior is not changed. Bodies gonna look better. Look, the things that we did here and here are really some kind of actions and for action remember this word action? They think that was done. You can create a function. What did we do here? Really Well, we got here. We count it. The frequency off task off completed tasks, right? No. Okay. So we can create a function ladies definition of function that counts Task. It's not feeling these, right? Okay, wait. We can take you argument tasks, and now we need to count how to count it. Well, we did it here, so they just copy and cut it from here and paid it here. And then what should we do? We should just return it right. Three turn now. We should just involved here, so called can't compete. That's frequency center tasks. So this function count completed task frequency and it returns it right. You can just say it like these Onda We can run our module and it works saying right by now when we are looking here. Oh, so this part of coat is counting task Right before we had everything here, right? This was like something like a blood off cold like this. Now imagine that this code would be longer like you had, like, 200 of lines and everything was like one after another like this, and you would be ok. Does this party does this part Do right. You will need to analyze it. Here we are just looking at this name. Okay? We're counting completed tasks frequency or sending the task from here. Okay. And we got it here and now we are doing again in action. An accident off. What we should call it not is that this is the cold from the previous lecture with a narrow , and I will show you know why it's easier to spot in their own. Now on now. Let's define a function. Get for example, users get users with top completed tusks. It is right on tasks. Frequency by user is the argument that you won't. Let's just got bit him. No notice. Um, you need tops because there are just too many of them right now. Uh, right this Now, what is Why is easy to spot the herb? Because we and something see that this it's under these. All right, this instruction works for this. Look when their man instruction one after another, when you need to just so be a person with a really good focus. Okay on. And it's just easy to make him the error when there are instructions after instruction. And there are so many Indians and there's someone things like this, right in the US It's just easy to make a narrow here. We can instantly that Hey, we need to move it here. Right on. What? We are returning it. You can see that Going to return it at the end? No, this And now we won't make covers. We need to know just involved it. And we can instantly said that. See that we're not getting users with top completed amount of tasks. And we are just sending them the and we can something like this. And now we can print Cokie's for around the module. And, as you can see, it works fine, right? Wait. Look how how It is easy to maintain a cold. Ladies, please notice that I always start name off function with verb, right? With the action that it does that way, When you're looking here, we exactly know what we are doing. Which said saves us time, Right? In the future, where Cody's gonna be easier to Maine for thing we we have re factored the coat. It doesn't mean that it is perfect. You can you could try to re factor it even more because you know, my get more universally good use classes. We don't know about classes yet because you haven't talk about objective programming. So you know, he could re fact that we factory factor the cult until it's almost perfect. The real honest is almost never super perfect. They are. You need to sometimes, you know, right? Call this father's possible because you don't have time. You person that wants you to write the code ones. It's for now, right for today. And then you need to write it faster, right? But you know, when you have some time, it's good idea to improve your called. So it's easier to maintain for you later to not Onley for others but for you too. Okay, I would like to also show you and matted Teoh you know, manc universal methods. Brooke here we have got good users with top completed amount of tasks. Toby, all nest. We called really mega function from it that get kiss with top values from some kind off diction ever. Right, Because the truth is, that's what we are doing, right? We're getting case. So, for example, one toes free or seven in our case. It's five and 10 that has got the biggest value, and the things like that might happen a lot, right? We want to get just kids with the biggest values, so we can use them later and let's make it universal. So we need to do the same thing that we did here. But right now we are thinking about universal names, so well, let's start from this loop. Maybe that's only days. So for each key and for each value inside the dictionary, that is gonna be sent here. And we need to get high items from this dictionary like we did before then. And as you can see, we count in Mark's amount of completed task here more than once. Also, he's called his ego here because we need to count it only ones why we don't need to come to the each time because it takes time. As you can see, it's easier to spot errors inside the function because there are not many Indians, right? So, um what? No, For each key like this, we need toe make a company reason so we can to stop. Thank these if number of completed tasks what this number of completed tasks. So it's a value right that these, uh, here is equal to Marx. Amount of completely task or this marks amount of completed tasks. It is something that discount and like, the same things. Well, it's really this think here, right. Going to change the name here. So this is the dictionary that we are working with. So we should like this and well, let's maybe not make it, you know, with the indent. But that's make it at least comprehension. Uh, it's cardinal, right? But why not to use the things that we can? And if the thing like this captain's, we just need a king, right? Meaning to key, that's all. Andi. Well, when you have a keen what can you return it? Uh, we don't know. Here. Okay, get keys with the values and consent to it. What the completed Thus we consider you, sir, just going to see it's 5 10 So now we can send these type off dictionary and get what keys with top values. Or we can get sent here and totally and, uh, dictionary that we want to process and going to get from a case with the values. So, to be honest, this function is something that you can save in your own module that you can use. When you want to process Jason data, you can save it for later. For future use it right. You will save your time this way. So we created the function like these with the specific names for situation like this. But if you see the A situation like this happens a lot and she will again ride function like this and in gaining the game, it means that it's time to make something like a universal function like these. And that was an exercise. Put it into your own module. And just remember that you have a function like this. The deacon later use to save your time. You need to just look at the name of function, what it does. You don't need to remember. How does it work? You doesn't to know that it get case with top values. You send their dictionary and you will get a dictionary. What? You would get a kiss from this dictionary with top values. You don't care what happens here unless you want to, you know, optimize it because it's some specific case, right? But for most cases uses, uses, you don't care. It works fine. And that's how you program things. Okay, As an exercise, I like you too. Do everything that you have learned in the last two lectures. It's so important. You cannot imagine how these two lectures were important. Do not start with functions like these. Start from what I did. My girls try to do it on your own. Then change the cold that we had here into a coat like this and then make it universal function. It's gonna be so impactful for your future programming adventure. Really? You going again? Lots of lots of practical experience. By doing this, these are really things that you have learned in these last lectures. Did you do as programmer? Programmers are really people who are processing data. You just need to know how to process data that way. So it's easy to maintain its easy to read for others and, well, you solved the problem. Firstly, right? And then you're a program. Programmers are people who are using functions created by others. You don't need to invent. I called each time from scratch, right? You can Sometimes you need to invent the code Well, but later you can reuse it. Maybe you could find somewhere or Internet a cold function that does exactly this, right? You could, You know, Ty being go how to get case with top values from dictionary. Behave, be somebody. We provide a vory similar things like this and then you don't need to even write account. To be honest, you are a programmer, and you need to just know how to process data. What do you want to do with your data? You really know height out. There are some anus solutions in Internet that you can almost google everything. So there's only that lesson and good luck in your future programming Attention. 9. SOLUTION 1: Retrieving a few users from server at the same time: In the last lecture, we gave cookies to the guy number five. Number 10. Guy number five come to a place. Who is the guy? Number five. We don't know, Right? And I don't think that somebody would like to be called guy number five. We need to get it going to extract it from the Jason. Fine. Here. Users from Jason Placeholder users. As you can see, we have got here 10 guys, all of them have unique I. D. And the idea that we got was five. So we want to get this churchy did reach. And we want to get the guy called. Well, it's a woman, Clementina and how to do it. Well, you can do it with a few different type off ways. Now I will show you the one that is not the best one. But we improve With age lectured, a better solution came. I think this where you will learn lords off Good start instead of, you know, looking at me like I if I was a genius that knows how to solve everything, I'll show you how really we programmers works. Okay, that that's that way. Who learned it's off more so how to solve it? Well, first, going to connect the user right here. So But we need to do something like this. Well, let's call it our love to something like you're probably thinking why I put this coat here . Not here, because it will not look good here. We should create a function for this connection and do the same things that we did with the coat when we re factory during the last lecture. But I won't do this because you will do it later. Were you gunnery factor discovered? It is an exercise after the lectures that you will see. So Okay, you would just type every cold. Did what I did here into the function. Eso you learn more? No, this is not the proper way to write a code because, you know, we are trying to connect to users and we're gonna try to get users that are gonna be from here. But I don't care. You are the guy gonna fix it as an exercise. So we got users and we need them as Jason Andi. Now we've got user said we can. Yet, as you can see, the squeeze text is like this. You have got the lease of dictionaries of all users from here, so we can process it right. We can just go to each of the user. So, for example, we can don't go like it like this for each user inside users. And if the user off I d Because when when I do it like this you're not is that we have got the user. Here is each time addiction are like this. Now we can easily get in tow, user off id'ing. And if it's equal to well, we are looking for the guy. Well, I defy so I could write it five, but are I could write it like these, but, you know, it's totally unprofessional because if I get different guys than it will not work. Right? So we have got here, users idea. We've marks, competed, amount of toxics, and we can check it like these in. So if the user of idea is inside is least Hi, this is the guy that we're looking for. We can, you know, do something like that. Okay, User off ideas. He's a guy Asi could see now when I run module, there's a guy with I d number five intent. So when I do these, I would get instantly the name off Chelsea D Treat and Clementi. Now. So the one that we're that we're looking for, right? Number five and number done right, isn't it? So we can get a noun, right? Cookies for cookie, for we don't need to write this now and now we call Chelsea Detroit each by name, not by number, right? Mm. We can optimize it a bit. How? For example? Look, when we write users idea with marks, competed, amount of tasks even notice that even though we found Chelsea d treat with i d five, we've are still checking in the condition here. If the user i d 10 is inside the least off these two guys. But we don't need to. We can remove if we are not going to use this idea later. Ah di di that we have found and around the modules you can see. Now we have 5 10 then 10 on. And after that, that is emptied varieties. Entire place is empty. So we have optimized their be in cases like these when there is on the two records. Of course we didn't, you know? Oh, save lots of time. Really? So we don't need toe care about things like optimization for such a small dip databases? Nothing. Case off big databases. Optimization is useful. I'm just showing you what you could improve him. So this is the solution. Number one. What is the problem about the solution? Well, we need to connect to the users and get entire user is there's. So if there were, like, 1000 we need to, you know, get all of them here and then go through each of the record. So it's not the best thing. I you know, I think you should We should get exactly the guy that we are looking for, But in order to do it, we need to learn a few new things and we learn how to do it in the next, like 10. SOLUTION 2: Retrieving a few users from server at the same time: So Solution number one had a weakness. We needed to get entire database into our program. How to get a specific person, the one that we are looking for instantly. If we are retrieving Jason from the server like this well, you see, when we goto the Jason Thais placeholder here, you will notice that we can use the get on the get is really here. Right? So when you are using to get method, we can do something like this. When they go into posts, you notice that we got all the posts. But when we do something like that, we'll get post number one. So you know what a case when we do. So I think that we go get just one single guy did We are looking for, right? We're looking for the guy number five. So we need the guy like this in days. So we to request the specific guys we are looking for. Okay, let's come back to our code in. So we need to really do something like this. I want to get a specific guy, So I want to go get guy number one. So we have got here on the one guy. You can run our module and even not. Is that inside user we've got now? You know, I think OK, because here's use the Hearst. Um, as you can see, we would provided here the wrong You are right on how we have got here. A single user inside the dictionary, and they think we always Retief on the one record that. That's why we don't have at least around this dictionary. So we can easily now just down to something like this brain user name. And this is the guy that wants to get the cooking. But they want the guy number five. Thank these. So we need to somehow provide here with the number right from the list and also notice that we have got here. We can only get one request at the time. We can do something like 5 10 right. These will not work. Went into something like these five concedes, just not working so we can get two guys at once. So we need to go through. I were you, sir. I d with marks completed Amount of tasks. We have got the release of the five and 10. So for each user I d inside this dressed. I need to make a request and I need to at five here. Right. So how can we at five here we can just do something like this. But the problem is that it's not gonna work. Why? Because integer can't be connected to string. However, we can solve this problem easy by I think us er before here we are casting user I d to string. And this way we go. The guys did we're looking for right? The same like here hovers. There was still a problem in this ocean. Notice that way are connecting. Requesting the website to time. So we really did Something is okay. Want the guy number five now I want the guy number 10 and notice Pres noted that in order to convict to a server you need, you know, send bites to server from another or another place in the world, right? And did needs the best bite must come back to you, right? It takes time. This is slow. This is probably one of the slowest process. If you have just one thing to get, this is a good solution. But for more than one this is probably slower than this, right? Even if the database well, we'll speak. Okay, so there's not a good solution at all. Then why have shown you this? Because we can improve it in the next election. So this is a solution number? 11. SOLUTION 3: Retrieving a few users from server at the same time: okay. It's time for the best solution that allows you to get exactly the guys that were looking for. We've just one request. So how to use it? Well, we can use an expression like this with you Are else using the get a H eight dp netted when I got here. Exclamation are Mark I can use, for example, the key. So, for example, I d and I can click here, Type five senses could see we go. The guy number five. So these words very similar to days, right? But there is a big difference because we can now add here more than one guy using the conjunction so we can make in conjunction. And, for example, I want to get a guy number 10. And as you can see, we got only two guys and we can, you know, involved something like this in our requests and get exactly these guys and just, you know, print them. We don't need to look for them because you already looked for them here, right? And I think this is the best solution. So let's go be a code from here to him because we need to do a similar thing. But now we need to send a parameter how to send a parameter to the link. That issue. You need to use the name argument parameters. And then you do something like this. Well, when I do, like did, we're gonna get the guy number five. When I run the moderately we notice that it's not gonna work. Why? Because the user is now really a list off single dictionary because, well, you know, there could be more than one dictionary, right? So they should call, you know, users. And as you can see when we do something, for example, I the equals 10. So we want to get I d. Guy number five and 10. Now, when run our module even notice that we have got a list of two guys. So in order to get the first guy, I need to, you know, again in tow, in the user's off zero. Then name right this way. If I want to get the second guy, I need to do something like this or we can use Look, to go true, each of the guy so we can do something. Like for his user inside users. I just want to print the user. Name all cars. Remember that you are giving them a cooking. Okay around DiMaggio. As you can see, we gave them a cooking. So it's working fine That way it's looking better. Bob, this is a good solution. Of course not. Because I typed five and 10 manually here, so we need to somehow. And this is gonna be pretty interesting going to somehow changed kiss. So list off two numbers that could be more into something that we look like this right for cargo to change this interdict. Well, I think it's a good idea to create a function that will do it for our. So we need to a function that will change list into conjunction. So connection by this off parameters from L. A is that is gonna be sent here. So we need toe return at the end. Something like this. Now I am doing it manually so I can show you the process of creating the in function how to how to, you know, play with function from beginning How how to create it from beginning how toe, you know, invent. So change. These of conjunction parameters were sending their this and do you want to have here Conjunction of parameters and we want to send them here like this. So now is gonna work fine. But only because we manually type them here. So now we need to change the least from here that is coming here into this. So how to do it? Let's go through each item inside our released Let's bring it in order to sit. So we have got now number five and 10. We need to somehow at before five i the equals. And after it 1% either equals We were created of a Rabel here that we called conjunction parameter and we were assigned to eat either equals But my dear escalate parameters string and we'll return it later here when it's correct. We completed before because I want toe art everything into this drink. Okay, so I want to add to it 5 10 but with I d So with something like that. Conjunction parameter string plus item. Right? Um and when I do it this way, you will notice that our model will not run because the item is really a string You can't connect string with integer. So again we need to change it like these Now one rial now I'm Or do we not? Is that idea is equal to five? I physical to tend. Not bad, Not bad. We've got now I d before numbers, but the conjunction of parameter string is really at the end. I d because we're not changing. It is We're just adding string to string. But we're not changing conjunction parameter strength. So we need to do is really something like this. I'm going to change the value to the thing that was there. Plus this drink off off the number that we are changing into that. As you can see now we need to bring it because otherwise won't see it. Ron DiMaggio. And as you can see, I d is equal to five. But we don't have anything between 500 but not bad, right? We have got now five and now we need to somehow at here. What the conjunction, Right wing toe. Have this part somehow. So how to do it? Well, we just need to, you know, do something like that. Plus Comb Junction I d equals now. When we were on our module, you will notice that we've got. It's not bad, right? We just have one too many here. Andi. So what we could, you know, improve the looking of this card Because when you're adding, they're very ableto itself in the next round, we can ride it this way, right? I think it's 10 times better now, so it works the same, but we've got the problem with this. To be honest, we don't have any problem because not this that one. I do stuff like that. Ah, when I talked 19 que I were, you know, get mattered is still working. Fine. So it means that we can easily return it to him. And it's still gonna work. Fine. If you can see we got guys that we want it And, you know, way could send difference Now, guys keeping, you know, my old the guy Number 12 and free, for example. And now, when our model, we're already going to get the guy number one to and free This could see these aren't names that order. Oh, this is cool, right? Because we now have a universal cold, but I think it's unprofessional. Toe livid like these, right? When the conjunction parameters is looking like this. We have an additional thing here at the end. We should remove it. The problem is how to remove it. Going to somehow in the last eight alliteration off the slope. Not at this. Think at the end. In order to support the last iteration, we need to know how many times this lease is gonna go. So let off my list is gonna give you how many times these thes look for is gonna eat a right. How many times is gonna get the right so run through the instruction inside that look for and you can save it. Like, for example, last iteration like days right? And we can make on a temporary of are able Did we count the number of iterations? Look, when I do this way do not is that we have what to alterations want to and we can do something like these. If there I is equal to last iteration, then we need to do something different, right? Otherwise do what we did before. So now we just need to do the same. But we've out the last thing. So the part that was added here and not want to run our module, You will notice that I program, um, around my module. It's not working. It's not working because I have somehow managed to remove the that here. I'm so sorry. So now simple mistakes make hard to spot errors. Andi. Now you will notice that we've got the proper result. 10th we want Okay, we can make this solution a bit more universal. Not is that now we got here, I d But what if I wanted toe? You know, get guys not bye bye, I d but, for example, by name or whatever, Think else we wanted to achieve here. Then you need to just, you know, do something. These, for example, the parameter with the default value idea. So we don't need toe. Then do anything with the later part of our coat on. And we didn't do something like that. Keep Plus. And here we'll come to something like this key and now it's still gonna work. Fine, because the default values i d. But we can easily now, now here's sent a different thing that we are looking for right now. We are looking for I d. So I'm not going to change it. But, you know, you can now easily change the key that you are looking for. And you don't need toward that. It's not gonna work fine removed here by mistake. Okay, so we've sold the problem. And as you can see, this is the best solution. Because you have just one request and we're getting exactly the guys that were looking for now as an exercise right to, you know, move it from here to here and also make a function that well, you know, make this action here, so call it properly. So it's time for toe re factor discolored and make it look better that selling that lesson , Thank you very much. 12. defaultdict supplying missing values of dictionary when accessing empty element: hell over about the There is a collections bite on Standard Library, where you can find a few containers data types, which means that they are some kind off, for example, dictionaries that work differently from the one that you have learned about. So what's the difference between, for example, default dictionary from the one that we have left? It says here that different dictionaries supplies missing values, which means that when you're trying toe access key from a dictionary doesn't exist, it's gonna be created automatically. I will show you a practical example where you can use is not is that when you're counting completed task frequency. We were creating a dictionary, which was empty here, and we had a problem because when we tried toe actus a key that doesn't exist like this, and we try to add to 81 We had a Kiara Ryan, and we solved the problem using the try except block. And this is a good solution by they think it's unprofessional when you could use just a default dictionary from collections, right? So we can do something like that from collections import only default dictionary, right? So we are important. Only the guy that you are that we need. And now not instead. For example, when I create a dictionary called Aim On and we do it this way, let's do it first like days and when we tried toe act is something that doesn't exist. Alright, we're trying to act as a key that doesn't exist. And when you run our country, do notice that we have a key Herro. However, when we use the default dictionary And now when I ran moderately we noticed that it's also gonna give you Caro because you need to remember I didn't on purpose. You need to send an argument default dictionary which is the type that should be by default set for each element off dictionary that you are trying toe access that doesn't exist. So in our case, it's ain't right now And now when we run our moderately notice, you have zero because zero is a default value for in teacher. When I use fault, you were not. Instead, we have 0.0 when I use str you're not is that there is nothing here because well, we created this element just by trying to access it, you would notice But when we do something like these, the element was created as you can see. Default dictionary. It's a class. Don't worry. The causes some kind off. I think that we cannot talk about later. It just have something like that inside of it right now way are just caring about the content. So, um, it's been created. That's the most important thing for us, right? If we wanted to create more off things, we just need to access it. And we can create more content like this with empty values. So if I do integer, then it's gonna be zero for this key. Zero for Diskin. Just because we tried toe accesses it right are we would do something like, for example, notice that when we do something like that, so we try toe add one to it, and then we're gonna bring Did you re not, is that we can do it easy, right? Using the normal dictionary. We can't do it because we have a key error and then we need to do, try, accept. And I think this doesn't make our code good rights harder to read with on. It's just better to use default dictionary So in order to to use it, we go. Now that what we can adjust to something like that Here. Default dictionary with default values integer. And now we can remove this lines and our coat will work. That's uncommon here around the module and everything works fine. We've out a narrow right. If we do it again like this, we're gonna have a key role. So not only practical example. I've just shown you where, for example, default dictionary can be used. I could create a special section where we would use everything for from the collections module module. But e the problem about sections like that and teachers who do it this way is that it would not give you a specific case like this, right? You wouldn't exactly know what you are leading to be honest when you're looking at something like this. For example, default dictionary. First time you are wondering, why would I even care for having a dictionary with default values, right? Why can't we just use a normal dictionary? Why would I even care to lend next container? They just take so much time. I So if I could shown you default dictionary in a section collections where it would have a theoretic example. You will be frustrated, right? Because you'll just know that it exists on Do you will know you would just see on example like I have just shown the state start of the lecture. This thing, on the other hand, shows you exactly where it can be used on practical example. Right. As you can see, our cold looks better now it's easier to read, so it's easier to maintain. Also, that's only that lesson. Thank you very much. 13. What is public API? Where to find them? stackoverflow API: Hello, my friend. End up coming the videos. You will learn what is public AP I How toe find 80 ice like that and how to use one off them. We will ride the program that we select questions from the side that is cold stock overflow dot com And the questions will meet requirements like minimum 15 points sorted in descending order from they will be from the last week and they will be from the fight in category. And then we open all the questions inside our Web browser. So pretty useful, right? But before we do it, we need to think about death. Think you what is a P I, As you can see, it stands for application programming interface. But what is interface interface? It's something you see and you can use it not knowing how it works. Let's analyze what is interface ward constructed off In order to understand you see enter in English means between right face means face. So it is something between your face simply saying so for example, a tm So the cash machine did gives you the cash from the card credit count has an interface consisted off batons that are between the machine and your face, right? You don't know how everything works in the background. You don't care about it to be illness. Some will have programmed it. That's all you did. You need to know. So someone shared an interface with you in the form of bottles which will allow you to look into your bank account toe, withdraw money after entering the correct pain. So this interface gives you features and methods like withdrawing money and resources like how much money you have on your account, for example, Right, So but what is a P I This air interface that is designed for programming applications? So you also get the service that shares the resources like in case off the cash machine, ADM. But this time in the form of Jason, for example and some kind of website logic that you can use so methods. And so so, for example, some methods will allow you to change something like, yeah, it will allow you to add new questions to websites, and you will not need to know how it works in the background. You don't care how it works. Use the public ap I and that's all you need to know. Someone allowed you to do it through the interface. Okay, so where can we find public AP eyes go to get Have that come public dash a p I slash public AP eyes. And here you will find lots off public AP eyes salted by index so we can find something about animals. Complete can get some cat fax off course. You might be wondering. Is it in a useful? But some people may beans public a B I that can be used for getting cut Fox or some around them. One, maybe images. But you know, for most time you want to find something useful like, for example, type Google here. As you can see, we can find Google books here. AP I. You can find Google Analytics Cocoa Calendar. All these might be very useful. And Google drive Google Caravan Jean Google Mops right can create customer digital maps based on Google maps state down. That's cool, right? Something more useful, however, in this lecture will focus on something like stock exchange, which is in question and answers forum for the developers that do. You should definitely know if you don't then something is wrong, really, because so far you have probably had a few questions by when you were watching my videos. And most time when you have a question and you type the question in Google, you will find the answer on something with this cold stack. Overflow Stuck over four Low is like one of many websites that are inside of stock exchanges. Like it's Strange is a network of side with lots of questions and answers. Various topics from programming like stark overflow physics, photography as you can see game development. So something simple, like giving board games so user supposed there and they can gain a reputation all they can even lose it on that reputation that this gain there can be used by you on your reasoning. So why? Because these website is a very valuable place on the Web. So when you use this website, you can become popular person, and you can use this reputation in order to get the job right and, well, we focus on stock overflow. So you remember. Stack Overflow is a very important place for you, where you can find search for answers. Did you have regarding programming? Probably when you're wondering house to do something, the answer is already here. If it's not, then you can ask him and somebody will help you. So this is very, very useful place for developers. Okay, if you want to become popular in these website, you will probably sign up here and answer questions that are popular. So they have high school and they don't have too much answers or well, it doesn't matter because your answer will be the best, right? But you want to look for programming language like fightin language that you know. So you want to filter them. You want to sort them. You want to somehow get information from these websites, and almost any very popular website has got something like a public a B I. Some think that we allow you that we'll give you methods to do such things, and you don't need to know how it works in the background, right? That's why it's called Interface. So how would we find the A P A for stock over for? I would just type stock over long, and then I would add a P I inside the Google search and the first website as you can see is a stock exchange ap I So this is an A P I for every website inside. You know the network. So we need to somehow choose the stock overflow light. And here you have good at description off. What you can do is pretty long. And to be honest, it takes time to analyze every a p I and this is what you are paid for. So I am not ableto Did you every be I that is available on the Internet be This is impossible You need toe But you need to know that every off them are very similar are they follow the same rules, right? So if you learn how to use one to off them then you should be pretty fine using any other Look Look at the stock exchange. As you can see when we scroll down here we have something that we talked about in the previous lectures. Look here you have a Z can see links like answers. So when you get here, even not is dead we get to know how to use answers, get method and you have an example how to use it when we around the module. We have got their response in. Jason was more. When I click on it, you will notice that we got into the website like this. 80 i dark stock exchange dot com 2.2, which means the version off the FBI that we want to ask for. And then we have answers. And what apparent perimeters, right, right that were used in order together. Response like this. So we can is the used this here right now a request that get we can copy deace to days place We cannot on that were module. Of course, we need to this time that this would be answers and we need to print them all right around the module. Andi, we go to squeeze answers that are, you know, ordered by this They are descending so and they are sold by activity and we get them from the stock over floor. And this is pretty easy, right? We could, you know, filtered them a bit more to get specific things that we are looking for. But we learned how to do it in future, but because it's pretty easy. You have examples here you can read how to use it and well, we are focusing on what we are accusing. No questions. So we get the questions and we want to use this part here. However, this thing will do in the next lecture. But before dead, I would like to add a few very important things. Notice that we are requesting something from server. And you know, the to be honest thesis takes time off the CPI. You'll off the computer that you are requesting the information from, and you are limited to some kind of amount off request that you can do anonymously. So we've all telling who you are and sometimes and sometimes most times you need toe register. So you to make some kind of authentication, right? And you have good information. How? Toe enter. Have attending Kate here in your applications, how to register that you can see in tow register here, for example, in this website on other websites, maybe you don't need toe register at all on some website, you need to register instantly because you won't be able to use even no, you know, basics methods. As you can see in order to to use answers admitted, you need Toby, uh, registered right, So in orderto adds new items, which is allowed in to be registers. And it will our answers directly to your accounting stock overflow, for example. So it's pretty cool, right, because you can write your own program toe search. But with the graphical user interface, you can, you know, like desktop application. You can make your own application to set for questions and post on and response toe questions right? You can make your own application for that. Has using the public a P I. If you are ready, stick right. Four. Things like getting poses. You can see you can easily do it right now without already stirring. However you are limited, so some kind of amount off requests in our most time it's enough, right? So you don't need toe war about, and you should understand this perspective. Uh, because not is that Derek would be people who, you know, may gallop, and they just palmed the server. I could make a four loop on, make it the infinite Lubin just spawned the server with requests, and it would just kill the sir very many people who do it right. That's why you are limited to some kind off amount of requests in a minute. That way we all can have features like this available. Okay, that song did lesson. Thank you very much. 14. Getting JSON from stackoverflow.com -> getting top questions from last week: Hello. In this lecture, we will get from the stock over floor questions that we follow below rose. They would be minimum 15 points that would be sort in descending order from the last week. And they will be from the piped in category. How to do it. We are hearing on the a p i stock exchange dot com website Onda. We will try toe read the example here. So it tells that that this method allows you to make fairly flexible queries across the entire corpus of questions inside. So we query entire, so require me is asking an entire side. So when we query it like this so we type to your questions, Um, like these, we get questions from entire site no doughnuts, takeover fall. And because this is too big as you can see, we get our message because it's a side is required. It says that we need to specify the sites are going to say something side he calls toe and win to say that I want to get questions from the stock overflow. How did I know that I should die? They recite well from the example. So you're looking, for example, here, you can see. And it says that in order to specify side you type site equals stock over for probably is written somewhere else till parties. Well, we need to read, read and again with, but you can easily on by looking by seven descriptive names like site, um, gas that is going on. Specify decided. So we have just testified the site and we got answers from stock cover. For now, when we're on our module, we get many questions. You know, many questions on and in We don't know what order because we didn't specify that. So it means that it will be in the default order on. We need to specify a few more parameters here. That's why I think we should make this program a bit easier to read on. Bag parameters dictionary. Um, that will send here, uh, here. We can then easily see what parameter is doing. What can also serve like side is in called stack overflow. Right key. What key value. And now we want toe what we want toe get where we want to sort our questions by votes by the score, right in a descending god, we can see that here It thought they told us that, for example, getting all questions asked in the last week off general first with scores off 10 or more in a single choir ing with parameters. So we need to type sort equals votes. So we need to use something like that. Sort votes. Ah, the scar minimum off 10. We want to have a minimum of 15 points, so minimum off 15 and you want toe solve them hard. We want tohave a descending order, as you can see here it says that in order to get descending gotta wing type, order equals descending nothing they don't like creek on it. We also get an example off the result here. So you know we go something except by we want toe sort by votes. The conceit sorted now buy votes on. But there is a very high score here, which means this program. But the most popular question on the stock overflow right? Ah, because we got it from the entire website. Questions from entire website from stock overflow sort sorted by votes apart, this car ordered by descending from so from the best. And we need to somehow specify that we want them from the last week because probably now, when we answer the question in the soap opera topic, we will not get an AB vote. Right? So minimum 15 votes in our case on Do we want to sort it in order a descending on and, well, when Iran, our model will get what we remember about the sign here. Well, good that you can see now when they get here. We got the same things like we did here, but it is not so well formatted, like the Firefox to for us. Maybe we import pp preened. No, let's change it. So it no one here. Another module. You notice that we have got it pretty printed. Um, so as you can see the scar series, this Big Scott is a very big one. As you can see, it's printing for Sloan Armesto in order to show us how it's printed. Then it will speed up on what? So we got the same results like here on now. We need to good get what we need. Teoh. Good items from last week. Not is that in order to get, he said in the week off, General, 1st 2011 They talked here some kind of number, which is weird for you. I could tell the world and how to create number like these. But to be honest, I know also that you can right here something like these here we can like something like from that instead of the weird number, we can just say, Hey, I want the values from the date that comes like this, for example. Okay, So from last week, Right? For me? Last week, right now Way. So, for example, like, um, I think like these. And now when we run, our module will not get so many records. As you can see, we don't have as many records as before. Um, we could his name. That's maybe see it here because I think these everything easier to see what happens when any type things here. Uh, as you can see now, the highest score is 130 right? But the problem is that we got questions from the top is that we, for example, don't understand. For example, C plus plus right? Um c plus plus plus. Plus, it concedes three people pretty popular there. Mm. So we should look for pipeline, right? In order to look for Parton when we go theory were not is that here is something with this tact what is called tact which will allow you to choose the attack that you are looking for , so attacked and we say I want to look for Piketon And now when we run these are we until the team right salt and on tact equals fightem, then we'd definitely have herself the response You right on. We need to get the minimum because way didn't adhere it menu in 15. That's going to see only one question from the last week is equal to baiting. So we increase maybe this to make it a bit, according the last next lecture to, for example, a month like this. And now we have a few more, right? It's called 44 here 22 and we can easily Now you click on the link and get to know who posted it. We can quick also on the link here, and we can instantly get to the question and you can answer right? So we got very popular questions with no answers. Maybe we can provide a better one. If we can, we can prosecutor and gain some reputation. However, right now, when we do it this way, right Mm. To be honest, it takes time. Right wing would toe. We could get links from it, and we do it from the next sector. Toby knows it would be cool if we could just click on our script and our script would get all the links and opened them instantly in our default. Where, brother, that would be cool, right? We don't need to do a job that can be done manually when it can be replaced by Pytorch script. That's why we are becoming programmers to make sure that we don't do things manually. You want to automate them as much as possible so we can say I have some free time to, you know, play to play gangs, to spend time with your family and so on. That's why programmers are so useful to, and that's what we will do in the next lecture. 15. Opening websites from Python code - webbrowser module: okay. It's trying to analyze the response that we got. We need to somehow get link from here, right? This link here and we want to open it instantly. Jazz By invoking our script, we want to open all the links that follow the rules like this. I don't know how to do such fink. The first thing is to analyze what we got in Jason. I think that for analyzing is better to use Firefox. Why? Because not is that when we are here, it's the l a bit hard to with. Even if you're using people print here, we can, you know, expand. I dance. We can easily see that items consists off. What? 012345678 records. Right. And you can open them in order to get to link. So you need to get first in tow items before you start analyzing each guy here. It's not like you have you notice that I dents has more quarterbacks, coterie, my in whatever these things are are on the same 11 like items. So in order to get so in order to get toe each of the items on, do not worry about, you know, things, like has more, but so so we can instantly process things inside. We need to get into questions off items not. Is that went? Now, when I run our model, we have just items and we don't have any more of the least off items, and we don't have at the end wanted That has more quota marks and other things like that. So we toe analyze how we got our Jason response. Every server might give you something different. And now for each question inside the questions, I don't. So inside this list of questions, we can, you know, print that question link. So when we bring the question link, we will get just links. And, well, we could open these things by you. Copy based, huh? But why? When we can automate this process how to do it? Well, there is a web browser module which allows you to open my brothers so we can import it. It is a standard library. You don't need to install it. And here we have something like open new top open. You Taepo something ladies going to just write something like this instead of print. And now when I run I module balm. Every website has been just opened. And now we can just analyze this, not to see what answers it. God, Maybe I will have a better one, right? That's cool, isn't it? We can we have Good. Now the most popular questions from the last month in pipe. Very invite on topic. We could get for the last week from the last hour. Whatever you want. The coolest thing is that you can get the questions you that are popular and try to make a great answer. Or you could use this information in order to make a tutorial about this and posted on YouTube. Whatever this is up to you, right. What you are going to use the information for? The cool thing is that you can, you know, no, make a choir that you want just by analysing the a p. I did. You have got to hear any textiles off time. Really? In order to analyze each a p I This is not an easy process. You'll make me stakes. I make me six almost every time. But the cool thing is that you can play with it very easy, right you can open for example, five fours and play with the parameters and to see the results. And even if you do something wrong, you don't destroy anything. You just get the wrong result and you can change it. You can change it until you make it proper. The wonder Do you really want it, right? That's only good lesson. Thank you. 16. Getting records from last week - datetime module: Oh, my friend. This part of cold from the last lecture is really bad. Why do I think so? Because we put date here manually on this is wrong on murder levels. Look, when I want to get questions from last week, I shouldn't have to open the program and change here. This part of coat each time, right? This program just every time when we run it, it should generate you a proper date. So we need to somehow learn how to generate this part of coat instead of putting it manually him. So we need to learn how to create a date. And in order to do this, we're gonna use something with this cold date time module. And we cannot import from eight time Delta on day time. You know, due to do it, we're gonna right here from the time import, daytime, date, time and time, Delta. This way. We wanted to rights. You know, every time when you want to get into the date time, we don't need to do something like that, right? We or like that we just need to write like this or this. So it's gonna be shorter. No. No. How can we use it? Well, winning a two day time, you will notice that it has good methods. And he was omitted that we're interested in. It's gonna return us. What time is today? So let's do something in the sprint daytime today. Let's invoke today Faction, Let's run the module. And as you can see, we have got a date that is formatted like this. We have got day month? Yeah. Hour, minute, second meanly seconds. So now we need to somehow subtract, deduct seven days from it, Right? We need Teoh. Make an operation on this data. In order to do it, we need to somehow create seven days in time that can be subtracted. And Delta Time is gonna be used for that Because Delta means the difference in time. What exactly does it do it? Well, when you go here and the cooking time Delta, you will notice that time Del Power re presents adoration The difference between two dates or times. Sony, We understand it a bit better when we involved. But notice that we have got to hear something with this time Delta and you can send a tow it day seconds microseconds many seconds, minutes, hours, weeks and by default, all these values are equal to zero bond. You can change. So when we do something that time, Del Tom, and we don't send anything to it. And we're on our module. You would notice that we have got here 2nd 0000 But if we aren't here, for example hours and I said it form, you will notice. Did we have got here now for Which means we have four hours now Here, on when they out, something like days. Three days. You will notice that we have three days and four hours. And these the result from time Delta can be used with our today date, right. We can subtract time Delta from today and we'll have the results that we want if we set days toe seven days, for example. Right? So if we said it days equal seven. Right? So when I do it this way, you notice we have seven days. So this is the time that we want to subtract. So this is a time before No. And we want to do something like this today minus time before, and we can assign this to for example, Saturday Date. So it's a day that we are looking for and while we could assign it here But there is a small problem you will notice soon. What problem is that when you run our module? Notice that we have this under common. So it's not working and we invoke searched it. As you can see, this is some with time. This is not exactly what we want, right? We can print. It is easy because Piketon knows this type. And when you sense there this into the print, you would get print table version off the date. As you can see, we had have now 11 September here We have fought September, so we managed to come back in time. Seven days. It's OK, but it's not how we printed value here, right? It was like, huh? Uh huh. Like this. Right? So we need to have it in the same version so we can go resolve this into West one. These remove this part right from the string. Oh, we can do it a bit more professional way There is. Something would is called time Stumped. Programmers decided that on 1st January 1970 they will stump the time so they will start counting the time and they will count it from things then by adding seconds and not this that here on stock exchange, they provided from day using a big number like this and thes dig these number Here is really seconds that have passed since First General in 1970. Okay, up to January 1st 2011 So we can somehow change surgeon date into times, Tom. Yes, we can do it. The easy way is to do it like this search date that time stamp. And as you can see, we have good now at times stumbled it. So how in the second have passed scenes 1st January 19 7 However, these have here milliseconds and we can't use it as the value for the key from that. That's why we go not cast it into integer. And then we're gonna have a proper thing that we're looking for. And now when we pass it here, we'll have the value that we're looking for. So now we're gonna get from the stock over four votes sorted but descending order with the score minimal 15 from date that IHS said here and we said it like this. We said that we want them from the last seven days. And now when you're on our module and I'm commented here, we have good one question open from the last seven days that followed these rules. If we increase, they stew, for example hurting. We're on our model. You'll notice that more websites have been open with more questions, right? The cool thing is that now we do not need to jump in tow our program and count Calculate. What did we had seven days ago? Tell two days ago, right? We just said it here, and it should just work fine. So let's set it to seven days. And now I liked at one more thing notice that the Python script can be run like this. When I open it, it's gonna be executed. And as you can see, we have, as a result, a question like this. And why do I think it's school? I think it's cool, because now you can put a short cut on your desktop call. It's like get the most popular questions from Python from last week, and you do not need to even, you know, open your It'll you can just opened the script like this. And it's gonna open websites that follow the rules like this, this school, right? And they will be from the last week. You do not need to jump into the program every time you run. Your screaming of script is universal. Just gonna always run properly. And this is the most cool thing about program. You make everything work. We vowed you having to change things. That's going good lesson. Thank you very much. 17. EXERCISE: Cat facts: Hello. I think it's a good idea to see a few more examples off AP eyes because you know, some people might document them a bit different way. So let's focus on Cat Fox That's open cat fax. So cut Fox. We'll return. Cat fax would exactly return. Let's go toe ap I documentation. So the first thing when you open one off the website like that you want to find a P I recommendation. So let's start developing. And as you can see, they say that is your health for all endpoints. He's looking like that. What? This endpoint Well, endpoint is a place where you can retrieve data OK, and for facts, you need to goto the slash fax for users going to goto the slash users. But you need toe start from my base URL so disobeys the oil and now you need to just add thanks. And as you can see, we have good now here low the facts about cats Some of them are very interesting. Like for example, domestic are spent about 70% of the day sleeping and 15% of their day grooming. Is it true? I don't know, probably is the fact, but how can we use this, right? Do we want all fax at once? Well, let's see. What can we exactly do with factors? Go into faxing as you can see it. Has that what exactly we can dowith fax. It tells us this website what choir it parameters we can use on they think here. First thing it say that we can go in tow, random catalogue. So when we do something like random, you will notice that we'll get a rundown fact. Okay, it is more useful, right? Because you can make a website or some kind of program that we give some random fact about cat. That's better now, Hmm. It says that we can change anymore type, right? Maybe I want to some facts about dogs. Let's check if it will give us a fact about dog. It's true. It's not giving us facts about dogs. When the puppies born, he's blind if and to Fleiss. Okay, that's pretty interesting, Andi. Well, here we have also something good is called amount because you need to know that if we go toe, for example, him and now we do something like that for these lying that so we get random fact and we bring the runoff thanked him. Let's bring it from PP print import PP print around the mojo. As you can see, we have got a random fact. But what if I wanted to have more? Would I request many times for every FARC, like, for example, 10. These would take too much time. Remember, every time they do, you think that you need to get the requests. Try to find another way, because otherwise your program, we work slow. And what's more, we probably get Bond because you will just get too much off the processor off CPU The geese request right? There was some kind of limit, so don't over do it. You want to limit amount off requests as much as you can. So the guys who created this a p I have predicted it, and they gave you the amount barometer did you can use, right? So let's use it. Amount on that study to, for example, five. That way we'll get five fax instantly without having to request five times just want, and that's cool. And then we can do what we can just look like for each card in content. Do something lying, Bring cut off. And we are looking at the text here because it's the thing that is useful for now because you can see we have been about five random fox. So it was pretty easy, right? And we just need toe read what is here as an exercise. I would like you to make a menu where somebody can choose dog or cat and depending on what the user has chosen, give him cats or dogs, right? And show him five. For example, 10 facts. Random facts about Carter dogs. Okay, Does your exercise And But this is not all in his lecture, not He said we have good moral things here. We will not get in the A P key authorization right down. We'll do it later. Right now, look at things like, for example, this random cap. As you can see this things a bit easier, it just returns as a random image off cart, Right? Maybe, chief, but it returns around them. Think so? How could we use it? Mega program where you will show aim it. And below that you is show around the fact cut, right? Right now we do not know how toe show an image inside the program. God just throw away and you're all OK. There is more cool websites like, for example, she based online. Let's go down, and as you can see, it's gonna show us. Let's use these to see what's gonna show us. She based Link does the very beautiful breed, and at the time we get a random image right. It's also cool, and also we can see the query parameters that allows you to get more she base. So count, for example, tenant. We get 10 instantly, and you can show them on, for example, website or open them instantly, and our Web browser that we can do already right. 18. EXERCISE: random CAT images by breed: it's time for next exercise here. You can get pictures off cuts from Tumbler. When we go into cats, we're going toe documentation. Weaken search cuts by breath. It's a beard. A bit more specific searching, right. We don't have random cuts, which is critical because can, uh, get cut by the bridge. And I want you to read what is here. Fine. And based on that, I want you to create a program that will open free, random cut images in Web browser off specific breed that the person chooses. Okay, you can nothing choose from free. For example, I will give you some hands. Now, notice that they say that you can get Brits here like India's link you. And as you can see, they are specified here. Okay, which means And also they say that you can search by breathe this way. So when they open it in the new link and we type something like breed ideas. So it was like Brit these and then I used, for example, American Bobtail. So thank these. I will get random cat That raises somebody conducting. And here is your Oh, when I open Interested? Can't see Well, it's pretty beautiful card. Nice one. And each time I refresh, I get Rondon cotton. Of course, they can repeat some things. Andi. Well, that's how we get one single. Got The question is, how do we get more than one image at once? We don't want to get on one rind. We could make more requests. But as I said, always look for another option. Check out the bridge. A PR reference in the left panel. Also, there can be more About the only there is breadth. It's the culprits. Mm, where parameters they are not prepare, really describe. But probably because theme it is a self descriptive time is gone. Limit the amount off car that cannot be shown here, right? So if I probably do something like that and lame, it equals two free. I would get freakouts once. Let's see 012 freakouts so we can go through each of them. Get there you are well and offer them in the Web browser. But it's your job. That's a little lesson. Thank you very much. 19. Authorization - log in using API key - getting holiday calendar: Hello Today you learn with this A P I key. What? It is used for how to use it. So today you will learn how toe authorize yourself on websites. Because so far we didn't need to write. We were using things like cat fax and they didn't require you to create something. What is called a P I key? So what is a p a ki? It is a key toe. Identify you toe authorize you as a person. They need to make authorization. But why do they need to do it? There can be a few reasons they might want toe identify you because they want toe Analyze how you use They're happy they might want toe identify you because they want toe limit and only mouths request as much as they can. They might want to authorize you because they want toe See if you are you because some AP eyes allow you to look into your for example may where you have your e mais and they allow you to reply to emails. Right? So they need to know that you are you. Some people want to get some money for home you because if you want to make more requests if you are using the the A P I calmer Shelly. So your are getting my from it. Oh, then you need to pay them right? So let's choose one off, then AP ice that requires authorization. Like what? World holidays and working days calendar index that's going to you. As you can see, it allows you to get database from over 200 countries worldwide, and it's constantly being updated so you can choose Hon off So many countries here and see what holidays do they have. You don't need to be. Remember about updating. You don't need toe. Hire a person to update it right? Imagine that you have a big company and you have hired people from many places around the world and they have the holidays and you want to be nice to them and give them, for example, free time on the Holy Days. Or you want to send in a business card on the Holy Days. It's nice that the person who runs the component of things about the people, for example, right there like that and the holiday it might change. In some countries, you might not know about some holidays and they just made the work for you, right? So it's pretty useful. And but in order to use it, you are required to have a key AP I okay. And in order to get key a p I most time, you just need to sign up on the website. They will give you a link, right? For example, the sign up for pre. I could go on it and you need to just provide your email, enter your full name and choose the Passport Creek register white for email. If you don't get it, check this bomb and then you just need to log in. So you come looking signing. And now, as you can see, we have got here something or this called a p I key. Also, it says about our a p. I use it, which means how many times I have requested something from these websites. Why do they date? Show me this because it's limited in free plan. As you can see, these guys want toe and some money, and I think it's pretty OK, because they need to probably hire people who update the Colin discolored right. So they need toe Get this money back. But for private use or just for the sake of you being ableto test it, you have got some free plan where you have good, limited amount of requests. And when we goto pricing notice that you have got Haman requests 1000 permanent. So, you know, for private use you want to just find out something about the world. You can easily use it for free. And you have got won the quiz per second right here. You have only me to the requests per cycle on the middle A B I request for now we will stick to it. You have probably noticed that I have blurt house off my a p I king. Why did I do so? Because you need to remember that this a p I key identifies me as a person. If you had started using it, you could use it. No, the way that it was intended. You could, you know, spawned them. Or you could, for example, use it calmer, Shelly. Right? And I would have problems because of that. Right? So remember that this identifies you. That's why you need this key at the start, right? toe. Make sure that the person who use it is the right guy. And so do not share it with other people, right in the key that will be generated for you. Notice that they share a P I key here. But some people might have not programmed the panel like these. You might get the A p I key through the email. They were just send you eating simply and you will not have the account details or something like that and really depends on what websites you gonna use. Okay, let's use it. How to use it As always, we're gonna use for a P I documentation. So we click here and as you can see, it's pretty well documented. It says at the end point, you are all it's like day, so we need to use it like this, right? So let's copy it into here. But it says that we need a P I key which is required, so we must provide. So we need to do something like this a p I key as perimeter like this, then we need to provide it, so we need to give it from the camp panel. So from here. We need to provide it here. Okay. Like this. And now we are signed up. Look, when we run our module this but content here you will notice that there is some arrow type coal fired, which means that we didn't provide all parameters. But when we do something like that, you can see without on the A P i G authorization failed. So something was wrong here. I change it to wrong guy also. As you can see, it's also wrong. So we need to give them a P I key. Exactly Like it's like like they get generated for us. It identifies us. Then it allows us toe process something from the website. Right? And now we need to learn how to use it. As you can see, it says we are required toe up a p I parameters like a P I key, which we did. Now we need to at country and the air, so we need to collect toe veto holidays. So let's copy these. Yeah, and we need to add more parameters like, yeah, so in to choose what year we want to check XB for example, 2019 and we need toe also choose the country that we want toe check holidays for right Because there are many countries Andi, which country want to choose and how to choose it here We have supported countries on their website when I open it. As you can see, there are many countries this support on that's shoes. For example Great Brittain which is G B United Kingdom. So we need to use G B these two letters here. If you want another country like for example spank, you need toe use e s right. So we type here g b. And now we have chosen a great Britain United Kingdom. Ah, let's run our module. And as you can see, we have got off lots off on Let's people printed. As you can see in the United Kingdom, there are lots off toilets. But you need to remember that we have chosen old ties off holy days from entire year. So when we go here, we can see below that we can limit it a bit. Like so. For example, I want only holy days for this Pacific month, like for example, December so we can do something like a month 12 and we'll get on Lee. Holy days for December, as they consider, are still a lot off them. But remember that they can be local and national religious observance holidays so we can limit them even more like, for example, the national holidays. And as you can see, there are only two of them, right? There are also so premium AP I parameters and that's something you write. What does it mean? They are primo. It means that if you pay for them So you are not on the free plan you can limit what you gonna see even more. Or you can change how the result from the website like this a p I is gonna look like And this is pretty useful. That's why is probably priced. What does it do? It says that it returns the name off the holiday in the official language off the country. If available, this is default set toe English. So the result we have here is in English, right? But if you want to show toe person from, for example Poland, the description off the holiday in polish. So you want to be a bit more profit? Shona right? You don't want toe show everything English. Then you need to pay for it. And then you can set that language. For example. Uh, Poland saw pl on. Then they was the people from Poland with c their holiday description in police, right. You don't need to translate it on your own. But this is a private website. It can stop existing in, I don't know, two years time on maybe 10 years all. Maybe we will be there almost forever. Right? But if you're watching discussing it's not available Now, don't worry. Because to be honest, you are practicing right now the way AP eyes Look, everyone, one of them will be a bit different. And you will probably want to use different 80 IES. Probably you will not need to use the holiday calendar, right? You might. Something else from here and there. Interface of the website is gonna be different. And that's what we are paid for As programmers toe, you know, great things. A fee, eyes, Andi, analyze them, and then used them. Right? There are gonna be similarities. That's why I want to show you a few of them. That way you will be able to process any off them right. You will be used to how the a p I look and how toe use thumb that's holding the lesson. Thank you very much. 20. Authorization through header - settings: Hello Today I will teach you what are headers and how to use them. In order to look in into one off the public A B I will use the carts 80 high from here. And you will notice that when we go toe this top it says to us that the best and most secure way is to send the head. What is Heather? Heather is additional information that you can add toe the request and it this safer to send requests through hell there because the values like these are not visible toe everyone because they're not sent in the URL, right? That's why it's best to use this mattered if it's possible to use if they allow hey eso in order to use it first. Well, we need toe generate the A p I key Andi, in order to do is they say that you need to go here, then you need to sign up for free type. The my made the application description Say something about your application. Would we do your cats, for example? You want to make a library off favorite Scots and tell them you want to do hint and just click on sign up. After that, you receive an email, remember? Two checks pommy if you haven't. And there you will get the A P I key and user I d did. You should say for yourself, right? Okay. If you have it, then it's time to log in. So we have got here a request to favorites netted. So we want to get favorites from here. As you can see, it says that a P I key is required to do it. Let's check it. When we're on our module, you notice that they say, Hey, there is a narrow you need to send your a P I key they needed so they can identify your favorites cuts right, because there can be many users on his website. So in order to do this, we need to send a header. How? Toe send a header. You need to send here as another argument named Argument that this called Heather's. Now you type that equals sign and you send the key value off the heather that you want to send. And as you can see it say that our header need still be used to look like this X a p I okay . And now I will provide the A p I key that I got from email. So I pays it here, and we need to send it somehow. Here. We can just do it. These rank. But also, we can create a special variable that we hold. All our keys came so like this. And now we can do something. These others Heather's around the module. Oh, of course. Look like this because justice one single. Hello? Yeah, Indians mothers in fightin. So, like this around the module does you can see we are reluctant because we have an empty leased here. And empty leased means that we haven't added an IQ heart toe our favorite least yet. So everything is fine. We look in, I do. If we change our key like this, then it was tell us that Hey, there is no valid X a p I key in header. There is no the proper king going to change it back. And then we are locked in. So now we know how to look in using headers. We just need to send a named argument and then here type the value for you might be wondering why this name is the same as here. How our interpreter knows which is which. Well, the left side off the parameter is just known as the name argument, right? He just noted, on the left side you're naming argument here. You're sending value for the argument on the right side, and he knows that he should take it from here. Okay, So don't worry. These names are the same. And I don't think that you should invent your no only things like others A B. I like something like that. That right? Because maybe you can. You could add more keys value. Here's right. You can add more off them, so just use the same thing I had. Theirs equals others. Everything works fine. So we are sending headers and eight works. Fine. However, I want you to improve this a bit. Let's create a fight inside our folder here. So let's company fighting five. And let's create fight. It will calls, thinks okay on we open it like this, we cannot remove everything but our key. So we live the key here on and we'll create a variable like that ap king and now will import some things. And here instead of writing in like, days we conduct Does something like that settings dot ap Okay. And now when we run our module, you notice it works fine. Right? But we have AP. I keep from the settings from 15 where all settings are start. They're doing that lesson. Thank you very much. 21. EXERCISE: CAT system - introduction: at all in this lesson, the you learn how to make an application that we show the user the favorite cuts. You will learn how to look for random cuts and prepare the baby for further steps that I will talk about suit. We'll use their code from the last lecture where we looked in using Heather on the cut AP High website. And now I would tell you about assumptions for our application. We are going to imagine that the user is logging in in our program so high provide the logging and passport next. Imagining that below, we are checking whether they're looking and password are correct. Now we are assuming that logging was completed correctly, right? So the user has locked in because he gave the proper logging passport. And then we retrieve from database properties like User I. D and User Dame for further reference so we can reference the guy that has just looked in. We're not doing these instructions because we don't have data base. Just imagine that they are here. And we just dug in a guy with the user. I d like that age g age to, um, for example, whatever you can write to hear whatever you want is just the user I d that can identify this specific user and user name, which is, for example, are cartoonish, right? So, again, user I d allows us to identify a specific user if somebody provide different Logan different passport thes user I d is gonna be different because this is gonna identify the different user, right? So we want to make a program that will show what are favorite cuts off the user that has just locked in with show memorandum, kittens and script will ask if the user wants to out the kid into the favorite list. Eso we need to make also it possible, Toe added. So we need to make an ability and function to add new cuts. And also we'll make a function that we allow him to remove the kitty from favorites. In this lecture, we focus on showing him things in getting things from server. So this is like an exercise. In next lectures, we will talk about how toe post some things, how to send something to server to the FBI. Because, yes, we can do it. And we also can delete from there However, right now we need to make the foundation for our program. So let's bring in something like hi, user name. So he knows that he's in our program. And now we want to get the favorite cuts. So get favorite cuts. We create a function like that, they can't get favorite cuts and we will save the result. Teoh favorite cuts like that. We need to tell from whom do we want to get the cats? Right. So we sent User I d here to the Kate Favorite cuts function that we haven't created it and then we will print. Your favorite carts are and we just provided like this. So now we need toe. Define these functions. God favorite facts going to define it. So define, get favorite cuts We get here user idea and we want to return here. Favorite cuts. As you can see, we did it already right here. So we gonna use this request? However, we need to add it a bit. Why not? Is that when we goto favorites and we get to the get your favorite? It says that it gets the favorites belonging to York account whose account the person who created the A P I key, which is us, but we want to create an application where there will be many users with different set list off favorite cuts, sewing toe. I don't define themselves, and you can see that there is an option toe. Filter them by sub i d. In our case, Obaidi is really the user. I d right, and we want to send it as sub i d. As a request parameter. So we need to create here parameters object like that's the dictionary and we will use the fact that we can do something like sap I d. And we can set it to user i d the descent here and we can then send it at as parameters. That way we can now return the response on and but the responses skill, right? So we need to somehow use disc out here like this. And then, for example, we could do it. Mike, this ah thought like these things did there. But the problem is that we will need to repeat, try, except else. Every time, every time when we make a function like get favorite cards like, for example, other a new cut toe favorite ways to remove the favorite card. We would need toe make the same called lying This. That's why we create another function in order to know, repeat ourselves like, get Jason content from response. And we sent here a response. So now we'll just do something like dicks. So we're gonna return the content, but change our into response like this, and now we can just return like this. So we are jumping from here to here, and we're sending them a response from here. Right then we are changing the response into the content. That is Jason. We're off course trying to do it. If something goes wrong, we say you're something in Polish. Improper for months. Otherwise we returned. The content here on the continent is again returned to the place where the function good favorite carts is invoked. So in our case is it's here on that way. We go now. See favorite cuts. As you can see, your favorite cuts are empty lease. Everything is fine because you haven't added any carded, right? But in future here, we're gonna see favor Kratz off the user that identifies himself as this idea. Now it's timeto draw a random cuts. So let's do something like that. Random card equals to get random cat, right? And that's created function. Get random cut. We do not need to create parameters because it just gonna return a random card. Not is that Now we can make a request and use this function from here so we don't need to repeat ourselves, right, Andi? Well, do We don't need parameters right now because we just get around on Cat. But how do we get around uncut? It says that we need to load. Think, and we'll get that Jason response by default is it's giving us single cut. That is random. Okay? And we return it here. So, random card, it is already here. We can copy this line of coat here. Um, so your favorite cards now, uh, you can right here. Drone keeping on. And it's here, right in the random cat. As you can see now we have around on kidding him when he ran the module. A gang. The random kitchen will be different because the idea is different. You are always different. And, well, we could show him this brand, um, kitten Right on for example, the Ural imagine that we have here a graphical user interface where we show the guy at this image. You know that to get this the image waiting to get into these you are. And we can get there in two ways. Not is that this is a list off single dictionary, right? There could be more of them if we had sent a parameter here that would allow us to search for more guys for more carted ones. But we didn't do it this way. We just good one cup. So we know that at the position zero off this list, there is the result that we're looking for so we can type sound like random cut of zero. Not this. Now that when you run our module, we are having here just a single dictionary, right? They wanted we want. And now we can get into u R l. And this way we have you are all off drone kitten. And you know, we can copy days into the harbor, brother, we can open in the instance in the Web browser and where we should ask the person if he wants to add these newly drone kitten into his favorite lease. But we'll do it in the next lecture. I will show you one more way to get that first alignment from the positive from the first position off. This not is that it's not cool that we need to remember here as the users of this function that we need to access the first element right? We can. But we could do something like this. We are returning the content and we're returning the first element here instantly. And then we can just do it like this. So, as you can see, this is, I think, a better solution because functions should be created this way. So we don't need to look into them in order to know how do they work? It's imagine for a second that you totally don't know how this function is created. Right on. Did you involve Get random? Can't. What do you expect as a result, to expect that you need toe get into random cart off the first element in order to get this random card. But probably not right. If dysfunction was called, get random cards. So this name was suggested that you're gonna get a list off murder cart, then Yeah, you would expect that You need to go through some list. But this way you're expecting by just looking at the name that you gona have just one single cut. So going through, at least it's something unexpected. And this is not a good way to solve this problem. This is the better solution when we do it like this. So I would remind you hear that this thing is gonna be, you know, here. We gonna have a list, right? List off cats. Right? And we get the first element, which is a dictionary in our case, is this part and we return it here to this place. And this is a run a random card right now, which is presented by dictionary. Okay, there's only lesson. Thank you, very 22. POST - adding a cat to favourite list: Hello. It's time to add the kitten that has been just drone using the post method from requests before that when it's to ask our user. So we've used import instead of print and asking, Do you want to add Eat to your favorite list and he can answer yes or no like this. And we need to save it to some variable like, for example, at two favorite, uh, response like that. And we need to check the response. Right? So if after favorite response is equal to yes, then we'll add it. So we involved a function like, for example, at favorite cut, and we send er the user i d. So we can identify the list off these user right lighter and with Send their around Don't cut I d. So random cat off the I d on the car, off the cup. Right. So it looks like like this Not instead in the random cat has I d right. So we're gonna send their i d off cat. This is idea off category that this cut is included. Mm. We are talking about deciding right. Is this card specific cut I did that can identify on what we can print the result off the out Favorite cut. So we know what happened if it happened or not not. Is that we're asking. Do you want to add it to your favorite beast? And we ask for the upper case off the letter. Why our is someone use small case on this condition will not be met, right? That's why we go now. Use the upper matted on the response which will increase small letter to the upper case letter, right, That I think it's a bit more professional because this is just some data validation. We're not focusing on on the stopping in the structure. Uh, however, I would I wanted to just show the practical example of the methods that you have learned long time ago. So we are sending into the function like this, but we need to define a function like that. So, art, favorite card use writing the card idly and now what? We need to make a requires like we always didn't right. So we got no make a request. But this time this request is gonna be called post. Not is that when we goto favorites here safe and an image as favorite, we have a post name here. Post means that you're just posting something right, sending to the server something on the order to use post adjustment to type your post. It's the difference, however. We need to send something there right and how to send it. But we can create a variable like, for example, cat card data. You can call it whatever you want. It doesn't make it a dictionary. And there it says that we should provide image. Idea writes. We need toe tell which image we're adding toe favorite list, and in our case, we send it here right the random cuts idea so we can just do stuff like hot idea and this is enough. But it says that you can also provide the sub i D. And in our case, we want to provide it. Otherwise, every cut that would be added would belong. Toe our A council to the A P. I kidded we have created. So let's set this toe user Heidi and now we need to send it. How do we send it? We do not use the parameters argument here. We use the Jason argument, and we do something angry. Why is that so? Because this a PR expects that the data will be in Jason, former and these everything that is gonna be assigned toe the Jason named argument is gonna be changed into Jason, if possible. S so this is the best way to do it. We just right. Jason equals cab data. And now we change this into Jason automatically without worrying how to do it, which is gonna happen here. And well, that's all right. We've just How did something there, if this is gonna be involved? Well, otherwise, if he doesn't want to Ah, ah, the kitten. We need to say something. For example, why you're brutal being the card will be sat right now. I think he's kind of brutal wine. Know that Cut to his favorite list, which is looking at him like, for example, this one here They wanted to add it to your favorite leads. We will not be brutal being Yes, we want to add it. And it's as to our success. We just added a new guy with I d like this. And now when you don't know what a module we not is that we have in our favorite card list And you favorite cut with Heidi. I d like this here is a user I d that I got from from the a p I cart website. Here is an image I d that we have added before. And here's the somebody off the user that is right now locked in right. And he's here is you are well that you can access the drone kitten that was here so we can show all favorite cuts in In where? In here. Right. To be honest, it's up to you how you will four muddied right now. We are, you know? Sure. Struggling the list off. Favorite cuts in that are looking like this. You can show on the your wiles if you want to. You can open them in website if you want toe. Right now, this is the only options that you know. But in future, maybe you can show them on your website or you can ah, throat in the graphical user in for interface. It's all up to you when you click. No, why you brittle being the card will be stuff. Okay, This is all in this lecture. In the next lecture, I will tell you how to deal it Cuts from the favorite list 23. DELETE - removing a cat from favourite list: Hello. It's time to delete one off the cards that are in our favorite least as you have noticed. I've added one more cat How to do it. Well, let's go here. And as you can see, there is a matter that is called the need, which, as the name suggests, deal. It's removes the favorite cup in somebody's list. In order to do it, you just need to send favorite I d. After the favorites mattered after the euro. So here, exactly in this place. So let's create a function that we will call, for example, remove favorite cat and we'll send the favorite cat I d that we want to remove, right? We need toe Exactly. Tell Hey, I want this guy. Hold this guy and we need to provide it in the euro. Let's copy this from here because I don't like writing code just for the sake of writing it . And we are not sending Jason now. We're not posting, gonna think we're deleting, so we'd gonna just change post in tow. Dill it. We still gonna get a response from response. We'll get information. The thing was deleted or not, we can use this response in order to tell the user if something happened properly or not. But you would do it on your own as a practice in future. However, right now we need to remove a cut and they think we need to do together. Otherwise we could go crazy. Come on. Removing the cat alone. We need to greet. Do it together. Okay, so, uh, how to do it? We need to add the favorite card. I d here. How to do it. Just like this little. So the favorite card I d. Is, um, just here. We just added it, and everything is gonna work. Fine. We need to Now just ask for this idea until let's ask the user So you don't have a heart like this and tell him provide I d off cards to deliver this. So we are asking for the idea on going to save itself. I d cut to remove and we need toe invoke our function like this. We can print the result like this. Andi, remember that import is returning string, so I d card to remove Israeli s drink and we need a string in order to committee connect it with the plus, So we don't need to make any casting here. So everything we work fine. However, how does the user we know which cut he wants to remove? We should show him he could look up here. However, this information is not process where? Right here. So he could make a mistake. We should just show him idea of carts and also next to it. The u R l. So he knows which cut he wants to dilate, unfortunately, and then he can decide so how to do it? Well, we can process. They think here, right, Because when they access it, we can still access it and then miss many times as you want you want. We don't need to make again a request for all favorite cars. We just need to access the favorite cuts variable. Right the least in our case. So we need to transform it. Do you remember where we were talking about transforming about, changing the way data looks? There was a section about this and we were talking about least expression. Yes. Finally we gonna use the knowledge from these lectures on practical example in our situation, we want to do something with these dictionaries that are inside a list. Right? So we need to and we need toe show at the end. The user i d. That is connected toe you are else. So we could make a dictionary like this so we could make a dictionary expression. And we got other stuff like that for each cart inside favorite cards. So, inside the card, there were before the first occurrence, this one and then this one. And now we need to say what we want to do with it. We want to create a king that is gonna be looking like this. Cut off id'ing and value will look like this. Cut off you are. Is it proper? No, it's not. Why? Because notice that when we look here image he's really another dictionary, which has girl inside. So we need to first get into image and then into Europe. And this way, we have got now a favorite card by a D. So we can say favorite cuts by idea. And while we can print it and now motives that we run our module, um on we saw? No, we don't want to remove a cut, As you can see, we have. Good. Now I d That is here. And you are well off the cut that this identified in this Placed under this idea. That's great, right? And now the person is asked, So you don't have a hard provide i d off cut to delete. Of course, he could make a mistake, and then we're not gonna remove it. We have got here in value data and you know, we could Teoh make some data validation you can do it is the practice. However, we we provide the proper one. So we want to delete, for example, this guy that scope it so I don't make a mistake and bomb message success so we can use the success here on so we can check ive. The message is equal to success. It means that the card was removed and tell that boss start brutal person that he removed him. Oh, as practice And what we can check it right now. Right now when we're on our module. You were not Is that there is only one kitten right now. We will not at any kitten and we'll remove the last kitten from our list. And now the list is empty. So has considered. It's pretty cool because we can use external server to not only getting toe retreat things from it, but we can use it. The sent information to delete information from it, you might be wondering. Okay, all right. But you're using it on example off carts. Really, How us police that it's best to lend on things that are easy to imagine. Believe me. So what could be a bit more practical? Example? Well, imagining that you can use it to post, so to send inmates who can do it in Mays did it this palm from your e mail? Because one off your email provider allows you to use a P. I like this. Maybe he allows you to do check it CVS. Then you can do it. There is still one arrow in our program that is that if you add a new cat, the cut will not be shown here in our favorite cuts. By idea why? Because we are using the favorite cards from get favorite carts method and we. But we added the favorite card after right? Not before, which means that after adding a new cat, we should update our favorite cards least So we should, you know, do something like favorite carts and get again all the cuts that are on our server. And, well, this will work fine. Not astounded when we say no. And we're on our module and let's out the newly drawn kitten here. Not is it. Now we have what to cut that we can remove also, the one that you have just added right, So we can remove one of it. But is a good idea to make a request for something like this. Is it really the best thing that we could dio taking into consideration that requests are slow? Notice that we have a random cut that is still available. Look, random cat still available so we can easily get into random card of your well, right. This is an information that we need it and also notice that here, after adding properly we go the message success and I d off the newly added cut to the favorite lift so we could get it from here. And just a sign this king this and then we could save it here. Maybe not in the same variable we could call it differently and later, Adi, that the end after transforming this list or here in design. So let's do it. Let's make our coat better by better I mean faster. And this is really something that optimizes our program. Because, as I said earlier, again, requests are very slow. You need to somehow jump from your computer to the server that is far, far away from your computer. So this takes time running through Luke 1000 times 2000 times will be probably faster than this, Right? So take in mind that this kind off the optimization is really important. Okay, so we need rundown card I d. So let's could create newly added cut dictionary, and we'll do something like that. Random card I d. And we we don't not do it like the things we need to save the result from our dink favorite card. And notice that this is underworld under idea. Right? So we should, um we don't need I d here. We need I apologize. We need result from the favorite cut, and we need i d from it. But from random Kat, we won't just, um Europe. It is. Okay, lets right. Check if the newly added card will be here. So let's run our model on. Come on, the parentis and well, let's are. Then you cut and what we don't remove on the bottom. And as you can see, the Ural Off drone kitten is here, and it is a new I d for this John kitten, but we need to know added after transforming to the list. So we need toe somehow at a new element here. And now imagine that you don't remember how to do it. So you go here on, do you write something? Ideas, Dictionary are think new elements vital. And you click, for example, pytorch ab newly item to the dictionary. And you They tell you that in order to other new item, you can use the update matted. Also, I just use the update. Pettitte and I send their the newly added cut dictionary on. So this way, when we run that were module you were not is that we have got here now. One guy, one cut to cuts and we have another one. And as you can see now we have 12 free. Great, isn't it? And we didn't need to make a request to the server. So we really optimize our code. We made it better. Please note is again that I didn't use just the update function here. To be honest, I forgot how it was called. And I decided you to show how a programmer works in real life. We don't remember all methods. Come on. It would be just too hard notice that I know many programming languages and every mattered in every programming language. Even if they do similar things, they can be called differently. How to remember all of these? How fast could I find the result like this? I just type a few words in Google and I got the result. So this is a very important skill. If you don't know how to do something like this, just have it into Google and probably somebody already on stock overflow. I had a question similar and the question with the answer there properly, you can just use the answer like I did and we did it right. They I told you about the update mated method in this course. However, I still forgot about it, but I knew that it was possible, right? I knew that it was possible to have data dictionary like this. So this is important to know about possibility. But it's not important for you, Toe, remember everything by heart. Because if you forget something, really, it takes like 10 seconds to find out how to do it properly. The don't listen. Thank you very much.