Re: for -- else: what was the motivation?

2022-10-08 Thread Chris Angelico
On Sun, 9 Oct 2022 at 16:05, Axy via Python-list  wrote:
>
>
> On 09/10/2022 05:47, Chris Angelico wrote:
> > On Sun, 9 Oct 2022 at 15:39, Axy via Python-list  
> > wrote:
> >> Got it, thanks!
> >>
> >> Actually the reason I never used "else" was the violation of the rule of
> >> beauty "shortest block first". With if--else you can easily follow this
> >> rule by inverting "if" expression, but with for--else you can't. The
> >> loop body of the simplest example is already three lines, in real life
> >> things are much worse.
> >>
> > That's not a rule I've ever been taught; how important is it?
> >
> > ChrisA
>
> It gets important if the lifetime of your project is more than three
> months and is extremely important if more than 10 years. But, it depends.

Yes, I'm aware that code readability becomes irrelevant for
short-duration projects. Beside the point. I'm wondering how important
it really is to have the shortest block first.

> I also might be wrong in terminology, anyway, there are many rules that
> make programmer's life easier, described in the literature from the old
> good "How to write unmaintainable code" to "The Art of Readable Code".
> And I hope there are a lot of recent books on this subject I did not
> track and read yet.

Also not really a justification for "shortest block first". Wanting
some elaboration on that. What's the value in it?

Given that for-else is an excellent, if rarely-used, construct, I
would say that, *at least*, it is worth setting aside this rule for
that particular situation. It is also generally worth using fewer
commas than I just did. Take my advice with a grain of salt.

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


Re: for -- else: what was the motivation?

2022-10-08 Thread Axy via Python-list



On 09/10/2022 05:47, Chris Angelico wrote:

On Sun, 9 Oct 2022 at 15:39, Axy via Python-list  wrote:

Got it, thanks!

Actually the reason I never used "else" was the violation of the rule of
beauty "shortest block first". With if--else you can easily follow this
rule by inverting "if" expression, but with for--else you can't. The
loop body of the simplest example is already three lines, in real life
things are much worse.


That's not a rule I've ever been taught; how important is it?

ChrisA


It gets important if the lifetime of your project is more than three 
months and is extremely important if more than 10 years. But, it depends.


I also might be wrong in terminology, anyway, there are many rules that 
make programmer's life easier, described in the literature from the old 
good "How to write unmaintainable code" to "The Art of Readable Code". 
And I hope there are a lot of recent books on this subject I did not 
track and read yet.


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


Re: for -- else: what was the motivation?

2022-10-08 Thread Chris Angelico
On Sun, 9 Oct 2022 at 15:39, Axy via Python-list  wrote:
>
> Got it, thanks!
>
> Actually the reason I never used "else" was the violation of the rule of
> beauty "shortest block first". With if--else you can easily follow this
> rule by inverting "if" expression, but with for--else you can't. The
> loop body of the simplest example is already three lines, in real life
> things are much worse.
>

That's not a rule I've ever been taught; how important is it?

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


Re: for -- else: what was the motivation?

2022-10-08 Thread Axy via Python-list

Got it, thanks!

Actually the reason I never used "else" was the violation of the rule of 
beauty "shortest block first". With if--else you can easily follow this 
rule by inverting "if" expression, but with for--else you can't. The 
loop body of the simplest example is already three lines, in real life 
things are much worse.


So it was probably the first time I used "else" because I had only one 
line in my loop which appended data packets to the buffer and if "else" 
behaved as I thought it would meant I have no more data and could just 
return early, terminating outer loop with no other boolean logic.


I have no idea why I thought so, some language might had such a 
semantic. Maybe my own I developed 20 years ago, but I could not invent 
that by myself, I definitely had some source of inspiration.


Python is awesome because it's semantic is clear for the majority, but 
there are places that look odd. In case of "for", "else" looks logically 
tied with "for" clause, but actually it is not. It's tied with "break" 
statement and I overlooked that even after re-reading the language 
reference. If "else" was named like "never_broken_loop" or "nobreak", 
the semantic would be perfectly clear. But, what's done is done.


I guess the real motivation was avoiding moving such patterns to a 
separate functions, say, "find_banana" where early returns make "else" 
absolutely unnecessary.


Cheers.

Axy.


On 08/10/2022 06:49, rbowman wrote:

On 10/7/22 21:32, Axy wrote:
So, seriously, why they needed else if the following pieces produce 
same result? Does anyone know or remember their motivation?


In real scenarios there would be more logic in the for block that 
would meet a condition and break out of the loop. If the condition is 
never met, the else block runs. To steal from w3schools:



fruits = ["apple", "peach", "cherry"]
for x in fruits:
  print(x)
  if x == "banana":
    break
else:
  print("Yes we got no bananas")



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


Re: Polot severa figures inside a for loopin

2022-10-08 Thread Cameron Simpson

On 08Oct2022 16:22, "Jorge Conrado Conforte"  wrote:
I already use the IDL to plot data. Inside a for looping I plot and 
save my data and kill the window using the wdelete IDL command for 
close the window automatically.

Now I'm using the Python to rea several netcdf 2d data. I plot my data and save 
it. But, I don't know how can I kill the window automatically to plot the next 
figure.
Please how can I do this using PYthon,


Can you show us the code you've got to do the first part with `wdelete`?  
Please paste the code inline in the message; this list strips 
attachments such as screenshots. (And we can't copy/paste from a 
screenshot anyway.)


And what's IDL? Can you provide a URL to it?

Cheers,
Cameron Simpson 
--
https://mail.python.org/mailman/listinfo/python-list


Re: TkSheet

2022-10-08 Thread Michael F. Stemper

On 08/10/2022 07.58, Benny Rieger wrote:

What a great work;-)

I need a solution for save my tabel as csv. How to do that? Has someone a 
solution for that?


Is this what you're seeking?



--
Michael F. Stemper
No animals were harmed in the composition of this message.
--
https://mail.python.org/mailman/listinfo/python-list


Re: for -- else: what was the motivation?

2022-10-08 Thread rbowman

On 10/7/22 21:32, Axy wrote:
So, seriously, why they needed else if the following pieces produce same 
result? Does anyone know or remember their motivation?


In real scenarios there would be more logic in the for block that would 
meet a condition and break out of the loop. If the condition is never 
met, the else block runs. To steal from w3schools:



fruits = ["apple", "peach", "cherry"]
for x in fruits:
  print(x)
  if x == "banana":
break
else:
  print("Yes we got no bananas")


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


Polot severa figures inside a for loopin

2022-10-08 Thread "Jorge Conrado Conforte"


HI, 

I already use the IDL to plot data. Inside a for looping I plot and save my 
data and kill the window using the wdelete IDL command for close the window 
automatically. 
Now I'm using the Python to rea several netcdf 2d data. I plot my data and save 
it. But, I don't know how can I kill the window automatically to plot the next 
figure. 
Please how can I do this using PYthon, 

Thanks, 

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


Re: Ref-strings in logging messages (was: Performance issue with CPython 3.10 + Cython)

2022-10-08 Thread Weatherby,Gerard
Logging does support passing a callable, if indirectly. It only calls __str__ 
on the object passed if debugging is enabled.

class Defer:

def __init__(self,fn):
self.fn = fn

def __str__(self):
return self.fn()

def some_expensive_function():
return "hello"

logging.basicConfig()
logging.debug(Defer(some_expensive_function))


From: Python-list  on 
behalf of Barry 
Date: Friday, October 7, 2022 at 1:30 PM
To: MRAB 
Cc: python-list@python.org 
Subject: Re: Ref-strings in logging messages (was: Performance issue with 
CPython 3.10 + Cython)
*** Attention: This is an external email. Use caution responding, opening 
attachments or clicking on links. ***

> On 7 Oct 2022, at 18:16, MRAB  wrote:
>
> On 2022-10-07 16:45, Skip Montanaro wrote:
>>> On Fri, Oct 7, 2022 at 9:42 AM Andreas Ames 
>>> wrote:
>>> 1. The culprit was me. As lazy as I am, I have used f-strings all over the
>>> place in calls to `logging.logger.debug()` and friends, evaluating all
>>> arguments regardless of whether the logger was enabled or not.
>>>
>> I thought there was some discussion about whether and how to efficiently
>> admit f-strings to the logging package. I'm guessing that's not gone
>> anywhere (yet).
> Letting you pass in a callable to call might help because that you could use 
> lambda.

Yep, that’s the obvious way to avoid expensive log data generation.
Would need logging module to support that use case.

Barry

> --
> https://urldefense.com/v3/__https://mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!mrESxAj9YCHsdtNAfkNiY-Zf6U3WTIqaNrgBmbw1ELlQy51ilob43dD0ONsqvg4a94MEdOdwomgyqfyABbvRnA$
>

--
https://urldefense.com/v3/__https://mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!mrESxAj9YCHsdtNAfkNiY-Zf6U3WTIqaNrgBmbw1ELlQy51ilob43dD0ONsqvg4a94MEdOdwomgyqfyABbvRnA$
-- 
https://mail.python.org/mailman/listinfo/python-list