-
Notifications
You must be signed in to change notification settings - Fork 0
/
py-codenvy-cheatsheet.py
120 lines (101 loc) · 3.84 KB
/
py-codenvy-cheatsheet.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
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
# Copyright 2014 Arn-O. See the LICENSE file at the top-level directory of this
# distribution and at
# https://github.com/Arn-O/py-codenvy-cheatsheet/blob/master/LICENSE
import logging
import timeit
import time
import webapp2
from google.appengine.api import memcache
from google.appengine.api import taskqueue
MEMCACHE_I_KEY = 'cached_i'
MEMCACHE_DIVISORS_KEY = 'cached_divisors'
def get_divisors(number):
'''Compute the divisors and the duration time (ms).'''
time_start = timeit.default_timer()
divisors = []
for i in range(1, int(number/2) + 1):
if not number%i:
divisors.append(i)
time_end = timeit.default_timer()
duration = (time_end - time_start) * 1000
return divisors, duration
class DivisorWorker(webapp2.RequestHandler):
def post(self):
logging.getLogger().setLevel(logging.INFO)
divisors, duration = get_divisors(10000000)
logging.info('Divisors: %s', str(divisors))
logging.info('Duration: %s ms', str(duration))
logging.getLogger().setLevel(logging.WARNING)
class MainPage(webapp2.RequestHandler):
def get(self):
output_str = 'Hello, world!'
self.response.headers['Content-Type'] = 'text/plain'
self.response.write(output_str)
class Stone1(webapp2.RequestHandler):
def get(self):
output_str = 'Bonjour, monde!'
output_str += '\n'
output_str += 'This is basically like formating a flat file'
output_str += '\n'
output_str += 'And remember all kind of variable can be displayed like a string'
self.response.headers['Content-Type'] = 'text/plain'
self.response.write(output_str)
class Stone2(webapp2.RequestHandler):
def get(self):
divisors, duration = get_divisors(10000)
output_str = str(divisors) + '\n'
output_str += 'Total computation time: ' + str(duration) + ' ms'
self.response.headers['Content-Type'] = 'text/plain'
self.response.write(output_str)
class Stone3(webapp2.RequestHandler):
def get(self):
number = 1000
i = memcache.get(MEMCACHE_I_KEY)
divisors = memcache.get(MEMCACHE_DIVISORS_KEY)
if i is None:
i = 1
divisors = []
output_str = 'Number to divide: ' + str(number) + '\n'
output_str += 'Iteration: ' + str(i) + ' - '
if not number%i:
output_str += 'New divisor found!' + '\n'
divisors.append(i)
else:
output_str += 'This is not a divisor' + '\n'
output_str += str(divisors) + '\n'
if i < (int(number/2) + 1):
i += 1
else:
output_str += 'Last iteration' + '\n'
memcache.set(MEMCACHE_I_KEY, i)
memcache.set(MEMCACHE_DIVISORS_KEY, divisors)
self.response.headers['Content-Type'] = 'text/plain'
self.response.write(output_str)
class Stone4(webapp2.RequestHandler):
def get(self):
output_str = 'Log level set at DEBUG just for this stone' + '\n'
logging.getLogger().setLevel(logging.DEBUG)
logging.debug('Debug message generated by the stone 4')
logging.info('Info message generated by the stone 4')
logging.warning('Warning message generated by the stone 4')
logging.error('Warning message generated by the stone 4')
logging.critical('Critical message generated by the stone 4')
output_str += 'The messages are displayed in the Administration Console'
logging.getLogger().setLevel(logging.WARNING)
self.response.headers['Content-Type'] = 'text/plain'
self.response.write(output_str)
class Stone5(webapp2.RequestHandler):
def get(self):
taskqueue.add(url='/divisorworker')
output_str = 'Task added, check the Administration Console'
self.response.headers['Content-Type'] = 'text/plain'
self.response.write(output_str)
application = webapp2.WSGIApplication([
('/', MainPage),
('/stone1', Stone1),
('/stone2', Stone2),
('/stone3', Stone3),
('/stone4', Stone4),
('/stone5', Stone5),
('/divisorworker', DivisorWorker),
], debug=True)