Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

"There... I've run rings 'round you logically" -- Monty Python's Flying Circus


devel / comp.lang.python / A technique from a chatbot

SubjectAuthor
* A technique from a chatbotStefan Ram
+* Re: A technique from a chatbotPiergiorgio Sartor
|+* Re: A technique from a chatbotThomas Passin
||`* Re: A technique from a chatbotMark Bourne
|| +* RE: A technique from a chatbot<avi.e.gross
|| |`- Re: A technique from a chatbotMark Bourne
|| +- Re: A technique from a chatbotThomas Passin
|| `* Re: A technique from a chatbotStefan Ram
||  +- Re: A technique from a chatbotStefan Ram
||  `- Re: A technique from a chatbotMark Bourne
|+- RE: A technique from a chatbot<avi.e.gross
|+- Re: A technique from a chatbotThomas Passin
|`- RE: A technique from a chatbot<avi.e.gross
+* Re: A technique from a chatbotGilmeh Serda
|`* Re: A technique from a chatbotMichael F. Stemper
| `- Re: A technique from a chatbotGilmeh Serda
`- Re: A technique from a chatbotPieter van Oostrum

1
A technique from a chatbot

<chatbot-20240402181409@ram.dialup.fu-berlin.de>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=29790&group=comp.lang.python#29790

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!rocksolid2!news.neodome.net!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.lang.python
Subject: A technique from a chatbot
Date: 2 Apr 2024 17:18:16 GMT
Organization: Stefan Ram
Lines: 61
Expires: 1 Feb 2025 11:59:58 GMT
Message-ID: <chatbot-20240402181409@ram.dialup.fu-berlin.de>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de 3X8pLkTXGcuuqfYVeJjcGwdmUS3IwdxmJ0nLzj3m47zYSC
Cancel-Lock: sha1:Rs/VAKciprzdfa2s6TZJdqrAPfM= sha256:rQFdBL7OFGwpFO/rzXwQ4yON5J1lZEKh15pJJJgr03c=
X-Copyright: (C) Copyright 2024 Stefan Ram. All rights reserved.
Distribution through any means other than regular usenet
channels is forbidden. It is forbidden to publish this
article in the Web, to change URIs of this article into links,
and to transfer the body without this notice, but quotations
of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
services to mirror the article in the web. But the article may
be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: en-US
 by: Stefan Ram - Tue, 2 Apr 2024 17:18 UTC

Some people can't believe it when I say that chatbots improve
my programming productivity. So, here's a technique I learned
from a chatbot!

It is a structured "break". "Break" still is a kind of jump,
you know?

So, what's a function to return the first word beginning with
an "e" in a given list, like for example

[ 'delta', 'epsilon', 'zeta', 'eta', 'theta' ]

? Well it's

def first_word_beginning_with_e( list_ ):
for word in list_:
if word[ 0 ]== 'e': return word

. "return" still can be considered a kind of "goto" statement.
It can lead to errors:

def first_word_beginning_with_e( list_ ):
for word in list_:
if word[ 0 ]== 'e': return word
something_to_be_done_at_the_end_of_this_function()

The call sometimes will not be executed here!
So, "return" is similar to "break" in that regard.

But in Python we can write:

def first_word_beginning_with_e( list_ ):
return next( ( word for word in list_ if word[ 0 ]== 'e' ), None )

. No jumps anymore, yet the loop is aborted on the first hit
(if I guess correctly how its working).

And it is this combination of "next", a generator, and "None" that
the chatbot showed me when I asked him how to get the first component
of a list that matches a condition!

PS: Let's verify the earliness of the exit out of the loop:

Main.py

def list_():
list__ =[ 'delta', 'epsilon', 'zeta', 'eta', 'theta' ]
for entry in list__:
print( f'Now yielding {entry}.' )
yield entry

def first_word_beginning_with_e( list_ ):
return next( ( word for word in list_() if word[ 0 ]== 'e' ), None )

print( first_word_beginning_with_e( list_ ))

sys.stdout

Now yielding delta.
Now yielding epsilon.
epsilon

Re: A technique from a chatbot

<fvstdk-607.ln1@lazy.lzy>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=29791&group=comp.lang.python#29791

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!rocksolid2!news.neodome.net!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: piergiorgio.sartor.this.should.not.be.used@nexgo.REMOVETHIS.de (Piergiorgio Sartor)
<piergiorgio.sartor.this.should.not.be.used@nexgo.REMOVETHIS.de>
Newsgroups: comp.lang.python
Subject: Re: A technique from a chatbot
Date: Tue, 2 Apr 2024 19:47:59 +0200
Organization: A noiseless patient Spider
Lines: 65
Message-ID: <fvstdk-607.ln1@lazy.lzy>
References: <chatbot-20240402181409@ram.dialup.fu-berlin.de>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 02 Apr 2024 17:50:00 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="523d82d193eafc2369f09615d88bc6c5";
logging-data="3525213"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19kGdygnblmd0jBnV4UDgf0"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:GAFiBVhNziz35GlI4U3Dq6S0E7k=
Content-Language: it, en-GB
In-Reply-To: <chatbot-20240402181409@ram.dialup.fu-berlin.de>
 by: Piergiorgio Sartor - Tue, 2 Apr 2024 17:47 UTC

On 02/04/2024 19.18, Stefan Ram wrote:
> Some people can't believe it when I say that chatbots improve
> my programming productivity. So, here's a technique I learned
> from a chatbot!
>
> It is a structured "break". "Break" still is a kind of jump,
> you know?
>
> So, what's a function to return the first word beginning with
> an "e" in a given list, like for example
>
> [ 'delta', 'epsilon', 'zeta', 'eta', 'theta' ]
>
> ? Well it's
>
> def first_word_beginning_with_e( list_ ):
> for word in list_:
> if word[ 0 ]== 'e': return word
>
> . "return" still can be considered a kind of "goto" statement.
> It can lead to errors:
>
> def first_word_beginning_with_e( list_ ):
> for word in list_:
> if word[ 0 ]== 'e': return word
> something_to_be_done_at_the_end_of_this_function()
>
> The call sometimes will not be executed here!
> So, "return" is similar to "break" in that regard.
>
> But in Python we can write:
>
> def first_word_beginning_with_e( list_ ):
> return next( ( word for word in list_ if word[ 0 ]== 'e' ), None )

Doesn't look a smart advice.

> . No jumps anymore, yet the loop is aborted on the first hit

First of all, I fail to understand why there
should be no jumps any more.
It depends on how "return" and "if" are handled,
I guess, in different context.
Maybe they're just "masked".
In any case, the "compiler" should have just
done the same.

> (if I guess correctly how its working).

Second, it is difficult to read, which is bad.
The "guess" above is just evidence of that.

My personal opinion about these "chatbots", is
that, while they might deliver clever solutions,
they are not explaining *why* these solutions
should be considered "clever".
Which is the most important thing (the solution
itself is _not_).

bye,

--

piergiorgio

Re: A technique from a chatbot

<mailman.58.1712110350.3468.python-list@python.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=29792&group=comp.lang.python#29792

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: list1@tompassin.net (Thomas Passin)
Newsgroups: comp.lang.python
Subject: Re: A technique from a chatbot
Date: Tue, 2 Apr 2024 15:31:26 -0400
Lines: 87
Message-ID: <mailman.58.1712110350.3468.python-list@python.org>
References: <chatbot-20240402181409@ram.dialup.fu-berlin.de>
<fvstdk-607.ln1@lazy.lzy>
<7d38d9e2-78fb-43af-971f-e0d4afb8b039@tompassin.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de FXd1+Vr95sv9DKebpvcaXg5I1aCzyGd37PgDl7tg6H0Q==
Cancel-Lock: sha1:5Efk3OAeh9/tTFu76Vk7valgwxo= sha256:miuSPaptfg2S+wtbwOLQyHJwZkubTaIo9GNkSosBCao=
Return-Path: <list1@tompassin.net>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=tompassin.net header.i=@tompassin.net header.b=vXPYfA79;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.017
X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'argument': 0.04; 'def':
0.04; 'loop': 0.07; 'modification': 0.07; 'ram': 0.07; 'string':
0.07; 'else:': 0.09; 'received:23.83.212': 0.09;
'received:elm.relay.mailchannels.net': 0.09; 'that.': 0.15;
'"not': 0.16; "'e'": 0.16; 'context.': 0.16; 'executed': 0.16;
'explaining': 0.16; 'found,': 0.16; 'jumps': 0.16;
'received:10.0.0': 0.16; 'received:64.90': 0.16;
'received:64.90.62': 0.16; 'received:64.90.62.162': 0.16;
'received:dreamhost.com': 0.16; 'wrote:': 0.16; 'python': 0.16;
'instead': 0.17; "can't": 0.17; 'pm,': 0.19; 'to:addr:python-
list': 0.20; 'all,': 0.20; 'maybe': 0.22; 'returns': 0.22;
"what's": 0.22; 'code': 0.23; 'list,': 0.24; 'depends': 0.25;
'programming': 0.25; 'stefan': 0.26; 'function': 0.27; 'done':
0.28; 'example,': 0.28; 'header:User-Agent:1': 0.30; "doesn't":
0.32; 'guess': 0.32; 'python-list': 0.32; 'received:10.0': 0.32;
'received:mailchannels.net': 0.32;
'received:relay.mailchannels.net': 0.32; 'but': 0.32; 'there':
0.33; 'header:In-Reply-To:1': 0.34; 'target': 0.36; 'people':
0.36; 'subject:from': 0.37; "it's": 0.37; 'read': 0.38; 'break':
0.39; 'still': 0.40; 'beginning': 0.40; 'difficult': 0.40;
'match': 0.40; 'should': 0.40; 'initial': 0.61; 'above': 0.62;
'skip:\xc2 10': 0.62; 'personal': 0.64; 'deliver': 0.64;
'opinion': 0.64; 'similar': 0.65; 'look': 0.65; 'well': 0.65;
'let': 0.66; 'improve': 0.66; 'lead': 0.67; 'smart': 0.67;
'header:Received:6': 0.67; 'received:64': 0.67; 'that,': 0.67;
'skip:w 20': 0.69; 'solutions': 0.70; 'skip:f 30': 0.71; 'read,':
0.75; 'skip:f 20': 0.75; '8bit%:100': 0.76; 'supposed': 0.76;
'more.': 0.82; 'know?': 0.84; 'picks': 0.84; 'solutions,': 0.84;
'statement.': 0.84; '\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0': 0.84
X-Sender-Id: dreamhost|x-authsender|tpassin@tompassin.net
ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1712086292; a=rsa-sha256;
cv=none;
b=N5X6rvgH9jhbsJ6Nheh+ExMZ5mKuu9BYqiSd1Z5XE1IdkmYD0t6nSkNhCDCRhaXWe6BaPn
sBrC8IEFm63kE/YRIoyIfeX1xXO+Q505TAaDoCHOAnjb2kM2Bl2C9KfSdKjn3je00ir1Ij
YS5DZ2tQ0sZP7ZCcY7R0zsD/eYqCQuSORtDPNSTfi10ZPDfg4KGfccd2R1RmjeqdypAK0s
drWWhPNymFOwgJB71BA3H7fdatuL6wArh43vFy68zlwj9bRg0/ODAMTVSfQA6UODjSSpNc
7s7l9pzlkGIxlWfh3Wnd+5Y8PKZ2TWMHFCUYE5Aklh1s0ggjNhvNP1zUBZ1oKw==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed;
d=mailchannels.net; s=arc-2022; t=1712086292;
h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
to:to:cc:mime-version:mime-version:content-type:content-type:
content-transfer-encoding:content-transfer-encoding:
in-reply-to:in-reply-to:references:references:dkim-signature;
bh=BipbH/rUa0W7vanqGHgYf7ASHd9//YRWiwshktF9cmQ=;
b=IwMuYaZi0/cNhc7rxT3C7qwiZXJOgAIXZgVP/rJxnfmByW6LwREN8sQonqVX5VZRPYLt72
2fV34Z/gCyHcc/TOiVxlOmPQ96VEku5uPqwbhi4NtX5Q/rDIKKmoRddKboTliEkHB3Bzzb
qZzJ3ic1oKXsz9McKVBf2pjMXOoBzHbpZ9zXB1s1ojUmrZgEi3rU0xjjPdRwhAGwLcZrOK
eCldmklxhd+tckQevY/DHbiP3J6GzHy8sxhK0RAVP6AHg+lJRi3jGa6dNcFdzPgnXtufzK
D0feOD1zrOeXe7GjiXjxJwnWUvJRzwvGsZ4ImwsAZi1md2/LAOFt5y9EGxmuzg==
ARC-Authentication-Results: i=1; rspamd-5ffc56d49c-s8fzv;
auth=pass smtp.auth=dreamhost smtp.mailfrom=list1@tompassin.net
X-Sender-Id: dreamhost|x-authsender|tpassin@tompassin.net
X-MC-Relay: Neutral
X-MailChannels-SenderId: dreamhost|x-authsender|tpassin@tompassin.net
X-MailChannels-Auth-Id: dreamhost
X-Illustrious-Interest: 5db7c16015678896_1712086292993_3660312904
X-MC-Loop-Signature: 1712086292993:1945381688
X-MC-Ingress-Time: 1712086292993
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tompassin.net;
s=dreamhost; t=1712086292;
bh=BipbH/rUa0W7vanqGHgYf7ASHd9//YRWiwshktF9cmQ=;
h=Date:Subject:To:From:Content-Type:Content-Transfer-Encoding;
b=vXPYfA79bzr41bg1tqx/8y8jJX4cSrJXWxkJW3JvLlzfn0BftvfNIJGSStMM3aBGN
iEq8TqyZlKSE1N+mvD5lMYoPucx7kdxjGRmYFWoSf455SEzkCdbEjTa2dh/l+XEI8F
/NrJ9LA4RCbrnR4FudhJely64rV/JHYKAQ7IuvBvQrrH23t098fMqQNDC1J6r4c1WG
IBKfUSjjTvMJp70WQ03gvnrmQgLm2NdbWkTs/f4H2rpg9/lnaoD/liuBLfF4T1NuYb
dlxm4fhBLd2HBiSztY3Gq6bX4SqHc/dSdZR8nPLM+o6S8slY7mmmsymT9paLICWI6n
7VGuVmD1CNlxQ==
User-Agent: Mozilla Thunderbird
Content-Language: en-US
In-Reply-To: <fvstdk-607.ln1@lazy.lzy>
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <7d38d9e2-78fb-43af-971f-e0d4afb8b039@tompassin.net>
X-Mailman-Original-References: <chatbot-20240402181409@ram.dialup.fu-berlin.de>
<fvstdk-607.ln1@lazy.lzy>
 by: Thomas Passin - Tue, 2 Apr 2024 19:31 UTC

On 4/2/2024 1:47 PM, Piergiorgio Sartor via Python-list wrote:
> On 02/04/2024 19.18, Stefan Ram wrote:
>>    Some people can't believe it when I say that chatbots improve
>>    my programming productivity. So, here's a technique I learned
>>    from a chatbot!
>>    It is a structured "break". "Break" still is a kind of jump,
>>    you know?
>>    So, what's a function to return the first word beginning with
>>    an "e" in a given list, like for example
>> [ 'delta', 'epsilon', 'zeta', 'eta', 'theta' ]
>>
>>    ? Well it's
>> def first_word_beginning_with_e( list_ ):
>>      for word in list_:
>>          if word[ 0 ]== 'e': return word
>>
>>    . "return" still can be considered a kind of "goto" statement.
>>    It can lead to errors:
>>
>> def first_word_beginning_with_e( list_ ):
>>      for word in list_:
>>          if word[ 0 ]== 'e': return word
>>      something_to_be_done_at_the_end_of_this_function()
>>    The call sometimes will not be executed here!
>>    So, "return" is similar to "break" in that regard.
>>    But in Python we can write:
>> def first_word_beginning_with_e( list_ ):
>>      return next( ( word for word in list_ if word[ 0 ]== 'e' ), None )
>
> Doesn't look a smart advice.
>
>>    . No jumps anymore, yet the loop is aborted on the first hit

It's worse than "not a smart advice". This code constructs an
unnecessary tuple, then picks out its first element and returns that.
The something_to_be_done() function may or may not be called. And it's
harder to read and understand than necessary. Compare, for example,
with this version:

def first_word_beginning_with_e(target, wordlist):
result = ''
for w in wordlist:
if w.startswith(target):
res = w
break
do_something_else()
return result

If do_something_else() is supposed to fire only if the target is not
found, then this slight modification will do:

def first_word_beginning_with_e(target, wordlist):
result = ''
for w in wordlist:
if w.startswith(target):
res = w
break
else:
do_something_else()
return result

[Using the "target" argument instead of "target[0]" will let you match
an initial string instead of just a the first character].

> First of all, I fail to understand why there
> should be no jumps any more.
> It depends on how "return" and "if" are handled,
> I guess, in different context.
> Maybe they're just "masked".
> In any case, the "compiler" should have just
> done the same.
>
>>    (if I guess correctly how its working).
>
> Second, it is difficult to read, which is bad.
> The "guess" above is just evidence of that.
>
> My personal opinion about these "chatbots", is
> that, while they might deliver clever solutions,
> they are not explaining *why* these solutions
> should be considered "clever".
> Which is the most important thing (the solution
> itself is _not_).
>
> bye,
>

RE: A technique from a chatbot

<mailman.59.1712122026.3468.python-list@python.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=29793&group=comp.lang.python#29793

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: <avi.e.gross@gmail.com>
Newsgroups: comp.lang.python
Subject: RE: A technique from a chatbot
Date: Wed, 3 Apr 2024 01:27:00 -0400
Lines: 153
Message-ID: <mailman.59.1712122026.3468.python-list@python.org>
References: <chatbot-20240402181409@ram.dialup.fu-berlin.de>
<fvstdk-607.ln1@lazy.lzy>
<7d38d9e2-78fb-43af-971f-e0d4afb8b039@tompassin.net>
<01d501da8587$8dd66c50$a98344f0$@gmail.com>
Mime-Version: 1.0
Content-Type: text/plain;
charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Trace: news.uni-berlin.de i1PY9bv7wx94MInLyKlEMwj0EqsdEoVmuhwjzoadnDWA==
Cancel-Lock: sha1:thtUwIQdqdNAHMswor+6/+nqCwI= sha256:cIKUwWlZEVDDTGh0eA8/fTSPouc+Akzi1HiMuOECRNs=
Return-Path: <avi.e.gross@gmail.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=gmail.com header.i=@gmail.com header.b=EKY0Vm5k;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.012
X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'argument': 0.04; 'def':
0.04; '3.8': 0.05; 'fairly': 0.05; 'variable': 0.05; 'loop': 0.07;
'matching': 0.07; 'modification': 0.07; 'ram': 0.07; 'string':
0.07; 'suggestion': 0.07; 'else:': 0.09; 'received:108': 0.09;
'received:mail-qk1-x72b.google.com': 0.09; 'url:mailman': 0.15;
'that.': 0.15; '"not': 0.16; "'e'": 0.16; '2024': 0.16;
'compiled': 0.16; 'confused': 0.16; 'context.': 0.16; 'executed':
0.16; 'explaining': 0.16; 'found,': 0.16; 'found.': 0.16; 'goto':
0.16; 'jumps': 0.16; 'none)': 0.16; 'nothing.': 0.16; 'variable,':
0.16; 'variant': 0.16; 'ways.': 0.16; 'wrote:': 0.16; 'problem':
0.16; 'python': 0.16; 'instead': 0.17; "can't": 0.17; 'message-
id:@gmail.com': 0.18; 'pm,': 0.19; 'to:addr:python-list': 0.20;
'all,': 0.20; 'maybe': 0.22; 'returns': 0.22; "what's": 0.22;
'version': 0.23; 'code': 0.23; 'skip:p 30': 0.23; 'list,': 0.24;
'skip:- 10': 0.25; 'url-ip:188.166.95.178/32': 0.25; 'url-
ip:188.166.95/24': 0.25; 'depends': 0.25; 'url:listinfo': 0.25;
'programming': 0.25; 'url-ip:188.166/16': 0.25; 'pattern': 0.26;
'stefan': 0.26; 'else': 0.27; 'function': 0.27; 'done': 0.28;
'example,': 0.28; 'url-ip:188/8': 0.31; "doesn't": 0.32; 'fine.':
0.32; 'guess': 0.32; 'python-list': 0.32; 'but': 0.32; 'there':
0.33; 'header:In-Reply-To:1': 0.34; 'received:google.com': 0.34;
'running': 0.34; 'complex': 0.35; 'runs': 0.35;
'from:addr:gmail.com': 0.35; 'target': 0.36; 'people': 0.36;
'necessarily': 0.37; 'subject:from': 0.37; "it's": 0.37; 'read':
0.38; 'least': 0.39; 'single': 0.39; 'text': 0.39; 'list': 0.39;
'use': 0.39; 'break': 0.39; 'still': 0.40; 'beginning': 0.40;
'difficult': 0.40; 'match': 0.40; 'should': 0.40; 'including':
0.60; 'initial': 0.61; 'above': 0.62; 'from:': 0.62; 'to:': 0.62;
'finally': 0.62; 'gain': 0.62; 'come': 0.62; 'simply': 0.63;
'complete': 0.64; 'personal': 0.64; 'clear': 0.64; 'full': 0.64;
'deliver': 0.64; 'opinion': 0.64; 'others,': 0.64; 're:': 0.64;
'similar': 0.65; 'look': 0.65; 'well': 0.65; 'less': 0.65; 'let':
0.66; 'improve': 0.66; 'lead': 0.67; 'shows': 0.67; 'smart': 0.67;
'that,': 0.67; 'counter': 0.69; 'it:': 0.69; 'skip:w 20': 0.69;
'solutions': 0.70; 'conditions': 0.70; 'skip:f 30': 0.71;
'longer': 0.71; 'read,': 0.75; 'skip:f 20': 0.75; 'supposed':
0.76; 'sent:': 0.78; 'returned': 0.81; 'more.': 0.82; 'points':
0.84; 'eastern': 0.84; 'know?': 0.84; 'picks': 0.84; 'solutions,':
0.84; 'statement.': 0.84; 'return,': 0.91; 'underneath': 0.91;
'immediate': 0.97
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20230601; t=1712122024; x=1712726824; darn=python.org;
h=content-language:thread-index:content-transfer-encoding
:mime-version:message-id:date:subject:in-reply-to:references:to:from
:from:to:cc:subject:date:message-id:reply-to;
bh=tVY2ViajLa0J9R3pq4ZG6XBuwktyQHPJonyosD5gAgY=;
b=EKY0Vm5knYQEyIpSz7sQjVt12/XG8mUyZFGP/aZCp/Om7ePg3IcQ2yM5O0RpIvAEgc
FsBcphhnLhEXT7bjrEyBO2YJho4NbIbENfHfBRIGdM8t7icbGdWLeGOCV4OIviiURRhX
2e81MP8IqcfQVK+iatPY3jspwEo49diRwqbuL6G8s6yRv9orJTatzhrJx5cngLy+t5Ri
fS0/G+IHsBkxn0/D4tkbusE5mDIZ6zD2eA7COiWrg1yEVWRpkZ3eVNF5BV5/l5gW9bI7
gGedQgL7pUNJwjikiLFb7oOq4fSIg6pxeIrDLwppXr90UCZCk9vqPyky0hdKI47S6ijF
G+gw==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20230601; t=1712122024; x=1712726824;
h=content-language:thread-index:content-transfer-encoding
:mime-version:message-id:date:subject:in-reply-to:references:to:from
:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to;
bh=tVY2ViajLa0J9R3pq4ZG6XBuwktyQHPJonyosD5gAgY=;
b=t6qmEZI9m+laoAaD8Hz2u2xBOkQYHlO0dvupP9tRAa7DU9KVmMOHmoDLnoyBPkEkGA
apYPuw+iSnwU10KsDTTV6OzqJ1NL6zqRbiG8Pxzoj77Jd2+7ICE1iP6O47fGRedKLvzd
5yC5GWMijmcks4d0Pywp/FQtJ5OHsh1vj4tnDF6G07SPKj8H1UkSXzQkm/flV6yeyM8W
KeNG6fdgW/9Gs98D/0v16poXvPLKca75tOlqCbY6vWqX7V2VDWxGEUh9xZhqYbYlWTlc
qn9ZpaTXe1donsD1qb2fzxLNGoJzvmCoud3Xj+VxUmDTECE9PAeeKvNWeCDVRATd762j
V8jQ==
X-Gm-Message-State: AOJu0Ywzvu1p/slkRXGOnTWOgaFKSBQQUw+Tj6Df1Jp+QpBU1+wf6ExO
fvjvPXrlnNP0so2tTe8GklhUMAIiW6l8Tvj2tQYaK5JVv7yFemocbjIdFnPz
X-Google-Smtp-Source: AGHT+IGbL7MquukcGkTzs+Es+0A7PyvLLxxb0O3VAU6oQRL6e9uWciP5eGXKxz12F7/jj0Zz0GjcZw==
X-Received: by 2002:ae9:ee15:0:b0:789:d43b:3702 with SMTP id
i21-20020ae9ee15000000b00789d43b3702mr13999614qkg.59.1712122023598;
Tue, 02 Apr 2024 22:27:03 -0700 (PDT)
In-Reply-To: <7d38d9e2-78fb-43af-971f-e0d4afb8b039@tompassin.net>
X-Mailer: Microsoft Outlook 16.0
Thread-Index: AQG2as0aVd2VIulEmuXRHp684y8JCQJBvrQcAfnDloGxfFEv4A==
Content-Language: en-us
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <01d501da8587$8dd66c50$a98344f0$@gmail.com>
X-Mailman-Original-References: <chatbot-20240402181409@ram.dialup.fu-berlin.de> <fvstdk-607.ln1@lazy.lzy>
<7d38d9e2-78fb-43af-971f-e0d4afb8b039@tompassin.net>
 by: <avi.e.gross@gmail.com> - Wed, 3 Apr 2024 05:27 UTC

I am a tad confused by a suggestion that any kind of GOTO variant is bad. The suggestion runs counter to the reality that underneath it all, compiled programs are chock full of GOTO variants even for simple things like IF-ELSE.

Consider the code here:

>> def first_word_beginning_with_e( list_ ):
>> for word in list_:
>> if word[ 0 ]== 'e': return word
>> something_to_be_done_at_the_end_of_this_function()

If instead the function initialized a variable to nothing useful and in the loop if it found a word beginning with e and it still contained nothing useful, copied it into the variable and then allowed the code to complete the loop and finally returned the variable, that would simply be a much less efficient solution to the problem and gain NOTHING. There are many variants you can come up with and when the conditions are complex and many points of immediate return, fine, then it may be dangerous. But a single return is fine.

The function does have a flaw as it is not clear what it should do if nothing is found. Calling a silly long name does not necessarily return anything.

Others, like Thomas, have shown other variants including some longer and more complex ways.

A fairly simple one-liner version, not necessarily efficient, would be to just use a list comprehension that makes a new list of just the ones matching the pattern of starting with an 'e' and then returns the first entry or None. This shows the code and test it:

text = ["eastern", "Western", "easter"]

NorEaster = ["North", "West", "orient"]

def first_word_beginning_with_e( list_ ):
return(result[0] if (result := [word for word in list_ if word[0].lower() == 'e']) else None)

print(first_word_beginning_with_e( text ))
print(first_word_beginning_with_e( NorEaster ))

Result of running it on a version of python ay least 3.8 so it supports the walrus operator:

eastern
None

-----Original Message-----
From: Python-list <python-list-bounces+avi.e.gross=gmail.com@python.org> On Behalf Of Thomas Passin via Python-list
Sent: Tuesday, April 2, 2024 3:31 PM
To: python-list@python.org
Subject: Re: A technique from a chatbot

On 4/2/2024 1:47 PM, Piergiorgio Sartor via Python-list wrote:
> On 02/04/2024 19.18, Stefan Ram wrote:
>> Some people can't believe it when I say that chatbots improve
>> my programming productivity. So, here's a technique I learned
>> from a chatbot!
>> It is a structured "break". "Break" still is a kind of jump,
>> you know?
>> So, what's a function to return the first word beginning with
>> an "e" in a given list, like for example
>> [ 'delta', 'epsilon', 'zeta', 'eta', 'theta' ]
>>
>> ? Well it's
>> def first_word_beginning_with_e( list_ ):
>> for word in list_:
>> if word[ 0 ]== 'e': return word
>>
>> . "return" still can be considered a kind of "goto" statement.
>> It can lead to errors:
>>
>> def first_word_beginning_with_e( list_ ):
>> for word in list_:
>> if word[ 0 ]== 'e': return word
>> something_to_be_done_at_the_end_of_this_function()
>> The call sometimes will not be executed here!
>> So, "return" is similar to "break" in that regard.
>> But in Python we can write:
>> def first_word_beginning_with_e( list_ ):
>> return next( ( word for word in list_ if word[ 0 ]== 'e' ), None )
>
> Doesn't look a smart advice.
>
>> . No jumps anymore, yet the loop is aborted on the first hit

It's worse than "not a smart advice". This code constructs an
unnecessary tuple, then picks out its first element and returns that.
The something_to_be_done() function may or may not be called. And it's
harder to read and understand than necessary. Compare, for example,
with this version:

def first_word_beginning_with_e(target, wordlist):
result = ''
for w in wordlist:
if w.startswith(target):
res = w
break
do_something_else()
return result

If do_something_else() is supposed to fire only if the target is not
found, then this slight modification will do:

def first_word_beginning_with_e(target, wordlist):
result = ''
for w in wordlist:
if w.startswith(target):
res = w
break
else:
do_something_else()
return result

[Using the "target" argument instead of "target[0]" will let you match
an initial string instead of just a the first character].

> First of all, I fail to understand why there
> should be no jumps any more.
> It depends on how "return" and "if" are handled,
> I guess, in different context.
> Maybe they're just "masked".
> In any case, the "compiler" should have just
> done the same.
>
>> (if I guess correctly how its working).
>
> Second, it is difficult to read, which is bad.
> The "guess" above is just evidence of that.
>
> My personal opinion about these "chatbots", is
> that, while they might deliver clever solutions,
> they are not explaining *why* these solutions
> should be considered "clever".
> Which is the most important thing (the solution
> itself is _not_).
>
> bye,
>

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

Re: A technique from a chatbot

<mailman.62.1712153666.3468.python-list@python.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=29796&group=comp.lang.python#29796

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: list1@tompassin.net (Thomas Passin)
Newsgroups: comp.lang.python
Subject: Re: A technique from a chatbot
Date: Wed, 3 Apr 2024 07:50:55 -0400
Lines: 45
Message-ID: <mailman.62.1712153666.3468.python-list@python.org>
References: <chatbot-20240402181409@ram.dialup.fu-berlin.de>
<fvstdk-607.ln1@lazy.lzy>
<7d38d9e2-78fb-43af-971f-e0d4afb8b039@tompassin.net>
<01d501da8587$8dd66c50$a98344f0$@gmail.com>
<435c439b-148b-4440-b2d2-ea3611679216@tompassin.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: news.uni-berlin.de lojx6PBNV939TS9377/AiQhuCyNyyJx2+YxBPgMLIoFQ==
Cancel-Lock: sha1:y6FO8RJQEz3UCOkbMvtabJnSAGE= sha256:Reittz976mKxWS1lC8y+ItvETAhPIb/qskbYc0zs1ew=
Return-Path: <list1@tompassin.net>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=tompassin.net header.i=@tompassin.net header.b=hFaI4GFq;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.020
X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'def': 0.04; '3.8': 0.05;
'fairly': 0.05; 'variable': 0.05; 'loop': 0.07; 'matching': 0.07;
'suggestion': 0.07; "'e'": 0.16; 'avi': 0.16; 'compiled': 0.16;
'confused': 0.16; 'constructed': 0.16; 'found.': 0.16; 'goto':
0.16; 'gross': 0.16; 'none)': 0.16; 'nothing.': 0.16;
'received:10.0.0': 0.16; 'received:64.90': 0.16;
'received:64.90.62': 0.16; 'received:64.90.62.162': 0.16;
'received:dreamhost.com': 0.16; 'variable,': 0.16; 'variant':
0.16; 'ways.': 0.16; 'wrote:': 0.16; 'problem': 0.16; 'python':
0.16; 'instead': 0.17; 'probably': 0.17; 'to:addr:python-list':
0.20; 'all,': 0.20; 'returns': 0.22; 'version': 0.23; 'code':
0.23; 'skip:p 30': 0.23; 'seems': 0.26; 'pattern': 0.26; 'else':
0.27; 'function': 0.27; '>>>': 0.28; 'header:User-Agent:1': 0.30;
'am,': 0.31; 'fine.': 0.32; 'python-list': 0.32; 'received:10.0':
0.32; 'received:mailchannels.net': 0.32;
'received:relay.mailchannels.net': 0.32; 'but': 0.32; 'there':
0.33; 'header:In-Reply-To:1': 0.34; 'running': 0.34; 'complex':
0.35; 'runs': 0.35; 'necessarily': 0.37; 'special': 0.37;
'subject:from': 0.37; 'could': 0.38; 'least': 0.39; 'single':
0.39; 'text': 0.39; "we've": 0.39; 'list': 0.39; 'use': 0.39;
'still': 0.40; 'beginning': 0.40; 'done.': 0.40; 'match': 0.40;
'want': 0.40; 'should': 0.40; 'including': 0.60; 'finally': 0.62;
'gain': 0.62; 'come': 0.62; 'simply': 0.63; 'complete': 0.64;
'clear': 0.64; 'full': 0.64; 'others,': 0.64; 'less': 0.65;
'shows': 0.67; 'bad': 0.67; 'header:Received:6': 0.67;
'received:64': 0.67; 'counter': 0.69; 'it:': 0.69; 'conditions':
0.70; 'experience.': 0.70; 'truly': 0.70; 'longer': 0.71; 'skip:f
20': 0.75; 'returned': 0.81; 'points': 0.84; 'eastern': 0.84;
'poorly': 0.84; 'posed': 0.84; 'caused': 0.86; 'return,': 0.91;
'underneath': 0.91; 'immediate': 0.97
X-Sender-Id: dreamhost|x-authsender|tpassin@tompassin.net
ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1712145055; a=rsa-sha256;
cv=none;
b=BvNUtWYzwlX7zkXfU7pqtSHe3OBIytZpab8hu7Yyl6DNr4bjypeTdHm44wfpLH4bjx7Sl+
w5PNiZ1oqw/AmEbtBpmnu1GiZ3hn4zVd9/Lj2Sg8ldJHMxcyVkAv3F3ije6uxAHbOqxfNw
710VAT/BP8ojZaUJsL0Z6vqTUM1/TPRCgAXwSh25jUWkWER3aHiSVu4ELWKmTJYkc3rS06
ODT/UgV8h8YLHviMtb15IWFkNka+lgR9BTYuiRv8tBX22yccf/gzF7X/bpMQgrLOAWPiQr
OhOGNhogZzWCkOSvuMWchCsoxp0X5N+8cgV6nphk2mAWf6fdzXrFHG8galEzuA==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed;
d=mailchannels.net; s=arc-2022; t=1712145055;
h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
to:to:cc:mime-version:mime-version:content-type:content-type:
content-transfer-encoding:content-transfer-encoding:
in-reply-to:in-reply-to:references:references:dkim-signature;
bh=y3iEyJOgvJ2c5PWwttr8+ftAmv2olIrOT1nzc8Uyoiw=;
b=vQAVeAM1RYoIP+xt9u46PmJdq3XOz17swxJdjjJB5GeEExzVxOp5bdabdv+nvYykvMmJvf
tcCVqvSMT1wmkpfes560cngedlcY3SXhaM6zbD6x1/Rqd+bdBxTRpjkpokJHzoVukfKyUx
ETX6QEGMDPAt/ZuR5LWxa6DFMxmBh4TClfErFAHK/GZDdS1Rk3WkPeY+u6+OHlJxdeenA7
2CkYWlGDv//B5ZsD8W3QuFFoLupCOXVGnnPtvlbnbnx3Ma6Pz/0j+0yuijkfZllGXKSqIX
QSc79NEQtnj+yckgpnmPqeoP7DkRaknJBGgLI2FTBF2YehdsqhM1WS6vC9Fd1g==
ARC-Authentication-Results: i=1; rspamd-5ffc56d49c-h99q5;
auth=pass smtp.auth=dreamhost smtp.mailfrom=list1@tompassin.net
X-Sender-Id: dreamhost|x-authsender|tpassin@tompassin.net
X-MC-Relay: Neutral
X-MailChannels-SenderId: dreamhost|x-authsender|tpassin@tompassin.net
X-MailChannels-Auth-Id: dreamhost
X-Trade-Whispering: 59d3fbf1222f6090_1712145056025_3528444617
X-MC-Loop-Signature: 1712145056024:3744907088
X-MC-Ingress-Time: 1712145056024
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tompassin.net;
s=dreamhost; t=1712145055;
bh=y3iEyJOgvJ2c5PWwttr8+ftAmv2olIrOT1nzc8Uyoiw=;
h=Date:Subject:To:From:Content-Type:Content-Transfer-Encoding;
b=hFaI4GFqcNL0CCnBI7b35jFh2O57ebzZl4fAtLehG9olVl7qxrR0B7MQ9ZZR4h6gm
FuHAvWySLdpGhsFQrsakPYYBZc/1Wr2TSmjLNcKJcrFskwF4k6eXGhnl+bLSjKlswW
BGsA4XzahXz7qDZ2qu85r6xO027T8uDxRVSEzki09ZTY7ojC0ZxKtSdXnGA4jCX4Yk
SHL56JKspsNmUwragkNNU57Li1LEw0T2hDMs1ROBG03EcVsxvE3IgH/CnqSawNj5B6
jOfXUpQOO+GMeufFNP7EPUzVamkTBlfWCvHZ3BQZzJyo/iXsCMdRsjnPqC1hyXZws2
27lSyeRhY86EQ==
User-Agent: Mozilla Thunderbird
Content-Language: en-US
In-Reply-To: <01d501da8587$8dd66c50$a98344f0$@gmail.com>
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <435c439b-148b-4440-b2d2-ea3611679216@tompassin.net>
X-Mailman-Original-References: <chatbot-20240402181409@ram.dialup.fu-berlin.de>
<fvstdk-607.ln1@lazy.lzy>
<7d38d9e2-78fb-43af-971f-e0d4afb8b039@tompassin.net>
<01d501da8587$8dd66c50$a98344f0$@gmail.com>
 by: Thomas Passin - Wed, 3 Apr 2024 11:50 UTC

On 4/3/2024 1:27 AM, AVI GROSS via Python-list wrote:
> I am a tad confused by a suggestion that any kind of GOTO variant is bad. The suggestion runs counter to the reality that underneath it all, compiled programs are chock full of GOTO variants even for simple things like IF-ELSE.
>
> Consider the code here:
>
>>> def first_word_beginning_with_e( list_ ):
>>> for word in list_:
>>> if word[ 0 ]== 'e': return word
>>> something_to_be_done_at_the_end_of_this_function()
>
> If instead the function initialized a variable to nothing useful and in the loop if it found a word beginning with e and it still contained nothing useful, copied it into the variable and then allowed the code to complete the loop and finally returned the variable, that would simply be a much less efficient solution to the problem and gain NOTHING. There are many variants you can come up with and when the conditions are complex and many points of immediate return, fine, then it may be dangerous. But a single return is fine.
>
> The function does have a flaw as it is not clear what it should do if nothing is found. Calling a silly long name does not necessarily return anything.
>
> Others, like Thomas, have shown other variants including some longer and more complex ways.
>
> A fairly simple one-liner version, not necessarily efficient, would be to just use a list comprehension that makes a new list of just the ones matching the pattern of starting with an 'e' and then returns the first entry or None. This shows the code and test it:
>
> text = ["eastern", "Western", "easter"]
>
> NorEaster = ["North", "West", "orient"]
>
> def first_word_beginning_with_e( list_ ):
> return(result[0] if (result := [word for word in list_ if word[0].lower() == 'e']) else None)
>
> print(first_word_beginning_with_e( text ))
> print(first_word_beginning_with_e( NorEaster ))
>
> Result of running it on a version of python ay least 3.8 so it supports the walrus operator:
>
> eastern
> None

The OP seems to want to return None if a match is not found. If a
Python function ends without a return statement, it automatically
returns None. So nothing special needs to be done. True, that is
probably a special case, but it suggests that the problem posed to the
chatbot was not posed well. A truly useful chatbot could have discussed
many of the points we've been discussing. That would have made for a
good learning experience. Instead the chatbot produced poorly
constructed code that caused a bad learning experience.

> [snip...]

RE: A technique from a chatbot

<mailman.63.1712158372.3468.python-list@python.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=29797&group=comp.lang.python#29797

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!rocksolid2!news.neodome.net!fu-berlin.de!uni-berlin.de!not-for-mail
From: <avi.e.gross@gmail.com>
Newsgroups: comp.lang.python
Subject: RE: A technique from a chatbot
Date: Wed, 3 Apr 2024 11:32:44 -0400
Lines: 89
Message-ID: <mailman.63.1712158372.3468.python-list@python.org>
References: <chatbot-20240402181409@ram.dialup.fu-berlin.de>
<fvstdk-607.ln1@lazy.lzy>
<7d38d9e2-78fb-43af-971f-e0d4afb8b039@tompassin.net>
<01d501da8587$8dd66c50$a98344f0$@gmail.com>
<435c439b-148b-4440-b2d2-ea3611679216@tompassin.net>
<001901da85dc$2cecd9b0$86c68d10$@gmail.com>
Mime-Version: 1.0
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: 7bit
X-Trace: news.uni-berlin.de DFjJnKNB1WuauTjPvxHZCQ67VDWTY7lsR4FiCIg2wW0Q==
Cancel-Lock: sha1:fBibC47DRW1dCm0dBVeKpbGpHAs= sha256:vAckpaMWz+0jfEj+wJSN2D+f1CB0/eSVENhNYbfwhnc=
Return-Path: <avi.e.gross@gmail.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=gmail.com header.i=@gmail.com header.b=LlOJ4BcN;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.010
X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'def': 0.04; '3.8': 0.05;
'fairly': 0.05; 'variable': 0.05; 'intermediate': 0.07; 'loop':
0.07; 'matching': 0.07; 'suggestion': 0.07; 'received:108': 0.09;
'received:mail-qk1-x72e.google.com': 0.09; 'steps': 0.11;
'url:mailman': 0.15; "'e'": 0.16; '2024': 0.16; 'avi': 0.16;
'calculations': 0.16; 'cases,': 0.16; 'compiled': 0.16;
'confused': 0.16; 'constructed': 0.16; 'found.': 0.16; 'goto':
0.16; 'gross': 0.16; 'none)': 0.16; 'nothing.': 0.16; 'sadly,':
0.16; 'variable,': 0.16; 'variant': 0.16; 'ways.': 0.16; 'wrote:':
0.16; 'problem': 0.16; 'python': 0.16; 'instead': 0.17;
'probably': 0.17; 'message-id:@gmail.com': 0.18; 'to:addr:python-
list': 0.20; 'all,': 0.20; 'returns': 0.22; 'version': 0.23;
'code': 0.23; 'skip:p 30': 0.23; 'skip:- 10': 0.25; 'url-
ip:188.166.95.178/32': 0.25; 'url-ip:188.166.95/24': 0.25;
'url:listinfo': 0.25; 'url-ip:188.166/16': 0.25; 'seems': 0.26;
'pattern': 0.26; 'wednesday,': 0.26; 'else': 0.27; 'function':
0.27; '>>>': 0.28; 'am,': 0.31; 'url-ip:188/8': 0.31; 'fine.':
0.32; 'python-list': 0.32; 'but': 0.32; 'there': 0.33; 'header:In-
Reply-To:1': 0.34; 'received:google.com': 0.34; 'running': 0.34;
'complex': 0.35; 'runs': 0.35; 'yes,': 0.35;
'from:addr:gmail.com': 0.35; 'people': 0.36; 'necessarily': 0.37;
'special': 0.37; 'subject:from': 0.37; 'could': 0.38; 'read':
0.38; 'quite': 0.39; 'least': 0.39; 'single': 0.39; 'text': 0.39;
"we've": 0.39; 'list': 0.39; 'use': 0.39; 'on.': 0.39; 'still':
0.40; 'beginning': 0.40; 'done.': 0.40; 'match': 0.40;
'something': 0.40; 'want': 0.40; 'should': 0.40; 'including':
0.60; 'from:': 0.62; 'to:': 0.62; 'seen': 0.62; 'finally': 0.62;
'gain': 0.62; 'come': 0.62; 'simply': 0.63; 'complete': 0.64;
'clear': 0.64; 'full': 0.64; 'others,': 0.64; 're:': 0.64; 'less':
0.65; 'shows': 0.67; 'back': 0.67; 'bad': 0.67; 'and,': 0.69;
'counter': 0.69; 'it:': 0.69; 'conditions': 0.70; 'experience.':
0.70; 'truly': 0.70; 'longer': 0.71; 'skip:f 20': 0.75; 'sent:':
0.78; 'returned': 0.81; 'points': 0.84; 'eastern': 0.84; 'poorly':
0.84; 'posed': 0.84; 'caused': 0.86; 'return,': 0.91;
'underneath': 0.91; 'immediate': 0.97
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20230601; t=1712158369; x=1712763169; darn=python.org;
h=thread-index:content-language:content-transfer-encoding
:mime-version:message-id:date:subject:in-reply-to:references:to:from
:from:to:cc:subject:date:message-id:reply-to;
bh=ESfYUssCRqBIT6jNC24ZzD70URQGOF2LYF84V6kq+6Y=;
b=LlOJ4BcNfLqMv3ULjOIhN/Q22ZrIr7B8ZAhkFhNjLyGRciypmhe8gO9l2XVSWWV9s0
IfgCsn3OC3p6qUiSJYSxTls4quHfMoDOofHF1g+GKrKXETbbJcHytUOavBbZD9wnDCdM
O0ES4Bdjmcg/P/EJCFm+iQZ2cgJcnRpdIBOopAoeyDDkhKS6EmbAVeGw3iuXxMmedUj5
jbkE9yt4eY15HM9MBagpu2rHDjoAV71LwU0LNkcBFCUFw4znBGEE/K/K1KUvXfmCU28b
rIoM5V4IB9JRhxwaPH3U2E5TWDgAY+/9br1m1rTbarjkTa5uo+2a+jacaUrP2c8KMAwG
cRkg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20230601; t=1712158369; x=1712763169;
h=thread-index:content-language:content-transfer-encoding
:mime-version:message-id:date:subject:in-reply-to:references:to:from
:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to;
bh=ESfYUssCRqBIT6jNC24ZzD70URQGOF2LYF84V6kq+6Y=;
b=TtHEtrjjA1FtZuYR8boVxIGqZoR3MBCtE0iP+9DzEQUeQEzD09jJ9xhveyZkCJChlD
zmz2TRqxRFZdGFIhuHDplNLHEYghLtGgzVNexBMUc8GwZCQo8xTmweeOdRq0fFzih3wY
w/HLOw4Jo+Z6lx5ZpeeYSz47smdvg9GsgHGjHPXsarpshrUkLETMuvS1NSlNUAf8vQp3
lBCavnv21TR0hNsiK4A5tvlK2aENZJZh6EgkVLBeBlU/qSJurCjUdrHSpBkygNfibX/p
e0PbILgKFeJYW37k0EBdbrHZoTBfCZWytZbsh9/TF8cL8soW20/GUNMugQj7OoRxPlir
eGQA==
X-Forwarded-Encrypted: i=1;
AJvYcCWM1yGTPaGZRUSCZJCR6D/AxQWJ2sRAX6mbuYlhK1BRNtLnI9RoL+ti/aVy9ms05PKPppNtWoP5ohWEk3+XEKhbryEn6FnW
X-Gm-Message-State: AOJu0YzuPUvnFe+lvCwSAxTbdvZ+hdncUPkoE0hrmTIIifysxZQ8Y/k2
W+0pa4+gv7hI5B9qdYqOjVra5assDU9GYzLaHnvtDb5kh9B+b7Xz5IiDCmN1
X-Google-Smtp-Source: AGHT+IGwG0r2AL5+1wiSVaR3Lr/5xqi2rM8U4Iq6pCzAYlXCxMHvrJTiw6ayENpx4M4qc2RXvAjhFA==
X-Received: by 2002:a05:620a:d83:b0:78b:ea1a:4171 with SMTP id
q3-20020a05620a0d8300b0078bea1a4171mr8731076qkl.36.1712158369198;
Wed, 03 Apr 2024 08:32:49 -0700 (PDT)
In-Reply-To: <435c439b-148b-4440-b2d2-ea3611679216@tompassin.net>
X-Mailer: Microsoft Outlook 16.0
Content-Language: en-us
Thread-Index: AQG2as0aVd2VIulEmuXRHp684y8JCQJBvrQcAfnDloEBbJq5qQGVLTBOsWT3WtA=
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <001901da85dc$2cecd9b0$86c68d10$@gmail.com>
X-Mailman-Original-References: <chatbot-20240402181409@ram.dialup.fu-berlin.de> <fvstdk-607.ln1@lazy.lzy>
<7d38d9e2-78fb-43af-971f-e0d4afb8b039@tompassin.net>
<01d501da8587$8dd66c50$a98344f0$@gmail.com>
<435c439b-148b-4440-b2d2-ea3611679216@tompassin.net>
 by: <avi.e.gross@gmail.com> - Wed, 3 Apr 2024 15:32 UTC

Sadly, Thomas, this is not even all that new.

I have seen people do searches on the internet for how to do one thing at a
time and then cobble together some code that does something but perhaps not
quite what they intended. Some things are just inefficient such as reading
data from a file, doing some calculations, writing the results to another
file, reading them back in and doing more calculations and writing them out
again and so on. Yes, there can be value in storing intermediate results but
why read it in again when it is already in memory? And, in some cases, why
not do multiple steps instead of one at a time and so on.

How many people ask how to TEST the code they get, especially from an
AI-like ...?

-----Original Message-----
From: Python-list <python-list-bounces+avi.e.gross=gmail.com@python.org> On
Behalf Of Thomas Passin via Python-list
Sent: Wednesday, April 3, 2024 7:51 AM
To: python-list@python.org
Subject: Re: A technique from a chatbot

On 4/3/2024 1:27 AM, AVI GROSS via Python-list wrote:
> I am a tad confused by a suggestion that any kind of GOTO variant is bad.
The suggestion runs counter to the reality that underneath it all, compiled
programs are chock full of GOTO variants even for simple things like
IF-ELSE.
>
> Consider the code here:
>
>>> def first_word_beginning_with_e( list_ ):
>>> for word in list_:
>>> if word[ 0 ]== 'e': return word
>>> something_to_be_done_at_the_end_of_this_function()
>
> If instead the function initialized a variable to nothing useful and in
the loop if it found a word beginning with e and it still contained nothing
useful, copied it into the variable and then allowed the code to complete
the loop and finally returned the variable, that would simply be a much less
efficient solution to the problem and gain NOTHING. There are many variants
you can come up with and when the conditions are complex and many points of
immediate return, fine, then it may be dangerous. But a single return is
fine.
>
> The function does have a flaw as it is not clear what it should do if
nothing is found. Calling a silly long name does not necessarily return
anything.
>
> Others, like Thomas, have shown other variants including some longer and
more complex ways.
>
> A fairly simple one-liner version, not necessarily efficient, would be to
just use a list comprehension that makes a new list of just the ones
matching the pattern of starting with an 'e' and then returns the first
entry or None. This shows the code and test it:
>
> text = ["eastern", "Western", "easter"]
>
> NorEaster = ["North", "West", "orient"]
>
> def first_word_beginning_with_e( list_ ):
> return(result[0] if (result := [word for word in list_ if
word[0].lower() == 'e']) else None)
>
> print(first_word_beginning_with_e( text ))
> print(first_word_beginning_with_e( NorEaster ))
>
> Result of running it on a version of python ay least 3.8 so it supports
the walrus operator:
>
> eastern
> None

The OP seems to want to return None if a match is not found. If a
Python function ends without a return statement, it automatically
returns None. So nothing special needs to be done. True, that is
probably a special case, but it suggests that the problem posed to the
chatbot was not posed well. A truly useful chatbot could have discussed
many of the points we've been discussing. That would have made for a
good learning experience. Instead the chatbot produced poorly
constructed code that caused a bad learning experience.

> [snip...]

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

Re: A technique from a chatbot

<4thPN.242011$oD2.235883@fx11.ams4>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=29801&group=comp.lang.python#29801

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!news.in-chemnitz.de!3.eu.feeder.erje.net!feeder.erje.net!feeder1-2.proxad.net!proxad.net!feeder1-1.proxad.net!193.141.40.65.MISMATCH!npeer.as286.net!npeer-ng0.as286.net!peer01.ams1!peer.ams1.xlned.com!news.xlned.com!peer03.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!fx11.ams4.POSTED!not-for-mail
From: gilmeh.serda@nothing.here.invalid (Gilmeh Serda)
Subject: Re: A technique from a chatbot
Newsgroups: comp.lang.python
References: <chatbot-20240402181409@ram.dialup.fu-berlin.de>
MIME-Version: 1.0
x-no-archive: yes
User-Agent: Pan/0.155 (Kherson; fc5a80b8)
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Lines: 16
Message-ID: <4thPN.242011$oD2.235883@fx11.ams4>
X-Complaints-To: abuse@easynews.com
Organization: Easynews - www.easynews.com
X-Complaints-Info: Please be sure to forward a copy of ALL headers otherwise we will be unable to process your complaint properly.
Date: Wed, 03 Apr 2024 18:45:20 GMT
X-Received-Bytes: 1145
 by: Gilmeh Serda - Wed, 3 Apr 2024 18:45 UTC

On 2 Apr 2024 17:18:16 GMT, Stefan Ram wrote:

> first_word_beginning_with_e

Here's another one:

>>> def ret_first_eword():
.... return [w for w in ['delta', 'epsilon', 'zeta', 'eta', 'theta'] if w.startswith('e')][0]
....
>>> ret_first_eword()
'epsilon'

--
Gilmeh

Linux is addictive, I'm hooked! -- MaDsen Wikholm's .sig

Re: A technique from a chatbot

<m2le5up2qh.fsf@cochabamba-1.kpn>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=29804&group=comp.lang.python#29804

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: pieter-l@vanoostrum.org (Pieter van Oostrum)
Newsgroups: comp.lang.python
Subject: Re: A technique from a chatbot
Date: Wed, 03 Apr 2024 23:15:18 +0200
Lines: 25
Message-ID: <m2le5up2qh.fsf@cochabamba-1.kpn>
References: <chatbot-20240402181409@ram.dialup.fu-berlin.de>
Mime-Version: 1.0
Content-Type: text/plain
X-Trace: individual.net MWG5iDLBWD5Fv8FvhxHUcg7750S7LZgu3xyjnIYy8HP1F8rj7r
Cancel-Lock: sha1:WMt3h8PcZZv+RJJgvr6JFbmhFW0= sha1:IbM++nvx5M8AJpHYmcoZBsK5bZM= sha256:nbI7no8i9fjGsjlpkMvetnbqe9AEnYztG7J0OVDgwxE=
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (darwin)
 by: Pieter van Oostrum - Wed, 3 Apr 2024 21:15 UTC

ram@zedat.fu-berlin.de (Stefan Ram) writes:

> It can lead to errors:
>
> def first_word_beginning_with_e( list_ ):
> for word in list_:
> if word[ 0 ]== 'e': return word
> something_to_be_done_at_the_end_of_this_function()
>
> The call sometimes will not be executed here!
> So, "return" is similar to "break" in that regard.

That can be solved with finally:

def first_word_beginning_with_e( list_ ):
try:
for word in list_:
if word[ 0 ]== 'e': return word
finally:
print("something_to_be_done_at_the_end_of_this_function()")

--
Pieter van Oostrum <pieter@vanoostrum.org>
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]

Re: A technique from a chatbot

<uuki4v$5i2o$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=29805&group=comp.lang.python#29805

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: michael.stemper@gmail.com (Michael F. Stemper)
Newsgroups: comp.lang.python
Subject: Re: A technique from a chatbot
Date: Wed, 3 Apr 2024 16:36:30 -0500
Organization: A noiseless patient Spider
Lines: 34
Message-ID: <uuki4v$5i2o$1@dont-email.me>
References: <chatbot-20240402181409@ram.dialup.fu-berlin.de>
<4thPN.242011$oD2.235883@fx11.ams4>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 03 Apr 2024 21:36:31 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="91f8df7e4896e441aaa7fd6ab7c6ac8e";
logging-data="182360"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18QHmOhEZFlMDneNfg5ELq5uIx5EIyyhbc="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:lQN5p2Bb1fW/hOJiCe2RvhAGTe0=
In-Reply-To: <4thPN.242011$oD2.235883@fx11.ams4>
Content-Language: en-US
 by: Michael F. Stemper - Wed, 3 Apr 2024 21:36 UTC

On 03/04/2024 13.45, Gilmeh Serda wrote:
> On 2 Apr 2024 17:18:16 GMT, Stefan Ram wrote:
>
>> first_word_beginning_with_e
>
> Here's another one:
>
>>>> def ret_first_eword():
> ... return [w for w in ['delta', 'epsilon', 'zeta', 'eta', 'theta'] if w.startswith('e')][0]
> ...
>>>> ret_first_eword()
> 'epsilon'

Doesn't work in the case where there isn't a word starting with 'e':

>>> def find_e( l ):
... return [w for w in l if w.startswith('e')][0]
...
>>> l = ['delta', 'epsilon', 'zeta', 'eta', 'theta']
>>> find_e(l)
'epsilon'
>>> l = ['The','fan-jet','airline']
>>> find_e(l)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in find_e
IndexError: list index out of range
>>>

--
Michael F. Stemper
If it isn't running programs and it isn't fusing atoms, it's just bending space.

Re: A technique from a chatbot

<FxBPN.457968$ET2.132548@fx12.ams4>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=29810&group=comp.lang.python#29810

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.nntp4.net!usenet.goja.nl.eu.org!3.eu.feeder.erje.net!feeder.erje.net!npeer.as286.net!npeer-ng0.as286.net!peer01.ams1!peer.ams1.xlned.com!news.xlned.com!peer03.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!fx12.ams4.POSTED!not-for-mail
From: gilmeh.serda@nothing.here.invalid (Gilmeh Serda)
Subject: Re: A technique from a chatbot
Newsgroups: comp.lang.python
References: <chatbot-20240402181409@ram.dialup.fu-berlin.de>
<4thPN.242011$oD2.235883@fx11.ams4> <uuki4v$5i2o$1@dont-email.me>
MIME-Version: 1.0
x-no-archive: yes
User-Agent: Pan/0.155 (Kherson; fc5a80b8)
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Lines: 47
Message-ID: <FxBPN.457968$ET2.132548@fx12.ams4>
X-Complaints-To: abuse@easynews.com
Organization: Easynews - www.easynews.com
X-Complaints-Info: Please be sure to forward a copy of ALL headers otherwise we will be unable to process your complaint properly.
Date: Thu, 04 Apr 2024 17:35:33 GMT
X-Received-Bytes: 2092
 by: Gilmeh Serda - Thu, 4 Apr 2024 17:35 UTC

On Wed, 3 Apr 2024 16:36:30 -0500, Michael F. Stemper wrote:

> On 03/04/2024 13.45, Gilmeh Serda wrote:
>> On 2 Apr 2024 17:18:16 GMT, Stefan Ram wrote:
>>
>>> first_word_beginning_with_e
>>
>> Here's another one:
>>
>>>>> def ret_first_eword():
>> ... return [w for w in ['delta', 'epsilon', 'zeta', 'eta', 'theta']
>> if w.startswith('e')][0]
>> ...
>>>>> ret_first_eword()
>> 'epsilon'
>
> Doesn't work in the case where there isn't a word starting with 'e':
>
> >>> def find_e( l ):
> ... return [w for w in l if w.startswith('e')][0]
> ...
> >>> l = ['delta', 'epsilon', 'zeta', 'eta', 'theta']
> >>> find_e(l)
> 'epsilon'
> >>> l = ['The','fan-jet','airline']
> >>> find_e(l)
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> File "<stdin>", line 2, in find_e
> IndexError: list index out of range
> >>>

Wow! It wasn't production code. And still isn't. (o.Ô)

>>> def find_e(l):
.... try:
.... return [w for w in l if w.startswith('e')][0]
.... except IndexError:
.... return None # or 0 or '' or whatever you want
....
>>> find_e(l)

--
Gilmeh

Drop in any mailbox.

Re: A technique from a chatbot

<uumtii$qum4$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=29811&group=comp.lang.python#29811

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: nntp.mbourne@spamgourmet.com (Mark Bourne)
Newsgroups: comp.lang.python
Subject: Re: A technique from a chatbot
Date: Thu, 4 Apr 2024 20:03:45 +0100
Organization: A noiseless patient Spider
Lines: 56
Message-ID: <uumtii$qum4$1@dont-email.me>
References: <chatbot-20240402181409@ram.dialup.fu-berlin.de>
<fvstdk-607.ln1@lazy.lzy>
<7d38d9e2-78fb-43af-971f-e0d4afb8b039@tompassin.net>
<mailman.58.1712110350.3468.python-list@python.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 04 Apr 2024 19:03:47 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="602200ba60396d3f151c99d76fd0de90";
logging-data="883396"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1++O9+T7p0WlzIN0QbgMEDd"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
SeaMonkey/2.53.18.1
Cancel-Lock: sha1:JZAMOpPOKwBgfbxt38Bmo3vPAcQ=
In-Reply-To: <mailman.58.1712110350.3468.python-list@python.org>
 by: Mark Bourne - Thu, 4 Apr 2024 19:03 UTC

Thomas Passin wrote:
> On 4/2/2024 1:47 PM, Piergiorgio Sartor via Python-list wrote:
>> On 02/04/2024 19.18, Stefan Ram wrote:
>>>    Some people can't believe it when I say that chatbots improve
>>>    my programming productivity. So, here's a technique I learned
>>>    from a chatbot!
>>>    It is a structured "break". "Break" still is a kind of jump,
>>>    you know?
>>>    So, what's a function to return the first word beginning with
>>>    an "e" in a given list, like for example
>>> [ 'delta', 'epsilon', 'zeta', 'eta', 'theta' ]
>>>
>>>    ? Well it's
>>> def first_word_beginning_with_e( list_ ):
>>>      for word in list_:
>>>          if word[ 0 ]== 'e': return word
>>>
>>>    . "return" still can be considered a kind of "goto" statement.
>>>    It can lead to errors:
>>>
>>> def first_word_beginning_with_e( list_ ):
>>>      for word in list_:
>>>          if word[ 0 ]== 'e': return word
>>>      something_to_be_done_at_the_end_of_this_function()
>>>    The call sometimes will not be executed here!
>>>    So, "return" is similar to "break" in that regard.
>>>    But in Python we can write:
>>> def first_word_beginning_with_e( list_ ):
>>>      return next( ( word for word in list_ if word[ 0 ]== 'e' ), None )
>>
>> Doesn't look a smart advice.
>>
>>>    . No jumps anymore, yet the loop is aborted on the first hit
>
> It's worse than "not a smart advice". This code constructs an
> unnecessary tuple, then picks out its first element and returns that.

I don't think there's a tuple being created. If you mean:
( word for word in list_ if word[ 0 ]== 'e' )

....that's not creating a tuple. It's a generator expression, which
generates the next value each time it's called for. If you only ever
ask for the first item, it only generates that one.

When I first came across them, I did find it a bit odd that generator
expressions look like the tuple equivalent of list/dictionary
comprehensions.

FWIW, if you actually wanted a tuple from that expression, you'd need to
pass the generator to tuple's constructor:
tuple(word for word in list_ if word[0] == 'e')
(You don't need to include an extra set of brackets when passing a
generator a the only argument to a function).

--
Mark.

RE: A technique from a chatbot

<mailman.71.1712262845.3468.python-list@python.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=29812&group=comp.lang.python#29812

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: <avi.e.gross@gmail.com>
Newsgroups: comp.lang.python
Subject: RE: A technique from a chatbot
Date: Thu, 4 Apr 2024 16:33:57 -0400
Lines: 122
Message-ID: <mailman.71.1712262845.3468.python-list@python.org>
References: <chatbot-20240402181409@ram.dialup.fu-berlin.de>
<fvstdk-607.ln1@lazy.lzy>
<7d38d9e2-78fb-43af-971f-e0d4afb8b039@tompassin.net>
<mailman.58.1712110350.3468.python-list@python.org>
<uumtii$qum4$1@dont-email.me>
<00e701da86cf$6b3e2cc0$41ba8640$@gmail.com>
Mime-Version: 1.0
Content-Type: text/plain;
charset="utf-8"
Content-Transfer-Encoding: quoted-printable
X-Trace: news.uni-berlin.de scRqQTeiWNF7JuNVGRRxMAvjqjoTkgPqkxG3niYFNTpw==
Cancel-Lock: sha1:1kcHuKVSm2zo5+LbjS0/ya8XluY= sha256:3ECYUDJytkXeiLtiX16nEV7QibyoIsROVyZVs05NsAo=
Return-Path: <avi.e.gross@gmail.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=gmail.com header.i=@gmail.com header.b=lhqtYcUR;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.003
X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'argument': 0.04; 'def':
0.04; 'fairly': 0.05; 'parallel': 0.05; 'loop': 0.07; 'mark.':
0.07; 'ram': 0.07; 'deeper': 0.09; 'expression': 0.09;
'instances': 0.09; 'overhead': 0.09; 'populate': 0.09;
'received:108': 0.09; 'way?': 0.09; 'url:mailman': 0.15; 'that.':
0.15; '"not': 0.16; "'e'": 0.16; '2024': 0.16; 'calculations':
0.16; 'dictionary,': 0.16; 'executed': 0.16; 'expressions': 0.16;
'for.': 0.16; 'generates': 0.16; 'happen.': 0.16; 'indeed': 0.16;
'instance': 0.16; 'invoke': 0.16; 'item,': 0.16; 'jumps': 0.16;
'mean:': 0.16; 'odd': 0.16; 'presumably': 0.16; 'something.':
0.16; 'stick': 0.16; 'tuple': 0.16; 'wrote:': 0.16; 'python':
0.16; "can't": 0.17; 'message-id:@gmail.com': 0.18; 'implement':
0.19; 'it?': 0.19; 'pm,': 0.19; 'to:addr:python-list': 0.20;
'creates': 0.22; 'problem,': 0.22; 'returns': 0.22; "what's":
0.22; 'code': 0.23; 'goal': 0.23; 'list,': 0.24; 'anything': 0.25;
'skip:- 10': 0.25; 'url-ip:188.166.95.178/32': 0.25; 'url-
ip:188.166.95/24': 0.25; 'url:listinfo': 0.25; 'cannot': 0.25;
'programming': 0.25; 'url-ip:188.166/16': 0.25; 'space': 0.26;
'stefan': 0.26; 'creating': 0.27; 'leave': 0.27; 'bit': 0.27;
'function': 0.27; '>>>': 0.28; 'sense': 0.28; 'example,': 0.28;
'seem': 0.31; 'approach': 0.31; 'url-ip:188/8': 0.31; 'think':
0.32; "doesn't": 0.32; 'end.': 0.32; 'point,': 0.32; 'python-
list': 0.32; 'structure': 0.32; 'mark': 0.32; 'unless': 0.32;
'but': 0.32; 'there': 0.33; 'able': 0.34; 'same': 0.34; 'mean':
0.34; 'header:In-Reply-To:1': 0.34; 'received:google.com': 0.34;
'complex': 0.35; 'one.': 0.35; 'from:addr:gmail.com': 0.35;
'files': 0.36; 'people': 0.36; 'necessarily': 0.37; 'main': 0.37;
'subject:from': 0.37; 'really': 0.37; 'using': 0.37; "it's": 0.37;
'others': 0.37; 'file': 0.38; 'way': 0.38; 'could': 0.38; 'use':
0.39; 'on.': 0.39; 'still': 0.40; 'match': 0.40; 'including':
0.60; 'search': 0.61; "there's": 0.61; 'from:': 0.62; 'to:': 0.62;
'ever': 0.63; 'pass': 0.64; 'range': 0.64; "you'd": 0.64; 'our':
0.64; 'key': 0.64; 'down': 0.64; 'full': 0.64; 'finished': 0.64;
'imagine': 0.64; 're:': 0.64; 'your': 0.64; 'came': 0.65;
'similar': 0.65; 'look': 0.65; 'well': 0.65; 'improve': 0.66;
'lead': 0.67; 'generally': 0.67; 'smart': 0.67; 'away': 0.67;
'right': 0.68; 'items': 0.68; 'created.': 0.69; 'generator': 0.69;
'them,': 0.70; 'skip:f 20': 0.75; '(you': 0.76; 'requested': 0.76;
'sent:': 0.78; 'more.': 0.82; 'extra': 0.84; 'brackets': 0.84;
'know?': 0.84; 'levels.': 0.84; 'picks': 0.84; 'statement.': 0.84;
'body,': 0.91; 'folders': 0.91
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20230601; t=1712262842; x=1712867642; darn=python.org;
h=thread-index:content-language:content-transfer-encoding
:mime-version:message-id:date:subject:in-reply-to:references:to:from
:from:to:cc:subject:date:message-id:reply-to;
bh=5Jdnq8PblqNiGC5VtKo2aN/I81kCkcrQXPEsN4CxIiM=;
b=lhqtYcURQP6S8E+Kqn37OfFI++RtUwOCS+nkvlY4XnqE8vs+ic0CwS8p3bUc2S818b
220x2JZ5cyNrVT7aNkqG80G+DThUe7Z9nbNGfi//SLjtW5iyy9kFIkJ6yzUT2aTZvGN7
EPI6bboWQWlodn/x6FaOMAt+/TvuQadvY686HIMC1T/DF9Q/ER2R71oQ4wNRU64epbfA
DbnYoyTkO2zrvkWxTIv9zRRPQTbEdCOFMNJNhUvaJWXbeKaQF3gztMs59buLl9D0p+QB
MCxsb8qxWYzLwBZqHflazeM7ltRAGNTtbFthp6CAMAnVsxqSPbVlmXU3HPX0RC5oVdpx
VCJg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20230601; t=1712262842; x=1712867642;
h=thread-index:content-language:content-transfer-encoding
:mime-version:message-id:date:subject:in-reply-to:references:to:from
:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to;
bh=5Jdnq8PblqNiGC5VtKo2aN/I81kCkcrQXPEsN4CxIiM=;
b=BLItNtR8V+YcG1L0QSFpD9gJhsvbF4R0AfaxYXE7CKeHSfnEms5kzGWRj7jI/Xa3Sc
9FFkXuj2XfuhxcC1DcrDGuWGWcC6574IeROSm3RXwA9Z9Sxp3DA6QXcYiEcxxK+oRoAA
2tU7ZkVC0kje+YfzJAMZ16j34Qy6UoqCNi1KDnNXTOuqACBodr2tif66wD+Z/YV2pG1L
LdOFuJCK3fZNm2Z7+XEYTP+Q7JfeKWiZh3Jde287h9sFOdGFTtX5M/qKpbuRJ36nRWgR
y+dpruGmEtQs8nnih4vpTG+z/SSr1a2stQ+sq3bwfQ8nlknMVs6TURfSSKDO1UV8rao8
mPCw==
X-Forwarded-Encrypted: i=1;
AJvYcCWNAMIX7nGnK0tM3epSH2Jp0UiQxlzc1hSMGJrVZfsazarSH7bUaaoV0dI5fvh3lX+MrrnbxRdlHRU4PtS+0IE2u80qyrvS
X-Gm-Message-State: AOJu0YxLKoMHNQ99cqr7UatV2t6/wuqFpxBOzJUCnzoisPD3nLFz4NGs
7GBQGw0ZUWpRxT8c4yb+mvoGNFjUjLF/n1uB6JHSL2LVYHhKL1kjOl4h/UxR
X-Google-Smtp-Source: AGHT+IGYf1yH7QjtUSTbuTwYlZIyctyqLfK/QTr5LjxZZf5fLtn4d44GJjSA1q5POypPcmnkUCtxEw==
X-Received: by 2002:a05:6214:1cc6:b0:699:1689:70cf with SMTP id
g6-20020a0562141cc600b00699168970cfmr2846022qvd.61.1712262841624;
Thu, 04 Apr 2024 13:34:01 -0700 (PDT)
In-Reply-To: <uumtii$qum4$1@dont-email.me>
X-Mailer: Microsoft Outlook 16.0
Content-Language: en-us
Thread-Index: AQG2as0aVd2VIulEmuXRHp684y8JCQJBvrQcAfnDloECWzchzgF8CqnRsWApyOA=
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <00e701da86cf$6b3e2cc0$41ba8640$@gmail.com>
X-Mailman-Original-References: <chatbot-20240402181409@ram.dialup.fu-berlin.de> <fvstdk-607.ln1@lazy.lzy>
<7d38d9e2-78fb-43af-971f-e0d4afb8b039@tompassin.net>
<mailman.58.1712110350.3468.python-list@python.org>
<uumtii$qum4$1@dont-email.me>
 by: <avi.e.gross@gmail.com> - Thu, 4 Apr 2024 20:33 UTC

That is an excellent point, Mark. Some of the proposed variants to the requested problem, including mine, do indeed find all instances only to return the first. This can use additional time and space but when done, some of the overhead is also gone. What I mean is that a generator you create and invoke once, generally sits around indefinitely in your session unless it leaves your current range or something. It does only a part of the work and must remain suspended and ready to be called again to do more.

If you create a generator inside a function and the function returns, presumably it can be garbage-collected.

But if it is in the main body, I have to wonder what happen.

There seem to be several related scenarios to consider.

- You may want to find, in our example, a first instance. Right afterwards, you want the generator to disassemble anything in use.
- You may want the generator to stick around and later be able to return the next instance. The generator can only really go away when another call has been made after the last available instance and it cannot look for more beyond some end.
- Finally, you can call a generator with the goal of getting all instances such as by asking it to populate a list. In such a case, you may not necessarily want or need to use a generator expression and can use something straightforward and possible cheaper.

What confuses the issue, for me, is that you can make fairly complex calculations in python using various forms of generators that implement a sort of just-in-time approach as generators call other generators which call yet others and so on. Imagine having folders full of files that each contain a data structure such as a dictionary or set and writing functionality that searches for the first match for a key in any of the dictionaries (or sets or whatever) along the way? Now imagine that dictionary items can be a key value pair that can include the value being a deeper dictionary, perhaps down multiple levels.

You could get one generator that generates folder names or opens them and another that generates file names and reads in the data structure such as a dictionary and yet another that searches each dictionary and also any internally embedded dictionaries by calling another instance of the same generator as much as needed.

You can see how this creates and often consumes generators along the way as needed and in a sense does the minimum amount of work needed to find a first instance. But what might it leave open and taking up resources if not finished in a way that dismantles it?

Perhaps worse, imagine doing the search in parallel and as sone as it is found anywhere, ...

-----Original Message-----
From: Python-list <python-list-bounces+avi.e.gross=gmail.com@python.org> On Behalf Of Mark Bourne via Python-list
Sent: Thursday, April 4, 2024 3:04 PM
To: python-list@python.org
Subject: Re: A technique from a chatbot

Thomas Passin wrote:
> On 4/2/2024 1:47 PM, Piergiorgio Sartor via Python-list wrote:
>> On 02/04/2024 19.18, Stefan Ram wrote:
>>> Some people can't believe it when I say that chatbots improve
>>> my programming productivity. So, here's a technique I learned
>>> from a chatbot!
>>> It is a structured "break". "Break" still is a kind of jump,
>>> you know?
>>> So, what's a function to return the first word beginning with
>>> an "e" in a given list, like for example
>>> [ 'delta', 'epsilon', 'zeta', 'eta', 'theta' ]
>>>
>>> ? Well it's
>>> def first_word_beginning_with_e( list_ ):
>>> for word in list_:
>>> if word[ 0 ]== 'e': return word
>>>
>>> . "return" still can be considered a kind of "goto" statement.
>>> It can lead to errors:
>>>
>>> def first_word_beginning_with_e( list_ ):
>>> for word in list_:
>>> if word[ 0 ]== 'e': return word
>>> something_to_be_done_at_the_end_of_this_function()
>>> The call sometimes will not be executed here!
>>> So, "return" is similar to "break" in that regard.
>>> But in Python we can write:
>>> def first_word_beginning_with_e( list_ ):
>>> return next( ( word for word in list_ if word[ 0 ]== 'e' ), None )
>>
>> Doesn't look a smart advice.
>>
>>> . No jumps anymore, yet the loop is aborted on the first hit
>
> It's worse than "not a smart advice". This code constructs an
> unnecessary tuple, then picks out its first element and returns that.

I don't think there's a tuple being created. If you mean:
( word for word in list_ if word[ 0 ]== 'e' )

....that's not creating a tuple. It's a generator expression, which
generates the next value each time it's called for. If you only ever
ask for the first item, it only generates that one.

When I first came across them, I did find it a bit odd that generator
expressions look like the tuple equivalent of list/dictionary
comprehensions.

FWIW, if you actually wanted a tuple from that expression, you'd need to
pass the generator to tuple's constructor:
tuple(word for word in list_ if word[0] == 'e')
(You don't need to include an extra set of brackets when passing a
generator a the only argument to a function).

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

Re: A technique from a chatbot

<mailman.72.1712265569.3468.python-list@python.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=29813&group=comp.lang.python#29813

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: list1@tompassin.net (Thomas Passin)
Newsgroups: comp.lang.python
Subject: Re: A technique from a chatbot
Date: Thu, 4 Apr 2024 17:10:34 -0400
Lines: 61
Message-ID: <mailman.72.1712265569.3468.python-list@python.org>
References: <chatbot-20240402181409@ram.dialup.fu-berlin.de>
<fvstdk-607.ln1@lazy.lzy>
<7d38d9e2-78fb-43af-971f-e0d4afb8b039@tompassin.net>
<mailman.58.1712110350.3468.python-list@python.org>
<uumtii$qum4$1@dont-email.me>
<966cf7ea-d6e9-44f3-bfdd-a2052d9dcc9d@tompassin.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de 2JMCn48sp/Gg+lTMqpkyeAuFrONAoz9tka9m1nrzWt9w==
Cancel-Lock: sha1:46MP8XQaDoweFnm8Q3hIDyJ9oy0= sha256:qpYlaux5r4MKp0xFlchkojeSdVmRTwFowaz8Km8UhvU=
Return-Path: <list1@tompassin.net>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=tompassin.net header.i=@tompassin.net header.b=m+0XWxP4;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.053
X-Spam-Evidence: '*H*': 0.89; '*S*': 0.00; 'argument': 0.04; 'def':
0.04; 'loop': 0.07; 'ram': 0.07; 'that.': 0.15; '"not': 0.16;
"'e'": 0.16; '>>>>': 0.16; 'executed': 0.16; 'expressions': 0.16;
'generates': 0.16; 'item,': 0.16; 'jumps': 0.16; 'mean:': 0.16;
'odd': 0.16; 'received:10.0.0': 0.16; 'received:64.90': 0.16;
'received:64.90.62': 0.16; 'received:64.90.62.162': 0.16;
'received:dreamhost.com': 0.16; 'tuple': 0.16; 'wrote:': 0.16;
'python': 0.16; "can't": 0.17; 'pm,': 0.19; 'to:addr:python-list':
0.20; 'returns': 0.22; "what's": 0.22; 'code': 0.23; 'list,':
0.24; 'programming': 0.25; 'stefan': 0.26; 'creating': 0.27;
'bit': 0.27; 'function': 0.27; '>>>': 0.28; 'header:User-Agent:1':
0.30; 'think': 0.32; "doesn't": 0.32; 'python-list': 0.32;
'received:10.0': 0.32; 'received:mailchannels.net': 0.32;
'received:relay.mailchannels.net': 0.32; 'mark': 0.32; 'but':
0.32; 'header:In-Reply-To:1': 0.34; 'one.': 0.35; 'yes,': 0.35;
'people': 0.36; 'subject:from': 0.37; "it's": 0.37; 'necessary':
0.39; 'wrote': 0.39; 'still': 0.40; 'beginning': 0.40; 'here.':
0.61; "there's": 0.61; 'skip:\xc2 10': 0.62; 'ever': 0.63; 'pass':
0.64; "you'd": 0.64; 'came': 0.65; 'benefit': 0.65; 'similar':
0.65; 'look': 0.65; 'well': 0.65; 'improve': 0.66; 'lead': 0.67;
'smart': 0.67; 'header:Received:6': 0.67; 'received:64': 0.67;
'generator': 0.69; 'machinery': 0.69; 'them,': 0.70;
'performance': 0.71; 'skip:f 20': 0.75; '8bit%:100': 0.76; '(you':
0.76; 'extra': 0.84; 'brackets': 0.84; 'know?': 0.84; 'picks':
0.84; 'readability.': 0.84; 'statement.': 0.84;
'\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0': 0.84
X-Sender-Id: dreamhost|x-authsender|tpassin@tompassin.net
ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1712265034; a=rsa-sha256;
cv=none;
b=1Ss43OB1KYTNHeR2DEH4ldKKB3ul1HFGLZzBbndLs6CJ2Y5YrfOHRZ2eRKrvGT0AIpTIdD
3i6uIAFaNcTow+JFkMiiyMNbUDRUUt9BVffaRYXzPEr3Q5ZLvq2b3P16pft7GH9VEXn/S6
uNYoBusUuZ4evrjhs9yg4wJxsh8J0nyTq7sCoBl3fReAdTTDYi+z0t5PjhEF/bzFnHZ1AA
kWOfd6E7ERTf0KnWfpFLm78UeC2RJpvMT9/kjDHMfUZK2QloqID6/1bE0/5R2/0EI6yGEv
7MQGsK78Cd+0gGcRs6has5sAjq1mlY26VJ8b1WkjjztlJ6S0PrgzVhx/2z8DpA==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed;
d=mailchannels.net; s=arc-2022; t=1712265034;
h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
to:to:cc:mime-version:mime-version:content-type:content-type:
content-transfer-encoding:content-transfer-encoding:
in-reply-to:in-reply-to:references:references:dkim-signature;
bh=ERNcCxXMQpUW9/7ByDpUsMaOlc8CqGpNjVKZSsBRoLU=;
b=eksVnxkLm9wo8ZSoZG0qWeSbiKU4QR+1gZ65eOn3IA1qTIec6E4LBRoSUCDfJCWUXcmoRR
NyRv/loUz4OVA10riTrs3mp+iwqzjBNsyi9Iu/gtmeExEknoJhJYRWu5KGiipxJ35u4/I0
LAOx3p9RDGlBsAB2Adx8tYE+q4U5xuaSnqHGOB+0omH6gCN1BttCQZzIGXNxwjIaV2N+RJ
dmfffiCJpGCxFTSevm6CVawhdjGlF43Z9tkYNaYqQGqBAUtg8zE5+xWCvtsP2pJgG8fM7L
W108yp+RBD8giNym4rNE9M4GDOJT3hiUWsyTzfwyx56Ukftzesm9v1138FmP0A==
ARC-Authentication-Results: i=1; rspamd-687b9dd446-ln9wn;
auth=pass smtp.auth=dreamhost smtp.mailfrom=list1@tompassin.net
X-Sender-Id: dreamhost|x-authsender|tpassin@tompassin.net
X-MC-Relay: Neutral
X-MailChannels-SenderId: dreamhost|x-authsender|tpassin@tompassin.net
X-MailChannels-Auth-Id: dreamhost
X-Abortive-Company: 59e022e84c111285_1712265034429_1564379013
X-MC-Loop-Signature: 1712265034429:3425399994
X-MC-Ingress-Time: 1712265034429
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tompassin.net;
s=dreamhost; t=1712265034;
bh=ERNcCxXMQpUW9/7ByDpUsMaOlc8CqGpNjVKZSsBRoLU=;
h=Date:Subject:To:From:Content-Type:Content-Transfer-Encoding;
b=m+0XWxP46xpqLmaCzbcvOUUilOl5Jjjuw73zYL3jq1K4FYfraHQbAydttdWn08a7e
/JAzZBztbZcwlpr9NAX4ladfrM173X75tLx16DMZN9KpoNwrd3IJRNLnLY5jwkkF++
1luvR+hurTcJQDTFU6g78wPgwEQHk4lVvEDCwERoWp7FEb+No+zm6vX2F8fZNN5ugV
1bcWUhpFqcjPF/0f7PAytl3USCEZJ72/m8PIV19XuTk+ztKCie3PKq13VUdT5UEzJD
62GSZujBeebdtA4zHDMtL4p4Yem9Zr2Uy9LknYp/movUWzg4B76fbzbJsiNrHv3n/f
inaV206bonrcg==
User-Agent: Mozilla Thunderbird
Content-Language: en-US
In-Reply-To: <uumtii$qum4$1@dont-email.me>
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <966cf7ea-d6e9-44f3-bfdd-a2052d9dcc9d@tompassin.net>
X-Mailman-Original-References: <chatbot-20240402181409@ram.dialup.fu-berlin.de>
<fvstdk-607.ln1@lazy.lzy>
<7d38d9e2-78fb-43af-971f-e0d4afb8b039@tompassin.net>
<mailman.58.1712110350.3468.python-list@python.org>
<uumtii$qum4$1@dont-email.me>
 by: Thomas Passin - Thu, 4 Apr 2024 21:10 UTC

On 4/4/2024 3:03 PM, Mark Bourne via Python-list wrote:
> Thomas Passin wrote:
>> On 4/2/2024 1:47 PM, Piergiorgio Sartor via Python-list wrote:
>>> On 02/04/2024 19.18, Stefan Ram wrote:
>>>>    Some people can't believe it when I say that chatbots improve
>>>>    my programming productivity. So, here's a technique I learned
>>>>    from a chatbot!
>>>>    It is a structured "break". "Break" still is a kind of jump,
>>>>    you know?
>>>>    So, what's a function to return the first word beginning with
>>>>    an "e" in a given list, like for example
>>>> [ 'delta', 'epsilon', 'zeta', 'eta', 'theta' ]
>>>>
>>>>    ? Well it's
>>>> def first_word_beginning_with_e( list_ ):
>>>>      for word in list_:
>>>>          if word[ 0 ]== 'e': return word
>>>>
>>>>    . "return" still can be considered a kind of "goto" statement.
>>>>    It can lead to errors:
>>>>
>>>> def first_word_beginning_with_e( list_ ):
>>>>      for word in list_:
>>>>          if word[ 0 ]== 'e': return word
>>>>      something_to_be_done_at_the_end_of_this_function()
>>>>    The call sometimes will not be executed here!
>>>>    So, "return" is similar to "break" in that regard.
>>>>    But in Python we can write:
>>>> def first_word_beginning_with_e( list_ ):
>>>>      return next( ( word for word in list_ if word[ 0 ]== 'e' ), None )
>>>
>>> Doesn't look a smart advice.
>>>
>>>>    . No jumps anymore, yet the loop is aborted on the first hit
>>
>> It's worse than "not a smart advice". This code constructs an
>> unnecessary tuple, then picks out its first element and returns that.
>
> I don't think there's a tuple being created.  If you mean:
>     ( word for word in list_ if word[ 0 ]== 'e' )
>
> ...that's not creating a tuple.  It's a generator expression, which
> generates the next value each time it's called for.  If you only ever
> ask for the first item, it only generates that one.

Yes, I was careless when I wrote that. Still, the tuple machinery has to
be created and that's not necessary here. My point was that you are
asking the Python machinery to do extra work for no benefit in
performance or readability.

> When I first came across them, I did find it a bit odd that generator
> expressions look like the tuple equivalent of list/dictionary
> comprehensions.
>
> FWIW, if you actually wanted a tuple from that expression, you'd need to
> pass the generator to tuple's constructor:
>     tuple(word for word in list_ if word[0] == 'e')
> (You don't need to include an extra set of brackets when passing a
> generator a the only argument to a function).
>

Re: A technique from a chatbot

<benchmark-20240405190253@ram.dialup.fu-berlin.de>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=29814&group=comp.lang.python#29814

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.lang.python
Subject: Re: A technique from a chatbot
Date: 5 Apr 2024 18:29:22 GMT
Organization: Stefan Ram
Lines: 65
Expires: 1 Feb 2025 11:59:58 GMT
Message-ID: <benchmark-20240405190253@ram.dialup.fu-berlin.de>
References: <chatbot-20240402181409@ram.dialup.fu-berlin.de> <fvstdk-607.ln1@lazy.lzy> <7d38d9e2-78fb-43af-971f-e0d4afb8b039@tompassin.net> <mailman.58.1712110350.3468.python-list@python.org> <uumtii$qum4$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de 7doB6KUYNRHEWkoJEP9nnQ4FRkoahC2AZPx0UZiug/xmuM
Cancel-Lock: sha1:gLtysVdziOHXllnhweodJqn/D8Y= sha256:rA+G2fhGLLvIcGULoIoIbquqZPnA9Y1fPz9g0Bj/dSw=
X-Copyright: (C) Copyright 2024 Stefan Ram. All rights reserved.
Distribution through any means other than regular usenet
channels is forbidden. It is forbidden to publish this
article in the Web, to change URIs of this article into links,
and to transfer the body without this notice, but quotations
of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
services to mirror the article in the web. But the article may
be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: en-US
 by: Stefan Ram - Fri, 5 Apr 2024 18:29 UTC

Mark Bourne <nntp.mbourne@spamgourmet.com> wrote or quoted:
>I don't think there's a tuple being created. If you mean:
> ( word for word in list_ if word[ 0 ]== 'e' )
>...that's not creating a tuple. It's a generator expression, which
>generates the next value each time it's called for. If you only ever
>ask for the first item, it only generates that one.

Yes, that's also how I understand it!

In the meantime, I wrote code for a microbenchmark, shown below.

This code, when executed on my computer, shows that the
next+generator approach is a bit faster when compared with
the procedural break approach. But when the order of the two
approaches is being swapped in the loop, then it is shown to
be a bit slower. So let's say, it takes about the same time.

However, I also tested code with an early return (not shown below),
and this was shown to be faster than both code using break and
code using next+generator by a factor of about 1.6, even though
the code with return has the "function call overhead"!

But please be aware that such results depend on the implementation
and version of the Python implementation being used for the benchmark
and also of the details of how exactly the benchmark is written.

import random
import string
import timeit

print( 'The following loop may need a few seconds or minutes, '
'so please bear with me.' )

time_using_break = 0
time_using_next = 0

for repetition in range( 100 ):
for i in range( 100 ): # Yes, this nesting is redundant!

list_ = \
[ ''.join \
( random.choices \
( string.ascii_lowercase, k=random.randint( 1, 30 )))
for i in range( random.randint( 0, 50 ))]

start_time = timeit.default_timer()
for word in list_:
if word[ 0 ]== 'e':
word_using_break = word
break
else:
word_using_break = ''
time_using_break += timeit.default_timer() - start_time

start_time = timeit.default_timer()
word_using_next = \
next( ( word for word in list_ if word[ 0 ]== 'e' ), '' )
time_using_next += timeit.default_timer() - start_time

if word_using_next != word_using_break:
raise Exception( 'word_using_next != word_using_break' )

print( f'{time_using_break = }' )
print( f'{time_using_next = }' )
print( f'{time_using_next / time_using_break = }' )

Re: A technique from a chatbot

<return-20240405193045@ram.dialup.fu-berlin.de>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=29815&group=comp.lang.python#29815

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.lang.python
Subject: Re: A technique from a chatbot
Date: 5 Apr 2024 18:32:22 GMT
Organization: Stefan Ram
Lines: 52
Expires: 1 Feb 2025 11:59:58 GMT
Message-ID: <return-20240405193045@ram.dialup.fu-berlin.de>
References: <chatbot-20240402181409@ram.dialup.fu-berlin.de> <fvstdk-607.ln1@lazy.lzy> <7d38d9e2-78fb-43af-971f-e0d4afb8b039@tompassin.net> <mailman.58.1712110350.3468.python-list@python.org> <uumtii$qum4$1@dont-email.me> <benchmark-20240405190253@ram.dialup.fu-berlin.de>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de hCxXqkIUUPGZctchMLlVRwuGfp+MuIwUKtCfAdlAVtVOHL
Cancel-Lock: sha1:hL1ZmxIWy/Lhjpmj5/q8ogXOLzE= sha256:5Axut+/vncRpu7SCviOk5+MJckSvrL8m8b55+437Ev4=
X-Copyright: (C) Copyright 2024 Stefan Ram. All rights reserved.
Distribution through any means other than regular usenet
channels is forbidden. It is forbidden to publish this
article in the Web, to change URIs of this article into links,
and to transfer the body without this notice, but quotations
of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
services to mirror the article in the web. But the article may
be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: en-US
 by: Stefan Ram - Fri, 5 Apr 2024 18:32 UTC

ram@zedat.fu-berlin.de (Stefan Ram) wrote or quoted:
>However, I also tested code with an early return (not shown below),
>and this was shown to be faster than both code using break and
>code using next+generator by a factor of about 1.6, even though
>the code with return has the "function call overhead"!

See "return" benchmarked against "break" below!

import random
import string
import timeit

print( 'The following loop may need a few seconds or minutes, '
'so please bear with me.' )

def get_word_using_return( list_ ):
for word in list_:
if word[ 0 ]== 'e':
return word
return ''

time_using_break = 0
time_using_return = 0

for repetition in range( 100 ):
for i in range( 100 ): # Yes, this nesting is redundant!

list_ = \
[ ''.join \
( random.choices \
( string.ascii_lowercase, k=random.randint( 1, 30 )))
for i in range( random.randint( 0, 50 ))]

start_time = timeit.default_timer()
for word in list_:
if word[ 0 ]== 'e':
word_using_break = word
break
else:
word_using_break = ''
time_using_break += timeit.default_timer() - start_time

start_time = timeit.default_timer()
word_using_return = get_word_using_return( list_ )
time_using_return += timeit.default_timer() - start_time

if word_using_return != word_using_break:
raise Exception( 'word_using_return != word_using_break' )

print( f'{time_using_break = }' )
print( f'{time_using_return = }' )
print( f'{time_using_return / time_using_break = }' )

Re: A technique from a chatbot

<uupk72$1ikj2$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=29816&group=comp.lang.python#29816

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: nntp.mbourne@spamgourmet.com (Mark Bourne)
Newsgroups: comp.lang.python
Subject: Re: A technique from a chatbot
Date: Fri, 5 Apr 2024 20:42:15 +0100
Organization: A noiseless patient Spider
Lines: 136
Message-ID: <uupk72$1ikj2$1@dont-email.me>
References: <chatbot-20240402181409@ram.dialup.fu-berlin.de>
<fvstdk-607.ln1@lazy.lzy>
<7d38d9e2-78fb-43af-971f-e0d4afb8b039@tompassin.net>
<mailman.58.1712110350.3468.python-list@python.org>
<uumtii$qum4$1@dont-email.me> <00e701da86cf$6b3e2cc0$41ba8640$@gmail.com>
<mailman.71.1712262845.3468.python-list@python.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 05 Apr 2024 19:42:27 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="b5490a3d20a845f179151ce05c90226a";
logging-data="1659490"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/YjkpSYhfYUqppQg2GZWgr"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
SeaMonkey/2.53.18.1
Cancel-Lock: sha1:u08rKHSNEwZQCePq6T9t5dfkkKg=
In-Reply-To: <mailman.71.1712262845.3468.python-list@python.org>
 by: Mark Bourne - Fri, 5 Apr 2024 19:42 UTC

avi.e.gross@gmail.com wrote:
> That is an excellent point, Mark. Some of the proposed variants to the requested problem, including mine, do indeed find all instances only to return the first. This can use additional time and space but when done, some of the overhead is also gone. What I mean is that a generator you create and invoke once, generally sits around indefinitely in your session unless it leaves your current range or something. It does only a part of the work and must remain suspended and ready to be called again to do more.

It goes out of scope at the end of the function. Unless you return it
or store a reference to it elsewhere, it will then be deleted.

Or in this case, since the `first_word_beginning_with_e` function
doesn't even have a local reference to the generator (it is just created
and immediately passed as an argument to `next`), it goes out of scope
once the `next` function returns.

> If you create a generator inside a function and the function returns, presumably it can be garbage-collected.

Exactly. It probably doesn't even need to wait for garbage collection -
once the reference count is zero, it can be destroyed.

> But if it is in the main body, I have to wonder what happen.

If you mean in the top-level module scope outside of any
function/method, then it would remain in memory until the process exits.

> There seem to be several related scenarios to consider.
>
> - You may want to find, in our example, a first instance. Right afterwards, you want the generator to disassemble anything in use.
> - You may want the generator to stick around and later be able to return the next instance. The generator can only really go away when another call has been made after the last available instance and it cannot look for more beyond some end.
> - Finally, you can call a generator with the goal of getting all instances such as by asking it to populate a list. In such a case, you may not necessarily want or need to use a generator expression and can use something straightforward and possible cheaper.

Yes, so you create and assign it at an appropriate scope. In the
example here, it's just passed to `next` and then destroyed. Passing a
generator to the `list` constructor (or the `tuple` constructor in my
"FWIW") would behave similarly - you'd get the final list/tuple back,
but the generator would be destroyed once that call is done. If you
assigned it to a function-local variable, it would exist until the end
of that function.

> What confuses the issue, for me, is that you can make fairly complex calculations in python using various forms of generators that implement a sort of just-in-time approach as generators call other generators which call yet others and so on.

Yes, you can. It can be quite useful when used appropriately.

> Imagine having folders full of files that each contain a data structure such as a dictionary or set and writing functionality that searches for the first match for a key in any of the dictionaries (or sets or whatever) along the way? Now imagine that dictionary items can be a key value pair that can include the value being a deeper dictionary, perhaps down multiple levels.
>
> You could get one generator that generates folder names or opens them and another that generates file names and reads in the data structure such as a dictionary and yet another that searches each dictionary and also any internally embedded dictionaries by calling another instance of the same generator as much as needed.

You probably could do that. Personally, I probably wouldn't use
generators for that, or at least not custom ones - if you're talking
about iterating over directories and files on disk, I'd probably just
use `os.walk` (which probably is a generator) and iterate over that,
opening each file and doing whatever you want with the contents.

> You can see how this creates and often consumes generators along the way as needed and in a sense does the minimum amount of work needed to find a first instance. But what might it leave open and taking up resources if not finished in a way that dismantles it?

You'd need to make sure any files are closed (`with open(...)` helps
with that). If you're opening files within a generator, I'm pretty sure
you can do something like:
```
def iter_files(directory):
for filename in directory:
with open(filename) as f:
yield f
```

Then the file will be closed when the iterator leaves the `with` block
and moved on to the next item (presumably there's some mechanism for the
context manager's `__exit__` to be called if the generator is destroyed
without having iterated over the items - the whole point of using `with`
is that `__exit__` is guaranteed to be called whatever happens).

Other than that, the generators themselves would be destroyed once they
go out of scope. If there are no references to a generator left,
nothing is going to be able to call `next` (nor anything else) on it, so
no need for it to be kept hanging around in memory.

> Perhaps worse, imagine doing the search in parallel and as sone as it is found anywhere, ...
>
>
>
> -----Original Message-----
> From: Python-list <python-list-bounces+avi.e.gross=gmail.com@python.org> On Behalf Of Mark Bourne via Python-list
> Sent: Thursday, April 4, 2024 3:04 PM
> To: python-list@python.org
> Subject: Re: A technique from a chatbot
>
> Thomas Passin wrote:
>> On 4/2/2024 1:47 PM, Piergiorgio Sartor via Python-list wrote:
>>> On 02/04/2024 19.18, Stefan Ram wrote:
>>>> Some people can't believe it when I say that chatbots improve
>>>> my programming productivity. So, here's a technique I learned
>>>> from a chatbot!
>>>> It is a structured "break". "Break" still is a kind of jump,
>>>> you know?
>>>> So, what's a function to return the first word beginning with
>>>> an "e" in a given list, like for example
>>>> [ 'delta', 'epsilon', 'zeta', 'eta', 'theta' ]
>>>>
>>>> ? Well it's
>>>> def first_word_beginning_with_e( list_ ):
>>>> for word in list_:
>>>> if word[ 0 ]== 'e': return word
>>>>
>>>> . "return" still can be considered a kind of "goto" statement.
>>>> It can lead to errors:
>>>>
>>>> def first_word_beginning_with_e( list_ ):
>>>> for word in list_:
>>>> if word[ 0 ]== 'e': return word
>>>> something_to_be_done_at_the_end_of_this_function()
>>>> The call sometimes will not be executed here!
>>>> So, "return" is similar to "break" in that regard.
>>>> But in Python we can write:
>>>> def first_word_beginning_with_e( list_ ):
>>>> return next( ( word for word in list_ if word[ 0 ]== 'e' ), None )
>>>
>>> Doesn't look a smart advice.
>>>
>>>> . No jumps anymore, yet the loop is aborted on the first hit
>>
>> It's worse than "not a smart advice". This code constructs an
>> unnecessary tuple, then picks out its first element and returns that.
>
> I don't think there's a tuple being created. If you mean:
> ( word for word in list_ if word[ 0 ]== 'e' )
>
> ...that's not creating a tuple. It's a generator expression, which
> generates the next value each time it's called for. If you only ever
> ask for the first item, it only generates that one.
>
> When I first came across them, I did find it a bit odd that generator
> expressions look like the tuple equivalent of list/dictionary
> comprehensions.
>
> FWIW, if you actually wanted a tuple from that expression, you'd need to
> pass the generator to tuple's constructor:
> tuple(word for word in list_ if word[0] == 'e')
> (You don't need to include an extra set of brackets when passing a
> generator a the only argument to a function).
>

Re: A technique from a chatbot

<uupl7t$1ird0$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=29817&group=comp.lang.python#29817

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: nntp.mbourne@spamgourmet.com (Mark Bourne)
Newsgroups: comp.lang.python
Subject: Re: A technique from a chatbot
Date: Fri, 5 Apr 2024 20:59:54 +0100
Organization: A noiseless patient Spider
Lines: 87
Message-ID: <uupl7t$1ird0$1@dont-email.me>
References: <chatbot-20240402181409@ram.dialup.fu-berlin.de>
<fvstdk-607.ln1@lazy.lzy>
<7d38d9e2-78fb-43af-971f-e0d4afb8b039@tompassin.net>
<mailman.58.1712110350.3468.python-list@python.org>
<uumtii$qum4$1@dont-email.me>
<benchmark-20240405190253@ram.dialup.fu-berlin.de>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 05 Apr 2024 19:59:58 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="b5490a3d20a845f179151ce05c90226a";
logging-data="1666464"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+pbgZBteB3XV1SoE4icKM3"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
SeaMonkey/2.53.18.1
Cancel-Lock: sha1:XAUpjhPMRzx4tDymSqSqAoy7r6U=
In-Reply-To: <benchmark-20240405190253@ram.dialup.fu-berlin.de>
 by: Mark Bourne - Fri, 5 Apr 2024 19:59 UTC

Stefan Ram wrote:
> Mark Bourne <nntp.mbourne@spamgourmet.com> wrote or quoted:
>> I don't think there's a tuple being created. If you mean:
>> ( word for word in list_ if word[ 0 ]== 'e' )
>> ...that's not creating a tuple. It's a generator expression, which
>> generates the next value each time it's called for. If you only ever
>> ask for the first item, it only generates that one.
>
> Yes, that's also how I understand it!
>
> In the meantime, I wrote code for a microbenchmark, shown below.
>
> This code, when executed on my computer, shows that the
> next+generator approach is a bit faster when compared with
> the procedural break approach. But when the order of the two
> approaches is being swapped in the loop, then it is shown to
> be a bit slower. So let's say, it takes about the same time.

There could be some caching going on, meaning whichever is done second
comes out a bit faster.

> However, I also tested code with an early return (not shown below),
> and this was shown to be faster than both code using break and
> code using next+generator by a factor of about 1.6, even though
> the code with return has the "function call overhead"!

To be honest, that's how I'd probably write it - not because of any
thought that it might be faster, but just that's it's clearer. And if
there's a `do_something_else()` that needs to be called regardless of
the whether a word was found, split it into two functions:
```
def first_word_beginning_with_e(target, wordlist):
for w in wordlist:
if w.startswith(target):
return w
return ''

def find_word_and_do_something_else(target, wordlist):
result = first_word_beginning_with_e(target, wordlist)
do_something_else()
return result
```

> But please be aware that such results depend on the implementation
> and version of the Python implementation being used for the benchmark
> and also of the details of how exactly the benchmark is written.
>
> import random
> import string
> import timeit
>
> print( 'The following loop may need a few seconds or minutes, '
> 'so please bear with me.' )
>
> time_using_break = 0
> time_using_next = 0
>
> for repetition in range( 100 ):
> for i in range( 100 ): # Yes, this nesting is redundant!
>
> list_ = \
> [ ''.join \
> ( random.choices \
> ( string.ascii_lowercase, k=random.randint( 1, 30 )))
> for i in range( random.randint( 0, 50 ))]
>
> start_time = timeit.default_timer()
> for word in list_:
> if word[ 0 ]== 'e':
> word_using_break = word
> break
> else:
> word_using_break = ''
> time_using_break += timeit.default_timer() - start_time
>
> start_time = timeit.default_timer()
> word_using_next = \
> next( ( word for word in list_ if word[ 0 ]== 'e' ), '' )
> time_using_next += timeit.default_timer() - start_time
>
> if word_using_next != word_using_break:
> raise Exception( 'word_using_next != word_using_break' )
>
> print( f'{time_using_break = }' )
> print( f'{time_using_next = }' )
> print( f'{time_using_next / time_using_break = }' )
>

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor