Conditions | 7 |
Total Lines | 60 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | # Licensed to the StackStorm, Inc ('StackStorm') under one or more |
||
40 | def __call__(self, environ, start_response): |
||
41 | start_time = clock() |
||
42 | status_code = [] |
||
43 | content_length = [] |
||
44 | |||
45 | request = Request(environ) |
||
46 | |||
47 | # Log the incoming request |
||
48 | values = { |
||
49 | 'method': request.method, |
||
50 | 'path': request.path, |
||
51 | 'remote_addr': request.remote_addr, |
||
52 | 'query': request.GET.dict_of_lists(), |
||
53 | 'request_id': request.headers.get(REQUEST_ID_HEADER, None) |
||
54 | } |
||
55 | |||
56 | LOG.info('%(request_id)s - %(method)s %(path)s with query=%(query)s' % |
||
|
|||
57 | values, extra=values) |
||
58 | |||
59 | def custom_start_response(status, headers, exc_info=None): |
||
60 | status_code.append(int(status.split(' ')[0])) |
||
61 | |||
62 | for name, value in headers: |
||
63 | if name.lower() == 'content-length': |
||
64 | content_length.append(int(value)) |
||
65 | break |
||
66 | |||
67 | return start_response(status, headers, exc_info) |
||
68 | |||
69 | retval = self.app(environ, custom_start_response) |
||
70 | |||
71 | endpoint, path_vars = self.router.match(request) |
||
72 | |||
73 | log_result = endpoint.get('x-log-result', True) |
||
74 | |||
75 | if isinstance(retval, types.GeneratorType): |
||
76 | content_length = [float('inf')] |
||
77 | log_result = False |
||
78 | |||
79 | # Log the incoming request |
||
80 | values = { |
||
81 | 'method': request.method, |
||
82 | 'path': request.path, |
||
83 | 'remote_addr': request.remote_addr, |
||
84 | 'status': status_code[0], |
||
85 | 'runtime': float("{0:.3f}".format((clock() - start_time) * 10**3)), |
||
86 | 'content_length': content_length[0] if content_length else len(b''.join(retval)), |
||
87 | 'request_id': request.headers.get(REQUEST_ID_HEADER, None) |
||
88 | } |
||
89 | |||
90 | if log_result: |
||
91 | values['result'] = retval[0] |
||
92 | log_msg = '%(request_id)s - %(status)s %(content_length)s %(runtime)sms\n%(result)s'\ |
||
93 | % values |
||
94 | else: |
||
95 | log_msg = '%(request_id)s - %(status)s %(content_length)s %(runtime)sms' % values |
||
96 | |||
97 | LOG.info(log_msg, extra=values) |
||
98 | |||
99 | return retval |
||
100 |