Improvement to imports, what is a better way ?

avi.e.gross at gmail.com avi.e.gross at gmail.com
Fri Jan 20 00:21:38 EST 2023


I stand corrected. Thomas is (mostly) writing valid Python if you use the
module that looks (deliberately) like the R implementation. The use of "+"
in two contexts such as when not needed to concatenate strings, reminded me
too much of R.

Either way, this thread has moved on from any original question. Now it is
about the long and short of it.

-----Original Message-----
From: Python-list <python-list-bounces+avi.e.gross=gmail.com at python.org> On
Behalf Of Thomas Passin
Sent: Thursday, January 19, 2023 9:37 PM
To: python-list at python.org
Subject: Re: Improvement to imports, what is a better way ?

On 1/19/2023 7:33 PM, avi.e.gross at gmail.com wrote:
> Just FYI, the example Dave supplied was not using python code and 
> using a rather strange re-definition in the R language package he was 
> using. Or maybe not.
> 
> Anyone not interested, skip the rest.
> 
> First, R does not use indentation for almost anything. So you can 
> break one long line up into many lines all over the place where it is 
> clear you are continuing.
> 
> Second, the ggplot package (another name for module) came along before 
> pipes were used a lot and made a choice to overload the meaning of the 
> plus sign selectively between "verbs" of sorts.
> 
> The code Thomas shared says:
> 
> - Set up the beginning of a plot using the arguments provided and 
> create a DATA STRUCTURE. This structure is a rather complex list 
> structure composed internally of many kinds of named parts, some optional.
> - Then call a verb of sorts, called geom_point() to update the same 
> data structure appropriately and store info to be used LATER when you 
> call the print driver that knows how to handle this data structure. In 
> a sense, the first command sends the dta structure to the second 
> command which changes it and passes it along to
> - the next verb is theme_bw() which changes all kinds of elements in 
> the data structure and returns the update data structure.
> 
> The result is stored as the value of g2 and NOTHING HAPPENS.
> 
> If you even wrote print(g2) or implicitly printed it at the REPL level 
> such as by typing "g2" or not assigning it at all to a variable at the 
> REPL level, then the driver that handles such an object generates a 
> graph to whatever output device is currently set in whatever format is 
> set such as a JPEG image or a PDF file.
> 
> There are errors in what Thomas wrote as an example but not what is 
> mentioned.

Just to be clear, my example was copy-pasted from working Python code. 
It uses the plotnine package, available from PyPi, which is a partial
implementation of R's ggplot2 API as adapted for Python;  both are
implementations of the ideas from The Language of Graphics of Wilkinson.

I shortened a line or two to prevent wrapping by email clients and perhaps I
chopped something off by mistake.

> The overall flow looks like:
> 
> g2 <- ggplot(ARGS) + geom_point(ARGS) + theme_bw(ARGS)
> 
> The parentheses around the RHS are NOT needed and the above is very 
> typically written as:
> 
> g2 <- ggplot(ARGS) +
> 	geom_point(ARGS) +
> 	theme_bw(ARGS)
> 
> The plus signs at the end of each line tell the REPL to keep reading.
> 
> The code shown is not working code but was an illustration. The 
> ggplot() function takes many potential arguments including one for the 
> data.frame or tibble that holds rows and columns of data. You then 
> have to map some aesthetics such as what X and Y are bound to directly 
> to some column, or perhaps to a function involving columns. That is 
> not shown inside the aes() inner function and what is shown is 
> nonsense. As usual, function arguments are separated by commas and I 
> assume a line or more are missing. This example does not tell us what 
> columns I being graphed against what other column or other necessary 
> things. That can sometimes be told or changed later but I see none in 
> this example. This function call should have ended without a comma and
with a close parentheses followed by a plus sign.
> 
> The geom_point() function that follows can be empty and then would ask 
> for a chart overlay showing just the points. This example added the 
> size and color of those points.
> 
> The last verb of theme_bw() adjusts all kinds of parameters to provide 
> a certain look to be basically black and white for various background
items.
> 
> The example chose to include parens around everything so the plus 
> signs could now be anywhere, including the start of future lines. I 
> generally do not do things this way but it is a valid way.
> 
> I do note there is a python version of the ggplot package and maybe 
> Thomas is writing from that point of view in python.  I have never used
that.
> 
> As noted, most other R constructs use PIPES not the plus sign and I 
> wish this package would change to conform but it is a bit late! LOL!
> 
> Avi
> 
> 
> 
> -----Original Message-----
> From: Python-list 
> <python-list-bounces+avi.e.gross=gmail.com at python.org> On Behalf Of 
> 2QdxY4RzWzUUiLuE at potatochowder.com
> Sent: Thursday, January 19, 2023 1:30 PM
> To: python-list at python.org
> Subject: Re: Improvement to imports, what is a better way ?
> 
> 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.)
> --
> https://mail.python.org/mailman/listinfo/python-list
> 

--
https://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list