AI on the Jetson Nano LESSON 45: Understanding Python Functions, Classes, Methods and Threading

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello guys this is Paul McWhorter with top tech boy comm and we are here today with lesson number 45 in our incredible tutorial series where you are learning artificial intelligence on the Jetson nano I am going to need you to pour yourself a nice enormous mug of iced coffee and if you like you may also have a nice cup of hot coffee and I need you to get ready to learn some cool news stuff so let's go ahead and get out our Jetson nano gear and as you're getting your gear out as always I want to give a shout out to you guys who are helping me out over at patreon your help and encouragement is keeping these great lessons coming you guys that are not helping out yet look down in the description below there is a link over to my patreon account think about hopping on over there and hooking a brother up but enough of this shameless self-promotion let's jump in and get ready to learn some cool new stuff so what are we going to learn today well let's think about what we've learned so far in this series of lessons we have been learning about artificial intelligence on the Jetson Nano and more specifically what we have been looking at recently is doing a facial recognition now facial recognition is not just where you find the faces in the picture but you figure out who's name whose face is in the Box you find the faces and then you identify who they are now what is finally starting to happen is what's finally starting to happen is we really need to sharpen our Python coding skills now up until this point I write Python code in a very logical linear way and the reason I do that is because it makes it a lot easier for you to understand like let's say we have a task in that task has ten different things that you need to do so you have kind of like line of code one line of code two on down to ten and then they all stack up to do the function that to do the the thing that you want and so that's kind of very logical it's easy to understand it's easy to learn it's easy to follow it's easy to do things on your own but what finally happens is there are just certain things you can't do using that simple easy to understand logical program flow and so like as an example I think in the last lesson where we ended up we were launching two different cameras and when we launched those two different cameras they weren't in sync one was about a second lag but behind the other one and we can really see that if we're gonna move forward we're gonna have to write better code so that our program is going to perform the way that we want it to so this is a good time to pause and we are gonna learn four things today and what I'm going to do is I'm going to take the mystery out of it and I'm going to show you step by step and you're actually gonna understand it but what we're gonna learn today is we're gonna learn functions we're gonna learn classes we're gonna learn methods and we're gonna learn threading okay now there's two reasons that we're going to do this one reason is we can start writing more sophisticated code that reaches the performance goals that we have for our program and number two now when you look at other people's Python code more sophisticated programmers who actually know what they're doing as opposed to me who doesn't really know what I'm doing when we look at more sophisticated programmers we can look at their code and we can understand what it is because I don't know if you've ever just popped open someone's Python code and it starts having underscore an it underscore self this self dot that and just whoa it's like what on earth is this crazy code doing that is classes and what I'm going to do today I'm gonna explain to you classes and methods where you're going to understand them you're gonna be able to write your own programs with classes and methods and when you look at the work that these smart boys are doing you're gonna understand what they're doing so I think it's time that we take a lesson and sharpen our Python skills so I need to get out of your way and I need you to come over here and open up a fresh new visual studio code I believe that we are working in face recognizer folders so we need to click on that and we need to add a new program and it looked like the last one I did was fake face recognizer seven that would make this program face recognize or eight eight and then I think I'll call it a py th just because we're learning a little bit about Python pi you guys know that that dot pi is indeed important we're going to get this out of the way okay I'm going to start really simple and then I'll take you through as quick as I can but making sure you understand it functions classes methods and threading you probably already understand functions we probably already work with those a little bit but let's just start and say that we have a bunch of rectangles and we want to calculate the area of the rectangle well what I could say is like the length of rectangle one is three the length or the width of rectangle one is equal to five again I don't have coronavirus it's those darn cleaning ladies in their bleach their bleaching everything and it is making me queasy okay so now what I could say is area 1 is equal to length 1 times width 1 and then I can print area 1 and let's say we had a bunch of different ones just for illustration I will do two so I'm going to take all that copy and then I'm going to come here and I'm going to paste it and so then let's say that this one was six and four and this becomes length to width to area to length to width two and then print area two and let's make it just a little nicer okay even this is a simple demo program we just can say area of rectangle one okay and then we'll make it a little easier by copying that control see I really hope that I can write eight lines of code this early in the morning without making a mistake but we will see ctrl V and then don't forget the comma so let's just see ooh how do you like that how could there be a mistake on line one do you guys see my mistake I don't think I have a mistake in line one I think this is just confused so I'm going to save this sometimes this visual studio gets confused and you've just gotta kill it and start it again okay here we are run python file in terminal okay now that's a real error there I didn't close I didn't put a comma there all right this should run hopefully you guys caught that okay area of rectangle one is 15 area of rectangle two oh I put the comma in the wrong spot that's what it was area of rectangle 2 is 24 alright so hopefully that makes perfect sense and let me get this up here where you can see everything okay so hopefully that makes perfect sense but you can see it gets tedious if you keep doing the same calculation over and over and so let's write a function okay and let's call this function rectangular okay now you could call it area but you'll see in a minute why I didn't call it rectangle area because I'm going to add a few more things to the function later and so I need to pass in parameters what do I need well I need the width of the rectangle and the length of the rectangle and so what you can see is you define a function with the key EEF you put a space and then the name of the function and then all of the lines of code underneath it that are indented those are going to be the steps of that function so what am I going to say well I'm going to say area is equal to W times L and then the next step is print area okay and so now the way I would do it I would come down here well I've defined length 1 and width 1 but now what I would do is I would call the function rectangular calculate okay I got to pass it what W 1 and L 1 okay and then I'm gonna call it again here a rectangle calculate and then I am going to call it W 2 and L 2 like that okay so then when it sees this line of code it's going to hop up here and do this and then it's going to come back to this step and then it's going to hop up here and do this so you see I can put my calculation up here and then the code is a lot clearer I define the parameters and then I do the function I define the parameters and I do the function so let's see if this works oh and let's say rectangle area is okay area and why did it not like that that sure looks 400 no comma me and I am really messing up my commas huh run Python file in terminal okay rectangle area is 15 in then rectangle area is 24 now what you see is is that this simplest function I've kind of got it defined one way I send the stuff up to the function and then the function does everything and there's nothing that comes back well really what you really want to do is you don't want to do everything in the function you want to do a calculation then you want to pass something back so really what we would want to is we would want to come down here and we would want to do the printing down here and I'll go back and again call this rectangle area rectangle one area and then I am going to also put it here rectangle to area all right now what I want you to see is I need to get this parameter area back down here to the main program and I don't want to be printing up here in the function so what do I need I need to return what I calculate so I'm going to return return what area and so the program now has a value area and it's going to jump back down here so in effect the way you should look at this is that function is passing the ball back to the main program in that ball is area well if the functions going to pass it the program had better wat catch it so it's going to return it and so I better put it somewhere so I'm gonna say area one equals so this rectangular calculate is going to come back with a value it's going to put it in area one and then I'm going to print area one and then here I'm going to say area 2 is equal to call the function now I will print area two alright so here I've got rectangle one area is 15 rectangle 2 area is 24 hopefully this makes perfect sense so what I want you to see is you pass the parameters up here inside the parentheses you pass the parameters to the function and then you return your values with the return statement now let's say I could also calculate here perimeter so I could say perimeter is equal to 2 times W 2 times W plus 2 times length and then what I would want to do is I would want to return perimeter and area now the thing that you have to see is you can't do two return statements because when it hits return it leaves the function so what you've got to do is you've got to stack things up inside of you know it with the same return statement so I'm going to return area I'm going to return perimeter now if I am returning area and perimeter what I can also do is I need to catch both of them so area 1 comma perimeter 1 so you see I've got two things on the left of the equal sign which we really haven't done in the past but it's going to calculate area and it's going to calculate perimeter if I'm throwing two balls I better catch two balls and the same thing here area two comma perimeter two and then what I could do is I could just print area 1 comma perimeter 1 and then I could print area 2 comma perimeter 2 and then rectangle 1 results just results and rectangle 1 results okay so let's run this and see if it's going to work okay so the rectangle results are 15 and 16 that would be the area and that would be the perimeter and for the second one the area is 20 and the permit when T 4 and the perimeter is 20 does that make sense you see now I guess what I could do is I could also you know just make more and more things now you could have if statements in here where this rectangle calculate did one thing or another based on an additional parameter that you pass so there's all kinds of different things but once you hit the return you're going to leave the function and go back to the main thing and if you throw a ball you got to catch it so hopefully this makes sense this is like a really really simple example but it's very powerful now if we look at the code it makes sense it's like okay I'm calculating the area and the parameter using this function I pass it these two parameters and it returns to me these two parameters and so the code starts getting a lot simpler and you can imagine as your calculations get complicated it's much easier to just have a function you call that's name something intuitive so functions are very very powerful and they're really easy to understand okay but what we need to do now is we need to go to the next thing which is more complicated but I'm going to explain it to you where you will really understand it so I'm going to say we need to come back over here and we're going to create our new program we will stay in we will stay in face recognizer we will add a new file and this will be face recognizer and this will be nine and this will be classes dot pi ok so we're going to learn how to do a class all right now let me just keep thinking about the rectangle you know kind of think of the rectangle you know as a box or something like that and what you can see is is that down in the main program like if I have rectangle one I'm having to keep track of the parameters like its length and its width and then I run up to the function I do a calculation and I come back but the real keeping track of things the real bookkeeping is being done down in the main program well if I've got 20 rectangles that I'm trying to keep up with that gets really confusing because maybe I haven't fooled with rectangle one in a long time and I've even forgotten what rectangle one was doing so instead of doing functions which do a calculation what we want to do is classes and a clasp will keep track of both functions like operations and data or parameters so think of a class as a kit that you have that will keep track of both the date of both the parameters of your rectangle and the functions okay and so let me just see if I can do like the simplest thing possible I'm going to define a class and the class is going to be right rectangle and notice that for what we need to do you really are not passing anything to the class I'm just defining this class that's going to be called a rectangle and now what I can do down here I can create a rectangle like I could say rectangle 1 is equal to what a rectangle okay so now I am calling that rectangle class and I am creating an object you see this wrecked one it's not a variable it's not a parameter it is an object a rectangle now up here in this rectangle class I'm going to describe and I'm going to keep track of all the things associated with my object my object is a rectangle well your object might be a car what would things that you would want to keep track of with you know your car class well maybe the color of the car maybe the number of seats maybe you could calculate miles per gallon but it would be a bunch of functions and it would be a bunch of parameters associated with a particular car then if I had three cars I could create three car objects car one car two and car three and then that object will keep track of everything associated with that car so so far I am just creating rectangle an object rectangle so wrecked one is going to create a rectangle it's going to be called wreck one and then it's going to be defined up here but when you create the object one time it is going to do the anit function so you know I'm only going to create rectangle one one time and when I create rectangle one what it is going to do is it is going to run the init function so I have to define it define underscore underscore init underscore underscore okay now what you're going to have is self and then what might I pass to it well I might pass color I might pass width and I might pass length okay so when I call rectangle now what I need to do is I need to pass it what color well let's just say I don't have to use the same variable but like c1 and then I have to pass it ww1 and then I gotta pass it l1 alright so when you create the object wrecked one it comes up here and it does the init function now another thing that you're going to learn when when you put a function inside of a class you don't call it a function you call it a method okay so if you hear people talk about methods it's real simple it's just a function you already understand functions it's just a function that's inside of a class okay so what are we going to do now we've got to understand like I passed it C 1 W 1 and L 1 and that's gonna be color W and L well what is self self is the name of the object so if I create this object wrecked 1 is equal to rectangle then when it comes up here itself is what itself is wrecked one so just when you see self what you know is is that that's going to be wrecked one well if I create wreck - then self is going to be wrecked - if I create wrecked three then self is going to be wrecked three so now I can create unique variables for rectangle one rectangle two rectangle three by calling them self dot one thing self dot something else you see so it's a way now that I could have many objects that are using and created creating in this in this class well now I have I got to put the colon here now what I've got to do is I've got to set up variables for this class to use well self dot width is equal to what width and then self dot height is equal to I messed up what did I pass it I passed it W okay and so self dot width is W self dot height is H or is why did I call that width and length okay let's call it width and link to keep it the same length is equal to L all right does that make sense okay and now what I can also say is self dot color is equal to C all right so what you got to do in your initialization you've got to take those parameters that are passed in and you've got to put them into self dot variables that can then be used in the rest of your function so in the rest of my it there in the rest of my class I wouldn't want to use color W or L I would want to use self dot with self dot length self dot color and that way you keep track of the difference between rectangle one rectangle two and rectangle three so let's come down here and we haven't really done anything but when we say rekt one we create the object rect one with the class rectangle one it's going to come up here to rectangle and then it is going to run this so now look what I could do and I guess I got to put it some variables okay so I could say that C 1 is equal to red and then I could say W 1 is equal to 3 and then I could say L 1 is equal to 4 and then what I could come down here and I could say that print and I could say your or what I could say is rectangle 1 is and then what I could say is self dot color okay why can I say self dot color because it ran it up here and it put something here but now down here it doesn't know what self is what do I know it's wrecked one dot color okay okay and then with length comma and what would I do rectangle 1 dot length wait good okay so now let's run that name see is not defined oh here I put let me make this see here okay so c1 c1 which is read passes into C and then it becomes self dot color well self is wrecked one so if I want to access it down here down here it no longer knows what self is I call it wrecked one dot color and it would sure be easier if I mean it like that wouldn't it okay so let's try that okay rectangle one is red with length four boom you see that but down here it doesn't know what self is you use your object name rect one okay you use your object name rekt one what what else could I do I could say self dot area is equal to what self dot with comma itself or times self dot length okay and then here what I could do is I could say with area B right two dot area okay with area twelve you see how that works all right now why do we do this self business instead of just saying CW n L will imagine that I created four objects then the CW and L would be for whatever the last object was that I called but you see like imagine I play with rectangle one and then create rectangle one play with it create rectangle to play with it and then I want to go back to rectangle one well if I wasn't doing this kind of self way of doing things it would keep overriding the data but the self sort of keeps track of w-want width in length of rectangle one width and length of rectangle two so I can create a lot of objects I can play with them in random order and it always keeps track of what is what alright but really you don't want to do everything in the anit function you want to create different functions inside of the class and what do we call those what do we call them we call them clatter or we call them methods so instead of calling them functions we're gonna call them methods and so what would be one method well I could have define and what I could say is I am going to have defined area and then what is area need while area needs self all right now these things that I've created they're gonna pass on down so it's gonna know down here inside of area it's gonna know self dot with it's gonna know no self dot length and self dot color all I've got a pass to it is self all right now what do I do well I can say area equal no no why can I not call it area because then it can't keep track of the area of rect one versus the area of row two so I need to make this what self dot area and that's going to be equal to self dot width times self dot link okay and then you know what I could do I could just leave it like that all right and let's just leave it like that and let's see what would happen okay so what I would do is I would create the rectangle okay I create the rectangle object all right now let's see if I just came down and said print rectangle one dot area alright let's say I print rectangle one dot area okay I get an error okay I get an error why do I get an error well when I call rectangle and create the object wrecked one it comes up to rectangle and it does what it does this so now when I try to print rectangle one area that has never been calculated okay so what I would need to do is after creating the object then I would need to call what rectangle one dot area like that okay and then what I could do let's see what just happens that's gonna call that and then it should know what it is so I've got to call this method CC I can call a method inside of a class by just telling it which object rect one's already been calculated now am I having to pass it the parameters no it already knows the parameters okay so look at that twelve now do you see the power of this when I called rect area I did not have to repass it the parameters because it kept track of it with this self way of doing things okay so wrecked one then called this and then I know what wrecked one dot area is now really that's not a very good way of doing things you really don't want to start referencing variables that are going on up here a better way of doing this would be okay like if I call area what should I do I should return what self dot area like that okay and now when I call it what I need to say is like my or I could say area rekt rekt 1 is equal to rekt 1 area now it's going to call rekt one area and then it's going to return self dot area and self dot area is going to be put in here ok so now I would print that like that ok so now let's run this ok wreck oh I misspelled it here ok wrecked one area wrecked 1 and this should be area wrecked 1 run Python file and terminal area wrecked 1 what is this area wrecked 1 upper case versus Lord case that was starting to scare me because I'm sure this works and I was starting to think do I know what's going on 12 okay CeCe when I create the object I pass it C 1 W 1 and L 1 this remembers that rekt 1 width and rect one length and rect one color okay so let's try this let's create a second one okay a second object okay so now what I'm going to have is I'm going to have a second color blue I'm going to have a second width a second length change these two two okay so I create my first rectangle and then I create my second rectangle and then I have all these second parameters all right so let's try this run Python file in terminal okay I get 12 and 12 because they were the same let's make this seven and 3.2 I wonder if that's gonna work seven and 3.2 let's see okay yeah yeah that worked okay so you see that is working but now what I want you to see is I played around with blue but I can go back to red now and let's just watch this watch this what I could say is print and I could say rectangle rectangle one is and then what could I call it I could call it rectangle 1 dot color so even though I've been playing around with this function with a new rectangle if I do this rectangle 1 is red you see because I have this is associated with that self alright so when I create an object wrecked one self becomes wrecked 1 okay and then self dot width is the same as saying rekt' 1 dot width self dot length is wrecked 1 dot link and then self dot color is wrecked one dot color then if I create the second object wreck 2 then self becomes wrecked two dot width well the good news is wrecked one dot width is still the same value because then this is like a different variable now I could do other things okay I could say I could say define perimeter okay and define perimeter all I need is self okay and then what I'm going to do is I am going to say self dot perimeter is equal to two times self dot width plus two times self dot length length I all right and so maybe I'm just interested in the perimeter of rectangle one well then what I could do down here is I could say perimeter 1 is equal to rect dot or rect what rekt one dot and what method do I want to call perimeter okay perimeter and now I don't have to pass it anything because it already knows the color the width and the length okay and then what I'm gonna do rectangle one is rectangle one has perimeter and I need to yeah that's going to return right did I return it I did not return it so I need to say return self dot perimeter okay and then down here pair one catches that self dot perimeter all right and then what I could just do is I could just say perimeter perimeter one because that's perimeter one run Python file in terminal rectangle one has a perimeter of fourteen okay is that right 6 plus 8 is 14 okay and you know what you could even do is you could say like let's make it a little fancier we could say the the space and then we could put rect 1 dot color comma and then rectangle has perimeter okay let's try that the red rectangle has perimeter 14 boom okay guys if this didn't make perfect sense go back and watch this again because man your life is going to be so much better if you can understand classes classes and a function in a class is called a method when you create an object with a class like rectangle 1 or rectangle 2 when you create the object it runs to find dot int and then after you create that object you can call whichever method you want and you can interact with these methods directly you don't have to interact at this level or you don't have to pass it parameters you pass it parameters at the top and then you can sit and work around and the nice thing is if I'm working with rectangle one and then I come and I do a whole bunch with rectangle two I can go back to rectangle one and everything is still in place it's still organized so it's sort of like I got a box rectangle one and it's a tool box that has both numbers and data and functions and then I've got a second tool box called rut to sew a class is like a kit that has both functions and data in it and then every object that you create kind of has its own kit does that make sense I sure hope it does this is really important and now when you look at other people's code you understand this init that is just what's run the first time that you create an object when create the object this is run and then these other ones are callable by that name self whatever you name that object okay I know I'm repeating myself go back and watch this a couple of times but man understand this and it will take your programming to the whole new level okay let me see if I can back up here a second and let's see I think this is where we left off last time let me see if I can run this thing I think in Lesson number 44 we were running two cameras let's see if it is hopefully this will run okay now look look at the two cameras okay let me see if I can point this a little bit better at me okay now now watch the two cameras do you see the problem the problem is that the the the PI cam has like a second or two of latency and that's just because the way we did it was I created camera one I created camera two I have read camera one and I read camera two well I'm reading camera one what's happening frames frames are stacking up from camera one okay so while I'm reading camera two it's tied up my processor and frames are stacking up in the queue on the other camera and therefore what it ends up with is while I'm working with this camera this camera over here is falling behind so your program gets out of sync so what I need is instead of having function one and function two and I call function one read camera one I call function to read camera two what I need is I need both of those reads to be running at the same time so I need to have functions that are not waiting on each other instead of going boom boom I need them to go boom boom boom where they're both running that my friend is called threading okay that is called threading and let's see if we can understand okay this is the way that I think I would explain I think I need to start another program here don't I will stay in face recognizer and then we are going to create a new file and we are going to call that new file face rec cook recognizer and this will be 10 and we will call this thread hi okay fresh new spankin new clean no errors yet and what we are going to learn is we are going to learn how to do a little threading and threading is I define functions just like I did before okay and then what I'm going to do is I am going to I am going to run the functions at the same time but defining the functions is the same as it was before and so to illustrate this imagine that I have a big box and I want to function that opens the big box waits five seconds closes the big bucks then I want a small box in that small box I want to open close open close open close now the problem is I want them to both be operating independently but the problem is if I call function one it goes open waits five seconds closes well what happens during that five second wait box box small is stuck because everything stops at that time dot sleeper at that wait okay see you see what happens is big box open big box close then little box open close open close open close then big box opens big box close open close open close open close you see the smaller one I'm opening and closing faster but what I want is I want while this is going on I want this to keep going on this is really hard to do make hands go at different areas but you see one box is opening and closing fast and the other box is opening and closing slow if you don't believe that go away and try to make that work with functions okay or try to make that work with what we know now you just can't do it okay so this is the way we're gonna do it first of all this is called threading so we're gonna need to import a library okay so we are going to say from the library threading I want to import something called thread okay and we'll use that in a minute also we're need to get do delays and sleep so if we're going to import time and now what I want to do is I want to define a function it's not inside of a class so it's not a method I'm going to define a function called big box okay and what I want that big box function to do is just sit and open and close the box so I want it to keep doing it so I'm just gonna say while true what am I gonna do when it's true true true is always true so this is an infinite loop I'm gonna say print okay imagine this is big box is open now imagine you had a function that's actually opening the box but I'm just going to print big boxes open now I wanted to say open how long five seconds someone says time saw dot sleep okay I want to sleep how long do I want to leave the box open five seconds now after that what do I want to do I want to close the box big a big box is closed now after I close it how long do I want to leave it closed five seconds okay now what do I also want to define I wanted to find a small box and notice this is just a function this is not a class that's just a function small box what do I want to do with small box what is that okay that looks good print small box is small box is open and then how long do I want to leave it open I forgot the wild truth same thing I need to that's what was messing me up I want to keep doing this and then I will need to tab that over will need to tab that over and tab that over and then what I want to do time dot sleep so I'm going to leave the small box open for one second and then I'm going to print small box is closed and then I want to time dot sleep okay and I want to sleep for one so the big box is going to open for five seconds closed for five seconds open for five seconds closed for five seconds and the small box is going to open and close every second okay now what you can see is just what I could do is I could come down here and I could just say what could I do I could just say call big box doesn't need any parameters so I could just go like call the function big when it sees big box it's gonna run up here and then what's it gonna do okay what is this import time do I really not know how to import time this is very provable that was sure that's how you did it it's line 14 that it does oh oh okay man that was driving me crazy I didn't close my parenthesis and it wasn't an error here it was an error there and then I have an extra parenthesis there scared me a second okay so no that's C big box I called it and then it is not showing anything oh oh oh okay nevermind I had it indented and so big box was smart Oh sports part of small box that scared me pretty bad there sometimes you think you actually don't know what you're doing okay big box is open okay big box just closed big box is open okay now if I just wanted to do small box all right let's let's kill this with a little trash icon there I didn't give this a neat way to exit so we just crash it so then I'm gonna call small box like that and now if I call small box what is the problem is gonna happen well when I to big box it just sort of stays there and so I never come back to start small box you see so it's kind of like I made an infinite loop so I once I go in that function I can't go in the other function and if I tried to do it down here with the weights I can't be opening and closing the small box fast as the big box is opening slow see see you just kind of get stuck so what we need to do is we need to create a thread okay so I'm going to create a thread which is big box thread and I need to use the function that I imported thread I'm going to create a thread and then I need to send it what the target function is my target function is big box and now if I had arguments I would put comma the arguments but I don't have any arguments so I'm going to leave that blank okay and then I'm going to create a second box called small bot thread and it's equal to thread and then what is the target the target is equal to the function small box okay and then the args all right the arguments again I don't have any arguments all right so now this is creating a thread that is going to run big box it's creating a thread that's going to run small box now there's something that just if I understand right it makes it like you go off and launch these threads and you could kill the program and that thread could keep running and you're sitting there trying to do things with unexpected results because you have old threads running and if I understand correct you need to do like a big box thread dot Damon is equal to true and I think that just locks things together so if you kill the program it kills the threads okay and if I don't completely understand that right let me know small box thread dot Damon equal true so so that should basically keep those two threads behaving nicely and now I've created the threads what do I need to do I need to run them so I can say big box thread dot dart okay and then I need to do small box thread thread start so I define the function I create the thread and now I'm starting the thread okay no this is the problem if I just ended the program here it would launch the big box thread it would launch the small box thread and then it gets to the end of the program and the program would stop and then what would happen the threads would stop so I gotta give the main I got to give the main program something to do so I'll just say while true and then just pass so it's just gonna sit here and lube and that's gonna keep the threads alive all right so let's look at this and see how many mistakes I made okay there's the first one big box comma argh what is wrong with that args uh args arguments tests aren't args args okay run Python file in terminal does it still not like it what is this business okay so I've got big box thread is equal to thread and then target is equal to big box okay maybe what I need to do here is like this and like that because that is a better way to call things still does not like that what did I do wrong here let me just take this args out for right now because we don't have any arguments and maybe it does not like that although I think it really should know this does not I'm setting up a target I'm not putting those in there like that I spelled that right I need to close that maybe I had not closed all those things up right let's try that small box is not defined well at least that's something ah uppercase s right uppercase s because that was an uppercase s now let's try it okay all right so now I want to go back those args that should work I had some typos I wasn't seeing args like that to pass the arguments inside that comma args to pass the arguments I think maybe I'd not kept good track of my parentheses now let's try it it's why does it not like just comma 8r Argos Argos I was sure that I'd done this earlier without passing it any arguments okay let's let's let's leave that out for now wondered if it was darn it all right it does not like that in there blanke for some reason okay let's try this again like this the simple way okay so what I want you to see is it opened the big box then small box is open closed open closed then it closed the big box and now the open the small box is open closed open closed open closed but you see the small box is opening and closing quickly as the big box is opening and closing quicker I am going to come over and pardon me but I'm going to come over to a different program which I believe I had in a different folder I'd done this earlier and I can't figure out why that is not working I called it Python learning and I called it simple thread and let's do this okay why why does this work over here what did i do differently oh it's an equal I think I left out my dad leave out my equal sign did I leave out my equal sign and that would be terrible now let's see here packing close this up simple thread let's kill that okay here we are okay here we are so real-time learning here they are GS equal like that and then comma Arg is equal like that I think I'd left out my equal sign I hope this works now it still doesn't like it man I'm just crashing and burning today target is big box oh I forgot my parentheses all right let's run it now this is going to drive me ah there is an extra parentheses I'm going crazy with parentheses in the wrong spot okay this is going to run this time okay big boxes open small box open close open close open close big box closes small box open close open close open close okay so even though this is a real simple program and a real simple concept what I want you to see is it solves huge problems okay so let's review okay I define a function big box I define a function small box I create a thread based on that function I create another thread based on the other function I tell those threads to behave nicely to play nicely together and then I start the two threads okay and then they sit and operate independently and then when I kill the program then both of the threads stop very good alright let me just do this time let me pass it an argument like red and let me pass it an argument blue okay and then I'm going to come up here and I'm going to say that I'm going to have a color and then a color so what I'm doing is I'm passing it a parameter to show you how you can pass a parameter in here and so this time I'm going to say here color comma box is open and then I'm going to say color box is open and then same thing here color box is open and then same thing here color and I think I forgot some commas up here like that okay so you see what I'm doing is now I'm passing it a parameter of red and blue and then hopefully that will grab in and up here and then let's see if this works color Oh got a extra quote there alright let's see doesn't like takes one positional argument but three were given okay so let's see I think maybe let's say C is equal to red and then let me pass it C and then C is equal to I'm sure I can pass a string to blue and then I pass it C let's see if that works still doesn't like it okay so this args thing is not working the way that I think it should okay I don't okay I see I think it needs that comma on the list of arguments let's see if I take that comma out yeah it doesn't like it without the comma so when you have the arguments you've got to put a comma afterwards and now let's try it okay red box is open blue boxes open blue clothes blue closed red okay so you see that's how you pass the arguments but for some reason it appears that you need a trailing comma when you do that all right but when I put the trailing comma in then it works in that just I'm just going to make sure that that's actually it if I take that trailing comma out it doesn't like it if I put the trailing comma end it likes it ok let's see if I could pass it what could I pass it C and X and C and X and then say X equal 5 and say X equal 4 and then I'm pet passing it see an X and then I can come up here and I said color comma of link comma L and I got to put the L here comma L I'm gonna pass it in I got to put it here comma L so I'm trying to pass it two parameters just to make sure that we understand this passing it to and then doing something with it of of length comma L of link , L closed of length comma L okay so I'm seeing can I pass it to parameters and it probably still wants the trailing comma I would guess so let's see run Python file in terminal okay link five length four four four yes what's passing those two in and just to see do you always have to put that trailing comma run Python town run Python file in terminal ha I guess if I have two parameters I don't have to use a trailing comma so do not understand exactly how this args work but if you only pass it one parameter put the comma the trailing comma if you are not going to do that and you have two parameters you can just pass it the two parameters without a trailing comma that is like the darndest thing I've ever seen I'll have to look into that you guys look into it and tell me how it's working tell me why it's working that way okay so we have learned functions we've learned methods we've learned e classes and we've learned threading and what you can see is the real power is if you start doing the threading inside of a class hint that's where we're going okay so let's see what our assignment is for next week this was our old simple old way of doing things trying to run two programs okay I mean run two cameras and when I run two cameras that happens okay do you see how I have that delay well what I want you to do is I want you to use what you learned today about classes and threading and what I want you to do is get something that operates like this with a little luck okay you notice I have two cameras one look at that boom alright so your homework for next week is to use classes methods and threading to get your two cameras working in sync okay I hope this was a little bit of a painful lesson I had a couple of typos getting started but the reason I don't go back and just completely edit those things out is everybody is going to make mistakes programming and I want you to see that how to kind of debug it and how to troubleshoot it the real thing that got me is and I don't understand I don't understand why if you just pass it one argument point wants a comma at the end of it but anyway if we put the comma at the end of it it does work all right you guys leave me a comment down below did this lesson make sense if you guys run into all that and knit stuff before and self stuff and not understood it hopefully you'll understand it now and I am telling you we have got some super exciting lessons that are coming up because as we can start running these things using classes and threading there is just a whole lot of exciting stuff we can do so hopefully you guys enjoyed this lesson if you enjoyed it give me a thumbs up if you haven't subscribed to the channel already go ahead and subscribe and get ready for some exciting new lessons coming down the pike this is pulmicort ER with top tech boy comm I will talk to you guys later
Info
Channel: Paul McWhorter
Views: 5,790
Rating: undefined out of 5
Keywords:
Id: 8Wx85nG8E9A
Channel Id: undefined
Length: 71min 31sec (4291 seconds)
Published: Sat Jun 13 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.