Commit 708e6d7c authored by Bruce Cowan's avatar Bruce Cowan

Initial commit

parents
Pipeline #1831 passed with stage
in 32 seconds
__pycache__
image: python:latest
stages:
- check
before_script:
- python -V
check:
stage: check
script:
- pip install flake8
- flake8 unit.py
\ No newline at end of file
[Core]
name = Unit
module = unit
[Documentation]
description = Unit conversions
[Python]
version = 3
from errbot import BotPlugin, botcmd
from pint import UndefinedUnitError, UnitRegistry
class Unit(BotPlugin):
"""Commands which do unit conversions"""
def activate(self):
super().activate()
self.ureg = UnitRegistry()
@botcmd
def convert(self, message, args):
"""Convert units"""
if not args or ' to ' not in args:
return "**Usage**: !unit <from> to <to>"
src, dst = args.split(' to ')
try:
res = '{:~.4P}'.format(self.ureg(src).to(dst))
except UndefinedUnitError as e:
return str(e)
else:
return res
@botcmd
def si(self, message, args):
"""Convert any value to base SI units"""
if not args:
return "**Usage**: !si <str>"
try:
res = '{:~.4P}'.format(self.ureg(args).to_base_units())
except UndefinedUnitError as e:
return str(e)
else:
return res
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment