Skip to main content

Python wrapper for FastLZ, a lightning-fast lossless compression library.

Project description

FastLZ

Python wrapper for FastLZ_, a lightning-fast lossless compression library.

Example:

from fastlz5 import compress, decompress s = decompress(compress("hello there world", level=1)) assert(s == "hello there world")

Example:

import os
import sys
import argparse

try:
    from fastlz5 import compress, decompress
except ModuleNotFoundError:
    sys.stderr.write(
        'fastlz lib not found, please install it with "python -m install fastlz5"')
    sys.exit(1)

# Breaks an array into chunks of n length each.
def chunks(l, n):    
    for i in range(0, len(l), n):
        yield l[i:i + n]

def bytes_to_c_array(bytes, variable_name):
    output_c_array = (
        "#include <stdint.h>\n" +
        f"extern const uint8_t {variable_name}[{len(bytes)}] = " + "{\n"
    )
    for byte_block in chunks(bytes, 64):
        output_c_array += '  '
        for byte in byte_block:
            output_c_array += f'{byte},'
        output_c_array += '\n'
    output_c_array += "};\n"
    return output_c_array

def main():
    parser = argparse.ArgumentParser(description='Prepare data file for teensy by compressing it and outputting a c-array')
    parser.add_argument('--input', help='Input file', required=True)
    parser.add_argument('--output', help='Output file, otherwise stream to stdout', required=False)
    parser.add_argument('--variable-name', help='Name for the variable of c_array', required=False)
    parser.add_argument('--no-compress', help='Disable FastLZ compression to file', action='store_true')

    args = parser.parse_args()
    if not os.path.exists(args.input):
        sys.stderr.write(f'File {args.input} does not exist.\n')
        sys.exit(1)

    with open(args.input, "rb") as fd:
        file_input_bytes = fd.read()

    if not args.no_compress:
        file_input_bytes = compress(file_input_bytes, level=1)

    var_name = args.variable_name or "data"
    output_c_array = bytes_to_c_array(file_input_bytes, var_name)

    if args.output:
        with open(args.output, "wt") as fd:
            fd.write(output_c_array)
    else:
        sys.stdout.write(f"{output_c_array}\n")

if __name__ == '__main__':
    main()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastlz5-0.0.1.3.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastlz5-0.0.1.3-cp39-cp39-win_amd64.whl (10.7 kB view details)

Uploaded CPython 3.9Windows x86-64

File details

Details for the file fastlz5-0.0.1.3.tar.gz.

File metadata

  • Download URL: fastlz5-0.0.1.3.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.9.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.0

File hashes

Hashes for fastlz5-0.0.1.3.tar.gz
Algorithm Hash digest
SHA256 efe61432433453ac56e2ad16a11c7c8f16a21a9d26b5532782c0ffa3c92e7fb9
MD5 e64afd7db04b0fa66e8b49e14a9e6ee0
BLAKE2b-256 6e3b0fced85526c3ebd8584494a6ed748a9b49bcfa44f13d4e69a9b238d606a3

See more details on using hashes here.

File details

Details for the file fastlz5-0.0.1.3-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: fastlz5-0.0.1.3-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 10.7 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.9.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.0

File hashes

Hashes for fastlz5-0.0.1.3-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 0625607cbbec91257237fd23280e85af583048b0cc65a7094d1710dcd4228514
MD5 8810d7c289a4fa753d2eb9e60253a21e
BLAKE2b-256 e88f441a7002713f63612cdf9448d2454ecb9147f6f6c800c61210df0586a200

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page