PPixelVeranda
Workflows

Coding workflows · Width-threshold image selection

03
Built around your work

A concrete starting point

Select among one to 64 URL-width pairs using a display width of 1–10000 and density ratio from 1/4 to 8. Test exact thresholds and largest-candidate fallback, rejecting duplicate widths or invalid numbers without fetching the selected URL.

01

Open the context

Choose the project folder and name the files or behavior involved.

02

Define the change

Describe the input, expected output and boundaries that must stay intact.

03

Inspect the work

Review the proposed edits and any commands before relying on their result.

04

Check the outcome

Compare actual behavior with the expected result. If a check fails, investigate it before applying more changes.

Width-threshold image selection

A concrete starting point
import math

def image_candidate(candidates, width, ratio):
    if type(width) is not int or not 1 <= width <= 10000 or type(ratio) not in (int, float) or not 0.25 <= ratio <= 8 or not math.isfinite(ratio):
        raise ValueError("image.viewport")
    if type(candidates) is not list or not 1 <= len(candidates) <= 64:
        raise ValueError("image.candidates")
    sizes, seen = [], set()
    for pair in candidates:
        if type(pair) not in (list, tuple) or len(pair) != 2:
            raise ValueError("image.pair")
        url, pixels = pair
        if type(url) is not str or not 1 <= len(url) <= 2048 or any(ord(c) < 32 or ord(c) == 127 for c in url):
            raise ValueError("image.url")
        if type(pixels) is not int or not 1 <= pixels <= 100000 or pixels in seen:
            raise ValueError("image.size")
        seen.add(pixels)
        sizes.append((pixels, url))
    sizes.sort()
    target = width*ratio
    return next((url for pixels, url in sizes if pixels >= target), sizes[-1][1])

The example is a starting point for a real project, not a claim that generated code is already tested. Adjust paths, dependencies and expected behavior to match your application.

AI coding workspace

Your next change starts here.

Get the Windows app ↗