Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

Q: Why was Stonehenge abandoned? A: It wasn't IBM compatible.


comp / comp.lang.python / Re: Flubbed it in the second interation through the string: range error... HOW?

SubjectAuthor
o Re: Flubbed it in the second interation through the string: range error... HOW?Thomas Passin

1
Subject: Re: Flubbed it in the second interation through the string: range error... HOW?
From: Thomas Passin
Newsgroups: comp.lang.python
Date: Wed, 29 May 2024 14:32 UTC
References: 1 2 3
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!fu-berlin.de!uni-berlin.de!not-for-mail
From: list1@tompassin.net (Thomas Passin)
Newsgroups: comp.lang.python
Subject: Re: Flubbed it in the second interation through the string: range
error... HOW?
Date: Wed, 29 May 2024 10:32:39 -0400
Lines: 103
Message-ID: <mailman.64.1716993171.2909.python-list@python.org>
References: <1064052561.5558929.1716987352732.ref@mail.yahoo.com>
<1064052561.5558929.1716987352732@mail.yahoo.com>
<407cdc1c-f579-452f-afca-5af50f14d2cb@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 kgrtwMxavNMtQMugHM9cQAEosg3/0kPbHhhCn8RH0Eog==
Cancel-Lock: sha1:P4creTdpY1tpptS5RUNymkswZqo= sha256:fD6wS/afNYf2z6nV37rwi5K+pZn1BHVW+x6gRCdnRkg=
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=p6BdfZN/;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.002
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'looks': 0.02; 'this:':
0.03; 'def': 0.04; 'debugging': 0.07; 'space,': 0.07; '"""': 0.09;
'angelico': 0.09; 'elif': 0.09; 'everywhere': 0.09; 'meant': 0.09;
'url:mailman': 0.15; 'that.': 0.15; '2024': 0.16; 'cameron': 0.16;
'chrisa': 0.16; 'conversion': 0.16; 'encoding': 0.16;
'everywhere,': 0.16; 'far,': 0.16; 'hardly': 0.16; 'readability':
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; 'simpson': 0.16; 'subject:second':
0.16; 'subject:string': 0.16; 'understand.': 0.16; 'useful.':
0.16; 'wrote:': 0.16; 'problem': 0.16; 'probably': 0.17; 'to:addr
:python-list': 0.20; 'all,': 0.20; 'way.': 0.22; 'to:name:python-
list@python.org': 0.24; '(and': 0.25; 'url-ip:188.166.95.178/32':
0.25; 'url-ip:188.166.95/24': 0.25; 'stuff': 0.25; 'url:listinfo':
0.25; 'url-ip:188.166/16': 0.25; 'brought': 0.26; 'wednesday,':
0.26; 'else': 0.27; 'chris': 0.28; 'output': 0.28; 'header:User-
Agent:1': 0.30; 'am,': 0.31; 'url-ip:188/8': 0.31; 'think': 0.32;
'attach': 0.32; 'good.': 0.32; 'personally': 0.32; 'python-list':
0.32; 'received:10.0': 0.32; 'received:mailchannels.net': 0.32;
'received:relay.mailchannels.net': 0.32; 'specified': 0.32;
"wouldn't": 0.32; 'but': 0.32; "i'll": 0.33; 'there': 0.33;
'header:In-Reply-To:1': 0.34; 'failed': 0.35; 'track': 0.35;
'count': 0.36; 'change': 0.36; "skip:' 10": 0.37; "it's": 0.37;
'could': 0.38; 'means': 0.38; 'thanks': 0.38; 'enough': 0.39;
'use': 0.39; 'block': 0.39; 'wed,': 0.39; 'advantage': 0.40;
'true.': 0.40; 'want': 0.40; 'should': 0.40; 'included': 0.61;
'detail': 0.61; 'format': 0.62; 'email': 0.63; 'leading': 0.63;
'pass': 0.64; 'among': 0.65; 'touch': 0.65; 'years': 0.65; 'back':
0.67; 'header:Received:6': 0.67; 'received:64': 0.67; '2015,':
0.69; 'subject:through': 0.69; 'little': 0.73; '(you': 0.76;
'"when': 0.84; "'this": 0.84; 'characters': 0.84; 'converted':
0.84; 'good,': 0.84; 'subject: \n ': 0.84; 'you:': 0.84; 'retain':
0.91
X-Sender-Id: dreamhost|x-authsender|tpassin@tompassin.net
ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1716993166; a=rsa-sha256;
cv=none;
b=FWbpYs2dQwpCu3YFUOML+QoGIYxWbixVH0HdWeBTkQ2U3yB5Hn5rUebMEgwcRZX8X8+8Fc
Qr4KEVAOikDFCnQOqSfUc7EcaD3D54u39aW2/z1tFxG+sbkxcXZHtGEiarSDbSduKpSAUQ
YHvTctb/mmObmnpdmj0h4h0uXkv8HkhjFeCH8UZztyNpp3f9sIQbmzdAUOUJOIRSo63Udr
OgOasKhZRPwHPr0PfYlrv4PCgOzvvzIEpgFxz5IG+XR8OQcUAH4i0K1zLMRMLNgT+abqCq
LTIRiOhoRkYUN5X971Pzpk+cVkCIDUbICdYKx8DAjLb/BvUb78/ZD8ogtfeZIg==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed;
d=mailchannels.net; s=arc-2022; t=1716993166;
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=6sFMbMoU5bScchkSy4GzI8Lg2/V0/NCe1+ZctDQsWWg=;
b=07Ee+7TXLvf3Psl1K9vNB/UZZXtGR7W1gRUmVUwrKNKLlDOGQlLvDABoZkfywpr5kRy3n9
hQ/zk0ujD5wgc2vsjb19CD3u8r7VunCJywp0DPPhYfElbAvSasdbE43KqNmBvL4Xr74lNJ
zOljpT4SyYAW74u8BTxFfkxE+D6i9v1G8RHGaRL6Cm+WoLkKbAM7KjGhP6riwWgVzzb2uS
zEaqK9qx+6CRvmBSXmnbvHPDLiNUaT1MiXNNEcqjqHZGYOIEPoBq9BtF1nedFm+w+DWWg9
5dC4AgZMjNlCxJqck6/Em8d3jms91GaQxsoYYVD8CalhsxtHK00uNRJJyBEZWg==
ARC-Authentication-Results: i=1; rspamd-68bbddc7f5-8g64c;
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-Reign-Robust: 3d4c26e651a498c1_1716993167375_4247556689
X-MC-Loop-Signature: 1716993167375:3733028786
X-MC-Ingress-Time: 1716993167375
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tompassin.net;
s=dreamhost; t=1716993163;
bh=6sFMbMoU5bScchkSy4GzI8Lg2/V0/NCe1+ZctDQsWWg=;
h=Date:Subject:To:From:Content-Type:Content-Transfer-Encoding;
b=p6BdfZN/+Qn6X1ySNWZhT1PeSRvfn+Tz/dwxVDAfnhiRV0UoCkmkfoXO5s4tLKs99
3atJ9MIexCeQJbBRH/2zPs9Vi7taWdeqYJEGvFjzFblZgz1uBZwcZZG7HuHi2GezFD
/8STPZbW2reVVkayZB4PtsClEndc12Zift9QV3scfZigkKnLlv2mdMSXZjkIIaHyHW
mLkVfRhzrsJunvDU5N257vmRcHHXiRjj4Lv6ySW52Mrd+OWEPrtrIuQ1IFVG6/bCaS
PTqv2vgun3UgJuyoDKy+tLrniOQLLMeXohREFcVcAA7WDRh32QcULIINZj09RmDUzz
EeUg7M3nHuGZA==
User-Agent: Mozilla Thunderbird
Content-Language: en-US
In-Reply-To: <1064052561.5558929.1716987352732@mail.yahoo.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: <407cdc1c-f579-452f-afca-5af50f14d2cb@tompassin.net>
X-Mailman-Original-References: <1064052561.5558929.1716987352732.ref@mail.yahoo.com>
<1064052561.5558929.1716987352732@mail.yahoo.com>
View all headers

On 5/29/2024 8:55 AM, Kevin M. Wilson wrote:
> Please recall, I said the format for the email failed to retain the
> proper indents.
> I'll attach a picture of the code!
> Purpose; to uppercase every other letter in a string.
>
> Thanks all, KMW

Simpler is good, and readability is good. For a simple conversion that
has a little touch of generality:

s1 = 'this is a test'
def convert(i, ch):
return ch.upper() if i % 2 else ch

result = ''.join([convert(i, ch) for i, ch in enumerate(s1)])
print(result) # tHiS Is a tEsT

However, this has a weakness: what to do about spaces. Should they be
counted among the characters to be uppercased? or should they not be
included in the count of the alternation? If you want to uppercase
every other letter that is not a space, things become a little more
complicated. And then do you want this to apply to all whitespace or
only spaces?

If you want to skip changing spaces, then you need to track the state of
converted characters in some way. It would probably be easier (and more
readable) to use a "for x in t:" construction:

def convert(convert_this, ch):
"""Convert character ch if convert_this is True.
Don't convert spaces.
"""
if convert_this:
if ch == ' ':
return (convert_this, ch)
elif convert_this:
return (False, ch.upper())
return (True, ch)

convert_next = False
result = ''
for ch in s1:
convert_next, ch = convert(convert_next, ch)
result += ch
print(result) # tHiS Is A TeSt

There could be even more complications if you allow non-ascii characters
but you were asking about processing character by character so I won't
get into that.

(You haven't specified the problem in enough detail to answer questions
like those).

> ***************************************************
> "When you pass through the waters, I will be with you: and when you pass
> through the rivers, they will not sweep over you. When you walk through
> the fire, you will not be burned: the flames will not set you ablaze."
> *Isaiah 43:2
> *
>
>
> On Wednesday, May 29, 2024 at 06:19:56 AM MDT, Thomas Passin via
> Python-list <python-list@python.org> wrote:
>
>
> On 5/29/2024 3:14 AM, Chris Angelico via Python-list wrote:
> > On Wed, 29 May 2024 at 16:03, Cameron Simpson via Python-list
> > <python-list@python.org <mailto:python-list@python.org>> wrote:
> >> By which Thomas means stuff like this:
> >>
> >>      print(f'if block {name[index]} and index {index}')
> >>
> >> Notice the leading "f'". Personally I wouldn't even go that far, just:
> >>
> >>      print('if block', name[index], 'and index', index)
> >>
> >> But there are plenty of places where f-strings are very useful.
> >
> > I wouldn't replace str.format() everywhere, nor would I replace
> > percent encoding everywhere - but in this case, I think Thomas is
> > correct. Not because it's 2024 (f-strings were brought in back in
> > 2015, so they're hardly chronologically special),
>
> I only meant that they have been around for 9 years and are usually more
> readable, so just change over already.  I had some inertia over them
> myself (imagine sticking with % formatting!) so I understand.
>
>
> > but because most of
> > this looks like debugging output that can take advantage of this
> > feature:
> >
> > print(f"if block {name[index]=} {index=}")
> >
> > ChrisA
>
> --
> https://mail.python.org/mailman/listinfo/python-list
> <https://mail.python.org/mailman/listinfo/python-list>

1

rocksolid light 0.9.8
clearnet tor