Two-Dimensional Expression Layout

Lawrence D’Oliveiro lawrencedo99 at gmail.com
Sat Aug 20 18:22:53 EDT 2016


On Friday, August 19, 2016 at 8:56:31 PM UTC+12, I wrote:
> It is handy to be able to keep complex expressions together sometimes, when
> breaking them up would simply obscure their structure. To avoid lines
> getting long, why not take advantage of the two available screen/page
> dimensions to make their structure clearer? As a bonus, spacing out
> parentheses makes them look less of a clutter.

Another example, from <https://github.com/ldo/qahirah>: the sequence of values is laid out to allow easy additions/modifications in future.

    for \
        symname, funcname \
    in \
        (
            ("FC_FONT", "ft_font_face_create_for_ft_face"),
            ("FT_FONT", "ft_font_face_create_for_pattern"),
            ("IMAGE_SURFACE", "image_surface_create"),
            # TODO: MIME_SURFACE, OBSERVER_SURFACE?
            ("PDF_SURFACE", "pdf_surface_create"),
            ("PNG_FUNCTIONS", "surface_write_to_png"),
            ("PS_SURFACE", "ps_surface_create"),
            ("RECORDING_SURFACE", "recording_surface_create"),
            ("SCRIPT_SURFACE", "script_create"),
            ("SVG_SURFACE", "svg_surface_create"),
            {"USER_FONT", "user_font_face_create"},
        ) \
    :
        setattr \
          (
            HAS,
            symname,
            hasattr(cairo, "cairo_" + funcname)
          )
    #end for

As a bonus, this also counts as an example of data-driven programming.



More information about the Python-list mailing list