Context managers in Python use the save-override-restore pattern to temporarily modify state within a code block and guarantee restoration afterward. In function-based context managers, you record the original state, yield a dictionary containing the modified state, and update the dictionary in the finally block after the code block completes. In class-based context managers, you save the original value in the constructor, override it in __enter__, and restore it in __exit__. This pattern ensures that changes are temporary and automatically reverted, which is essential for operations like timing code blocks, temporarily overriding configuration values, and managing database transactions.
Deep Dive
Prerequisite Knowledge
- No data available.
Where to go next
- No data available.
Deep Dive
Solving Python Problems "Context Managers" | AI Engineering for 365 Days | (Day 6/365)Added:
Hey, hey, hey.
already my testing. Okay, perfect. Let me go to the main camera. All right, we're live. We're live and as you guys can tell by title. So, today I'm going to be working with more content managers in advanced Python. That's it. Last stream I did go over like question 17 out of 25 and algorithms for the past three streams because that's been trying to complete all those 25 practice questions before we move on to the next topic. Speaking of which, we're almost done with these 25 questions and the our first topic which is related to advanced Python. I got to fact check and see my road map as to what comes next type stuff. That said, let me go ahead and move on to my game play screen. There we go. Okay. And guys, this is a brand new chat cuz I tried going with new chats like every single stream. So I avoid clumping or overusing tokens. Let's let's see. And yeah, I did set my uh cloth to adaptive. I want to see what that does. That's it. Let's go. Welcome back. You showed up again. All the basic stuff. So tell me drill 17 context manager for timing code locks. Do you remember what key concepts is that?
Okay, that makes this drill different from what you've done before. What are we yielding a dictionary instead of using evaluate um can I get the um instructional for the question again question mark.
Okay. And guys, I'm not warmed up just yet. That said, let me go ahead and grab my Visual Studio code up here real quick. RBS code. Yeah, this is the program that I've been working on like last stream. Although I did not really complete the whole stuff.
Okay, speaking of which, I might actually get plot in the other window here just so I can use it as a reference while I'm working on the program here. I will be reading it once like on stream as well. Okay, that's it. Let's just go up there.
Hold up, guys. There we go. Okay, perfect. So, firstly, the drill 17 context manager for time code blocks.
Write a context manager. Okay, this is a decorator for my context man manager. So I will have to include a context manager from my contextes li which stands for library. Write a context manager function called timer that takes a label parameter.
Okay, timer that takes a label parameter records the time records the start time on entry yields the dictionary. Okay, so we're going to have to use the dictionary as well. label label start time and elapse time. After the web block completes, update the dictionaries elapse time with the total time taken.
So you got to like subtract there. Ying Roger, appreciate you joining the stream. Hiding Pixel, appreciate you joining the stream. Okay. Always print label took elapsed for pawn. Oo. So label took these many seconds in the finally block. Test it with this exact code with timer by operation D. expected output still running all that basic stuff. Okay, so let's go ahead and work on the program. I forgot where you from.
I'm from abroad. That's it. Let's go ahead and grab the let's say instructional questions up here and then let's go ahead and delete this. Now let's start from scratch. So I'm going to go with something like from my context li which is which stands for context library. It's like a broad uh let's say library where I'm going to go ahead and get access to just my context manager. So, let's go with c n txt. I think I spelled it. Whoops, my bad. From context library, import context manager.
There we go. And now, uh, context manager is basically like a decorator inside of which I'm going to be adding my uh function of my timer. Therefore, I'm going to get access to my context manager's decorator. And then let's go ahead and define a function. So, the instruction says um to write a function called timer. So let's go ahead and define a function called timer that requires a parameter of a label. Let's go with l a zel. There we go. Okay. And then let's go with a colon.
Okay. Perfect. Next up records the start time on entry. I might actually create a start timeses uh let's say variable.
Just going to declare it here. Not really define it. Okay. What are you doing? I'm actually working on instructional based questions on my advanced Pythons is context managers section. Once I'm done with this, I'm going to move on to the next thing in my road map which basically is a road map for my AI engineering for 365 series for my gold buddy edu series is to make an agentic AI. And if you guys do want access to my road map, make sure to join the discord server and either go to the Python chat or like the let's say coding helps chat and mention my name there. I will be personally DMing it to you guys.
That said, don't feel pressured. Uh, okay. Buying a graphics card. Actually, I'm waiting for Memorial Day, guys, because that's what stuff is actually on. Uh, whatchamacallit, they're pretty cheaper those days. It's I think on the 25th or so. That said, so we're going to go ahead and define label. Next up, record the start time on entry. Okay, so record the start time on entry. Okay, so since I am going to be working with time here, let's go ahead and import our time module. There we go. And guys, in case I'm typing a little slow right now, is because I'm not warmed up just yet. That said, I'm going to go with something like, "You're the most intelligent in my subscriptions." Guys, I feel like that's one of the >> I think that's one of the most uh the best compliments that I've gotten so far. Let's go with something like that time cuz I'm not nearly as intelligent as I got to be. That said, uh records the start time on entry yields a dictionary. Okay, so we're going to be needing a dictionary as well. And the start time here is going to be the same as our dun enters uh let's say method.
Okay, dun enters this method where we're going to go ahead and let's say yield an empty dictionary which I will be defining as well. Let's go with something like dictionary that is going to be equal to >> okay let's go with empore dictionary.
>> Okay, when are you going to touch C++ memory management? I don't know if I'm actually going to be needing much of C++ for this series. I did need it for my last series where uh it was called programming for 250 days where my goal by the end of that series was to make a game in general that consist of AI logic in it. I am done with that game and you guys didn't get access to the game that I made. Uh it's the link is in my bio of the Twitch of the I forgot the name. Was it like it.io? Yeah, I'm pretty sure it's.io.
That's it. Happy BBQ. Let's go. Okay, so we're going to go ahead and create a big tree that consists of a few items in it.
label start time and elapse time. Okay, so let's yield my dictionary. PMP dictionary.
Okay, after the web block completes, records the start time on entry always prints label took that much. So I might need a elapse time variable as well.
There we go. That is going to be equal to my start time. Okay, this is my current time or do I need a separate time? Okay, so label took that much time. So we're going to go with our start time >> minus our label.
>> Okay, I think this is I think this should do the trick here. Oh, what did you get in your GCSE? Honestly, what's the full form of GCSE? Are you doing generative AI course? Actually, I'm not doing a course, but I am like following a road map that I actually created for this series. Blood said yes. So you are much intelligent, guys. I don't think I'm that intelligent. Let's let's see. Uh after the web block completes, update the dictionary elapsed. Oh, so we need to print a before and after dictionary, right? Okay. Test this code with this exact stuff here. Okay, you know what?
Let's let's go one step at a time. I'm going to test it first. Let's go with something like with my timer's function.
I'm going to let Oh, the label is going to be called my operator. So, this is not going to be label. Got to see what is that going to be. Um okay so start time is going to be my time dot time what is my elapse time going to be so the output should should be my operation took 1 second total time was 1 second oh so there's no subtraction here okay so let's go with something like my operation >> there we go is the major exam in the UK I see I just haven't done that yet? What is your LinkedIn? Uh, give it to me.
Actually, I don't really have a LinkedIn for now, but I do have my GitHub and like uh my Discord server. I do have most stuff, but I am going to be creating that soon.
>> I said, can we play watering waves tomorrow? Honestly, we can definitely play it once I actually get my graphics card. What does yield do? Yield is basically uh let's see. So, if I were to here, it's somewhat like return, but then it pauses after a condition is done. like return it instantly ends your entire program with yields waits until I don't want it to give me another uh let's say content from it for example a list of like 1 million numbers is going to give me access to all 1 million numbers but then yield is going to give me access to one number at a time I did have a literal definition up here as well which may be much uh maybe less vague there we go there we go okay so yield from works with any iterable nudges generators. Uh let's see.
>> Okay, yield versus return. Return gives everything at once like a list. Function is done. Goodbye. Yield gives one value.
After giving that one value, it pauses, remembers its place. Unlike uh unlike the continue that like gives a value and then it goes back to the um start of the loop, okay? And then waits for the next function to be called. Or we could use a for loop if you don't want to use a next. Okay. And the next used to manually ask the generator for the value. Okay, all that dishy stuff. And a quick little example of yield is if I define my generator and go with for every i in range 1 through six, I want to yield i. And then if I print or if I assign my generator function to a variable results and then print result, I'm going to be getting just a memory address. But if I use the next function and print the result, it's going to give me the first value of my loop, which is going to be one. If I print again, I'm going to get two. So on so forth, if that made any sense, guys. Okay, that's it. Let's go up here. And guys, I got to go I got to test the output here. Okay, so with timer, I want to go ahead and let's go with something like time. I want to make it sleep for 1 second, which means my program is going to be paused for an entire second. Then we're going to print Whoops. Let's go with sleep. And then we're going to go ahead and print something like an F string >> that says something like still running.
Let me go with something like still running. Okay. Da dot. And then let's go with something like label is of curly braces. Now here's the thing. This is the kind of format specifier that I didn't really work much with, but let's go with label.
>> Okay. So this is going to be our labels as parameter. Okay. Okay, the T is a I thought the T was supposed to be like an escape statement, but I got to I got to ask what this does. Okay, then lastly, let's go ahead and print something like total time taken was that much. Let's go with total time was I'm going to go with a curly braces. I need to see what this D here does. I haven't actually worked with the T.
>> Let's go with elapsed. And then I'm going to go ahead use a format specifier of a colon. 4F which means four decimal spots or four decimal places after the number ends. Let's go with seconds.
Okay. And then our expected output is that. Now to actually get the expected output, we're going to go ahead and read the instructions again. Okay. Let's see.
So takes a label parameter records the start time on entry. So I took a label parameter. I recorded the start time on entry which is going to be my time dot time and then yields a dictionary. Okay.
So my dictionary is going to be consisting of those variables in it. So I'm going to go ahead and first create an empty dictionary. Okay. So we have our label start time and none. Therefore I might get my dictionary like all the way down there. Okay. So this is who Okay. So this is what I get for my dictionary. Hit control alt n to run the program. So type error uh time. Takes exactly one argument. Wait, did I give any? My bad guys. I forgot to give an argument. Okay, now I'm playing Minecraft that actually sounds really cool. I actually wanted to try it, but I did play a game similar to it. Okay, type error unsupported formats non type line 17. Okay, I mean, yeah, I kind of knew it. Oh, wait. My bad, guys. Hold on.
>> So, wait, it's colon, okay, so we parenthesis.
Okay, wait. Let me actually try remove that.
Okay. So, still running label is my operation total time in seconds was none. Okay. So, rather than none, the output I'm looking for operation took 1 seconds. Okay. Let's go ahead and add a print statement. Do you have any plushies? I do actually have a Goku plushy, guys. Um, whatchamacall in case you guys did not know it, I was a massive Dragon Ball Z fan. Well, still kind of am.
>> That's it. My operation still running.
So, still running.
Oh wait, hold on. So still running and then I want to go ahead and execute my operation.
>> Okay, so still running label is my operation.
Label is >> the label name my operation. This executes when the program ends. So we need a print statement right here. My operation took that many seconds. So that is start time and that is none.
Okay, let's go with something like a print. I'm going to go with something like my operation >> took let's say something like let's change this to an F string. Okay, so my operation took how many seconds? I may go with something like start time.
>> Okay, start time.
>> Okay, cuz my operation is supposed to be taking 1 second. Let's go with start time next seconds. and you will be replaced.
>> Okay, let's go with control alt n.
>> My operation took those many seconds.
Still running.
>> Okay, still running. Label is my operation. Okay, here's a small little fix. Maybe let's go ahead and copy that from there. I may add it here, but I don't really know um how is it going to like change stuff here.
Okay, this may be my time dots.
Okay. So, label. I can't have access to my label here. So, Oh, I I think I see what the tier does. I know you uh you're also lover of black clover. Actually, I am a black I am like a black clover fan, guys. That's it. So, we have elapsed here. Oh, okay. Okay. So, can I go with start time? No, we got to go with elapsed.
Elaps.
Oh, wait. Are we are we getting access to this?
Maybe. Let's go with E. Let's just go with intlescence. You know what? Let's go with elapsed. Crl S control alt n.
Okay, my operation took noneconds. What if I change this to let's say a string of 1.00 then x?
Okay, I did kind of hardcode that. There we go. All right, I know that it's not like 100% accurate, but let's see. I'm going to go ahead and grab plot up here.
Have you studied a DL or neutral network? I haven't, but I if that's related if that's like anything related to AI, let me just say something like I may have not done all steps correctly.
Okay, that said, if that is related to like AI engineering, then I will definitely have it in my road map and I will eventually work with it. Cuz let me actually review my road map real quick.
Oh, are you getting interrupted by my commenting? Actually, I'm not, guys. I do love reading the comments here. That said, okay, so according to the road map, and you guys can get access to this road map if you guys join my Discord server. Can I get this in the other window? Nope, cannot.
Okay, I'm just going to go ahead and grab it here. I want to see what comes next. I'm pretty sure it's ASKI. There we go. So, the first step um in the advanced Python was decorators, which I'm done with, generators, which I'm done with, and context manager, which I am still working with. And then we're going to work with ASKO and ASKI/wave patterns. And then type hence pantic. I haven't heard of that stuff. But then later we're going to be working with developer tools like git and github which I have started working with. I just realized they are pretty important and nice. Linux CLI. I don't really know much about that. Arrays I know. And then hashmap stacks and all this stuff. I'm trying to find if there is let's see. Oh wow there is machine learning. For some reason, I thought the machine learning was not there. Okay, but that is going to be later on like I think a couple of months later type stuff. Then project three and all that basic stuff. That said, let's read this. Might ram much appear again.
There we go. Okay, good start. But you're missing the heart of this drill.
Here's what she got right. Yeah, it's related to LLM and AI. India, we make our own models and train like machine learning. That does sound cool. I might give that a try, guys. Okay, good start.
But you're missing the heart of the drill. Here's what you got correct.
Context manager start time dictionary structure yields empty on dictionary.
Two problems. Elapsed should start as none, not 1.00.
You're missing the try/f finally after yield. This is the whole point of the drill. After the vid block finishes, you need to calculate the elapse time.
Update the dictionaries is elapsed key.
Print the results. And finally the key question. Right now your dictionaries is elapsed never actually gets calculated.
It's just hardcoded string. How do you calculate elapse time after the bit block? You have start underscore time recorded. What do you do with it after you? And what keyword guarantees that code always runs? Okay. Uh before I work on it, um what is elapsed time? Question mark. I should have asked that at the very beginning. First I thought it was like the time that has been passed.
Okay. Elapse time equals to how long something took to run. I thought it was how long has passed. Well, it's sort of the same thing. It's the difference between when something started and when it ended. Machine learning is easy. Just deep learning is tough. I'm really excited to work with those things to be honest. Start equals to time dot time.
Okay, I knew we had a subtraction here.
And equals to time dot time. Oh, I see.
I see. I see. We record a start time. We uh run a code and then we record an end time. We um we get that time as well.
Then we say how much time has passed.
Wow.
All right, guys. I think I got it. This is going to start as none.
Okay. And then we're going to be having something like int time is going to be equal to time dot time after this code executes.
And then we're going to go ahead and grab that share. We're going to go ahead and paste that share. And this is going to be the updated dictionary. So start time.
>> Okay. And then let's go with ela.
lapsed time is going to be equal to >> I'm recommending the games. I've recommended a gotacha games for you to play in gaming streams. It's in Discord.
I'll definitely check it out as soon as the stream is over.
>> Hey, do you um you still have cloud tokens left, >> huh? Cloud tokens, guys. I hardly use cloud to be honest. That said, so elapse time is going to be our start underscore time. When did it begin? minus my end underscore time. When did it end?
>> Okay, and then we're going to just go ahead and add our elapse time right here.
>> Okay, I think this should do the trick, guys. Hold on. Just going to go with elapse time. And then we're going to go ahead and paste it here. And now moving down with my operator here. This is going to be our elapse time. And then since it's a decimal value, I'm going to go with something like dot4.
Okay.
>> Colon, do comma4.
Whoops.
>> Hold up. 4F. There we go.
>> Wait, wait, wait, wait, wait. Did I make a typo here?
>> Okay. Uh, do you prefer RPG games or MMO or both? It really depends if any games are like popular on streams. I just give it a try because I don't really play as many games in general. But then I just play these stuff that you guys en enjoy watching. Okay. And then let's go with something like T.
Let's actually remove the XX. Okay. And then after this we're going to go with F. Okay. Now let's let's run it.
It started. Label is my operation.
Operation took 0 seconds. It total time was 0 seconds. Did I not call it? Let's see. Maybe almost done. EMP you. Why are you Why are you going to EMP all our devices? Hello. Did you just lose your mind? Guys, EMP here stands for empty.
So empty. Victory. Okay, that's I I'm missing something really important here.
Maybe. Oh, wait. Never mind. Never mind.
Never mind. That is going to be start underscore time. There we go. Yeah, a small little calculation error there.
same output.
Okay. So, what is something crucial that I missed here? Let's see. So, we have something like end minus start. So, we have our start time that has been recorded. And then we ran some code down here. We have our end time that has been recorded. We run some more code up here.
And then we yielded our empty dictionary.
>> Elaps equals to that. Time returns float in seconds.
Interesting.
>> Okay. So, maybe I do I may add X's here.
Let's try adding X's my operation. There we go. Okay. And then let's add some X's here too.
>> Okay. I'm so confused as to like why is this showing zero? I wish I could have Wait, can I actually print it?
Okay, I'm just going to print start time and end time to see the difference.
Let's go with something like print and underscore time. Now let's control S control alt n. Where are you from, bro?
I'm actually from abroad. Let's Oh, wow.
No wonder they're exactly the same numbers, guys. The program is correct.
They're exactly the same numbers.
>> Since they're exactly the same numbers, where do I go?
>> Let's go here.
>> Okay. I guess your dictionary is so small it's taking less time. That does make sense, huh? I mean, >> well, since it does take less time, it's >> Wait, isn't that Wait, wait, wait, wait, wait, wait. Isn't that why we really went with this stuff here? Time dot sleep. Hold on a minute. I think this may go here.
No, I thought that time. was in charge of delaying it for a whole second. Use 6F.
I mean, I don't really think that it's going to change anything, but let's see.
Okay, just the number gets bigger.
Let's go ahead and grab cloud here.
Let's paste it and well, go with control arr get cloud here. Let's go with control arr.
Okay, here's the thing. I think I may have gotten a a slight bit of grasp here. Stop. You're calculating elapse time too early. I knew it.
>> Okay, look at your code. You're calculating before yielding. Okay, you're calculating before yield, meaning block runs. Okay, calculate after yield.
Okay, the vid block runs at yield.
Everything after yield runs after the block. The structure you need start time and then our dictionary. Yield the dictionary. Everything down here runs after the yield. So you're try that. Finally calculate this.
Oh. Oh. Oh. Oh. Is it recorded?
Actually, it's not pre-recorded. That's it, guys. Oh, hold up, hold up, hold up.
I see. I see. I see. Okay, this was this was a really um silly beginner mistake here. Let's copy that. Well, I mean I am sort of a beginner because it is my day six learning advanced Python. But okay, I got it. So, basically where I was messing up is that I forgot all about what Neil does. Even though I did just explain what it does, what Neil does is that it executes the code from here and then it pauses. It still remains here, but it pauses and waits for me to really call it again, which I do call it after a whole second is over here and then everything under it runs. Therefore, I might go ahead and let's say try something with my laps map. Let's go with try.
>> Okay. So, we're going to go ahead and try all this.
>> Okay. And then I'm going to go ahead and go with something like finally. I don't think I need an exception, right? Okay.
Wait, actually finally I'm going to go ahead and display my little dictionary here.
Okay. And then let's go ahead and control SR N. That is the number unsupported format passed to none. Type it in. Was it none or elaps time?
>> Okay. Well, that's none.
>> Try and then I might accept exception as error. Let's go with exeception as error. And then I'm just going to go ahead and print my eder.
>> There we go. Still running.
Wait, is something uh messing up before my e start time?
>> We don't really have an end.
>> Okay, still running label. So line 30 unsupported format string pass to non type dun format. So line 30 right here.
>> So it says elapsed is there. Maybe I got to go ahead and declare my variable first.
Paste it. Equals to zero.
>> Okay. Wait, it says that undefined there. Okay. Let's go with control C and copy there real quick. I'm pretty sure it's a silly mistake that I'm not seeing here. It literally said calculate elapse time here. Update elapse time here.
>> Wrench here.
>> Hold on.
Hold up guys. Wait, wait, wait, wait a minute.
>> So, we yield inside our try block. We update that stuff there. And then we create a dictionary >> elapse time except exception as error.
>> Okay, so we got a syntax error. Line one. The hell line one.
>> Okay, you roer. Guys, I'm anything but a procoder for now. You know what? I think I am like a lit like a bigger coder here. That said, I can't wait for me to get used to this because for now I'm used to the Unreal Engine syntax and how Unreal engines framework works. I've almost lost all my skills in Python.
>> This would have been like a piece of cake for me back when I had my Python for 100 series. That's it. Copy paste.
Yeah, I was going to do that just to ask where I messed up. H messed up again. Guys, it's so silly how when you don't work on uh something for like a long time, you almost forget most things. Cuz I did work a lot with dictionaries and all this stuff back in my Python series. So, I do know how they work, but I just need more practice.
Okay, you're so close. Two issues.
Problem one, you're creating a new dictionary. Okay, this creates a brand new dictionary outside the update.
T outside holds a reference to the original dictionary. If you reassign empor read t is t still points to the old one you have to mutate it.
Okay problem two elapsed time must be calculated in finally. So we try that and then elapsed time equals to time dot time minus start time and this is the ending time and that is the starting time. my empty dictionary add index of elapsed is going to be all equal to my elapsed and then we print it.
Okay, let's give that a try. Procoder just silly mistakes guys. That does make sense. That's it. I would just say a coder makes silly mistakes there. Okay, so let's remove the exception here. And now this is our first dictionary. We do not create well we do not create another dictionary here rather all the calculation happens here. Let's delete all that stuff. Okay. So we try and yield our dictionary. Finally, regardless of anything, we're going to go ahead and let's say create an elapsed underscore times variable that is going to be equal to my time do time, which is my current time right now minus my start underscore time. And then we're going to go ahead and update our dictionary. So my empty dictionary at index of my elapsed there we go of my elapsed key is going to be equal to the value of my elapsed time and then we're going to just go ahead and print something like this total time that was taken is this okay let's go with you can remove the sleep right actually the sleep is like important here but I will try without it as well cuz the sleep is what really gives a second after my program really runs. That said, let's go with something like label. Might go with like an S string. Then let's add a curly braces.
Label, which is going to be my operator took elapse time. I'm going to go with E L A S E D time. Okay, I might put colon um 4f. That removes the need for this to be honest. Label to get this much.
>> Okay. And then we're going to hit Ctrl S to save the code. And then go with Ctrl Alt. I don't think I really need that.
>> Okay, I'll just have it.
>> Name T is not defined in line one.
>> T is not defined.
>> By any chance, is T like >> Yeah, let's remove that.
>> What about actual exception handling? I need to actually do that. But this one is just for getting this stuff to work.
There we go.
Still running. Label is my operation.
Operation took 1.008 seconds. Now let's remove this.
>> Okay. Uh, still running. Operation took 0 seconds. And if I were to add my sleep again with 10 seconds. No, that's too long. Like 3 seconds.
>> That is the start time by the way.
Operation took 3 seconds. Okay.
>> Okay. I think I see I just need more practice with this part here. Now, let's go ahead and paste this here real quick and say >> I see I definitely need more practice though.
>> Okay, so we could do like a bunch more questions here.
>> 24.
>> Okay, I'm pretty sure there's like a slight error.
>> Okay, that's it. You got it. The logic is perfect, but what the output wasn't the same to me. Why this work? Say it back to me. Why does updating empore elapsed inside finally also update the outside the width block?
>> Firstly, I had one question go with something like I had one question.
>> What does the t do? At first I thought that it was the escape character but it does not have the backslash.
Okay. And then let's send it.
>> Let's see. Let's see.
>> Should I remove cloud from adaptive?
>> Okay. Great question. D here has nothing to do with escape characters. Back slashd equals to a tap character. Yeah, d here is just a variable named timer.
My operation. Oh god, guys. This is this is let's forget. I asked that. What does do remember? Okay. And then we have uh D.
Oh, this is Oh god. Okay. Okay. Okay.
Okay. Okay. Okay. There we go. This is the value to the variable after the as in a with statement.
Okay guys, how did I forget it? Whenever you yield something using the context manager after adding a code which is supposed to be your dungeon enters this territory or within your dungeon enters this territory and when you yield something that is going to become the value of your um variable after the as keyword in the width statements. There we go. Okay. So now t is basically my empty dictionary at index of label is going to give me my operator and then this would have been t at index of something else is or let's say t at index of elapsed is going to give me these many seconds. t at index of start is going to give me the start time.
>> Oh I see. Okay. Okay.
>> Okay. Why does mutating empty dictionary at index of elapse inside finally also update the outside the red block?
Because we >> because it's executed in the finally.
Let's go with something like finally, which means it uh runs regardless.
Okay. And then let's send it almost. But that's the wrong reason.
You're right. Finally always runs, but that's when But that's when it updates.
Not by Tes.
Think about this. Empty dictionary elapsed equals elapsed time mutating the existing dictionary.
emp add index. This is creating a brand new dictionary. That does make sense.
Why does the first one work, but the second one does not? EMP and T are two names pointing at the same object in the memory like two people having the same address. If someone repaints blah blah blah a dictionary. Okay. So the real reason the two >> the real reason T sees the update outside is not because finally it's because dictionaries are dash objects. Both empty dictionary and T point towards dash dictionary in memory. So mutating one mutate is mutating the other. Okay. So >> dictionaries are um I forgot what objects were they see are no their key value pairs are let's go with dash objects both empore dictionary and t point towards the same um reference in the memory.
Okay, so mutating one is the same as mutating the other. Okay, I forgot what goes in the first one. And guys, my spellings and phrases here were not accurate. Exactly.
Notes unlocked. Just one word you missed. Dictionaries are mutable objects. But yes, you nailed the concept completely. Drill 17 context managers for time coding blocks. The key concept yielding a dictionary allows mute mutation after use because dictionaries are mutable objects. Both dictionaries inside the function and T outside the and T outside the function point to the same object in the memory. Mutating one mutates the other. the pattern. We use our uh decorator here and then we define timer. We create a start time and then we create a dictionary. We try and yield our dictionary. Finally, we create a lapse time. How much time is taken.
Okay, we subtract it with start time and then we do all the dishy stuff. Usage ready. All right, guys. We got 17 and I'm pretty much warmed up now. I'm ready for 18. I'm literally sweating for some reason. It could be the heat outside or it could just be me trying hard here.
Yo, how do you sprungle? Appreciate you joining the stream. Okay, that's rule 18 context manager for temporarily overriding a config value.
Okay, this is a classbased context manager. Back to classes. Write a class called config which I'm assuming is configure. configure underscore override or configuration override that takes config a dictionary key and value as dunder in it parameters in the constructor on enter saves the original value sets the new value return self on exit restores the original original value no matter what okay before we work on questions if you think that I do not know a concept. Can you uh teach it to me first before we do practice questions? Question mark.
Okay, let's see. You know what? I think that's what I've been missing. I've just been blindly going in. Absolutely.
Class-based context managers, do you remember what Dr. Enter is supposed to return when you want to use as? I do hear the statement.
I do know that part.
Okay, let's see.
Okay, good pattern. Um, good. One more check. The core pattern of this drill is save, override, restore. Do you understand what that means in the context of this drill specifically? Why do we need to save the original value before overriding it?
I do know why, but I did not understand >> the prompt clearly to ask uh more questions if that makes any sense, guys. Yeah, I need to actually understand the prompt.
>> Got it. The prompt is asking you to temporarily change a value inside a dictionary and guarantee it gets put back when the bit block ends. Oh, I see.
Think of it like borrowing someone's car and putting the seat back exactly where then you're done. Oh, I see. Break it down. Config debug. Okay. With config override config debug true. So when you write, >> excuse me guys. So when you write config_right config debug true config the dictionary temporarily changes debug the key inside that dictionary you want to change true the temp the temporary value. Okay the temporary new value.
Okay I think I got it. I think I got it.
Let's give it a try. Going to get the instructions in the other window. And let's just increase the size so I can read everything clearly. I'm >> going to go all the way up to the question. There we go.
>> All right. Now, let's go ahead and work on creating a class. I guess I'm going to create a class called >> config_override that is going to go ahead and require a constructor.
My constructor is going to be let's say having a define dungeon in it that require self as a parameter. And now it says stakes config a diction's key and value as dunder in it parameters. Okay.
So we need something like >> there we go. And then let's just go ahead and initialize the self doconfig >> equals to an empty dictionary.
C O N F I G. There we go. Okay. Then next up on the dunder enter saves the original value. Sets the new value.
Return some saves the original value.
Original value. Okay. Let's see. Config.
So debug.
>> Okay. So debug false version 1.0. Debug true version 1.0. Debug false version 1.0. I know. Oh, I see. I see.
>> So, config equals to yada yada print that.
>> Okay. So, we need to uh set an initial configuration. Therefore, I'm going to go with something like >> debug is going to be set to false initially.
>> Okay. And then version >> is going to be equal to let's say something like 1.0.
Okay, so this is our initial value right here. And then we got to go ahead and get our configurations here. So we're going to go with something like print >> self.config.
>> Okay, so this is our initial value. We configure it and then we got to go ahead and replace it with a new value.
>> self.config. Okay, wait. Hold on. I might create a brand new label here >> original value.
>> Okay. And then let's go with something like self dot >> config.
>> Let me avoid typos by actually copying that self.config equals to config. Okay.
And then we print our original value first. Let's copy that. Let's paste it here. And then we're going to go ahead and manipulate our original value inside of our dunder enter. Therefore, I'm going to go ahead and define my dunger enters this dun method that requires cells as a parameter. And I'm going to go ahead and let's say add a prevent statement here. As soon as the program begins, I want to go ahead and replace the configurations by going with something like self.config.
Okay, >> this is going to be my configuration.
So, original value.
>> I'm going to go with something like print my original.
>> What up, guys? Let's go with self.config.
>> Okay, by see I'll catch you next stream, Raj. Appreciate you saying your Okay, let's see. So, we got to go with self.config. So, we have a false value.
So it says saves the original value, sets the new value. So my original value is saved and then we got to set the new value.
>> I see. Okay, >> let's copy that. Let's delete it from there. I'm going to go ahead and paste it here.
>> Then let's copy that. Delete it. And then we're going to paste it here.
>> There we go. Okay, so this is the original value. Then we're going to go with a new value. So I'm going to go with self dot original value at index of let's say debug is going to be equal to false.
Okay, so this is definitely my new value here.
>> Okay, saves the original value. Okay.
So, I'm going to go with something like self dot new value equals to >> self original value equals to that. And then we're going to go ahead and print our self new value.
>> There we go. On exit restores the original value no matter what. Okay. And then we're going to go ahead and use >> myself new value.
Okay, which in that case it's not really compulsory to like print it.
>> So I'm just going to go ahead and leave it like that.
>> Okay. And now let's go uh wait not >> return wait return our um okay so this is going to be return self dot new value.
Okay. And then let's go ahead and define another.
Okay, eight live and only three likes.
What does that mean? Okay, let's go with something like exit. There we go. Where I'm going to go ahead and require self as a parameter. Now about exit. Exit requires a bunch of parameters. It's going to be requiring an exe type which is an exception type. That is basically my errors name and then exec value my value or what error message did I get.
Lastly, it requires a trace back which is basically what line did the error occur in. Now for my exit, this is going to run regardless. Therefore, I'm going to go ahead and set my self new value or self original value.
>> Regardless, it does that right. So actually this is my new value is going to be equal to that. I'm going to go ahead and yield myself dot original value. This wasn't even okay return false. I won't be saying anything before I don't really run it. Let's see.
So we have a config and then so the output we're going to go with something like with our config override which is going to be my functions name here my class is name my bad guys with this class that requires a parameter of my config which is basically debug and then true.
Okay, so it requires a config and then debug is true inside you do that inside debug.
Okay, here's the thing. My config is supposed to be a dictionary, right?
Speaking of which, I don't really need that stuff.
Let's go with as uh something like C.
That is going to go ahead and print my C.
Okay. And then let's just go ahead and hit control S control 10. Okay. Name editor config is not defined. Wait, what what line was that on? Line number 17.
Config C. and return new value.
>> Okay, so key editor slice debug false.
What if what if I set this to be true?
Okay, so have the original value false key.
Okay, am I actually missing a whole concept here? Wait, let's go with a bunch of control Z's.
Uh, control shift C.
Okay. Is it that I'm missing an entire concept here?
Okay, let's see. Let me grab plot up here, guys. I think I'm missing something really important when it comes to the stuff. How come I'm not getting it here? Okay, wait. Let me say something like sensei.
Am I missing an important concept about context managers or something?
How come I uh do not understand prompt? Uh let's say something like at first I thought it was my basics and I still think it's my basics about this concept.
Although I do not um understand the syntaxes yet, I think.
Hold up, guys. I may not be I may not understand the syntaxes here cuz it shouldn't be that hard to get it done.
Yeah, right. Let's fix the foundations first. Look at look at this. Only taking one parameter.
The issue isn't context managers. You're hard- coding values instead of using parameters.
But we call it with three things.
Config_ride config is my dictionary key value.
I feel hey.
Hey, hey, hey.
already. Mic testing. Mic testing. What up, guys? How long have I been muted for? Wait, what is wrong with my webcam as well? How long have I been muted for, guys? Was it Please don't tell me it's been from the start of my stream. Let me just decrease some of my gain.
>> What up, guys?
>> There we go. Guys, have I been muted from the very start of my stream?
>> The only day when I did not check. Oh, god.
>> How long have I been muted for, guys?
With apply and then Okay.
Stay mad cuz they treat me like an all my mic ran out of battery and I wasn't really muted for all that long. I thought that I've been muted for the entire stream. There we go. Shouldn't be muted right now.
>> That's it. I appreciate you. I appreciate you for pointing that out, Pixels.
>> So, let's see. Now, same original value.
So, I'm going to go with something like original value or let's go with self dot. I might just go with o r i g i n a l unore value. Okay, it was only a short time. I'm so glad, guys, cuz later when I rewatch the stream, I'm like, "Oh, shoot. I was literally yapping the entire stream for just for it to just be muted."
>> Okay, so original value is going to be equal to self.config.
This is going to be my dictionary at index of my selfkey.
Okay, self.key which represents my initial value. And then I'm going to go with something like a new value that is going to be equal to my self dot let's go there config at index of self dot key equal to value. Okay, self dot value.
This is our new value. Okay, which was entered by the user. And then later on we go ahead and return our self. We return our self.
>> Okay, I need to ask why did we do that?
But then besides that, let's just Whoa.
>> There we go. Let's go ahead and define a dungeon exit.
that requires an exception type and exception value and trace back as parameters.
>> And then we're going to go with something like restore self dot config equals to my original value. Okay. So this is just going to be my key. All right. So selfkey equals to my original key false.
Okay. Restore self.config equals so self.config at index of key for the time being self.config dot self.key is going to be equal to my original value. You know what? Let's go with something like self dot and self dot cuz I want everyone to have access to it.
There we go. Okay, let's see.
Self dot key equals to original value.
Original value is my >> there.
>> You know what? Let's return false.
Okay, I'm pretty sure there may be a small >> three problems. Self is not defined.
Okay, let's get off that. Go back to terminal.
>> I don't really think um >> Delta. Oh my gosh. Of course.
There we go.
>> Okay, perfect.
>> Okay. Yo, dude, your code looks sick.
Last time I was here, you were doing SDL. Now look at you like a pro, guys.
It's anything but a pro right now. I'm struggling here. I mean, maybe because it is advanced Python and it is just my basics and all that stuff. That said, we're definitely improving later on, but for the time being, I'm not nearly as good as it looks. That said, uh, let me just go ahead and work on the part.
Go all the way up here to read the instructions again.
>> Okay, >> where's the test part?
Hold on, guys. Trying to see my test bar here.
>> Oh, wow. We don't really have a test.
So, we got to just create a test oursel.
>> All right, I guess it's fine. I'm going to go with something like >> save my class, which is going to be my config.
underscore override. I'm going to go ahead and do something. I'm going to require a few parameters.
>> Config is going to be my dictionary is name. My key here is going to be let's say something like um >> what key should I keep? What key should I keep? Uh debug.
My value is going to be false.
Okay. And now config is showing a red underline. So config is not defined by pyance. Maybe we're going to go with something like string. That is going to be my dictionary's name as um config.
>> There's no control in me. I'm like >> okay. And then we're going to go ahead and print our config.
Oh god.
>> See? Okay. So type string indices must be integers. Okay. So let's go with something like string indices must be integers.
>> What if I were to change this to a variable?
>> Int object is not subscriptable.
>> All right, I guess it's fine. Let's go with controller control C. I'm going to go ahead and grab plot here. I think that we've done much better here compared to before. Now I messed up >> again.
>> And guys, personally, the heat is killing me. It's so hot. Oh my gosh.
>> Okay, almost there. Small mistakes.
Config. This is a string.
Debug false version one. You never defined config dictionary. You need to create it first.
Define it first.
Okay. And then with override config debug true as C pass the variables also notice pass true not false otherwise nothing changes visually >> these things. Okay. So we're going to go with >> print a before config a during config and a after config.
Okay, so config is going to be an object actually a dictionary which is going to be requiring a few parameters debug which is going to be false and then a version which is going to be something like 1.0.
>> Huh.
>> Oh, wait. My bad. There we go.
>> It's a dictionary. And then we're gonna go with something like print my config.
I see how I see what's going on here now. Okay, let's go with something like with my config overrides this class that requires a few parameters. First is going to be my dictionary.
Let's go with config. I'm going to add a keys which is going to be debug and a value which is going to be true.
>> The reason it's set to true is cuz I did set it to be false here. Well, learning context management before static typing is interesting. You should probably learn that. But of course, don't get sidetracked with what you're doing right now. It actually does make sense. I didn't really have static typing in my road map for my AI engineering for 365day series. That said, I am eventually going to be going over that stuff as well. Speaking of which, with config_ride, we config that stuff. And then we're going to go ahead and print our new configured dictionary. And then we're going to print our old original configur.
Okay. Okay. So, debug false version 1.0.
Debug true version 1.0. Debug false version 1.0.
Finally. There we go. And guys, with that, I think I think I understood the concept.
I'm I'm getting it now.
I see. It may be because I had a reference uh for this last part, but I believe that I may be getting a grasp of this.
>> Okay, it's so much better than me 3 days ago. I was struggling so much back then.
But that said, one small thing. You drops the as >> works but as see this is the full pattern.
>> I mean sure.
>> Okay. Have you uploaded your road map somewhere? I would like it too.
Actually, if you guys do want my road map, make sure to join my Discord server. Uh the link is going to be in the description. The link is going to be in the bio. Or you guys can get access to my discord by tapping exclamation discord on chat and then either go to the coding helps chat or the pipes chat and mention my name there. I will personally DM it to you guys and actually many of you guys did really whatchamacallit um mention me there for the road map. So good luck with whoever actually mentioned me there with your journey and do keep me updated as well.
I want to see how far have you guys gone. That's sad. I see. Let's go control C.
Okay. And then let's paste it. Then let me say something like done.
Uh does adding the variable after the as make a difference? Cuz if I print that uh variable of C, I get a memory address. Then let's send it. Okay. And then let's see. Guys, I think adaptive thinking is pretty nice. I get detailed explanations here. Great question. Yes, printing C gives you the memory address because dunder enters return self.
Meaning C is the object itself. When printing this, okay, when you print an object that has no dunder string or dunder r, I have worked with dunder string but not the dunder r. I think it's repper method. Python just shows a memory address. Okay. to make C print useful stuff. You can just go with this. Okay, I mean those are just some decorating stuff or decorative stuff and all that things. I'm going go check out your GitHub. Happy coding until then.
Honestly, I appreciate you taking your stream. Um, is that a pen stream? I'll catch you next stream. That said, my GitHub is almost empty, guys. I got to fill stuff with that. Now, I've answered my question. What is the save override restore patterns doing in plain English?
What is the save override restore patterns doing?
Okay. So the save refers to the original uh let's say h save refers to the original list of dictionary not list my bad original dictionary and the override means uh we manipulate uh data inside of it.
However, we do not change the main dictionary when doing so. And restore is just accessing the saved dictionary.
There we go, guys. I got to work on my typos here. Almost one part is off. Oh, wow. Your save and override are on spot, but restore is not just accessing the saved value. Restore means putting the original value back into the configured dictionary. You're not just reading it, you're writing it back to the real dictionary. So, it's exactly how you found it. Like borrowing your friend's car, restoring it isn't just remembering where the seat was. It's actually moving the seat back where you returned it. Oh, I see. Full correct answer. Save stores the original value before touching anything. Overwrite temporarily sets the new value inside the dictionary.
Restore. Write the original value back into the dictionary when the width block ends. Guaranteed. Drill 18. Okay. All that stuff.
Temporarily overriding a configure value to save. Override. Restore pattern.
Temporarily change something guarantees it put back together. Okay.
We save our original dictionary first to the constructor. We go ahead and create our dictionary and then we get we save our key and value pairs. We kind of define it declare by we create an original values is a dictionary that is equal to my dictionaries is let's say key that changes my value as well.
>> Key is going to be set to value. We return self exit. self doconfigure equal self. Restore usage.
Okay. Yes. Ready. Oh god, guys. I'm starting to sweat here.
Is it just me or is it actually getting really hot? Might drink some water real quick. And guys, with that, we have just six more questions remaining. I'm determined to complete all of those today.
Okay. Drill 19, transaction context manager. This drill teaches the comet roll back pattern. I haven't heard of the comet roll back pattern.
Super important for databases later in your road map. Your task. Write a class called managed transaction that takes a database. A list as an dunder in it parameter. On enter saves a copy of the database return self. On exit if no exceptions prints transaction committed and keeps changes if there is an exception. Prints transaction rolled back and restores the original list. See >> tested with database equals to 1 2 3.
Test one successful transaction with manage transaction database database.
>> Okay. Key concepts. Dunder exit receives an exception. If none no exceptions happened, we're definitely going to be raising.
>> Okay. Let's have the instructions in the other map. Guys, I'm burning right now.
It's so hot here. Let me check the temperature.
>> What is the >> temperature?
>> Oh god, it's 92° F.
>> How much is that in Celsius?
>> Okay, that's it. Let's delete this stuff.
>> Now for drill 19, we're going to go ahead and create a class. We're working with object oriented programming here as well.
>> Let's go with database.
>> My bad.
There we go. So we create a managed transactions is class that takes a database which is going to be a list as an inte as an dun in it parameter.
Okay. Okay, so let's create a dungeon in it that requires self and database as a parameter and then uh a list. So self dot database equals to an empty list. And then we're going to go ahead and define another dungeon method called the enter method that requires only a self as a parameter saves a copy of the database and returns self. So we are going to be working with saving as well.
Self do uh save equals to self dot database >> takes a database.
a list as a parameter. So that is a list.
>> Um, >> there we go.
>> Okay. So, we save the list >> and return self.
>> Okay. Okay. And then we're going to go ahead and define a dunder existence method that requires self as a parameter, exception type as a parameter, exception underscore value as a parameter, and trace back as a parameter. And then we're going to go ahead and look for something. If no exceptions, print transaction committed and keep changes. Let's go a try.
Actually, I'll go with an if statement.
H if uh not >> exception type.
>> Okay. Okay. So if not exception type if we did not really uh have an error then I'm going to go ahead and print transaction committed.
Let's go with transaction c o m i t transaction committed and then we're going to add an else and keeps changes.
So, we're going to go ahead and keep our changes.
Save.
>> There we go. And then we keep our changes. Else we do not keep our sage our our changes. There we go.
If there is an exception, print transaction roll back and we store the original list.
>> Okay, so print transaction rolled back and then we're going to restore our list. Therefore, to restore our list, we're going to go with something like Okay. So, manage transaction self.
Database equals to the list of stuff that we're going to be getting. Self dot original is going to be equal to the empty list. You know what?
>> That's the saved one.
Okay. And restores the original list.
>> Okay. So, original list is basically >> saves a copy of the database and returns self. Saves a copy.
>> Oh, okay. Got it. Got it. Got it. Got it. Saves a copy. That is a copy.
Okay. And then else I'm going to go ahead and restore it back to myself database.
>> There we go.
There we go. So cell restore is going to be equal to cell database pair with t-shirt this is correct and restores the original list self.save is going to be our database that is a list that is a list and then test it with this. So we're going to go we're going to exit outside our class.
I'm going to go ahead and return something.
Let's return false cuz I want to see if I get an error.
>> Okay. So, let's go with something like database >> equals to list of one, two, and three.
>> And then we're going to test it one. I'm going to go with something like with manage transactions. I want to go ahead and do something. I'm going to add my database as a parameter.
There we go.
>> T, which stands for transactions. We're going to go with database.append want to append. Let's say four. And then database.append five.
>> Okay. And then I'm going to go ahead and print my database, which is going to be 1 2 3 4 5. That is what the expected output is supposed to be. else we're going to go with something like manage transactions that again requires database as a parameter as Steve we're going to go with the database append for >> all wait database append 999 a big enough number and then I'm going to raise an error >> raise exception Okay, so I'm going to say something went wrong in my exception and then we're going to print our database.
>> Okay, now what this is supposed to do is give me 1 2 3 4 5 again which is my already updated database control srl alt n. You don't understand the >> of course manage transaction object has no supported context manager protocol >> line 24 >> 24 database You know what? Let's Let's check for exception. Not not exception. Let's try that.
>> Got high hopes for this.
Then let's copy that. Delete those. And then we're going to go ahead and print that.
By print, I meant paste.
Okay. It's the same exact thing but on line 23.
Okay, I guess I'm going to go with control C. Let's get up clot up here.
And let's paste it. Uh, messed up again.
Oh god, guys. How many times am I going to mess up? It's like the 19th question out here.
Little Tim, let's fix the cabs. There are three issues. That is like one more than what I had to work with.
Okay, problem one. Typo itself. Of course.
Oh god, guys. Is it Is it just the heat right now or am I making excuses?
E N T E R S E L F. Wait, there is no typo.
Huh?
Dunder enter. The hell did I go with three?
Okay, so we get the roll back. And then the last one is exception and line 31.
Where is exception? Something went wrong.
Okay, let's see the second issue.
Wrapping database in a list just store it directly. 1 2 3 turns into that.
Problem three. Both point to the same list in the memory.copy.
Okay.
And when you're restoring, you need to clear and refill original list. All right.
Okay. So, when going with database, we don't really need to add that stuff cuz it's literally the same thing.
Okay. Okay. And then we're going to the save. We're going to have to save a copy.
>> Okay. And then uh problem three, saving and restoring incorrectly. Self.save equals to save.
When restoring, you need to clear and refill self.extend self.save.
>> Interesting. when restoring >> self dot um >> database.
>> Okay. Clear.
>> And then we're going to go with something like self dot database.extend.
>> I haven't worked with that function.
Okay, I'm going to go with sub.save, which is going to be my copy that I saved there.
>> Okay, so name add. Oh god, line 24. I had a typical >> transaction. And then this is the exception that we raised. Okay, so we raised an exception. I may catch the exception and may try error block cuz it does say something went wrong.
>> Okay, but we still get the error here.
>> That is the error that was raised on purpose.
>> You know what?
Try.
>> I'm going to try this.
>> We're going to accept exception as eder.
>> Except >> exception as eder.
>> I'm going to go ahead and say something like um >> something went wrong.
There we go. And then we're going to go ahead and just hit Ctrl Sl.
>> Okay. Wow. The exception as the editor did not really get through the whole thing before except exception as error.
I did do that stuff.
>> You know what? I think let's not do it.
>> Guys, half the things here is messing up because of the heat. It's so hot.
Then I'm going to go ahead and paste it and say done.
Okay, maybe I need some water.
>> Is burning right now. What the hell?
>> Okay, got the logic backwards. Look at what each block does.
>> If exception exception handling roll back restos does nothing useful. Else no exception equals defit self.AB database.clar cell.database.extend restore to the original and success.
>> Think about it. If exception happened, roll back else changes already the database.
some time and some clarity to find your identity. It's mind over everything.
>> Okay, so it said something went wrong.
The exception roll it back here. We're going to go with control arr. Now, if I were to go ahead and grab blood here, let's feast it and say um guys, I don't know what did I mean by just >> let's see. It's too hard for me to like start facing these things up.
Here's the thing. I wouldn't say it's hard. It's just it's just if your basics are good, you'd get it. So close. Two small things. Problem one, missing um committed print. Okay, pass and never prints transaction committed. Else prints transaction committed. Problem two, exception is not suppressed. Right now you return false means exception crashes the program crashes the program.
So the final print database and never runs. Then a roll back happens. You want to suppress the exception so the program continues. Oh.
return true, return false.
>> When an exception happens, you want to Oh, yeah. Okay, that makes sense.
>> So, if I do run into an exception, which I'm going to go ahead and run into here, let's return true. I want to suppress my error. Here we return false because no error happened. So, let's go ahead and print something like >> transaction.
um >> succeeded >> and we return false.
>> Okay, transaction r back. There we go.
I may want to print my original database as well.
There we go. Transaction succeeded.
Transaction rolled back. Okay.
Then let's go to plot here. Okay. Okay.
When the program is done, I start to see what it means.
When the program is working, I start to see what it means. However, when I need to think about what to do, I sort of get lost. Could it be? because I need more practice.
>> Oh god, it's hot. It's really, really hot right now. But that's it. Let me just check if my OBS is blazing. Okay, man.
>> Perfect. That's exactly it. Here's the truth. Understanding the code when you read it and being able to write it from scratch are two completely different skills. Reading equals to recognition.
Writing equals to recall. Recall only comes from repetition. The more drills you do, the more these patterns get burnt into your muscle memory. Soon you'll see transactions and your hands will just start typing clear extend automatically. I see you're actually doing great. All those done. Okay, that's the three drills today and you all of them by the end. That's the job.
What's the difference between commit and roll back in plain English?
Let's see. So comet basically means that a transaction happened >> and that we change our value. However, on the other hand, >> roll back means that the transaction failed and that we need to clear our list and uh get the previous value.
>> Okay. Wasn't that restore?
>> Exactly. Notes unlocked to drill 19 transaction and context managers. The key concept the commit roll back pattern. If everything succeeds, keep the changes. If anything fails, undo everything back to the original state.
This is the foundation of how real world databases works.
Okay. Enter self.copy. Make a copy. And then if exception transaction roll back self database clear self.extend Extend true false usage.
These things are easy. What is Okay, I just I just need more practice. Mutate original list outside the variable ready.
All right, guys. Question 20. Let's get it. Let's get it.
Okay, I'm going to go ahead and just rub that there. Get that there. Let me just wrap that up from here. Just decrease the size. Do that there. Get it there.
Okay, perfect. Now, while that clock is stinky, I guess I'm just going to be waiting here. Drill 20, temporary environment variable context manager.
This is like drill 18, but with a twist that reaches you that teaches you to handle missing keys. Your task, write a class called temp_env.
>> This is like drill 18, but with a twist that teaches you how to handle missing keys. Write a class called temp_enb environment that takes key and value as dunder in it parameters on enter saves the original os.environment environment value that may not exist >> sets the new value return self on exit if the key existed before restore it if it didn't exist before delete it so we tested by importing os environment key concept osen environment get key returns none if the key doesn't exist instead of crashing do you No.
>> Uh, no.
>> Okay, let's see.
>> Let's teach it first. What is OSE environment? OS.environ is a dictionary that stores your computers as environment variables. Environment variables are just key value pairs your operating system uses to store >> settings. That's that uses to store setting things like this where Python is installed, your username, API keys, app settings. You can access it like any dictionary. So we import os.environment.get username os.environ get my variable none if doesn't exist. Setting os.environ my variable equals to hello creates or updates dell. Okay. All right. Here's the thing. I honestly might check if I don't really like the idea of looking into tutorials, but I was thinking to check a few tutorials on context managers. Even though I do understand it here, I think a few tutorials would give me a general idea.
>> Okay. And then the twist is a key might not exist before you said it. Okay. Let me see.
>> Okay, since they are all new concepts, do you think that I should check a YouTube uh tutorial out first and then work on these problems? because I realized what really um this is when so when I work on a question I understand that topic but for the past 19 questions we changed the question format uh for every question before I got used to one format if that makes any sense guys. I think it's basically practice and repetition like we always go with a new format if that makes any sense.
>> That's actually real smart self awareness. You're right about two things. Watching group video absolutely see OS environment for 5 minutes.
>> The format changing that's actually intentional in the oh wow it is. Each drill teaches a new pattern but I hear you. You want to be solid before moving on. my suggestions. Search OS environment Python tutorial on YouTube.
Watch that. Come back. Okay. Honestly, I might really watch that stuff off stream. Also, I have been streaming for an hour 35 minutes and I got to record my programming for 250 days a review montage part eight. I think it was part eight, right? In fact, check out the latest part that's out because I have already one. Uh, actually, I do not have one scheduled because I usually schedule one on time based on how long does it take me to record that video. That said, I might slowly move on to my main camera. There we go. And yeah, I will be whatchamacallies real quick. And yeah, I will be or whatching the tutorial after I'm done recording the video cuz I got to do it before the sunlight goes as well. That said, let me hit okay. And all right, guys. So, that was pretty much it for the stream. I actually went I was planning to be done with all 25 questions this stream but from 17 I went all the way to uh let's say 19 which were like wait was it 17 or 16 I forgot whatever that number was I've got five more questions left which I will be working on next stream hopefully completing all five questions one annoying thing is that per question the idea/concept changes without me getting enough practice with the previous concept so once I'm done watching tutorial I will be working on some of these questions off stream as well. So on stream I can let you guys know what all I did and all that stuff.
That said, if you guys did enjoy the stream, make sure to like, share, subscribe, like helps my algorithm here, makes you smile, subscribe, and get on top. That said, I will start streaming on in uh what should we call it on Twitch and Ten once I get a better graphics card, which I will be getting pretty soon. And I will start streaming on Instagram and Tik Tok if I didn't meet the followers requirement.
Therefore, if you guys do enjoy my content, I haven't followed me there, make sure to follow me there. The link is going to be in the description. The link is going to be in the bio. You guys can get access to my socials by typing exclamation socials on chat and also join the discord server. That's where I usually talk to you guys, chat with you guys, have general conversations with you guys. The link is going to be in the description. The link is going to be in the bio. You guys can get access to my discord server also by typing exclamation discord on chat or staying on stream for like 15 minutes because a link pops up on the chat every 15 to 30ish minutes. That said, and also if you guys do want to follow me in this journey using my program, make sure to either go to my Python help, actually my Python chat or my coding helps to chat on my discord server and mention me there. I am going to be personally DMing you guys the road map. That said, I'll catch you guys next live stream or video. Peace.
>> Okay, let's end the stream.
Related Videos
Agentforce NOW AMA: Build with React and Salesforce Multi-Framework
SalesforceDevs
490 views•2026-05-28
How agent o11y differs from traditional o11y — Phil Hetzel, Braintrust
aiDotEngineer
450 views•2026-05-28
WEB TECHNOLOGIES UNIT-2 | Degree 4th sem BCOM Computers web technologies unit-2 full explanation💯✅
LearnwithSahera
1K views•2026-05-29
More tests are always better? How to use AI to identify tests that bring little value
Alliance4Qualification
335 views•2026-05-29
Search Algorithms Explained in 60 Seconds! 🤖💨
samarthtuliofficial
218 views•2026-06-01
People of Game of Thrones using JavaScript DOM
AltCampus
296 views•2026-05-30
Introduction to Problem Solving Part - 1 | Lecture 1 | Intermediate DSA
ascensionix
107 views•2026-05-29
So What's Odin Lang Even Good For
TechOverTea
131 views•2026-06-01











