forked from balapriyac/python-basics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
153 lines (118 loc) · 4.46 KB
/
main.py
File metadata and controls
153 lines (118 loc) · 4.46 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# instead of this
def process_sales_data(sales):
highest_sale = sales[0]
for sale in sales:
if sale > highest_sale:
highest_sale = sale
total_sales = 0
for sale in sales:
total_sales += sale
return highest_sale, total_sales, total_sales / len(sales)
# do this
def process_sales_data(sales):
return max(sales), sum(sales), sum(sales) / len(sales)
# Instead of this
def get_premium_customer_emails(customers):
premium_emails = []
for customer in customers:
if customer['membership_level'] == 'premium' and customer['active']:
email = customer['email'].lower().strip()
premium_emails.append(email)
return premium_emails
# Do this
def get_premium_customer_emails(customers):
return [
customer['email'].lower().strip()
for customer in customers
if customer['membership_level'] == 'premium' and customer['active']
]
# Instead of this
def has_permission(user_id, permitted_users):
# permitted_users is a list of user IDs
for p_user in permitted_users:
if p_user == user_id:
return True
return False
# Usage:
permitted_users = [1001, 1023, 1052, 1076, 1088, 1095, 1102, 1109]
print(has_permission(1088, permitted_users)) # True
# Do this
def has_permission(user_id, permitted_users):
# permitted_users is now a set of user IDs
return user_id in permitted_users
# Usage:
permitted_users = {1001, 1023, 1052, 1076, 1088, 1095, 1102, 1109}
print(has_permission(1088, permitted_users)) # True
# Instead of this
def find_errors(log_file):
with open(log_file, 'r') as file:
lines = file.readlines()
error_messages = []
for line in lines:
if '[ERROR]' in line:
timestamp = line.split('[ERROR]')[0].strip()
message = line.split('[ERROR]')[1].strip()
error_messages.append((timestamp, message))
return error_messages
# Do this
def find_errors(log_file):
with open(log_file, 'r') as file:
for line in file:
if '[ERROR]' in line:
timestamp = line.split('[ERROR]')[0].strip()
message = line.split('[ERROR]')[1].strip()
yield (timestamp, message)
# Usage:
for timestamp, message in find_errors('application.log'):
print(f"Error at {timestamp}: {message}")
# Instead of this
import re
from datetime import datetime
def find_recent_errors(logs):
recent_errors = []
for log in logs:
# This regex compilation happens on every iteration
timestamp_pattern = re.compile(r'\[(.*?)\]')
timestamp_match = timestamp_pattern.search(log)
if timestamp_match and '[ERROR]' in log:
# The datetime parsing happens on every iteration
log_time = datetime.strptime(timestamp_match.group(1), '%Y-%m-%d %H:%M:%S')
current_time = datetime.now()
# Check if the log is from the last 24 hours
time_diff = (current_time - log_time).total_seconds() / 3600
if time_diff <= 24:
recent_errors.append(log)
return recent_errors
# do this
import re
from datetime import datetime
def find_recent_errors(logs):
recent_errors = []
# Compile the regex once
timestamp_pattern = re.compile(r'\[(.*?)\]')
# Get the current time once
current_time = datetime.now()
for log in logs:
timestamp_match = timestamp_pattern.search(log)
if timestamp_match and '[ERROR]' in log:
log_time = datetime.strptime(timestamp_match.group(1), '%Y-%m-%d %H:%M:%S')
# Check if the log is recent (last 24 hours)
time_diff = (current_time - log_time).total_seconds() / 3600
if time_diff <= 24:
recent_errors.append(log)
return recent_errors
# instead of this
def generate_html_report(data_points):
html = "<html><body><h1>Data Report</h1><ul>"
for point in data_points:
# This creates a new string object on each iteration
html += f"<li>{point['name']}: {point['value']} ({point['timestamp']})</li>"
html += "</ul></body></html>"
return html
# do this
def generate_html_report(data_points):
parts = ["<html><body><h1>Data Report</h1><ul>"]
for point in data_points:
parts.append(f"<li>{point['name']}: {point['value']} ({point['timestamp']})</li>")
parts.append("</ul></body></html>")
return "".join(parts)