Conditions | 13 |
Total Lines | 61 |
Code Lines | 35 |
Lines | 61 |
Ratio | 100 % |
Changes | 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 glances.exports.glances_influxdb.Export._normalize() 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 | View Code Duplication | def _normalize(self, name, columns, points): |
|
90 | """Normalize data for the InfluxDB's data model. |
||
91 | |||
92 | :return: a list of measurements. |
||
93 | """ |
||
94 | ret = [] |
||
95 | |||
96 | # Build initial dict by crossing columns and point |
||
97 | data_dict = dict(zip(columns, points)) |
||
98 | |||
99 | # issue1871 - Check if a key exist. If a key exist, the value of |
||
100 | # the key should be used as a tag to identify the measurement. |
||
101 | keys_list = [k.split('.')[0] for k in columns if k.endswith('.key')] |
||
102 | if len(keys_list) == 0: |
||
103 | keys_list = [None] |
||
104 | |||
105 | for measurement in keys_list: |
||
106 | # Manage field |
||
107 | if measurement is not None: |
||
108 | fields = { |
||
109 | k.replace('{}.'.format(measurement), ''): data_dict[k] |
||
110 | for k in data_dict |
||
111 | if k.startswith('{}.'.format(measurement)) |
||
112 | } |
||
113 | else: |
||
114 | fields = data_dict |
||
115 | # Transform to InfluxDB data model |
||
116 | # https://docs.influxdata.com/influxdb/v1.8/write_protocols/line_protocol_reference/ |
||
117 | for k in fields: |
||
118 | # Do not export empty (None) value |
||
119 | if fields[k] is None: |
||
120 | continue |
||
121 | # Convert numerical to float |
||
122 | try: |
||
123 | fields[k] = float(fields[k]) |
||
124 | except (TypeError, ValueError): |
||
125 | # Convert others to string |
||
126 | try: |
||
127 | fields[k] = str(fields[k]) |
||
128 | except (TypeError, ValueError): |
||
129 | pass |
||
130 | # Manage tags |
||
131 | tags = self.parse_tags(self.tags) |
||
132 | if 'key' in fields and fields['key'] in fields: |
||
133 | # Create a tag from the key |
||
134 | # Tag should be an string (see InfluxDB data model) |
||
135 | tags[fields['key']] = str(fields[fields['key']]) |
||
136 | # Remove it from the field list (can not be a field and a tag) |
||
137 | fields.pop(fields['key']) |
||
138 | # Add the hostname as a tag |
||
139 | tags['hostname'] = self.hostname |
||
140 | # Add name as a tag (example for the process list) |
||
141 | for k in FIELD_TO_TAG: |
||
142 | if k in fields: |
||
143 | tags[k] = str(fields[k]) |
||
144 | # Remove it from the field list (can not be a field and a tag) |
||
145 | if k in fields: |
||
146 | fields.pop(fields[k]) |
||
147 | # Add the measurement to the list |
||
148 | ret.append({'measurement': name, 'tags': tags, 'fields': fields}) |
||
149 | return ret |
||
150 | |||
167 |