<commit_before>import argparse
from .core import (
    process_frequencies,
    create_tree_code,
    parse_tree_code,
    save_compressed_file,
    retrieve_compressed_file,
)


def main(filename, extract=False, compress=True, dest_file=None):
    assert extract is not compress, "Cannot both extract & compress"
    if compress:
        compress_file(filename, dest_file)
    if extract:
        extract_file(filename, dest_file)
    return 0


def compress_file(filename, dest_file=None):
    with open(filename, 'r') as f:
        freqs = process_frequencies(f.read())
    checksum = sum(c.freq for c in freqs)  # bytes
    t = create_tree_code(freqs)
    table = parse_tree_code(t)
    save_compressed_file(filename, table, checksum, dest_file)


def extract_file(filename, dest_file=None):
    retrieve_compressed_file(filename, dest_file)


def parse_arguments():
    parser = argparse.ArgumentParser(description="Compress text files.")
    parser.add_argument('filename', type=str,
                        help="Name of the file to process")
    group = parser.add_mutually_exclusive_group(required=True)
    group.add_argument('-c', '--compress', action='store_true',
                       help="Compress the file")
    group.add_argument('-x', '--extract', action='store_true',
                       help="Extract the file")
    args = parser.parse_args()
    return vars(args)


if __name__ == '__main__':
    args = parse_arguments()
    main(**args)<commit_msg>Add dest-file option to argparse.<commit_after>import argparse
from .core import (
    process_frequencies,
    create_tree_code,
    parse_tree_code,
    save_compressed_file,
    retrieve_compressed_file,
)


def main(filename, extract=False, compress=True, dest_file=None):
    assert extract is not compress, "Cannot both extract & compress"
    if compress:
        compress_file(filename, dest_file)
    if extract:
        extract_file(filename, dest_file)
    return 0


def compress_file(filename, dest_file=None):
    with open(filename, 'r') as f:
        freqs = process_frequencies(f.read())
    checksum = sum(c.freq for c in freqs)  # bytes
    t = create_tree_code(freqs)
    table = parse_tree_code(t)
    save_compressed_file(filename, table, checksum, dest_file)


def extract_file(filename, dest_file=None):
    retrieve_compressed_file(filename, dest_file)


def parse_arguments():
    parser = argparse.ArgumentParser(description="Compress text files.")
    parser.add_argument('filename', type=str,
                        help="Name of the file to process")
    group = parser.add_mutually_exclusive_group(required=True)
    group.add_argument('-c', '--compress', action='store_true',
                       help="Compress the file")
    group.add_argument('-x', '--extract', action='store_true',
                       help="Extract the file")
    parser.add_argument('-d', '--dest-file', type=str, default=None,
                        help="Destination File Name")
    args = parser.parse_args()
    return vars(args)


if __name__ == '__main__':
    args = parse_arguments()
    main(**args)
{'fraction_non_alphanumeric': 0.10791599353796445, 'fraction_numerical': 0.0006462035541195477, 'mean_word_length': 3.3667136812411846, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6111509', 'n_tokens_mistral': 952, 'n_tokens_neox': 932, 'n_words': 216}