<commit_before>#! /usr/bin/env python

from __future__ import print_function

import sys
import getopt

__version__ = 'v1.0.0'
__authors__ = '<NAME> <<EMAIL>>'
__license__ = '''Copyright (c) <NAME>

The contents of this file are subject to the terms and conditions defined 
within the file LICENSE.txt, located within this project's root directory.
'''

BUFSIZE = 32 # must be multiple of 32, so beautify works correctly

HEXCHARS = '0123456789ABCDEFabcdef'

ENCODE   = 0
DECODE   = 1
BEAUTIFY = 2

def print_usage():
    print('usage: hexcoder-py [-e] [-d] [-b]', file=sys.stderr)

def parse_args(args):
    optstr = 'edb'

    try:
        opts, args = getopt.getopt(args[1:], optstr)
    except getopt.GetoptError:
        raise ValueError('unrecognised option')

    # result dicts
    cmdargs = {}
    cmdopts = {'mode': ENCODE}

    for opt, arg in opts:
        if opt in ('-e'):
            cmdopts['mode'] = ENCODE
        elif opt in ('-d'):
            cmdopts['mode'] = DECODE
        elif opt in ('-b'):
            cmdopts['mode'] = BEAUTIFY

    return cmdopts, cmdargs

def read(f, size):
    return f.read(size)

def readhex(f, size):
    result = ''

    while len(result) < size:
        c = f.read(1)

        if c == '':
            break # end of file reached

        if not c in HEXCHARS:
            continue # skip non-hex chars

        result += c

    return result

def beautify(s):
    result = ''

    for i in xrange(len(s)):
        result += s[i]

        if i + 1 >= len(s):
            result += '\n'
            continue

        if (i + 1) % 2 == 0:
            result += ' ' # append space

        if (i + 1) % 32 == 0:
            result = result[:-1] + '\n' # replace space with newline
        elif (i + 1) % 8 == 0:
            result += ' ' # append second space

    return result

def main(args):
    try:
        cmdopts, cmdargs = parse_args(args)
    except ValueError as e:
        print_usage()
        return 1 # failure

    read_func = read
    if cmdopts['mode'] == BEAUTIFY or cmdopts['mode'] == DECODE:
        read_func = readhex

    buf = read_func(sys.stdin, BUFSIZE)
    while buf != '':
        if cmdopts['mode'] == ENCODE:
            buf = buf.encode('hex').upper()
        if cmdopts['mode'] == BEAUTIFY or cmdopts['mode'] == ENCODE:
            buf = beautify(buf)
        if cmdopts['mode'] == DECODE:
            if len(buf) % 2 != 0:
                buf = buf[:-1]
            buf = buf.decode('hex')

        sys.stdout.write(buf)
        buf = read_func(sys.stdin, BUFSIZE)

    return 0 # success

if __name__ == '__main__':
    try:
        sys.exit(main(sys.argv))
    except KeyboardInterrupt:
        # handle sigint
        sys.exit(1)
<commit_msg>Improved exception handling<commit_after>#! /usr/bin/env python

from __future__ import print_function

import sys
import getopt

__version__ = 'v1.0.0'
__authors__ = '<NAME> <<EMAIL>>'
__license__ = '''Copyright (c) <NAME>

The contents of this file are subject to the terms and conditions defined 
within the file LICENSE.txt, located within this project's root directory.
'''

BUFSIZE = 32 # must be multiple of 32, so beautify works correctly

HEXCHARS = '0123456789ABCDEFabcdef'

ENCODE   = 0
DECODE   = 1
BEAUTIFY = 2

def print_usage():
    print('usage: hexcoder-py [-e] [-d] [-b]', file=sys.stderr)

def parse_args(args):
    optstr = 'edb'

    try:
        opts, args = getopt.getopt(args[1:], optstr)
    except getopt.GetoptError:
        raise ValueError('unrecognised option')

    # result dicts
    cmdargs = {}
    cmdopts = {'mode': ENCODE}

    for opt, arg in opts:
        if opt in ('-e'):
            cmdopts['mode'] = ENCODE
        elif opt in ('-d'):
            cmdopts['mode'] = DECODE
        elif opt in ('-b'):
            cmdopts['mode'] = BEAUTIFY

    return cmdopts, cmdargs

def read(f, size):
    return f.read(size)

def readhex(f, size):
    result = ''

    while len(result) < size:
        c = f.read(1)

        if c == '':
            break # end of file reached

        if not c in HEXCHARS:
            continue # skip non-hex chars

        result += c

    return result

def beautify(s):
    result = ''

    for i in xrange(len(s)):
        result += s[i]

        if i + 1 >= len(s):
            result += '\n'
            continue

        if (i + 1) % 2 == 0:
            result += ' ' # append space

        if (i + 1) % 32 == 0:
            result = result[:-1] + '\n' # replace space with newline
        elif (i + 1) % 8 == 0:
            result += ' ' # append second space

    return result

def main(args):
    try:
        cmdopts, cmdargs = parse_args(args)
    except ValueError as e:
        print_usage()
        return 1 # failure

    read_func = read
    if cmdopts['mode'] == BEAUTIFY or cmdopts['mode'] == DECODE:
        read_func = readhex

    buf = read_func(sys.stdin, BUFSIZE)
    while buf != '':
        if cmdopts['mode'] == ENCODE:
            buf = buf.encode('hex').upper()
        if cmdopts['mode'] == BEAUTIFY or cmdopts['mode'] == ENCODE:
            buf = beautify(buf)
        if cmdopts['mode'] == DECODE:
            if len(buf) % 2 != 0:
                buf = buf[:-1]
            buf = buf.decode('hex')

        sys.stdout.write(buf)
        buf = read_func(sys.stdin, BUFSIZE)

    return 0 # success

if __name__ == '__main__':
    try:
        sys.exit(main(sys.argv))
    except KeyboardInterrupt:
        # handle sigint
        sys.exit(1) # failure
    except:
        print('error: unhandled exception', file=sys.stderr)
        sys.exit(1) # failure

{'fraction_non_alphanumeric': 0.12963294538943598, 'fraction_numerical': 0.01450313339301701, 'mean_word_length': 2.0210924824229313, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2078376', 'n_tokens_mistral': 1894, 'n_tokens_neox': 1796, 'n_words': 544}