| Conditions | 14 |
| Total Lines | 69 |
| Code Lines | 55 |
| 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 sopel.modules.currency.exchange() 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 |
||
| 121 | def exchange(bot, match): |
||
| 122 | """Show the exchange rate between two currencies""" |
||
| 123 | if not match: |
||
| 124 | bot.reply(UNRECOGNIZED_INPUT) |
||
| 125 | return NOLIMIT |
||
| 126 | |||
| 127 | try: |
||
| 128 | update_rates(bot) # Try and update rates. Rate-limiting is done in update_rates() |
||
| 129 | except requests.exceptions.RequestException as err: |
||
| 130 | bot.reply("Something went wrong while I was getting the exchange rate.") |
||
| 131 | LOGGER.error("Error in GET request: {}".format(err)) |
||
| 132 | return NOLIMIT |
||
| 133 | except ValueError: |
||
| 134 | bot.reply("Error: Got malformed data.") |
||
| 135 | LOGGER.error("Invalid json on update_rates") |
||
| 136 | return NOLIMIT |
||
| 137 | except FixerError as err: |
||
| 138 | bot.reply('Sorry, something went wrong with Fixer') |
||
| 139 | LOGGER.error(err) |
||
| 140 | return NOLIMIT |
||
| 141 | |||
| 142 | query = match.string |
||
| 143 | |||
| 144 | targets = query.split() |
||
| 145 | amount = targets.pop(0) |
||
| 146 | base = targets.pop(0) |
||
| 147 | targets.pop(0) |
||
| 148 | |||
| 149 | # TODO: Use this instead after dropping Python 2 support |
||
| 150 | # amount, base, _, *targets = query.split() |
||
| 151 | |||
| 152 | try: |
||
| 153 | amount = float(amount) |
||
| 154 | except ValueError: |
||
| 155 | bot.reply(UNRECOGNIZED_INPUT) |
||
| 156 | return NOLIMIT |
||
| 157 | except OverflowError: |
||
| 158 | bot.reply("Sorry, input amount was out of range.") |
||
| 159 | return NOLIMIT |
||
| 160 | |||
| 161 | if not amount: |
||
| 162 | bot.reply("Zero is zero, no matter what country you're in.") |
||
| 163 | return NOLIMIT |
||
| 164 | |||
| 165 | out_string = '{} {} is'.format(amount, base.upper()) |
||
| 166 | |||
| 167 | unsupported_currencies = [] |
||
| 168 | for target in targets: |
||
| 169 | try: |
||
| 170 | out_string = build_reply(amount, base.upper(), target.upper(), out_string) |
||
| 171 | except ValueError: |
||
| 172 | LOGGER.error("Raw rate wasn't a float") |
||
| 173 | return NOLIMIT |
||
| 174 | except KeyError as err: |
||
| 175 | bot.reply("Error: Invalid rates") |
||
| 176 | LOGGER.error("No key: {} in json".format(err)) |
||
| 177 | return NOLIMIT |
||
| 178 | except UnsupportedCurrencyError as cur: |
||
| 179 | unsupported_currencies.append(cur) |
||
| 180 | |||
| 181 | if unsupported_currencies: |
||
| 182 | out_string = out_string + ' (unsupported:' |
||
| 183 | for target in unsupported_currencies: |
||
| 184 | out_string = out_string + ' {},'.format(target) |
||
| 185 | out_string = out_string[0:-1] + ')' |
||
| 186 | else: |
||
| 187 | out_string = out_string[0:-1] |
||
| 188 | |||
| 189 | bot.reply(out_string) |
||
| 190 | |||
| 225 |