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