Conditions | 18 |
Total Lines | 58 |
Lines | 0 |
Ratio | 0 % |
Changes | 5 | ||
Bugs | 1 | 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:
Complex classes like stringify() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | # -*- coding: utf-8 -*- |
||
89 | def stringify(obj): |
||
90 | def bottle_builder(builder, bottle_object): |
||
91 | builder.append("{0} {1}{2} {3}".format(bottle_object.method, |
||
92 | bottle_object.path, |
||
93 | bottle_object.environ.get('QUERY_STRING', ''), |
||
94 | bottle_object.get('SERVER_PROTOCOL'))) |
||
95 | builder += ["{0}: {1}".format(key, value) for key, value in bottle_object.headers.items()] |
||
96 | builder.append('') |
||
97 | body = bottle_object.body.read().strip() |
||
98 | if body: |
||
99 | builder.append(body) |
||
100 | |||
101 | def requests_models_PreparedRequest_builder(builder, request_object): |
||
102 | builder.append(">>{0} {1} {2}".format(request_object.method, request_object.path_url, |
||
103 | request_object.url.split(':', 1)[0].upper())) |
||
104 | builder.extend("> {0}: {1}".format(key, value) |
||
105 | for key, value in sorted(request_object.headers.items(), |
||
106 | key=request_header_sort_key)) |
||
107 | builder.append('') |
||
108 | if request_object.body: |
||
109 | builder.append(request_object.body) |
||
110 | |||
111 | def requests_models_Response_builder(builder, response_object, include_content=False): |
||
112 | builder.append("<<{0} {1} {2}".format(response_object.url.split(':', 1)[0].upper(), |
||
113 | response_object.status_code, response_object.reason)) |
||
114 | builder.extend("< {0}: {1}".format(key, value) |
||
115 | for key, value in sorted(response_object.headers.items(), |
||
116 | key=response_header_sort_key)) |
||
117 | elapsed = text_type(response_object.elapsed).split(':', 1)[-1] |
||
118 | builder.append('< Elapsed: {0}'.format(elapsed)) |
||
119 | if include_content: |
||
120 | builder.append('') |
||
121 | content_type = response_object.headers.get('Content-Type') |
||
122 | if content_type == 'application/json': |
||
123 | builder.append(pformat(response_object.json(), indent=2)) |
||
124 | builder.append('') |
||
125 | elif content_type is not None and content_type.startswith('text/'): |
||
126 | builder.append(response_object.text) |
||
127 | |||
128 | try: |
||
129 | name = fullname(obj) |
||
130 | builder = [''] # start with new line |
||
131 | if name.startswith('bottle.'): |
||
132 | bottle_builder(builder, obj) |
||
133 | elif name.endswith('requests.models.PreparedRequest'): |
||
134 | requests_models_PreparedRequest_builder(builder, obj) |
||
135 | elif name.endswith('requests.models.Response'): |
||
136 | if getattr(obj, 'request'): |
||
137 | requests_models_PreparedRequest_builder(builder, obj.request) |
||
138 | else: |
||
139 | log.info("request is 'None' for Response object with url %s", obj.url) |
||
140 | requests_models_Response_builder(builder, obj) |
||
141 | else: |
||
142 | return None |
||
143 | builder.append('') # end with new line |
||
144 | return "\n".join(builder) |
||
145 | except Exception as e: |
||
146 | log.exception(e) |
||
147 |