-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreview.py
More file actions
191 lines (134 loc) · 3.77 KB
/
Copy pathreview.py
File metadata and controls
191 lines (134 loc) · 3.77 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
179
180
181
182
183
184
185
186
187
188
189
190
191
# Review 1
def add_to_list(value, my_list=[]):
my_list.append(value)
return my_list
"""
mutable object should not be used as default argument,
otherwise the second time you call the function the my_list argument will contain the value from the first call,
as shown below.
"""
def add_to_list_fix(value, my_list=None):
if my_list is None:
my_list = []
my_list.append(value)
return my_list
# Review 2
def format_greeting(name, age):
return "Hello, my name is {name} and I am {age} years old."
"""
improper usage of format string
"""
def format_greeting_fix(name, age):
return f"Hello, my name is {name} and I am {age} years old."
# Review 3
class Counter:
count = 0
def __init__(self):
self.count += 1
def get_count(self):
return self.count
class CounterFix:
count = 0
def __init__(self):
self.__class__.count += 1
@classmethod
def get_count(cls):
return cls.count
"""
The problem is that the author confused the usage of class attribute and instance attribute
In the __init__ method the += operator will get the initial value of self.count from cls.count because self.count are not defined before,
add 1 and then assign to self.count.
Therefore, cls.count is left unchanged, while self.count is created with value 1 every time a object is instantiated.
"""
# Review 4
import threading
class SafeCounter:
def __init__(self):
self.count = 0
def increment(self):
self.count += 1
class SafeCounterFix:
def __init__(self):
self.count = 0
self.lock = threading.Lock()
def increment(self):
with self.lock:
self.count += 1
def worker(counter):
for _ in range(1000):
counter.increment()
counter = SafeCounter()
threads = []
for _ in range(10):
t = threading.Thread(target=worker, args=(counter,))
t.start()
threads.append(t)
for t in threads:
t.join()
"""
The problem is that raise condition is not considered.
the increment method is not a atomic operation, and not thread-safe, but called in multiple threads,
"""
# Review 5
def count_occurrences(lst):
counts = {}
for item in lst:
if item in counts:
counts[item] =+ 1
else:
counts[item] = 1
return counts
def count_occurrences_fix(lst):
counts = {}
for item in lst:
if item in counts:
counts[item] += 1
else:
counts[item] = 1
return counts
import collections
def count_occurrences_fast(lst):
return collections.Counter(lst)
"""
'+=' should be used, not '=+'.
What's more, Counter from collections is a better tool for this job
"""
if __name__ == "__main__":
print("==================== review 1")
print(add_to_list(4))
print(add_to_list(5))
print(add_to_list_fix(4))
print(add_to_list_fix(5))
print("==================== review 3")
c1 = Counter()
print(c1.get_count())
c2 = Counter()
print(c1.get_count())
cf1 = CounterFix()
print(cf1.get_count())
cf2 = CounterFix()
print(cf2.get_count())
print("==================== review 4")
counter = SafeCounter()
threads = []
for _ in range(10):
t = threading.Thread(target=worker, args=(counter,))
t.start()
threads.append(t)
for t in threads:
t.join()
print(counter.count)
counter_fix = SafeCounterFix()
threads = []
for _ in range(10):
t = threading.Thread(target=worker, args=(counter_fix,))
t.start()
threads.append(t)
for t in threads:
t.join()
print(counter_fix.count)
print("==================== review 5")
l = [1,2,3,3,4,5,5,5]
print(count_occurrences(l))
print(count_occurrences_fix(l))
print(count_occurrences_fast(l))