forked from albertlauncher/python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base_converter.py
61 lines (53 loc) · 1.99 KB
/
base_converter.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# -*- coding: utf-8 -*-
"""Convert representations of numbers.
Synopsis:
<trigger> <src base> <dest base> <src>
<bin|oct|dec|hex> <number> [padding]"""
import numpy as np
from albertv0 import *
__iid__ = "PythonInterface/v0.1"
__prettyname__ = "Base Converter"
__version__ = "1.1"
__trigger__ = "base "
__author__ = "Manuel Schneider"
__dependencies__ = ["numpy"]
base_keywords = {"bin": 2, "oct": 8, "dec": 10, "hex": 16}
def buildItem(completion, src, dst, number, padding=0):
item = Item(id=__prettyname__, completion=completion)
try:
src = int(src)
dst = int(dst)
padding = int(padding)
integer = int(number, src)
item.text = np.base_repr(integer, dst)
if integer >= 0 and len(item.text) < padding:
item.text = '0'*(padding-len(item.text)) + item.text
item.subtext = "Base %s representation of %s (base %s)" % (dst, number, src)
item.addAction(ClipAction("Copy to clipboard", item.text))
except Exception as e:
item.text = e.__class__.__name__
item.subtext = str(e)
return item
def handleQuery(query):
if query.isTriggered:
fields = query.string.split()
if len(fields) == 3:
return buildItem(query.rawString, fields[0], fields[1], fields[2])
else:
item = Item(id=__prettyname__, completion=query.rawString)
item.text = __prettyname__
item.subtext = "Enter a query in the form of \"<srcbase> <dstbase> <number>\""
return item
else:
fields = query.string.split()
if len(fields) < 2 or fields[0] not in base_keywords:
return
src = base_keywords[fields[0]]
number = fields[1]
padding = 0 if len(fields) < 3 else fields[2]
results = []
for dst in sorted(base_keywords.values()):
if dst == src:
continue
results.append(buildItem(query.rawString, src, dst, number, padding))
return results