It is beautiful - Decorator
Step 1
Edit
Write a script. Import the time module.1
Then write a function called time_teller
. This will be our decorator. time_teller
contains another function that fills three Strings into “It is {date}. Since {hour} hours and {minutes} minutes.”.
Write a second function get_time
that return a list with string values of the date and the time split into hours and minutes.2
Use the decorator on get_time
and print this function
Run
Run your script.
Step 2
Edit
Change your decorator so that it accepts arguments. Change the String as well to “Now it is {date}”
Change get_time
to accept a sting as input (default should be "%Y/%m/%d %H:%M"
)3 and return the date formatted like this.
Add some more prints with custom date formats as you like.4
Run
Run your script and test if the values are correct.
-
Use
import time
↩ -
You can get formatted time using e.g.
time.strftime("%Y/%m/%d")
for the date for hours and minutes use%H
and%M
↩ -
You can define default values if there is no argument given (e.g.:
def my_func(value="Take this String if there is none")
↩ -
A table of the formats can be found in the Documentation ↩