Preview musings (and a question about pnmcrop)

José Matos jaomatos at gmail.com
Fri Aug 16 18:31:30 UTC 2024


On Fri, 2024-08-16 at 10:10 +0100, José Matos wrote:
> Question:
> Does anyone have a reason to keep the current two stages process?

The new code becomes simply:

def crop_files(pnmcrop, basename):
    for file in glob.glob(f"{basename}*.ppm"):
        tmp = tempfile.TemporaryFile()
        conv_call = f"{pnmcrop} -left -right -- {file}"
        conv_status = subprocess.run(conv_call, stdout=tmp)

        if conv_status:
            continue

        copyfileobj(tmp, open(file, "wb"), 1)

This is verbose but it is line with the other code in the file. :-)
With this code we can remove the pipes import and the code now also
works with Python 3.13.


As a weekend puzzle, to be terse the code could become:

def crop_files(pnmcrop, basename):
    for file in glob.glob(f"{basename}*.ppm"):
        if not subprocess.run(f"{pnmcrop} -left -right -- {file}",
                             stdout=(tmp := tempfile.TemporaryFile())):
            copyfileobj(tmp, open(file, "wb"), 1)

-- 
José Abílio


More information about the lyx-devel mailing list