Skip to content

Commit cafa46b

Browse files
committed
update style
1 parent 1d63cfe commit cafa46b

File tree

2 files changed

+52
-55
lines changed

2 files changed

+52
-55
lines changed

lectures/01-python/myprofile.py

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
1313
# start timing the 'my timer' block of code
1414
a.begin()
15-
15+
1616
... do stuff here ...
1717
1818
# end the timing of the 'my timer' block of code
@@ -33,78 +33,77 @@
3333
At present, no enforcement is done to ensure proper nesting.
3434
3535
"""
36-
36+
3737
from __future__ import print_function
3838

39-
import time
39+
import time
4040

4141
timers = {}
4242

4343
# keep basic count of how nested we are in the timers, so we can do some
4444
# pretty printing.
45-
stackCount = 0
45+
stack_count = 0
4646

47-
timerNesting = {}
48-
timerOrder = []
47+
timer_nesting = {}
48+
timer_order = []
4949

50-
class timer:
50+
class Timer(object):
5151

5252
def __init__ (self, name):
53-
global timers, stackCount, timerNesting, timerOrder
54-
53+
global timers, stack_count, timer_nesting, timer_order
54+
5555
self.name = name
5656

5757
keys = timers.keys()
5858

5959
if name not in keys:
6060
timers[name] = 0.0
6161
self.startTime = 0.0
62-
timerOrder.append(name)
63-
timerNesting[name] = stackCount
64-
62+
timer_order.append(name)
63+
timer_nesting[name] = stack_count
64+
6565

6666
def begin(self):
67-
global stackCount
68-
67+
global stack_count
68+
6969
self.startTime = time.time()
70-
stackCount += 1
70+
stack_count += 1
71+
7172

72-
7373
def end(self):
74-
global timers, stackCount
74+
global timers, stack_count
7575

7676
elapsedTime = time.time() - self.startTime
7777
timers[self.name] += elapsedTime
78-
79-
stackCount -= 1
80-
8178

82-
def timeReport():
83-
global timers, timerOrder, timerNesting
79+
stack_count -= 1
80+
81+
82+
def time_report():
83+
global timers, timer_order, timer_nesting
8484

8585
spacing = ' '
86-
for key in timerOrder:
87-
print(timerNesting[key]*spacing + key + ': ', timers[key])
86+
for key in timer_order:
87+
print(timer_nesting[key]*spacing + key + ': ', timers[key])
8888

8989

9090

91-
if __name__ == "__main__":
92-
a = timer('1')
91+
if __name__ == "__main__":
92+
a = Timer('1')
9393
a.begin()
9494
time.sleep(10.)
9595
a.end()
96-
97-
b = timer('2')
96+
97+
b = Timer('2')
9898
b.begin()
9999
time.sleep(5.)
100-
101-
c = timer('3')
100+
101+
c = Timer('3')
102102
c.begin()
103-
103+
104104
time.sleep(20.)
105-
105+
106106
b.end()
107107
c.end()
108-
109-
timeReport()
110108

109+
time_report()

lectures/01-python/python-modules.ipynb

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
},
2828
{
2929
"cell_type": "code",
30-
"execution_count": 3,
30+
"execution_count": 2,
3131
"metadata": {
3232
"collapsed": false
3333
},
@@ -45,7 +45,7 @@
4545
},
4646
{
4747
"cell_type": "code",
48-
"execution_count": 5,
48+
"execution_count": 3,
4949
"metadata": {
5050
"collapsed": false
5151
},
@@ -95,9 +95,9 @@
9595
"\n",
9696
"CLASSES\n",
9797
" builtins.object\n",
98-
" timer\n",
98+
" Timer\n",
9999
" \n",
100-
" class timer(builtins.object)\n",
100+
" class Timer(builtins.object)\n",
101101
" | Methods defined here:\n",
102102
" | \n",
103103
" | __init__(self, name)\n",
@@ -116,13 +116,13 @@
116116
" | list of weak references to the object (if defined)\n",
117117
"\n",
118118
"FUNCTIONS\n",
119-
" timeReport()\n",
119+
" time_report()\n",
120120
"\n",
121121
"DATA\n",
122122
" print_function = _Feature((2, 6, 0, 'alpha', 2), (3, 0, 0, 'alpha', 0)...\n",
123-
" stackCount = 0\n",
124-
" timerNesting = {}\n",
125-
" timerOrder = []\n",
123+
" stack_count = 0\n",
124+
" timer_nesting = {}\n",
125+
" timer_order = []\n",
126126
" timers = {}\n",
127127
"\n",
128128
"FILE\n",
@@ -145,7 +145,7 @@
145145
},
146146
{
147147
"cell_type": "code",
148-
"execution_count": 7,
148+
"execution_count": 4,
149149
"metadata": {
150150
"collapsed": false
151151
},
@@ -154,24 +154,22 @@
154154
"name": "stdout",
155155
"output_type": "stream",
156156
"text": [
157-
"main loop: 0.0028603076934814453\n",
157+
"main loop: 0.001512765884399414\n",
158158
"332833500.0\n"
159159
]
160160
}
161161
],
162162
"source": [
163-
"t = myprofile.timer(\"main loop\")\n",
163+
"t = myprofile.Timer(\"main loop\")\n",
164164
"t.begin()\n",
165165
"\n",
166-
"n = 0\n",
167166
"sum = 0.0\n",
168-
"while (n < 1000):\n",
167+
"for n in range(1000):\n",
169168
" sum += n**2\n",
170-
" n += 1\n",
171169
"\n",
172170
"\n",
173171
"t.end()\n",
174-
"myprofile.timeReport()\n",
172+
"myprofile.time_report()\n",
175173
"\n",
176174
"print(sum)"
177175
]
@@ -195,7 +193,7 @@
195193
},
196194
{
197195
"cell_type": "code",
198-
"execution_count": 9,
196+
"execution_count": 5,
199197
"metadata": {
200198
"collapsed": false
201199
},
@@ -204,9 +202,9 @@
204202
"name": "stdout",
205203
"output_type": "stream",
206204
"text": [
207-
"1: 10.010079145431519\n",
208-
"2: 25.018166065216064\n",
209-
" 3: 20.014001846313477\n"
205+
"1: 10.00622272491455\n",
206+
"2: 25.006877899169922\n",
207+
" 3: 20.00178837776184\n"
210208
]
211209
}
212210
],
@@ -216,7 +214,7 @@
216214
},
217215
{
218216
"cell_type": "code",
219-
"execution_count": 4,
217+
"execution_count": null,
220218
"metadata": {
221219
"collapsed": false
222220
},
@@ -225,7 +223,7 @@
225223
},
226224
{
227225
"cell_type": "code",
228-
"execution_count": 4,
226+
"execution_count": null,
229227
"metadata": {
230228
"collapsed": false
231229
},

0 commit comments

Comments
 (0)