1
00:00:05,520 --> 00:00:08,880
In this video, we're going to create a
function that'll let us print text

2
00:00:08,880 --> 00:00:12,980
with different effects. I'll include
escape codes for the main colors,

3
00:00:12,980 --> 00:00:16,880
as well as bold, underline
and inverted text. Now I

4
00:00:16,880 --> 00:00:20,160
mentioned in the last video, that
these escape codes were horrible.

5
00:00:20,160 --> 00:00:24,080
Something like backslash n is easy
to type and quite easy to remember.

6
00:00:24,080 --> 00:00:27,840
The codes we need, aren't. I'm not
going to ask you to type them in,

7
00:00:27,840 --> 00:00:31,060
and heck, I'm not even going to type them
in myself. You'll find them in the

8
00:00:31,060 --> 00:00:35,920
colour_codes.txt file, in the
resources for this video. We need

9
00:00:35,920 --> 00:00:39,280
to create a new Python file.
We'll go ahead and do that in our

10
00:00:39,280 --> 00:00:45,840
current project. I'm going to call this
one colour_print.py.

11
00:00:51,600 --> 00:00:55,840
Alright, I'm going to paste in
the contents of that text file.

12
00:00:55,840 --> 00:01:00,480
So the escape sequences are defined
in a series of Python constants.

13
00:01:00,480 --> 00:01:04,159
We can define them once and then use the
friendlier names, and that's much better

14
00:01:04,159 --> 00:01:10,600
than remembering that red is escape, open
square bracket, three, one, m.

15
00:01:59,260 --> 00:02:04,560
Those ANSI escape sequences on lines 2
through 14, start with an escape code,

16
00:02:04,560 --> 00:02:08,800
followed by an opening square bracket.
So that tells the terminal that the next

17
00:02:08,800 --> 00:02:12,480
few characters will be special commands,
that it should perform.

18
00:02:12,480 --> 00:02:16,560
All you have to do to use these escape
sequences is print them to a terminal,

19
00:02:16,560 --> 00:02:20,640
that understands the ANSI sequences. The
good news is that the IntelliJ run

20
00:02:20,640 --> 00:02:24,100
window does understand them.
So let's have a go at doing that.

21
00:02:24,100 --> 00:02:30,160
On line 16, I'm going to type
print parentheses RED comma

22
00:02:30,160 --> 00:02:34,540
and in double quotes, this will be in red.

23
00:02:34,540 --> 00:02:40,400
Okay, when I run this - and I'll just move
this run window down to the bottom.

24
00:02:41,360 --> 00:02:45,120
Alright, so you can see
that that's in red.

25
00:03:14,000 --> 00:03:18,640
Alright, so our text printed in red, as
you can see, in the IntelliJ run terminal.

26
00:03:18,640 --> 00:03:22,000
What's not obvious, is that
all output is now red.

27
00:03:22,000 --> 00:03:26,080
So if I add another line
- let's do that;

28
00:03:26,319 --> 00:03:33,840
print parentheses and so is this.
and if we run this,

29
00:03:34,319 --> 00:03:37,360
even though we didn't specify
red this time, the output

30
00:03:37,360 --> 00:03:40,480
is still red. The changes we
make to the colors, etc,

31
00:03:40,480 --> 00:03:44,799
will stay in effect until we cancel them.
Now we can reset the text effects,

32
00:03:44,799 --> 00:03:49,200
by printing the reset sequence. That's
on line 10, as you can see there.

33
00:03:49,200 --> 00:03:51,840
It's going to be a bit tedious doing
that every time, so this sounds like a

34
00:03:51,840 --> 00:03:54,959
good use for a function.
So I'm going to delete those two lines

35
00:03:54,959 --> 00:03:57,840
of output there, on lines
16 and 17, and replace that

36
00:03:57,840 --> 00:04:01,860
with a function declaration. I'm going to
call this function colour_print,

37
00:04:01,860 --> 00:04:06,560
so def colour_print, and in parenthesis,

38
00:04:06,560 --> 00:04:13,920
text colon space str comma effect
colon str. We're going to close it

39
00:04:13,920 --> 00:04:16,320
with a right parentheses,
and it returns None. So we've got our

40
00:04:16,320 --> 00:04:21,600
arrow, our dash and a greater than,
None and a colon.

41
00:04:21,600 --> 00:04:25,040
And you can see that I've annotated the
function. It takes the text to print, a

42
00:04:25,040 --> 00:04:28,560
string, as its first argument,
and the effect we want, again

43
00:04:28,560 --> 00:04:31,440
as a string, for the second one.
It's returning None because it doesn't

44
00:04:31,440 --> 00:04:35,820
return anything useful. The function prints
to the screen - it doesn't return a value.

45
00:04:35,820 --> 00:04:38,080
Alright, so I'd better document that.

46
00:04:38,080 --> 00:04:40,479
So I'm going to do just that with a Docstring, so

47
00:04:40,480 --> 00:04:45,060
three double quotes again. I'll press
enter there, and I'll close the run pane

48
00:04:45,060 --> 00:04:49,199
so you can see it a bit better. And
we're going to print - I'm going to use my

49
00:04:49,199 --> 00:04:55,360
backticks again - text using the ANSI

50
00:04:55,360 --> 00:05:02,160
sequence, or sequences rather,
to change colour, etc.

51
00:05:03,360 --> 00:05:10,220
In terms of the parameters, we've got
text, which is The text to print,

52
00:05:10,220 --> 00:05:16,080
and effect, well that's the effect we want,

53
00:05:18,720 --> 00:05:22,080
and it's one of the constants

54
00:05:22,680 --> 00:05:26,880
defined at the start of this module.

55
00:05:28,000 --> 00:05:36,080
Alright, I'm going to remove return because,
in fact, we're not returning anything.

56
00:05:36,080 --> 00:05:39,520
Okay, so the body of the
function will be quite simple.

57
00:05:39,520 --> 00:05:43,360
We want to print the ANSI escape
sequence, then the string that was passed

58
00:05:43,360 --> 00:05:46,880
into the function,
then the reset sequence. So let's start

59
00:05:46,880 --> 00:05:50,400
by building up the string using dot
format and printing it.

60
00:05:50,400 --> 00:05:53,919
Notice before I do that, I'm going to add
another line here, line 15, because

61
00:05:53,919 --> 00:05:56,880
remember, we want to have two blank
lines between the start and ending

62
00:05:56,880 --> 00:06:00,560
of a function, definition.
Alright, so line 25 now, I'm going to

63
00:06:00,560 --> 00:06:06,560
type output underscore string is equal
to, and in double quotes, we want

64
00:06:06,560 --> 00:06:10,800
three replacement strings.
So zero in curly braces, one in curly

65
00:06:10,800 --> 00:06:14,400
braces and two in curly braces, and we've got our

66
00:06:14,400 --> 00:06:20,000
double quote dot format, and
in parentheses, effect, text

67
00:06:20,000 --> 00:06:27,220
comma then reset, which is our constant,
defined on line 10. Okay, so we've

68
00:06:27,220 --> 00:06:32,620
defined our output string,
let's print it now; print output_string.

69
00:06:32,620 --> 00:06:35,100
Alright, so let's add some
code here - some test code.

70
00:06:35,100 --> 00:06:37,360
We're remembering here,
to add two blank lines

71
00:06:37,360 --> 00:06:42,319
after the end of our function.
So line 29, I'm going to paste in some

72
00:06:42,319 --> 00:06:45,919
code here - also available
in the resources section.

73
00:06:45,919 --> 00:06:50,560
So you can see that we're accessing our
new function, but on line 31, I've used

74
00:06:50,560 --> 00:06:52,960
the built-in print function to print some text.

75
00:06:52,960 --> 00:06:56,240
That tests that our function has
correctly reset the terminal to the

76
00:06:56,240 --> 00:07:00,160
default colour.
So run the program now and marvel

77
00:07:00,160 --> 00:07:03,840
at what we've done.

78
00:07:06,240 --> 00:07:09,520
So that's pretty cool, and we can make
our output more interesting.

79
00:07:09,520 --> 00:07:12,560
If you didn't get the different colours
and effects, don't worry. As I said, we'll

80
00:07:12,560 --> 00:07:15,199
be making a small change
so this code works for you,

81
00:07:15,199 --> 00:07:19,280
very soon. Our function isn't perfect,
though. There's no way to print yellow

82
00:07:19,280 --> 00:07:22,560
and bold, or blue and inverted, for example.

83
00:07:22,560 --> 00:07:26,100
And that's easily done, but we need
to know a bit more about functions.

84
00:07:26,100 --> 00:07:28,960
We haven't covered how to
write functions that take a variable

85
00:07:28,960 --> 00:07:33,440
number of arguments, and that's what we
need here. Once we've covered star args,

86
00:07:33,440 --> 00:07:37,440
we'll come back to this function and
improve it. We'll be doing that a bit later.

87
00:07:37,440 --> 00:07:39,680
Alright, so make sure you test the Docstring,

88
00:07:39,680 --> 00:07:42,400
and experiment using this
function, and in the next video,

89
00:07:42,400 --> 00:07:45,440
we'll run this code the way that our
users will run it. That's a very

90
00:07:45,440 --> 00:07:48,660
important test, and we'll see 
that it doesn't work on Windows.

91
00:07:48,660 --> 00:07:52,400
See you in the next video.

