#100DaysOfCode in Python Transcripts
Chapter: Days 19-21: Iteration with itertools
Lecture: Itertools - Cycle
Login or purchase this course to watch this video and the rest of the course contents.
0:00 So we're going to kick off our foray into EdiTools by using EdiTools.cycle. This is one of the infinite usages of EdiTools.
0:12 Okay, in the since that when you use it, it's just going to keep iterating over the item or series of items over and over again.
0:21 It doesn't stop until something tells it to stop, okay? So, let's have a go. First thing we're going to do is import EdiTools.
0:33 Now we're going to import, I'll explain all of this in a second, we're going to import sis and we're going to import time.
0:39 What we're going to make, and this is a really cool one, I absolutely love this one, we're going to make
0:44 one of those little, cool spinny line things, you know? Like a little loading line that you see on the command line.
0:51 You might actually like this or you might hate it depending how many times you've seen it. So, to make that, we're going to go symbols,
1:00 let's create a little item here called symbols. And it's going to be EdiTools.cycle. Now what we have to specify between the brackets is,
1:12 tool tip gave it away, is the iterable, the item that we're going to iterate over with cycle, okay?
1:19 Now that could be a variable, could be anything really, it could be any object, but, we're going to use just a specific set in a specific order.
1:30 So if you think about this, take a look at how these are lining up. This looks like a spinner right now. So it's going to start with a horizontal dash,
1:39 then it's going to go into the slash, then the pipe, and then the other, then the forward slash, and so on and so forth, okay?
1:45 And it's going to iterate through that. It's going to keep cycling through this and you'll see that it actually starts to spin.
1:53 Alright, it gives you that sort of feeling of a spin. Now, to do this, we need to put it in a loop. So we're going to give it a while loop.
2:03 So while true, and again, dangerous, this is just going to keep going until you control c out of it all. Snap out of it in some way, shape, or form.
2:13 To get this to work the way we want it to work, we need to use the sis module, okay? So, I won't go into sis now because this is pretty straightforward
2:27 and you may already know it and it's out of the scope, so we're going to essentially send this data to standard out, okay,
2:35 on the system, wherever you're running this from. And the little trick we want to do here is, I'll just show you, we're going, let me type it in first.
2:46 We're going to go next, symbols... Alright so what this line is going to do for every single loop it's going to do the next. Remember we covered next.
2:55 It's going to go through the next iteration of EdiTools.cycle, okay? And this here, the slash r, is going to negate
3:07 putting this on a new line, okay so, it's not going to return, it's not going to put it down on a new line, mkay?
3:16 Now, sis, this one here we sort of have to put in just for safekeeping. Just in case, okay? Oops, not flash.
3:23 Flash, so sis standard out flash, this guarantees, this will force whatever you're putting to standard out
3:31 to appear on the screen because from time to time you might actually get what you're writing to standard out
3:38 going into a buffer and we don't want that. We want it to be flushed out of the buffer and onto the screen. And then we're going to put a time delay.
3:46 That's why the imported time up above. Let's just make it one second, okay? So, our while loop, it's going to start iterating using next
3:54 through EdiTools.cycle, okay, it's going to cycle through this infinitely. It's going to flash it and make sure it appears on the screen
4:03 and then it's going to take a second, okay it's going to sleep for one second and it's going to do that for,
4:11 well, for eternity, until we exit out, okay? So look at that, we're starting to go through here. Just ignore the two here.
4:21 This is a by product of actually running through this in the Python shell. So what we're actually going to do is we're going to put this
4:31 into a, an actual Python file and we're going to run it from our Windows command line and you'll see how it actually works.
4:40 Okay, with some magic here we now have all of that in a little script and now all we have to do is run Python and what did we call it?
4:50 We called it cycle_demo, and look at that. Look at this funky little spinner over there. So, that looks kind of boring now...
5:03 Mkay, so let's go into this and change this in seconds to be zero point, five seconds. Let's try it again.
5:18 And look it's speeding up, okay it's gettin' quicker. It's gettin' quicker. And just, just for the fun of it, let's make it super fast.
5:25 And look at that, now it looks like a proper spinner. So this is a perfect, perfectly awesome and usable example of EdiTools.cycle