pcdshub / pcdsdevices

Collection of Ophyd device subclasses for IOCs unique to LCLS PCDS.
https://pcdshub.github.io/pcdsdevices/
Other
5 stars 58 forks source link

Use dedent instead of literal de-indented strings #861

Open ZLLentz opened 3 years ago

ZLLentz commented 3 years ago

Expected Behavior

Code should be easy to read and tabbed appropriately for general stylistic optimization and so that python editors can parse it properly.

Current Behavior

There are several instances in the code that look like:

    def some_method():

        <...snip...>

        return f"""\
{name}: Target {target_state_num} {target_state} [{composition}]
Target Position: {target_pos} [{t_units}]
{diode_type}Diode Position(x, y): \
{x_motor_pos}, {y_motor_pos} [{d_units}]
"""

With the intention being that when the text is printed, it is printed without any leading tabbing.

Possible Solution

Another way to accomplish this is:

import textwrap

<...snip...>

    def some_method():

        <...snip...>

        return textwrap.dedent(
            f"""
            {name}: Target {target_state_num} {target_state} [{composition}]
            Target Position: {target_pos} [{t_units}]
            {diode_type}Diode Position(x, y): \
            {x_motor_pos}, {y_motor_pos} [{d_units}]
            """.lstrip()

Context

My vim folds poorly and in vscode it looks odd

klauer commented 3 years ago

dedent(...).lstrip() to keep the meaning of the leading \

ZLLentz commented 3 years ago

Added to the solution text, thanks Ken!