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
179 lines (127 loc) · 4.59 KB
/
main.py
File metadata and controls
179 lines (127 loc) · 4.59 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# 1. bisect
from bisect import bisect_left, bisect_right, insort
# Let's create a grade tracking system
grades = [60, 70, 75, 85, 90, 95]
# Find where to insert a new grade while keeping the list sorted
new_grade = 82
position = bisect_left(grades, new_grade)
print(f"Insert 82 at position: {position}")
# Insert while maintaining sort order
insort(grades, new_grade)
print(f"Grades after insertion: {grades}")
# Find grade ranges
def grade_to_letter(score):
breakpoints = [60, 70, 80, 90] # F, D, C, B, A
grades = 'FDCBA'
position = bisect_right(breakpoints, score)
return grades[position]
print(f"Score 82 gets grade: {grade_to_letter(82)}")
print(f"Score 75 gets grade: {grade_to_letter(75)}")
# 2. itertools.pairwise
from itertools import pairwise
# Let's analyze temperature changes
temperatures = [20, 23, 24, 25, 23, 22, 20]
# Calculate temperature changes between consecutive readings
changes = []
for prev, curr in pairwise(temperatures):
change = curr - prev
changes.append(change)
print("Temperature changes:", changes)
# Calculate moving averages
moving_averages = []
for t1, t2 in pairwise(temperatures):
avg = (t1 + t2) / 2
moving_averages.append(avg)
print("Moving averages:", moving_averages)
# Finding the largest temperature jump
max_jump = max(abs(b - a) for a, b in pairwise(temperatures))
print(f"Largest temperature change: {max_jump} degrees")
# 3. statistics.fmean
from statistics import mean, fmean
import time
# Let's compare fmean with traditional mean using a real-world example
# Imagine we're analyzing daily temperature readings
temperatures = [
21.5, 22.1, 23.4, 22.8, 21.8,
23.2, 22.7, 23.1, 22.6, 21.9
] * 100000 # Create a large dataset
# Let's compare speed and precision
start_time = time.perf_counter()
regular_mean = mean(temperatures)
regular_time = time.perf_counter() - start_time
start_time = time.perf_counter()
fast_mean = fmean(temperatures)
fast_time = time.perf_counter() - start_time
print(f"Regular mean: {regular_mean:.10f} (took {regular_time:.4f} seconds)")
print(f"fmean: {fast_mean:.10f} (took {fast_time:.4f} seconds)")
# 4. itertools.takewhile
from itertools import takewhile
# Processing log entries until an error
log_entries = [
"INFO: System started",
"INFO: Loading data",
"INFO: Processing users",
"ERROR: Database connection failed",
"INFO: Retrying connection",
]
# Get all logs until first error
normal_operation = list(takewhile(
lambda x: not x.startswith("ERROR"),
log_entries
))
print("Logs before first error:")
for entry in normal_operation:
print(entry)
# 5. operator.attrgettr
from operator import attrgetter
from datetime import datetime
# Let's create a simple class to demonstrate
class Article:
def __init__(self, title, author, views, date):
self.title = title
self.author = author
self.stats = type('Stats', (), {'views': views}) # Nested attribute
self.date = date
def __repr__(self):
return f"{self.title} by {self.author}"
# Create some sample articles
articles = [
Article("Python Tips", "Alice", 1500, datetime(2025, 1, 15)),
Article("Data Science", "Bob", 2500, datetime(2025, 1, 20)),
Article("Web Dev", "Alice", 1800, datetime(2025, 1, 10))
]
# Sort articles by multiple criteria
get_author_views = attrgetter('author', 'stats.views')
# Sort by author and then by views
sorted_articles = sorted(articles, key=get_author_views)
for article in sorted_articles:
print(f"{article.author}: {article.title} ({article.stats.views} views)")
# You can also use it to extract specific attributes
dates = list(map(attrgetter('date'), articles))
print("\nArticle dates:", dates)
# 6. itertools.chain
from itertools import chain
# Let's say we're processing data from multiple sources
sales_data = [
[('Jan', 100), ('Feb', 150)],
[('Mar', 200), ('Apr', 180)],
[('May', 210), ('Jun', 190)]
]
# Flatten the data efficiently
flat_sales = list(chain.from_iterable(sales_data))
print("Flattened sales data:", flat_sales)
# List comprehension approach (creates intermediate list):
flat_list = [item for sublist in sales_data for item in sublist]
# chain.from_iterable approach (generates items one at a time):
flat_iterator = chain.from_iterable(sales_data)
# 7. itertools.product
from itertools import product
# Available options for a custom laptop
processors = ['i5', 'i7', 'i9']
ram = ['8GB', '16GB', '32GB']
storage = ['256GB', '512GB', '1TB']
# Generate all possible combinations
configurations = list(product(processors, ram, storage))
print("Possible laptop configurations:")
for config in configurations:
print(f"Processor: {config[0]}, RAM: {config[1]}, Storage: {config[2]}")