Improvement to imports, what is a better way ?

2QdxY4RzWzUUiLuE at potatochowder.com 2QdxY4RzWzUUiLuE at potatochowder.com
Thu Jan 19 13:30:19 EST 2023


On 2023-01-19 at 12:59:21 -0500,
Thomas Passin <list1 at tompassin.net> wrote:

> Well, it's an art, not a science [...]

+1

> # Create a plot
> g2 = (
>       ggplot(df2,
>       aes('Days Since Jan 22',  # Comments can clarify these params
>       + geom_point(size=.1, color='blue') # size, color params optional
>       + theme_bw() # Optional theme (background, grid color, ...)
>      )

You've got a comma followed by a plus sign in there, so I'm not exactly
sure where the parameters to which function begin and end.

When it starts to look like this, I begin breaking out the parameters:

label = 'Days Since Jan 22'
size = geom_point(size=.1, color='blue')
theme = theme_bw()
g2 = ggplot(df2, aes(label, size, theme))

> # Compose a long string:
> msg = ('A very long line .....\n'
> 	+ 'Another long bit of text ....'
> 	+ 'plus another ....'
>       )

If all the pieces are constants, then Python will concatenate them for
you:

msg = ('A very long line .....\n'
       'Another long bit of text ....'
       'plus another')

You can even mix in "f" strings:

msg = ('long line\n'
       f'left text {name} right text'
       'more here')

But watch out for missing spaces between the pieces!  :-)

> The PEP-8 rules are good, but they can't cover all cases perfectly.

Some the PEP-8 rules are debatable.  Regardless, they can't cover all
cases perfectly.  (IOW, we agree on the bit that's relevant to this
thread.)


More information about the Python-list mailing list