| Conditions | 10 |
| Total Lines | 68 |
| Code Lines | 36 |
| Lines | 0 |
| Ratio | 0 % |
| 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.plugins.smart.get_smart_data() 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 | # |
||
| 79 | def get_smart_data(): |
||
| 80 | """ |
||
| 81 | Get SMART attribute data |
||
| 82 | :return: list of multi leveled dictionaries |
||
| 83 | each dict has a key "DeviceName" with the identification of the device in smartctl |
||
| 84 | also has keys of the SMART attribute id, with value of another dict of the attributes |
||
| 85 | [ |
||
| 86 | { |
||
| 87 | "DeviceName": "/dev/sda blahblah", |
||
| 88 | "1": |
||
| 89 | { |
||
| 90 | "flags": "..", |
||
| 91 | "raw": "..", |
||
| 92 | etc, |
||
| 93 | } |
||
| 94 | ... |
||
| 95 | } |
||
| 96 | ] |
||
| 97 | """ |
||
| 98 | stats = [] |
||
| 99 | # get all devices |
||
| 100 | try: |
||
| 101 | devlist = DeviceList() |
||
| 102 | except TypeError as e: |
||
| 103 | # Catch error (see #1806) |
||
| 104 | logger.debug(f'Smart plugin error - Can not grab device list ({e})') |
||
| 105 | global import_error_tag |
||
| 106 | import_error_tag = True |
||
| 107 | return stats |
||
| 108 | |||
| 109 | for dev in devlist.devices: |
||
| 110 | stats.append( |
||
| 111 | { |
||
| 112 | 'DeviceName': f'{dev.name} {dev.model}', |
||
| 113 | } |
||
| 114 | ) |
||
| 115 | for attribute in dev.attributes: |
||
| 116 | if attribute is None: |
||
| 117 | pass |
||
| 118 | else: |
||
| 119 | attrib_dict = convert_attribute_to_dict(attribute) |
||
| 120 | |||
| 121 | # we will use the attribute number as the key |
||
| 122 | num = attrib_dict.pop('num', None) |
||
| 123 | try: |
||
| 124 | assert num is not None |
||
| 125 | except Exception as e: |
||
| 126 | # we should never get here, but if we do, continue to next iteration and skip this attribute |
||
| 127 | logger.debug(f'Smart plugin error - Skip the attribute {attribute} ({e})') |
||
| 128 | continue |
||
| 129 | |||
| 130 | stats[-1][num] = attrib_dict |
||
| 131 | |||
| 132 | if isinstance(dev.if_attributes, NvmeAttributes): |
||
| 133 | idx = 0 |
||
| 134 | for attr in dev.if_attributes.__dict__.keys(): |
||
| 135 | try: |
||
| 136 | attrib_dict = convert_nvme_attribute_to_dict(attr, dev.if_attributes.__dict__[attr]) |
||
| 137 | if dev.if_attributes.__dict__[attr] is not None: |
||
| 138 | serialized = str(dev.if_attributes.__dict__[attr]) |
||
| 139 | except Exception as e: |
||
| 140 | logger.debug(f'Unable to serialize attribute {attr} from NVME') |
||
| 141 | |||
| 142 | idx +=1 |
||
| 143 | |||
| 144 | stats[-1][idx] = attrib_dict |
||
| 145 | |||
| 146 | return stats |
||
| 147 | |||
| 236 |