| Conditions | 16 |
| Total Lines | 52 |
| 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): |
||
| 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 | builder.append('') |
||
| 118 | content_type = response_object.headers.get('Content-Type') |
||
| 119 | if content_type == 'application/json': |
||
| 120 | builder.append(pformat(response_object.json, indent=2)) |
||
| 121 | builder.append('') |
||
| 122 | elif content_type is not None and content_type.startswith('text/'): |
||
| 123 | builder.append(response_object.text) |
||
| 124 | |||
| 125 | try: |
||
| 126 | name = fullname(obj) |
||
| 127 | builder = [''] # start with new line |
||
| 128 | if name.startswith('bottle.'): |
||
| 129 | bottle_builder(builder, obj) |
||
| 130 | elif name.endswith('requests.models.PreparedRequest'): |
||
| 131 | requests_models_PreparedRequest_builder(builder, obj) |
||
| 132 | elif name.endswith('requests.models.Response'): |
||
| 133 | requests_models_PreparedRequest_builder(builder, obj.request) |
||
| 134 | requests_models_Response_builder(builder, obj) |
||
| 135 | else: |
||
| 136 | return None |
||
| 137 | builder.append('') # end with new line |
||
| 138 | return "\n".join(builder) |
||
| 139 | except Exception as e: |
||
| 140 | log.exception(e) |
||
| 141 |