Armand Halbert
Customer Success Engineer
DataKitchen


Contact:
armand.halbert@gmail.com
ajhalber@us.ibm.com
Resume - pdf
Resume - text

   
Published on:
Sunday, June 28, 2015




Alfred Baseconverter


I wrote a small workflow for Alfred 2: A system for converting between various bases in Alfred 2.

Why? … Mostly to help me me cheat at FTL.

FTL

Yep. I modify my save file in FTL, to give me advantages and such. In order to figure out the values in the file to modify, and what to change them to. I had to convert decimal numbers in game to binary values in the file. I found myself wishing I could use alfred 2 to quickly translate between them, rather than using google. I failed to find a workflow that did this, so I wrote my own workflow using python, learning how to create alfred workflows utilizing scripts in the process.

import sys
from workflow import Workflow

#map of digit to their base 10 value
chars = map(str, range(0,10)) + ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']

def main(wf):
    try: 
        query = wf.args[0]
        result = convert(query)
        wf.add_item(title=result, valid=True, arg=result, copytext=result)
    except:
        wf.add_item("Invalid input. format: {base}x{number} {target-base}")
    wf.send_feedback()

def convert(query):
    query = query.upper()
    query = query.split()
    source = parseSource(query[0])
    if len(query) == 1:
        if source[1] == 10:
            query.append(str(16))
        else:
            query.append(str(10))
    if len(query) == 2 and query[1].strip() != "":
        target = parseTargetBase(query[1])
        base10 = toBase10(*source)
        return fromBase10(base10, target)
    raise Exception

"""
checks for special expressions of bases and translates them to an int
0x - 16 
0 - 14
"""
def parseTargetBase(target):
    if target == "D":
        return 10
    if target == "B":
    return 2 
    if target == "0X":
        return 16
    if target in ("0", "O"):
        return 8
    return int(target)

def parseSource(source):
    if source[0] in ("D"):
        return source[1:], 10
    if source[0] in ("B"):
        return source[1:], 2
    if source[0] in ("0", "O") and source[0:2] != "0X":
        return source[1:], 8
    if source[0:2] == ("0X"):
        return (source[2:]), 16
    source = source.split("X")
    #nothing = base 10
    if len(source) == 1:
        return source[0], 10
    return source[1], int(source[0])

def toBase10(num, base):
    power = 0
    convertedNumber = 0
    for d in num[::-1]:
        if chars.index(d) >= base:
            raise Exception
        convertedNumber += chars.index(d)*int(pow(base, power))
        power += 1
    return convertedNumber

def fromBase10(num, target):
    convertedNumber = ""
    while num > 0:
        convertedNumber += chars[num%target]
        num = int(num/target)
    return convertedNumber[::-1]

if __name__ == u"__main__":
    wf = Workflow()
    sys.exit(wf.run(main))

Unfortunately, the examples are lacking in understanding how Alfred actually works. I still know very little about Alfred, and cobbled together a workflow from other people’s code. The library is also opaque. It’s supposed to help with debugging, but I have yet to figure out how that is done through the logger. It took me a while playing with add_item to actually add stuff and move it to the clipboard.

However, it did it help me make my workflow a reality, and I don’t want to be overly harsh on a library that helped me make my workflow possible. Especially a python module for helping users with a closed-source application.

A day seems a long time to make an app. It seems like it will be useful for other things, though. I’m hoping others will find it useful as well.

The last thing I will add is better error handling. I am still working out how to make useful error messages.


<< Previous Next >>