Problem Formulation and Solution Overview
Your boss at the Finxter Academy has asked you to create five (5) customized login greetings for their users. To keep your code clean, you have decided to place these functions in a new Python file, greetings.py
.
๐ก Note: For this article, we will be working with the fictitious Finxter Academy user, cdriver.
Contents of greetings.py file
def greeting_am(user): return f"Good Morning {user}." def greeting_lunch(user): return f"Enjoy Lunch {user}." def greeting_break(): return f"You need a break." def greeting_aft(user): return f"Good Afternoon {user}." def greeting_pm(user): return f"Good Evening {user}."
Preparation
import datetime
Method 1: Import a Single Function from another Python File
If a Finxter user logs in between the stated range (today.hour >= 1 and today.hour < 12
), this code assumes it is morning and the greeting_am
message displays.
from greetings import greeting_am today = datetime.datetime.now() if (today.hour >= 1 and today.hour < 12): from greetings import greeting_am print(greeting_am('cdriver'))
This code retrieves the current date by using datetime.datetime.now()
and saves it to today
.
Next, the if
statement retrieves the current hour (today.hour
). If this value falls between the stated range, one (1) function is called in from greetings.py
: greeting_am
and executes.
The result is output to the terminal.
Output
Good Morning cdriver. |
Method 2: Import Two Functions from another Python File
If a Finxter user logs in between the stated range (today.hour >= 12 and today.hour <= 13)
, this code assumes it is the user’s lunch period and displays the greeting_lunch
and greeting_break()
messages.
from greetings import greeting_lunch, greeting_break today = datetime.datetime.now() if (today.hour >= 12 and today.hour <= 13): from greetings import greeting_lunch, greeting_break print(greeting_lunch('cdriver')) print(greeting_break())
This code retrieves the current date by using datetime.datetime.now()
and saves it to today
.
Next, the if
statement retrieves the current hour (today.hour
). If this value falls between the stated range, two (2) functions are called from greetings.py
: greeting_lunch
and greeting_break
, and execute.
The result is output to the terminal.
Output
Enjoy Lunch cdriver. You need a break. |
๐ก Note: If your code only requires a few functions from an external Python file, it may be best to call in just the ones you need.
Method 3: Import All Functions from another Python File
If a Finxter user logs in between the stated range (today.hour > 13 and today.hour < 17)
, this code assumes it is afternoon and displays the greeting_aft
message.
from greetings import * today = datetime.datetime.now() if (today.hour > 13 and today.hour < 17): from greetings import * print(greeting_aft('cdriver'))
This code retrieves the current date by using datetime.datetime.now()
and saves it to today
.
Next, the if
statement retrieves the current hour (today.hour
). If this value falls between the stated range, the entire contents of greetings.py
is called in (from greetings import *
) and greeting_aft
executes.
The result is output to the terminal.
Output
Good afternoon cdriver. |
Method 4: Use read() to import another Python file
If a Finxter user logs in between the stated range (today.hour > 17 and today.hour < 24)
, this code assumes it is evening and displays the greeting_pm
message.
today = datetime.datetime.now() if (today.hour > 17 and today.hour < 24): with open('greetings.py', 'r') as fp: exec(fp.read()) print(greeting_pm('cdriver'))
This code retrieves the current date by using datetime.datetime.now()
and saves it to today
.
Then the if statement retreives the current hour (today.hour
). If this value falls between the stated range, the entire content of greetings.py
is read in (fp.read()
), allowing access to all the contents of this file. For this example, greeting_pm
executes.
Finally, we execute the file using the (dangerous!) exec()
function.
๐ Learn More: Python exec() โ A Hackerโs Guide to A Dangerous Function
Pythonโs exec()
function executes the Python code you pass as a string or executable object argument.
This is called dynamic execution because, in contrast to normal static Python code, you can generate code and execute it at runtime.
This way, you can run programmatically-created Python code.
The result is output to the terminal.
Output
Good Evening cdriver. |
Bonus Script
Working with the greetings.py
file and the examples above, this code streamlines it down to one concise file.
import datetime from greetings import * today = datetime.datetime.now() if (today.hour >= 1 and today.hour < 12): print(greeting_am('cdriver')) elif (today.hour >= 12 and today.hour <= 13): print(greeting_lunch('cdriver')) print(greeting_break()) elif (today.hour > 13 and today.hour < 17): print(greeting_aft('cdriver')) else: print(greeting_pm('cdriver'))
โกFinxter Challenge: Modify the greetings.py file.