sopel/modules/resistor.py
2017-11-22 19:26:40 -05:00

46 lines
1.2 KiB
Python
Executable File

#! /usr/bin/env python3
# coding=utf-8
"""
Resistor color band codes.
"""
import module
@module.commands('resist')
@module.example('.resist 10k', 'brown black orange gold')
def resist(bot, trigger):
"""Displays the color band code of a resistor for the given resistance."""
suffix = {'k': 1000, 'm': 10000000}
digit = {'0': 'black', '1': 'brown', '2': 'red', '3': 'orange', '4': 'yellow',
'5': 'green', '6': 'blue', '7': 'violet', '8': 'grey', '9': 'white',
'-1': 'gold', '-2': 'silver'}
if not trigger.group(2)[-1].isdigit():
value = trigger.group(2)[:-1]
else:
value = trigger.group(2)
try:
value = float(value)
except ValueError:
return 'Invalid input'
if not trigger.group(2)[-1].isdigit():
value = value * suffix[trigger.group(2)[-1]]
valueStr = str(value)
if value >= 10:
colorCode = digit[valueStr[0]] + " " + digit[valueStr[1]] + " "
else:
colorCode = digit[valueStr[2]] + " " + digit['0'] + " "
if value < 0.1:
return "Value to small. Just like your dick."
elif value < 1:
colorCode = colorCode + digit['-2']
elif value < 10:
colorCode = colorCode + digit['-1']
else:
colorCode = colorCode + digit[str(len(valueStr)-4)]
colorCode = colorCode + " gold"
bot.say(colorCode)