-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path28_functions_use_cases.py
More file actions
82 lines (66 loc) · 3.08 KB
/
Copy path28_functions_use_cases.py
File metadata and controls
82 lines (66 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#======================================================================================
# Types of Functions Based on Purposes or Use Cases
#======================================================================================
# 1) Action Function
# 2) Transformation Function
# 3) Validation Function
# 4) Orchestration Function
#======================================================================================
#--------------------------------------------------------------------------------------
# 1) Action Function
#--------------------------------------------------------------------------------------
# Designed to perform an operation in the system instead of running values.
#--------------------------------------------------------------------------------------
# TASK: Store application log messages in a file whenever an even occurs
def write_log(message):
with open(r'app.log', 'a') as file:
file.write(message + '\n')
write_log('App Started')
write_log('User Logged in')
write_log('App Stopped')
#--------------------------------------------------------------------------------------
# 2) Transformation Function
# Raw data goes in, gets transformed, and returns processed data
#--------------------------------------------------------------------------------------
# TASK: Clean email addresses and splits them into structured data(username and domain)
def clean_and_split_email(email):
clean_email = email.strip().lower()
username, domain = clean_email.split('@')
return {'username':username,
'domain':domain}
result =clean_and_split_email('Sara@gmail.com')
print(result)
#--------------------------------------------------------------------------------------
# 3) Validation Function
# Validates a condition and returns a boolean result(True or False)
#--------------------------------------------------------------------------------------
#TASK: Check if a password meets the minimum length of 8 characters
def is_valid_password(password):
return len(password) > 8
print(is_valid_password('abc123'))
print(is_valid_password('abcd123456'))
#TASK: Check whether an email has a basic valid format.
def is_valid_email(email):
return '@' in email and '.' in email
print(is_valid_email('sara@gmail,com'))
print(is_valid_email('sara@gmail.com'))
#--------------------------------------------------------------------------------------
# 4) Orchestrator Function
# Controls program flow by calling other functions in the correct order
#--------------------------------------------------------------------------------------
# TASK:
# 1) Receive an email from the user
# 2) Validate the email
# 3) If it is invalid, Log an error in a file
# 4) If it is valid, Clean and structure the emil
# 5) Log each step of the program
def process_user_email(email):
write_log('App Started')
if not is_valid_email:
write_log(f'Invalid Email received: {email}')
else:
clean_email = clean_and_split_email(email)
write_log(f'Processed Email:{clean_email}')
write_log('App Stopped')
email = input('Please enter your email: ')
process_user_email(email)