Skip to content

Commit 6a2ba46

Browse files
committed
TR update, first round
1 parent 9fb7d37 commit 6a2ba46

File tree

6 files changed

+30
-53
lines changed

6 files changed

+30
-53
lines changed
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class CustomList(list):
2-
def join(self, sep=" "):
3-
return sep.join(str(item) for item in self)
2+
def join(self, separator=" "):
3+
return separator.join(str(item) for item in self)
44

55
def map(self, action):
66
return type(self)(action(item) for item in self)
@@ -10,4 +10,4 @@ def filter(self, predicate):
1010

1111
def for_each(self, func):
1212
for item in self:
13-
yield func(item)
13+
func(item)

python-inherit-list-userlist/custom_list2.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33

44
class CustomList(UserList):
5-
def join(self, sep=" "):
6-
return sep.join(self.data)
5+
def join(self, separator=" "):
6+
return separator.join(str(item) for item in self)
77

88
def map(self, action):
99
return type(self)(action(item) for item in self.data)
@@ -13,4 +13,4 @@ def filter(self, predicate):
1313

1414
def for_each(self, func):
1515
for item in self.data:
16-
yield func(item)
16+
func(item)

python-inherit-list-userlist/number_list2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
class NumberList(UserList):
55
def __init__(self, iterable):
6-
self.data = [self._validate_number(item) for item in iterable]
6+
super().__init__(self._validate_number(item) for item in iterable)
77

88
def __setitem__(self, index, item):
99
self.data[index] = self._validate_number(item)

python-inherit-list-userlist/performance.py

Lines changed: 21 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def extend(self, other):
2424

2525
class StringList_UserList(UserList):
2626
def __init__(self, iterable):
27-
self.data = [str(item) for item in iterable]
27+
super().__init__(str(item) for item in iterable)
2828

2929
def __setitem__(self, index, item):
3030
self.data[index] = str(item)
@@ -44,51 +44,31 @@ def extend(self, other):
4444

4545
init_data = range(10000)
4646

47-
list_initialization = min(
48-
timeit.repeat(
49-
stmt="StringList_list(init_data)",
50-
number=1000,
51-
repeat=5,
52-
globals=globals(),
53-
)
54-
)
55-
56-
user_list_initialization = min(
57-
timeit.repeat(
58-
stmt="StringList_UserList(init_data)",
59-
number=1000,
60-
repeat=5,
61-
globals=globals(),
62-
)
63-
)
64-
65-
print(
66-
f"list is {list_initialization / user_list_initialization:.3f}",
67-
"times slower than UserList",
68-
)
69-
70-
7147
extended_list = StringList_list(init_data)
72-
list_extend = min(
73-
timeit.repeat(
74-
stmt="extended_list.extend(init_data)",
75-
number=5,
76-
repeat=2,
77-
globals=globals(),
48+
list_extend = (
49+
min(
50+
timeit.repeat(
51+
stmt="extended_list.extend(init_data)",
52+
number=5,
53+
repeat=2,
54+
globals=globals(),
55+
)
7856
)
57+
* 1e6
7958
)
8059

8160
extended_user_list = StringList_UserList(init_data)
82-
user_list_extend = min(
83-
timeit.repeat(
84-
stmt="extended_user_list.extend(init_data)",
85-
number=5,
86-
repeat=2,
87-
globals=globals(),
61+
user_list_extend = (
62+
min(
63+
timeit.repeat(
64+
stmt="extended_user_list.extend(init_data)",
65+
number=5,
66+
repeat=2,
67+
globals=globals(),
68+
)
8869
)
70+
* 1e6
8971
)
9072

91-
print(
92-
f"list is {list_extend / user_list_extend:.3f}",
93-
"times slower than UserList",
94-
)
73+
print(f"StringList_list().extend() time: {list_extend:.2f} μs")
74+
print(f"StringList_UserList().extend() time: {user_list_extend:.2f} μs")

python-inherit-list-userlist/string_list1.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
from collections import UserList
2-
3-
4-
class StringList(UserList):
1+
class StringList(list):
52
def __init__(self, iterable):
63
super().__init__(str(item) for item in iterable)
74

python-inherit-list-userlist/string_list2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
class StringList(UserList):
55
def __init__(self, iterable):
6-
self.data = [str(item) for item in iterable]
6+
super().__init__(str(item) for item in iterable)
77

88
def __setitem__(self, index, item):
99
self.data[index] = str(item)

0 commit comments

Comments
 (0)