Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
compresslevel vs. preset
#1
gzip.open() and bz2.open() both have a keyword argument compresslevel= to set the compression level while lzma.open() uses preset= for that. i am creating a function that selects one of these 3 to cal;. which keyword argument name should it use for compress level? the intent is for the caller to not need to know which algorithm is being used.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Name it compresslevel.

import bz2, gzip, lzma
import os


def open_compressed(file, mode, compresslevel=3):
    ext = os.path.splitext(file)[1]
    if ext == ".gz":
        return gzip.open(file, mode, compresslevel=compresslevel)
    elif ext == ".bz2":
        return bz2.open(file, mode, compresslevel=compresslevel)
    elif ext == ".xy":
        if mode == "r":
            return lzma.open(file, mode)
        else:
            return lzma.open(file, mode, preset=compresslevel)

testfiles = ("test.gz", "test.bz2", "test.xy")
# making testfiles
for file in testfiles:
    with open_compressed(file, "w", compresslevel=9) as fd:
        fd.write(file.upper().encode())

# reading
for file in testfiles:
    with open_compressed(file, "r") as fd:
        print(f"File: {file}: {fd.read().decode()}")
This is one of the design patterns, but I can't recognize the name.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
design patterns have names? ok, i'll use compresslevel= on my zopen() function.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020