우리는 임페리얼 단위를 영구히 퇴출시키기 위해 노력할 필요가 있다.
1. 클래스
from Utils.Log import Logger as lg
class unit():
# Convert Imperial to Metric
def imperial_to_metric(value, unit):
if unit == "in":
return value * 2.54, "cm"
elif unit == "ft":
return value * 0.3048, "m"
elif unit == "yd":
return value * 0.9144, "m"
elif unit == "mi":
return value * 1.60934, "Km"
elif unit == "gal":
return value * 3.78541, "L"
elif unit == "oz":
return value * 28.3495, "g"
elif unit == "lb":
return value * 0.453592, "Kg"
else:
lg.error(f'[imperial_to_metric]: {value}, {unit} is an invalid input.')
return 0, 'ERROR'
어려울 게 있겠는가?
가장 필요하겠다 싶은 것들만 담았다.
2. 명령어
봇에 올려야 하기 때문에 명령어를 구현한다.
@client.tree.command()
@app_commands.describe(value='값', imp='입력 가능 단위: in, ft, yd, mi, gal, oz, lb.' )
async def convertimp(interaction: discord.Interaction, value: int, imp: str):
"""기열 임페리얼을 기합 메트릭으로 변환."""
lg.info(f"{interaction.user.display_name} request convertImp().")
resVal, resMet = unit.imperial_to_metric(value, imp)
if (resMet is 'ERROR'):
lg.error("An error occured while processing convertImp()")
await interaction.response.send_message("처리 중 문제가 생겼어요. 단위와 값을 확인해 주세요.", ephemeral=True)
result = f"{value} {imp} = {resVal:.2f} {resMet}"
lg.info(f"{value} {imp} to Metric is {resVal:.2f} {resMet}")
await interaction.response.send_message(result, ephemeral=True)
원래는 드롭다운 메뉴를 활용하고 싶었는데,
명령어 입력을 위해 드롭다운 메뉴를 쓸 수 있는 방법이 없는 듯했다.
그래서 그냥 클래식한 방법으로 해결하기로 했다.
3. 결과
Done.
임페리얼이 완전 퇴출되는 그날까지.
'Study > Python' 카테고리의 다른 글
[Python] 웹 파싱 / 기존 기능 개선 (0) | 2023.08.11 |
---|---|
[Python] DeepL 번역 API 써보기 (0) | 2023.06.02 |
[Python / GPT] 봇 코드 리팩터링 (0) | 2023.05.17 |
[Python, AWS] csv에서 DynamoDB에로의 교체 (0) | 2023.05.10 |
[Python] 로깅 클래스 추가 (0) | 2023.04.03 |