| Conditions | 20 |
| Total Lines | 78 |
| Code Lines | 67 |
| 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.units.distance() 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 |
||
| 68 | @commands('length', 'distance') |
||
| 69 | @example('.distance 3m', '3.00m = 9 feet, 10.11 inches') |
||
| 70 | @example('.distance 3km', '3.00km = 1.86 miles') |
||
| 71 | @example('.distance 3 miles', '4.83km = 3.00 miles') |
||
| 72 | @example('.distance 3 inch', '7.62cm = 3.00 inches') |
||
| 73 | @example('.distance 3 feet', '91.44cm = 3 feet, 0.00 inches') |
||
| 74 | @example('.distance 3 yards', '2.74m = 9 feet, 0.00 inches') |
||
| 75 | @example('.distance 155cm', '1.55m = 5 feet, 1.02 inches') |
||
| 76 | @example('.length 3 ly', '28382191417742.40km = 17635876112814.77 miles') |
||
| 77 | @example('.length 3 au', '448793612.10km = 278867421.71 miles') |
||
| 78 | @example('.length 3 parsec', '92570329129020.20km = 57520535754731.61 miles') |
||
| 79 | def distance(bot, trigger): |
||
| 80 | """Convert distances""" |
||
| 81 | try: |
||
| 82 | source = find_length.match(trigger.group(2)).groups() |
||
| 83 | except (AttributeError, TypeError): |
||
| 84 | bot.reply("That's not a valid length unit.") |
||
| 85 | return NOLIMIT |
||
| 86 | unit = source[1].lower() |
||
| 87 | numeric = float(source[0]) |
||
| 88 | meter = 0 |
||
| 89 | if unit in ("meters", "meter", "m"): |
||
| 90 | meter = numeric |
||
| 91 | elif unit in ("millimeters", "millimeter", "mm"): |
||
| 92 | meter = numeric / 1000 |
||
| 93 | elif unit in ("kilometers", "kilometer", "km"): |
||
| 94 | meter = numeric * 1000 |
||
| 95 | elif unit in ("miles", "mile", "mi"): |
||
| 96 | meter = numeric / 0.00062137 |
||
| 97 | elif unit in ("inch", "in"): |
||
| 98 | meter = numeric / 39.370 |
||
| 99 | elif unit in ("centimeters", "centimeter", "cm"): |
||
| 100 | meter = numeric / 100 |
||
| 101 | elif unit in ("feet", "foot", "ft"): |
||
| 102 | meter = numeric / 3.2808 |
||
| 103 | elif unit in ("yards", "yard", "yd"): |
||
| 104 | meter = numeric / (3.2808 / 3) |
||
| 105 | elif unit in ("light-year", "light-years", "ly"): |
||
| 106 | meter = numeric * 9460730472580800 |
||
| 107 | elif unit in ("astronomical unit", "astronomical units", "au"): |
||
| 108 | meter = numeric * 149597870700 |
||
| 109 | elif unit in ("parsec", "parsecs", "pc"): |
||
| 110 | meter = numeric * 30856776376340068 |
||
| 111 | |||
| 112 | if meter >= 1000: |
||
| 113 | metric_part = '{:.2f}km'.format(meter / 1000) |
||
| 114 | elif meter < 0.01: |
||
| 115 | metric_part = '{:.2f}mm'.format(meter * 1000) |
||
| 116 | elif meter < 1: |
||
| 117 | metric_part = '{:.2f}cm'.format(meter * 100) |
||
| 118 | else: |
||
| 119 | metric_part = '{:.2f}m'.format(meter) |
||
| 120 | |||
| 121 | # Shit like this makes me hate being an American. |
||
| 122 | inch = meter * 39.37 |
||
| 123 | foot = int(inch) // 12 |
||
| 124 | inch = inch - (foot * 12) |
||
| 125 | yard = foot // 3 |
||
| 126 | mile = meter * 0.000621371192 |
||
| 127 | |||
| 128 | if yard > 500: |
||
| 129 | stupid_part = '{:.2f} miles'.format(mile) |
||
| 130 | else: |
||
| 131 | parts = [] |
||
| 132 | if yard >= 100: |
||
| 133 | parts.append('{} yards'.format(yard)) |
||
| 134 | foot -= (yard * 3) |
||
| 135 | |||
| 136 | if foot == 1: |
||
| 137 | parts.append('1 foot') |
||
| 138 | elif foot != 0: |
||
| 139 | parts.append('{:.0f} feet'.format(foot)) |
||
| 140 | |||
| 141 | parts.append('{:.2f} inches'.format(inch)) |
||
| 142 | |||
| 143 | stupid_part = ', '.join(parts) |
||
| 144 | |||
| 145 | bot.reply('{} = {}'.format(metric_part, stupid_part)) |
||
| 146 | |||
| 190 |