Great we have a while loop there that will make sure our script will run all the time.
Specifically every five seconds. Now inside this while loop we want to check the time.
So we're talking about a conditional. When we're talking about conditionals we think about if and else.
So while 2 which is always true and that means the script runs all the time and If.
You know something about how we can check if time is between this and that.
Well first of all we need to import the data to a module.
So we have a daytime class inside the date time module which should be so the date time class from the date time
module. So import datetime class. Maybe as dt.
So this will be assigned the dt namespace.
I'm doing this because this expresion will be quite long so it's good to have a short name there instead
of doing date time dot date time dot now etc..
Instead we do dt.now.
So we want to check the current time against 8:00 a.m. 4:00 p.m..
Now the current time would look something like let me open a Python shell here.
And from date time import date time as dt. Dt.now.
So this is the current time. Your month, day, hour, minutes, seconds and milliseconds.
Sorry, this is micro-seconds not milliseconds.
Now this current time is being generated automatically.
And as you know this is a spatial object.
So it's not a string or a number.
So it's a date time object.
And what I'm trying to get here is that to compare these objects against the date time that we pass
manually and you see how what I mean with passing manually, we need to compare these time objects with
a similar object. That would make sense if you would just pass
let's say you know something like a Tuple of 2016 and let's say five for month, five four day and then
eight and then you compare this with let me call dt.now.
So let me compare this with this Tuple. 2016, 5, 5 and 8.
Of course you will get an error.
So what you have to do instead is you'd call dt.now.
So this object here.
And then you create another object manually.
So you called dt which is the date time class that we have imported earlier just here.
And then inside the brackets you pass the values.
I see that's that.
And let me pass eight. And this is saying false.
So which means this value is not less than this.
Let me check the time now again and don't expect to get the same time as you did here.
So now it's actually 9 p.m. And if you're wondering why is that.
I had to interrupt the video so I had to pause the video around this time and then I went back like 12
hours later.
So now it's actually 9:00 p.m. in here and my computer was sleeping during this time.
So that's just that if you were wondering why and that means that That means this time it's not less than
this.
So it's not earlier than this.
If you do for instance Is it's greater then this, then you get true. Now in our case we need to check actually
against two date times.
So that would be something like a dt and you'd pass eight here so 8 a.m. and then here would be 16.
So let's do that in our script instead of writing this in here.
And yeah.
We are here.
So if dt and now I could pass here 2016 and this would probably work for this
year.
But it's not very sustainable.
So what you want to do is work with variables instead off of values.
What I'm trying to say is that instead of passing 2016 in there you can have Python generate a year
on the fly for you.
So dt.now.year.
That would return 2016.
Actually we can try that here. Dt.now.year. Yep.
So this returns this value which is a string.
So type.
Sorry, it's an integer I meant to say an integer.
So it's a number that dt ,the dt class reads it as the year.
So the year and then dt.now.
Again the month should also be a variable. Month and then dt.now.day.
The only thing that we want to have fixed is the type so 8.
And what we're trying to do is check if dt.now is greater than 8. So if it is greater than 8
that means we are in the working hours.
And also we want this to be less then 16.
So what you do is just copy this and I forgot the brackets here.
So you copy that paste it here bracket there and say 16 and then you have a column there for the if statement.
So if this is true than you want to execute your other segment. So let's write something more informative.
Working hours.
And yeah, let me execute this.
Python website blocker.
We've got an invalid syntax.
I think I messed up the brackets in here.
This is ok
but this one should be inside brackets.
Yeah.
This closes in here.
This one closes there.
This should be fine now.
Let me try again. Unorderable types. This is comparing against a Tuple.
Sorry about that.
It's actually not a good idea to copy and paste things. As a general rule in programming you shouldn't
copy and paste things. You should write them because when you copy and paste things you're
prone to making errors like I did here.
Now I'm doing copying and pasting here for the sake of video recording.
So this is probably more convenient for you as it saves you time watching me to write things down so
it's better to use your logic and just write things so that you focus on what you're doing.
And yeah I think this error was good but it came up because now you know that's it's good to write
things to write the code instead of pasting it.
So we have dt here that creates the date time objects.
This also creates a data time object and this one as well.
So let me go again. Execute and nothing is happening.
So that's actually the code is running but we don't have this print statement executed.
The reason for that is that now it's 9:00 p.m. in here so we're not within the working hours.
That brings us to the point that if interrupting this, if you want to print something when you are not
in the working hours you'd want to add a else statement here.
So say else print Fun hours. Save and yeah what we have this far is while True check for this condition
and do this if that condition is true.
So print out the working hours.
Otherwise in this condition is not true.
So if it is false as it was when I just executed the script then print this statement in here.
And then sleep for five seconds so don't do.
Don't execute the loop for five seconds and then go again and check the current time again.
Against these two values.
So yeah now we are in our fun hours. So every five seconds we get fun hours printed out. Great.
Yeah I think we have the structure of our script but we don't want to just print out this strings. What
we want is we want to modify this file.
So if it's between 8 and 4 pm we want to add those two lines in there.
Otherwise we want to make sure those two lines are not there in the file.
So it's quite a bit tricky to access that file and add this lines and check if these lines are there.
But we'll crack that in next lectures.
So see you there.