Created
December 1, 2014 16:18
-
-
Save dankerrigan/760a31eeb4245f69822d to your computer and use it in GitHub Desktop.
Bitpack an already terse office schedule encoding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
ENCODE = {'H': 0,'O': 1,'F': 2} | |
DECODE = dict([(v, k) for k, v in ENCODE.items()]) | |
def encode(data): | |
result = 0 | |
for i, d in enumerate(data): | |
result += ENCODE.get(d, ENCODE['H']) | |
if i != len(data)-1: | |
result <<= 3 | |
return result | |
def decode(data): | |
result = [] | |
while data > 0: | |
d = data & 3 | |
result.insert(0, DECODE[d]) | |
data >>= 3 | |
return ''.join(result) | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser(description='Encode your schedule') | |
parser.add_argument('--encode', dest='encode', help='encode a schedule in format [WFO]{5}', type=str, default=None, required=False) | |
parser.add_argument('--decode', dest='decode', help='decode a schedule', type=str, default=None, required=False) | |
args = parser.parse_args() | |
if args.encode: | |
trim_len = min(5, len(args.encode)) | |
encoded = encode(args.encode[:trim_len]) | |
print('0x{:x}'.format(encoded)) | |
if args.decode: | |
d = int(args.decode, 0) | |
decoded = decode(d) | |
print(decoded) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment