forked from bregman-arie/python-exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
refactor_1.py
39 lines (27 loc) · 998 Bytes
/
refactor_1.py
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
#!/usr/bin/env python
# coding=utf-8
from collections import namedtuple
import timeit
def before_refactor():
Mushroom = namedtuple('Mushroom', ['name', 'poisonous'])
mushrooms = [Mushroom('Portabello', False), Mushroom('Oyster', False),
Mushroom('Death Cap', True)]
i = 0
for mushroom in mushrooms:
i += 1
name = mushroom.name
print('%d:"%s"' % (i, name))
def after_refactor(): # <- Solution is here! :)
Mushroom = namedtuple('Mushroom', ['name', 'poisonous'])
mushrooms = [Mushroom('Portabello', False), Mushroom('Oyster', False),
Mushroom('Death Cap', True)]
for i, mushroom in enumerate(mushrooms):
i += 1
name = mushroom.name
print('%d:"%s"' % (i, name))
print(timeit.timeit(
"before_refactor()",
setup="from __main__ import before_refactor", number=100))
print(timeit.timeit(
"after_refactor()",
setup="from __main__ import after_refactor", number=100))