| Total Complexity | 44 | 
| Total Lines | 106 | 
| Duplicated Lines | 0 % | 
Complex classes like ResultSets 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 | import boto  | 
            ||
| 121 | class ResultSets(object):  | 
            ||
| 122 | |||
| 123 | def __init__(self):  | 
            ||
| 124 | self.foo = ''  | 
            ||
| 125 | |||
| 126 | def selector(self, output):  | 
            ||
| 127 | if isinstance(output, boto.ec2.instance.Reservation):  | 
            ||
| 128 | return self.parseReservation(output)  | 
            ||
| 129 | elif isinstance(output, boto.ec2.instance.Instance):  | 
            ||
| 130 | return self.parseInstance(output)  | 
            ||
| 131 | elif isinstance(output, boto.ec2.volume.Volume):  | 
            ||
| 132 | return self.parseVolume(output)  | 
            ||
| 133 | elif isinstance(output, boto.ec2.blockdevicemapping.BlockDeviceType):  | 
            ||
| 134 | return self.parseBlockDeviceType(output)  | 
            ||
| 135 | elif isinstance(output, boto.ec2.zone.Zone):  | 
            ||
| 136 | return self.parseEC2Zone(output)  | 
            ||
| 137 | elif isinstance(output, boto.ec2.address.Address):  | 
            ||
| 138 | return self.parseAddress(output)  | 
            ||
| 139 | elif isinstance(output, boto.route53.record.Record):  | 
            ||
| 140 | return self.parseRecord(output)  | 
            ||
| 141 | elif isinstance(output, boto.route53.zone.Zone):  | 
            ||
| 142 | return self.parseR53Zone(output)  | 
            ||
| 143 | elif isinstance(output, boto.route53.status.Status):  | 
            ||
| 144 | return self.parseR53Status(output)  | 
            ||
| 145 | elif isinstance(output, boto.ec2.ec2object.EC2Object):  | 
            ||
| 146 | return self.parseEC2Object(output)  | 
            ||
| 147 | else:  | 
            ||
| 148 | return output  | 
            ||
| 149 | |||
| 150 | def formatter(self, output):  | 
            ||
| 151 | if isinstance(output, list):  | 
            ||
| 152 | return [self.formatter(item) for item in output]  | 
            ||
| 153 | elif isinstance(output, dict):  | 
            ||
| 154 |             return {key: self.formatter(value) for key, value in six.iteritems(output)} | 
            ||
| 155 | else:  | 
            ||
| 156 | return self.selector(output)  | 
            ||
| 157 | |||
| 158 | def parseReservation(self, output):  | 
            ||
| 159 | instance_list = []  | 
            ||
| 160 | for instance in output.instances:  | 
            ||
| 161 | instance_data = self.parseInstance(instance)  | 
            ||
| 162 | instance_data['owner_id'] = output.owner_id  | 
            ||
| 163 | instance_list.append(instance_data)  | 
            ||
| 164 | return instance_list  | 
            ||
| 165 | |||
| 166 | def parseAddress(self, output):  | 
            ||
| 167 |         instance_data = {field: getattr(output, field) for field in FieldLists.ADDRESS} | 
            ||
| 168 | return instance_data  | 
            ||
| 169 | |||
| 170 | def parseInstance(self, output):  | 
            ||
| 171 |         instance_data = {field: getattr(output, field) for field in FieldLists.INSTANCE} | 
            ||
| 172 | return instance_data  | 
            ||
| 173 | |||
| 174 | def parseVolume(self, output):  | 
            ||
| 175 |         volume_data = {field: getattr(output, field) for field in FieldLists.VOLUME} | 
            ||
| 176 | return volume_data  | 
            ||
| 177 | |||
| 178 | def parseBlockDeviceType(self, output):  | 
            ||
| 179 |         data = {field: getattr(output, field) for field in FieldLists.BLOCK_DEVICE_TYPE} | 
            ||
| 180 | return data  | 
            ||
| 181 | |||
| 182 | def parseEC2Zone(self, output):  | 
            ||
| 183 |         zone_data = {field: getattr(output, field) for field in FieldLists.EC2ZONE} | 
            ||
| 184 | return zone_data  | 
            ||
| 185 | |||
| 186 | def parseRecord(self, output):  | 
            ||
| 187 |         record_data = {field: getattr(output, field) for field in FieldLists.RECORD} | 
            ||
| 188 | return record_data  | 
            ||
| 189 | |||
| 190 | def parseR53Zone(self, output):  | 
            ||
| 191 |         zone_data = {field: getattr(output, field) for field in FieldLists.R53ZONE} | 
            ||
| 192 | return zone_data  | 
            ||
| 193 | |||
| 194 | def parseR53Status(self, output):  | 
            ||
| 195 |         status_data = {field: getattr(output, field) for field in FieldLists.R53STATUS} | 
            ||
| 196 | return status_data  | 
            ||
| 197 | |||
| 198 | def parseBucket(self, output):  | 
            ||
| 199 |         bucket_data = {field: getattr(output, field) for field in FieldLists.BUCKET} | 
            ||
| 200 | return bucket_data  | 
            ||
| 201 | |||
| 202 | def parseEC2Object(self, output):  | 
            ||
| 203 | # Looks like everything that is an EC2Object pretty much only has these extra  | 
            ||
| 204 | # 'unparseable' properties so handle region and connection specially.  | 
            ||
| 205 | output = vars(output)  | 
            ||
| 206 | del output['connection']  | 
            ||
| 207 | # special handling for region since name here is better than id.  | 
            ||
| 208 |         region = output.get('region', None) | 
            ||
| 209 | output['region'] = region.name if region else ''  | 
            ||
| 210 | # now anything that is an EC2Object get some special marshalling care.  | 
            ||
| 211 | for k, v in six.iteritems(output):  | 
            ||
| 212 | if isinstance(v, boto.ec2.ec2object.EC2Object):  | 
            ||
| 213 | # Better not to assume each EC2Object has an id. If not found  | 
            ||
| 214 | # resort to the str of the object which should have something meaningful.  | 
            ||
| 215 | output[k] = getattr(v, 'id', str(v))  | 
            ||
| 216 | # Generally unmarshallable object might be hiding in list so better to  | 
            ||
| 217 | if isinstance(v, list):  | 
            ||
| 218 | v_list = []  | 
            ||
| 219 | for item in v:  | 
            ||
| 220 | # avoid touching the basic types.  | 
            ||
| 221 | if isinstance(item, (basestring, bool, int, long, float)):  | 
            ||
| 222 | v_list.append(v)  | 
            ||
| 223 | else:  | 
            ||
| 224 | v_list.append(str(item))  | 
            ||
| 225 | output[k] = v_list  | 
            ||
| 226 | return output  | 
            ||
| 227 |