Let's continue working with the standard modules.
And the next one is going to be very,
very important because not only is it a very useful module,
but it's also relevant to the world of cybersecurity.
And I'm talking about the date time module.
This is a, this is a module that you can use to display things like your time,
the current date,
you can manipulate date and so on. So
let me show you how to work with it.
I'm going to remove all of this and simply say import and then date time.
OK?
Now let's assume we wanted to log the current date and time
gonna come down in here
and I'm gonna say current
underscore time equals.
OK?
And now we'll have to call the module which is date time
dot And now the method
which will be used to actually display the current date and time.
And it's also called date time dot Now,
it's kind of interesting that in the module, date time,
you do have a function called datetime dot Now, that is actually used.
So all I have to do from this point right now is to simply say print
and then in brackets,
I can say the current date and time is space and then
you can add my, uh, my comma right here
and there's just a current
underscore
time.
And let's see, I'm going to run the program and there you go.
The current date and time is 27th of October 2024.
And that is the time right there, as you can see.
However,
I want to introduce you to something else.
See, given the fact that over here we have datetime
and then the day time again,
it's kind of redundant, right? Like, I mean, the code still works perfectly fine.
Don't get me wrong. The code does work.
But if you want to be very professional about this,
one thing we could do is
we could say that you know what
from the datetime module, let's import specifically the date time method.
So what I'm gonna do right now is over here,
I'm going to say from
datetime,
import datetime.
And now the beauty is that over here, we no longer have to say day time,
but you can just remove all of this
and just simply say day time dot Now
because right now we've told Pyt
that hey, from the module called day time,
I want you to import specifically
the class or the method called date time.
And now if I run the program again,
you can see
it still works perfectly fine.
It's just that this is a little bit more professional
and a much better way of writing your code.
Now,
I want us to take this program a step further and
actually combine the day time module with the random module.
So we're basically going to generate random activities like uh login logout file,
upload uh password change, you know, things that a user might do on a
system, we're gonna generate them randomly and also
generate them with some time stamps. OK? So
I'm gonna go back in here
and just after the daytime module, we're going to now import random.
OK?
Now let us indicate the different types of actions that we're going to randomize.
So I'm going to say actions
equals and now we can create our list
and let's add a variety of them. Ok? Let's first of all, go with like login
comma, let's go with log out as well. Ok?
We have some suggestions in here like register reset password
and
I think I like this four,
I think four is enough.
So different actions like login logout, register reset password. So
we're going to randomize these actions.
Let me just add
an underscore between reset and password whenever
your strings have more than one word.
If it's like two words, always use an on
call to join them together, it's a much better way of writing out your strings.
So
what I'm gonna do right now is
over here, I'm gonna keep the current time equals to datetime dot Now However,
we also need to create a variable that will hold the randomized action.
So
I'm gonna come over here right now and say user underscore action
equals and our random dot choice
and then in brackets
the actions
and there you go. And all we have to do right now
is just to print
that the user performs
action at a particular time.
So we can do this. I'm gonna say print and now the F string
and I will say the user
uh performed.
And now our color race is to represent the action so that we use the
underscore action.
And now I can say add and then the curly braces
current underscore time,
add the closing codes, close the brackets
and that should be it. So
let's go ahead right now and run the program and there you go,
the user performed, register at so and
so let's run it again.
Performed, login this time around. OK? Perform reset password
as you can see. So it is working. Now.
What if, what if just like in the previous lesson, if we wanted to
have more than one action being performed by the user? What are you going to do?
Very simply, we're gonna come down here, change choice to choices.
And then remember,
we'll have to indicate how many actions we want to randomize.
I'm gonna say K equals two.
And now over here, I can say the user performed.
Let's just keep the print text as it is,
let's run the program again.
And now you can see it says user perform
password log in at this,
run it again and now it's log in and now log out. So
that's how you can work with the day time module
in accordance with the random module to
just generate random actions at different times.
Thank you for watching the video. I will see you in the next class.