Problem in understanding this structure in a python string

Asked

Viewed 78 times

1

I was studying file compression when I came across the following script:

import sys
import os.path
import bz2
import zlib
import base64

################################################################################

def main():
    "Extract the command-line arguments and run the packer."
    try:
        pack(sys.argv[1])
    except (IndexError, AssertionError):
        print('Usage: {} <filename>'.format(os.path.basename(sys.argv[0])))

def pack(path):
    "Get the source, compress it, and create a packed file."
    data = read_file(path)
    builder, data = optimize(data)
    with open(os.path.splitext(path)[0] + '.pyw', 'w') as file:
        builder(os.path.basename(path), base64.b64encode(data), file)

def read_file(path):
    "Read the entire file content from path in binary mode."
    assert os.path.isfile(path)
    with open(path, 'rb') as file:
        return file.read()

def optimize(data):
    "Compress the data and select the best method to write."
    bz2_data = bz2.compress(data, 9)
    zlib_data = zlib.compress(data, 9)
    sizes = tuple(map(len, (data, bz2_data, zlib_data)))
    smallest = sizes.index(min(sizes))
    if smallest == 1:
        return build_bz2_extractor, bz2_data
    if smallest == 2:
        return build_zlib_extractor, zlib_data
    return build_b64_extractor, data

################################################################################

def build_bz2_extractor(filename, data, file):
    "Write a Python program that uses bz2 data compression."
    print("import base64, bz2, os", file=file)
    print("data =", data, file=file)
    print("with open({!r}, 'wb') as file:".format(filename), file=file)
    print("    file.write(bz2.decompress(base64.b64decode(data)))", file=file)
    print("os.startfile({!r})".format(filename), file=file)

def build_zlib_extractor(filename, data, file):
    "Pack data into a self-extractor with zlib compression."
    print("import base64, zlib, os", file=file)
    print("data =", data, file=file)
    print("with open({!r}, 'wb') as file:".format(filename), file=file)
    print("    file.write(zlib.decompress(base64.b64decode(data)))", file=file)
    print("os.startfile({!r})".format(filename), file=file)

def build_b64_extractor(filename, data, file):
    "Create a Python file that may not utilize compression."
    print("import base64, os", file=file)
    print("data =", data, file=file)
    print("with open({!r}, 'wb') as file:".format(filename), file=file)
    print("    file.write(base64.b64decode(data))", file=file)
    print("os.startfile({!r})".format(filename), file=file)

I had great difficulty understanding this script because I’ve never seen syntax like this:

print("import base64, bz2, os", file=file)

Specifically the part: file=file

What does that mean?

1 answer

2


According to the DOCS (was also unaware of this possibility of print()) that argument in this case will write from inside a file, ex:

print("import base64, bz2, os", file=open('tests.txt', 'w'))

Here we write inside the file tests.txt the string "import base64, bz2, os".

By default this argument (keyword argument in technical terms in the context of python) has the value of sys.stdout, which is for example the print we see on our terminal when we run without file=... is explicitly defined.

I didn’t run the program, but from what I’m seeing this program when executed will create another program by writing the code (as shown above) in another file already opened in mode write ( w ), is the argument file who enters the last three functions, ex: def build_bz2_extractor(filename, data, file):

Browser other questions tagged

You are not signed in. Login or sign up in order to post.