1
00:00:00,690 --> 00:00:06,480
Great we have a while loop there that will make sure our script will run all the time.

2
00:00:06,840 --> 00:00:13,310
Specifically every five seconds. Now inside this while loop we want to check the time.

3
00:00:13,770 --> 00:00:20,390
So we're talking about a conditional. When we're talking about conditionals we think about if and else.

4
00:00:20,810 --> 00:00:26,260
So while 2 which is always true and that means the script runs all the time and If.

5
00:00:26,520 --> 00:00:35,140
You know something about how we can check if time is between this and that.

6
00:00:35,880 --> 00:00:41,770
Well first of all we need to import the data to a module.

7
00:00:41,890 --> 00:00:50,580
So we have a daytime class inside the date time module which should be so the date time class from the date time

8
00:00:50,580 --> 00:00:57,290
module. So import datetime class. Maybe as dt.

9
00:00:57,750 --> 00:01:01,700
So this will be assigned the dt namespace.

10
00:01:02,130 --> 00:01:08,520
I'm doing this because this expresion will be quite long so it's good to have a short name there instead

11
00:01:08,520 --> 00:01:12,950
of doing date time dot date time dot now etc..

12
00:01:13,260 --> 00:01:14,210
Instead we do dt.now.

13
00:01:14,210 --> 00:01:15,120

14
00:01:15,420 --> 00:01:20,990
So we want to check the current time against 8:00 a.m. 4:00 p.m..

15
00:01:21,230 --> 00:01:25,500
Now the current time would look something like let me open a Python shell here.

16
00:01:26,460 --> 00:01:35,710
And from date time import date time as dt. Dt.now.

17
00:01:36,840 --> 00:01:45,190
So this is the current time. Your month, day, hour, minutes, seconds and milliseconds.

18
00:01:45,600 --> 00:01:49,410
Sorry, this is micro-seconds not milliseconds.

19
00:01:49,620 --> 00:01:53,630
Now this current time is being generated automatically.

20
00:01:54,120 --> 00:01:58,790
And as you know this is a spatial object.

21
00:01:59,280 --> 00:02:02,430
So it's not a string or a number.

22
00:02:02,490 --> 00:02:04,310
So it's a date time object.

23
00:02:04,530 --> 00:02:12,930
And what I'm trying to get here is that to compare these objects against the date time that we pass

24
00:02:12,950 --> 00:02:21,110
manually and you see how what I mean with passing manually, we need to compare these time objects with

25
00:02:21,110 --> 00:02:26,340
a similar object. That would make sense if you would just pass

26
00:02:26,340 --> 00:02:37,170
let's say you know something like a Tuple of 2016 and let's say five for month, five four day and then

27
00:02:37,260 --> 00:02:44,580
eight and then you compare this with let me call dt.now.

28
00:02:44,700 --> 00:02:53,080
So let me compare this with this Tuple. 2016, 5, 5 and 8.

29
00:02:53,140 --> 00:02:55,590
Of course you will get an error.

30
00:02:55,950 --> 00:03:01,800
So what you have to do instead is you'd call dt.now.

31
00:03:02,280 --> 00:03:04,580
So this object here.

32
00:03:05,050 --> 00:03:09,820
And then you create another object manually.

33
00:03:10,050 --> 00:03:17,480
So you called dt which is the date time class that we have imported earlier just here.

34
00:03:17,640 --> 00:03:21,720
And then inside the brackets you pass the values.

35
00:03:22,280 --> 00:03:24,950
I see that's that.

36
00:03:25,230 --> 00:03:30,290
And let me pass eight. And this is saying false.

37
00:03:30,290 --> 00:03:36,200
So which means this value is not less than this.

38
00:03:36,270 --> 00:03:45,220
Let me check the time now again and don't expect to get the same time as you did here.

39
00:03:45,630 --> 00:03:52,920
So now it's actually 9 p.m. And if you're wondering why is that.

40
00:03:53,040 --> 00:04:01,200
I had to interrupt the video so I had to pause the video around this time and then I went back like 12

41
00:04:01,200 --> 00:04:02,810
hours later.

42
00:04:02,820 --> 00:04:08,800
So now it's actually 9:00 p.m. in here and my computer was sleeping during this time.

43
00:04:08,880 --> 00:04:17,830
So that's just that if you were wondering why and that means that� That means this time it's not less than

44
00:04:17,830 --> 00:04:18,650
this.

45
00:04:19,290 --> 00:04:21,390
So it's not earlier than this.

46
00:04:21,510 --> 00:04:31,350
If you do for instance� Is it's greater then this, then you get true. Now in our case we need to check actually

47
00:04:32,670 --> 00:04:34,860
against two date times.

48
00:04:35,090 --> 00:04:43,950
So that would be something like a dt and you'd pass eight here so 8 a.m. and then here would be 16.

49
00:04:43,950 --> 00:04:44,310

50

00:04:44,310 --> 00:04:51,030
So let's do that in our script instead of writing this in here.

51
00:04:51,360 --> 00:04:52,200
And yeah.

52
00:04:52,220 --> 00:04:53,100
We are here.

53
00:04:53,280 --> 00:05:01,830
So if dt and now I could pass here 2016 and this would probably work for this

54
00:05:01,830 --> 00:05:02,030
year.

55
00:05:02,040 --> 00:05:03,700
But it's not very sustainable.

56
00:05:03,710 --> 00:05:11,180
So what you want to do is work with variables instead off of values.

57
00:05:11,190 --> 00:05:19,100
What I'm trying to say is that instead of passing 2016 in there you can have Python generate a year

58
00:05:19,100 --> 00:05:19,980
on the fly for you.

59
00:05:20,100 --> 00:05:23,200
So dt.now.year.

60
00:05:24,140 --> 00:05:27,950
That would return 2016.

61
00:05:28,490 --> 00:05:33,240
Actually we can try that here. Dt.now.year. Yep.

62
00:05:33,450 --> 00:05:38,820
So this returns this value which is a string.

63
00:05:39,250 --> 00:05:41,780
So type.

64
00:05:44,810 --> 00:05:47,710
Sorry, it's an integer I meant to say an integer.

65
00:05:47,840 --> 00:05:52,770
So it's a number that dt ,the dt class reads it as the year.

66
00:05:53,180 --> 00:05:55,260
So the year and then dt.now.

67
00:05:55,360 --> 00:06:05,320
Again the month should also be a variable. Month and then dt.now.day.

68
00:06:05,330 --> 00:06:11,550
The only thing that we want to have fixed is the type so 8.

69
00:06:11,730 --> 00:06:22,040
And what we're trying to do is check if dt.now is greater than 8. So if it is greater than 8

70
00:06:22,040 --> 00:06:23,250

71
00:06:23,330 --> 00:06:27,590
that means we are in the working hours.

72
00:06:27,590 --> 00:06:34,640
And also we want this to be less then 16.

73
00:06:34,750 --> 00:06:44,010
So what you do is just copy this and I forgot the brackets here.

74
00:06:45,230 --> 00:06:53,060
So you copy that paste it here bracket there and say 16 and then you have a column there for the if statement.

75
00:06:53,070 --> 00:06:53,440

76
00:06:53,810 --> 00:07:07,130
So if this is true than you want to execute your other segment. So let's write something more informative.

77
00:07:10,750 --> 00:07:11,620
Working hours.

78
00:07:14,910 --> 00:07:16,570
And yeah, let me execute this.

79
00:07:20,270 --> 00:07:23,930
Python website blocker.

80
00:07:25,250 --> 00:07:28,230
We've got an invalid syntax.

81
00:07:29,150 --> 00:07:32,130
I think I messed up the brackets in here.

82
00:07:32,380 --> 00:07:33,170
This is ok

83
00:07:33,230 --> 00:07:39,490
but this one should be inside brackets.

84
00:07:39,490 --> 00:07:39,990
Yeah.

85
00:07:40,080 --> 00:07:41,890
This closes in here.

86
00:07:42,470 --> 00:07:43,910
This one closes there.

87
00:07:43,910 --> 00:07:45,850
This should be fine now.

88
00:07:46,040 --> 00:07:53,470
Let me try again. Unorderable types. This is comparing against a Tuple.

89
00:07:53,480 --> 00:07:55,200
Sorry about that.

90
00:07:55,380 --> 00:08:03,090
It's actually not a good idea to copy and paste things. As a general rule in programming you shouldn't

91
00:08:03,110 --> 00:08:07,780
copy and paste things. You should write them because when you copy and paste things you're

92
00:08:07,790 --> 00:08:11,330
prone to making errors like I did here.

93
00:08:11,570 --> 00:08:16,600
Now I'm doing copying and pasting here for the sake of video recording.

94
00:08:16,610 --> 00:08:23,680
So this is probably more convenient for you as it saves you time watching me to write things down so

95
00:08:23,720 --> 00:08:29,130
it's better to use your logic and just write things so that you focus on what you're doing.

96
00:08:29,470 --> 00:08:36,190
And yeah I think this error was good but it came up because now you know that's it's good to write

97
00:08:36,200 --> 00:08:39,180
things to write the code instead of pasting it.

98
00:08:39,710 --> 00:08:44,210
So we have dt here that creates the date time objects.

99
00:08:44,210 --> 00:08:48,980
This also creates a data time object and this one as well.

100
00:08:49,790 --> 00:08:55,110
So let me go again. Execute and nothing is happening.

101
00:08:55,130 --> 00:09:02,170
So that's actually the code is running but we don't have this print statement executed.

102
00:09:02,170 --> 00:09:11,040
The reason for that is that now it's 9:00 p.m. in here so we're not within the working hours.

103
00:09:11,260 --> 00:09:19,640
That brings us to the point that if interrupting this, if you want to print something when you are not

104
00:09:19,790 --> 00:09:26,210
in the working hours you'd want to add a else statement here.

105
00:09:26,210 --> 00:09:40,270
So say else print Fun hours. Save and yeah what we have this far is while True check for this condition

106
00:09:40,460 --> 00:09:43,100
and do this if that condition is true.

107
00:09:43,220 --> 00:09:45,770
So print out the working hours.

108
00:09:45,780 --> 00:09:48,120
Otherwise in this condition is not true.

109
00:09:48,130 --> 00:09:55,700
So if it is false as it was when I just executed the script then print this statement in here.

110
00:09:57,090 --> 00:10:00,170
And then sleep for five seconds so don't do.

111
00:10:00,160 --> 00:10:05,560
Don't execute the loop for five seconds and then go again and check the current time again.

112
00:10:05,570 --> 00:10:10,050
Against these two values.

113
00:10:10,180 --> 00:10:22,280
So yeah now we are in our fun hours. So every five seconds we get fun hours printed out. Great.

114
00:10:22,380 --> 00:10:29,720
Yeah I think we have the structure of our script but we don't want to just print out this strings. What

115
00:10:29,720 --> 00:10:34,620
we want is we want to modify this file.

116
00:10:34,790 --> 00:10:40,590
So if it's between 8 and 4 pm we want to add those two lines in there.

117
00:10:40,610 --> 00:10:46,460
Otherwise we want to make sure those two lines are not there in the file.

118
00:10:46,460 --> 00:10:54,780
So it's quite a bit tricky to access that file and add this lines and check if these lines are there.

119
00:10:54,790 --> 00:10:54,970

120
00:10:55,250 --> 00:10:58,110
But we'll crack that in next lectures.

121
00:10:58,120 --> 00:10:59,410
So see you there.

