-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunitconvert.py
37 lines (37 loc) · 1.22 KB
/
unitconvert.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import bitmath
import core
PLUGINVERSION = 2
# Always name this variable as `plugin`
# If you dont, module loader will fail to load the plugin!
plugin = core.Plugin()
MSGTMPL = """
%(val_a)s = %(val_b)s
"""
FORMATTER = "{value:.4f} {unit}"
@plugin.command(command="/byte",
description="Converts memory sizes",
inline_supported=True,
hidden=False)
def convert(bot, update, user, args):
if len(args) > 0:
try:
if "to" in args:
args = "".join(args).split("to")
print(args)
d = {
"val_a": bitmath.parse_string_unsafe(args[0]),
}
d["val_b"] = bitmath.parse_string_unsafe("1" + args[1]).from_other(d["val_a"])
else:
va = bitmath.parse_string_unsafe(" ".join(args))
d = {
"val_a": va,
"val_b":va.best_prefix()
}
for value in d:
d[value] = d[value].format(FORMATTER)
return core.message(MSGTMPL % d)
except ValueError as e:
return core.message(str(e).title())
else:
return core.message("No data supplied!")