Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

"Pok pok pok, P'kok!" -- Superchicken


devel / comp.lang.python / Evaluation of variable as f-string

SubjectAuthor
* Evaluation of variable as f-stringJohannes Bauer
+* Re: Evaluation of variable as f-stringStefan Ram
|+- Re: Evaluation of variable as f-stringStefan Ram
|`* Re: Evaluation of variable as f-stringJohannes Bauer
| `* Re: Evaluation of variable as f-stringStefan Ram
|  `- RE: Evaluation of variable as f-string<avi.e.gross
+* Re: Evaluation of variable as f-stringChris Angelico
|`* Re: Evaluation of variable as f-stringJohannes Bauer
| `- Re: Evaluation of variable as f-stringPeter J. Holzer
+- Re: Evaluation of variable as f-stringAntoon Pardon
+* Re: Evaluation of variable as f-stringThomas Passin
|`* Re: Evaluation of variable as f-stringJohannes Bauer
| `* Re: Evaluation of variable as f-stringThomas Passin
|  `* Re: Evaluation of variable as f-stringJohannes Bauer
|   +- Re: Evaluation of variable as f-stringChris Angelico
|   +* Re: Evaluation of variable as f-stringStefan Ram
|   |+- Re: Evaluation of variable as f-stringChris Angelico
|   |+- To gateway or not to gateway a specific person (was: Evaluation ofPeter J. Holzer
|   |`- Re: Evaluation of variable as f-stringStefan Ram
|   `* Re: Evaluation of variable as f-stringThomas Passin
|    `* Re: Evaluation of variable as f-stringJohannes Bauer
|     `- Re: Evaluation of variable as f-stringPeter J. Holzer
+- Re: Evaluation of variable as f-stringRob Cliffe
+- Re: Evaluation of variable as f-stringRob Cliffe
+* Re: Evaluation of variable as f-stringChris Angelico
|`* Re: Evaluation of variable as f-stringJohannes Bauer
| +* Re: Evaluation of variable as f-stringChristian Gollwitzer
| |+* Re: Evaluation of variable as f-stringThomas Passin
| ||`* Re: Evaluation of variable as f-stringChristian Gollwitzer
| || `- Re: Evaluation of variable as f-stringThomas Passin
| |+- Re: Evaluation of variable as f-stringPeter J. Holzer
| |`- Re: Evaluation of variable as f-stringJohannes Bauer
| `- Re: Evaluation of variable as f-stringPeter J. Holzer
+- Re: Evaluation of variable as f-stringRob Cliffe
+* Re: Evaluation of variable as f-stringChris Angelico
|`* Re: Evaluation of variable as f-stringJohannes Bauer
| `* Re: Evaluation of variable as f-stringChris Angelico
|  `- Re: Evaluation of variable as f-stringJohannes Bauer
+- Re: Evaluation of variable as f-stringThomas Passin
+- Re: Evaluation of variable as f-stringRob Cliffe
+* Re: Evaluation of variable as f-stringChris Angelico
|`- Re: Evaluation of variable as f-stringJohannes Bauer
+* Re: Evaluation of variable as f-stringRob Cliffe
|`* Re: Evaluation of variable as f-stringStefan Ram
| `- Re: Evaluation of variable as f-stringChris Angelico
`- Re: Evaluation of variable as f-stringChris Angelico

Pages:12
Evaluation of variable as f-string

<tqmcbf$vdl$1@gioia.aioe.org>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!aioe.org!8bTATXmOJ3o6Ad9y0OWTvQ.user.46.165.242.75.POSTED!not-for-mail
From: dfnsonfs...@gmx.de (Johannes Bauer)
Newsgroups: comp.lang.python
Subject: Evaluation of variable as f-string
Date: Mon, 23 Jan 2023 17:24:15 +0100
Organization: Aioe.org NNTP Server
Message-ID: <tqmcbf$vdl$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="32181"; posting-host="8bTATXmOJ3o6Ad9y0OWTvQ.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.4.2
X-Notice: Filtered by postfilter v. 0.9.2
Content-Language: en-US
 by: Johannes Bauer - Mon, 23 Jan 2023 16:24 UTC

Hi there,

is there an easy way to evaluate a string stored in a variable as if it
were an f-string at runtime?

I.e., what I want is to be able to do this:

x = { "y": "z" }
print(f"-> {x['y']}")

This prints "-> z", as expected. But consider:

x = { "y": "z" }
s = "-> {x['y']}"
print(s.format(x = x))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: "'y'"

Even though

s = "-> {x}"
print(s.format(x = x))

Prints the expected "-> {'y': 'z'}".

This is supposedly for security reasons. However, when trying to emulate
this behavior that I wanted (and know the security implications of), my
solutions will tend to be less secure. Here is what I have been thinking
about:

1. Somehow wrap "s" into an f-string, then eval. E.g.:

eval("f'" + s + "'")

This is a pain in the ass because you have to know what kind of
quotation signs are used inside the expression. In the given case, this
wouldn't work (but 'f"' prefix and '"' suffix would).

2. Parse the expression (regex?), then eval() the individual arguments,
then run through format(). Pain in the ass to get the exact same
behavior as f-strings. Probably by regex alone not even guaranteed to be
parsable (especially corner cases with escaped '{' signs or ':' or '{'
included inside the expression as a literal).

3. Somehow compile the bytecode representing an actual f-string
expression, then execute it. Sounds like a royal pain in the butt, have
not tried it.

All solutions are extremely undesirable and come with heavy drawbacks.
Is there any standard solution (Py3.10+) that does what I would?
Anything I'm missing?

Thanks,
Johannes

Re: Evaluation of variable as f-string

<s-20230123174218@ram.dialup.fu-berlin.de>

  copy mid

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

  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: Evaluation of variable as f-string
Date: 23 Jan 2023 16:43:33 GMT
Organization: Stefan Ram
Lines: 10
Expires: 1 Jan 2024 11:59:58 GMT
Message-ID: <s-20230123174218@ram.dialup.fu-berlin.de>
References: <tqmcbf$vdl$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de NS85EE4R0IVcUBgbj1f9UwCMPpTALzMzXWq7FXq1CTpqwg
X-Copyright: (C) Copyright 2023 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
Accept-Language: de-DE, en-US, it, fr-FR
 by: Stefan Ram - Mon, 23 Jan 2023 16:43 UTC

Johannes Bauer <dfnsonfsduifb@gmx.de> writes:
>x = { "y": "z" }
>s = "-> {x['y']}"
>print(s.format(x = x))

x = { "y": "z" }
def s( x ): return '-> ' + x[ 'y' ]
print( s( x = x ))

Re: Evaluation of variable as f-string

<s-20230123175140@ram.dialup.fu-berlin.de>

  copy mid

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

  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: Evaluation of variable as f-string
Date: 23 Jan 2023 16:52:11 GMT
Organization: Stefan Ram
Lines: 24
Expires: 1 Jan 2024 11:59:58 GMT
Message-ID: <s-20230123175140@ram.dialup.fu-berlin.de>
References: <tqmcbf$vdl$1@gioia.aioe.org> <s-20230123174218@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 aMLPSEhGkfECozl8VTkYgAen4iGN3wWxU9Q943uXah+CIh
X-Copyright: (C) Copyright 2023 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
Accept-Language: de-DE, en-US, it, fr-FR
 by: Stefan Ram - Mon, 23 Jan 2023 16:52 UTC

ram@zedat.fu-berlin.de (Stefan Ram) writes:
>Johannes Bauer <dfnsonfsduifb@gmx.de> writes:
>>x = { "y": "z" }
>>s = "-> {x['y']}"
>>print(s.format(x = x))
>x = { "y": "z" }
>def s( x ): return '-> ' + x[ 'y' ]
>print( s( x = x ))

One can use an f-string just as well in the function.

import dis

x = { "y": "z" }

def s( x ): return '-> ' + x[ 'y' ]
dis.dis( s )
print( s( x = x ), end='\n\n' )

def s( x ): return f"-> {x['y']}"
dis.dis( s )
print( s( x = x ))

Re: Evaluation of variable as f-string

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: ros...@gmail.com (Chris Angelico)
Newsgroups: comp.lang.python
Subject: Re: Evaluation of variable as f-string
Date: Tue, 24 Jan 2023 05:02:56 +1100
Lines: 28
Message-ID: <mailman.1575.1674496990.20444.python-list@python.org>
References: <tqmcbf$vdl$1@gioia.aioe.org>
<CAPTjJmpKEBbYFxowgm4cDfDn0bmieo9sHLK_sNZh8WcqQ=Wqhw@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-Trace: news.uni-berlin.de jwTdxL8Hn8ijjWpMy0KBkAX9FzttpAPHPXC1yDd5ZH8A==
Return-Path: <rosuav@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=SxKC67kL;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.031
X-Spam-Evidence: '*H*': 0.94; '*S*': 0.00; 'variable': 0.05; '2023':
0.07; 'directly.': 0.07; 'string': 0.07; 'namespace': 0.09;
'runtime?': 0.09; 'chrisa': 0.16; 'context.': 0.16;
'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16;
'instead?': 0.16; 'received:209.85.218': 0.16; 'subject:string':
0.16; 'subject:variable': 0.16; 'well-defined': 0.16; 'wrote:':
0.16; 'tue,': 0.19; 'to:addr:python-list': 0.20; '(and': 0.25;
'behavior': 0.26; 'thinking': 0.28; 'code,': 0.31; 'message-
id:@mail.gmail.com': 0.32; 'but': 0.32; 'there': 0.33; 'header:In-
Reply-To:1': 0.34; 'received:google.com': 0.34; 'trying': 0.35;
'from:addr:gmail.com': 0.35; 'really': 0.37; 'using': 0.37;
'received:209.85': 0.37; 'way': 0.38; 'means': 0.38;
'received:209': 0.39; 'use': 0.39; 'want': 0.40; 'method': 0.61;
'gain': 0.62; 'here': 0.62; 'security': 0.64; "you'd": 0.64;
'full': 0.64; 'less': 0.65; 'implications': 0.69; 'solutions':
0.70; 'easy': 0.74; 'need,': 0.76; 'treat': 0.76; 'supposedly':
0.84; 'secure.': 0.91; 'tend': 0.91
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112;
h=to:subject:message-id:date:from:in-reply-to:references:mime-version
:from:to:cc:subject:date:message-id:reply-to;
bh=L6uBzMEYBoFFamnkCZA2PQp3Via5OdmeZOXufdl0eFk=;
b=SxKC67kLCs88gPu4jG2R9Xb+gwbafdXO3iouthHCv3SlVu0ifp5nd2qlMRNroWGGNh
kQDHrlBDOhdPVS47GHnShyHzBws5EUqJ0FJBSUHXbBkyDreWYNlYuBNtg7nLsV3ncPol
6t2x/iKGXit9k3f0gVxWtE41/Ssfp9nmcuH99mqukk/Yo4xzt8KB3nkv0uA9oQ7U96ND
VNJUJIOzOIL/Y/R7e4GgDp4bAlKqMg1Gcrs8u/u8VKJBuAkiGgcD3pUofD203wvLoPZJ
61067ppcdQTwr/AEI3mfmlO4CeigdzVTDM+ktZKLDv8QJeF+qyFhqHdr/4eGbg3zxBoX
+sJw==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20210112;
h=to:subject:message-id:date:from:in-reply-to:references:mime-version
:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to;
bh=L6uBzMEYBoFFamnkCZA2PQp3Via5OdmeZOXufdl0eFk=;
b=bZyhXTViC5RJKIeKO+OCuOgrTQX1pFj1yi4/iSuB1aCEV8H57UNBd0J1bZPgjF91/2
QaWKhH+4t/XwgHq8SmATgU2SBwYwaHUr0jwoK2bZXtkvRchCwAtF2GsYScQZRh8Bzky5
Kv4gRB1vBIsKXHK7CI+FuiN9pdYiUofr2orihp7ZhrXZ3sVuKEJgBXZF3XRj/82Jt/vH
cKaN19L8G08RiJMSfatImVFYs22xOgs2/uZG45iLOibv4Itf4x6tRd0e1+wC1AVnQc5I
KD8xljIIXc4YZ2QrTqjcWG857QwydR2OVx7KQsUnn2DtidkTdseHccO4MxVFHJ88fJqR
68KQ==
X-Gm-Message-State: AFqh2kq42uBBiznmaRGm4qNn+4nWqh8fVfoG3guX2+Iv3WQx7dE0H7JI
Y9Q3SJOh4Px3kXCKp1IKiUo9zYEDy/FD0hGboaIWi3rJ
X-Google-Smtp-Source: AMrXdXtOzY5uobXLjDwCJWxXx/76PhiUfZGfeLYJxMicww+upWa4skwgD3gkoqXt3oC2K/7HUVuyg1LLSEG19hzY040=
X-Received: by 2002:a17:906:9be9:b0:877:597b:5cb0 with SMTP id
de41-20020a1709069be900b00877597b5cb0mr2515266ejc.492.1674496987933; Mon, 23
Jan 2023 10:03:07 -0800 (PST)
In-Reply-To: <tqmcbf$vdl$1@gioia.aioe.org>
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: <CAPTjJmpKEBbYFxowgm4cDfDn0bmieo9sHLK_sNZh8WcqQ=Wqhw@mail.gmail.com>
X-Mailman-Original-References: <tqmcbf$vdl$1@gioia.aioe.org>
 by: Chris Angelico - Mon, 23 Jan 2023 18:02 UTC

On Tue, 24 Jan 2023 at 04:56, Johannes Bauer <dfnsonfsduifb@gmx.de> wrote:
>
> Hi there,
>
> is there an easy way to evaluate a string stored in a variable as if it
> were an f-string at runtime?
>
> ...
>
> This is supposedly for security reasons. However, when trying to emulate
> this behavior that I wanted (and know the security implications of), my
> solutions will tend to be less secure. Here is what I have been thinking
> about:

If you really want the full power of an f-string, then you're asking
for the full power of eval(), and that means all the security
implications thereof, not to mention the difficulties of namespacing.
Have you considered using the vanilla format() method instead?

But if you really REALLY know what you're doing, just use eval()
directly. I don't really see what you'd gain from an f-string. At very
least, work with a well-defined namespace and eval whatever you need
in that context.

Maybe, rather than asking for a way to treat a string as code, ask for
what you ACTUALLY need, and we can help?

ChrisA

Re: Evaluation of variable as f-string

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: antoon.p...@vub.be (Antoon Pardon)
Newsgroups: comp.lang.python
Subject: Re: Evaluation of variable as f-string
Date: Wed, 25 Jan 2023 19:26:47 +0100
Lines: 33
Message-ID: <mailman.1610.1674671214.20444.python-list@python.org>
References: <tqmcbf$vdl$1@gioia.aioe.org>
<d4a54200-1847-4fd2-c7e1-aa1189f63b78@vub.be>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de J05G9MEu1PVtM5dXBmpjAg4mEuIbObCrKBip1AU5lbQA==
Return-Path: <Antoon.Pardon@vub.be>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="1024-bit key; unprotected key"
header.d=vub.be header.i=@vub.be header.b=A9RDiErl; dkim-adsp=pass;
dkim-atps=neutral
X-Spam-Status: OK 0.000
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'this:': 0.03; 'traceback':
0.04; '(most': 0.05; 'last):': 0.05; 'variable': 0.05; 'string':
0.07; 'prints': 0.09; 'runtime?': 0.09; 'expected.': 0.16;
'from:addr:antoon.pardon': 0.16; 'from:addr:vub.be': 0.16;
'from:name:antoon pardon': 0.16; 'i.e.,': 0.16; 'message-
id:@vub.be': 0.16; 'schreef': 0.16; 'subject:string': 0.16;
'subject:variable': 0.16; 'probably': 0.17; 'to:addr:python-list':
0.20; 'header:User-Agent:1': 0.30; '"",': 0.32; "wouldn't": 0.32;
'but': 0.32; 'there': 0.33; 'able': 0.34; 'header:In-Reply-To:1':
0.34; 'following': 0.35; 'missing': 0.37; 'though': 0.37; 'file':
0.38; 'way': 0.38; 'something': 0.40; 'want': 0.40; 'easy': 0.74;
'received:eurprd01.prod.exchangelabs.com': 0.84
ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none;
b=lCw1jwjcM3eI7SDAXC31Ohu/9Pvm0xb783+g23irCAEQ4DERIWFLAJzjakHK77dsf/J+5j26TkMTsdacY5xGZSlfl2LNzZqE4qtP8K1gV8psVJhN/jZZEBMbrzYbH5djyuGtgm69/RHMTzQKtSfr/QaseTsN6jsrpTnMhbVF7+zyRahI5Ku1QjydsONaKHFj6Z67uH0Z22ZrbqgeblprFjZAJig4ikf2wpOeLwdw+cQb+b5AGrXihvUcGTos9xGK1E+A9YD81xIiRvbtW0QQ8LpaQSqTDtNu5+6Pan8HyyFidweXb0MUWHqy84YHzHfN+NeZoJ8RadfkBAdrWcnlpQ==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com;
s=arcselector9901;
h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1;
bh=GwyNFupFV+l40W63bjde1tFbq/b2pHT7kda0IvrvfRo=;
b=bCidsw4F1RwxnKQPlaOqbbs4b+Y3puijwrYo4bBxwfzN6rEOCw0xl3l43OeAgOycdMo2G+v9Q9MaGssbTRwo6cqWVZ+quCbl21LDpGGcafQYfeP9fwbQF5+E5nkDt58BqMA3erkxFyL2SvoP3yCuRaT5wUMydTd67ZMQBjDNyySVzsNI+psSPRYKoCAwT0rxynFxK0QMEUlBYZ+fnocxnItwwlxkgvNZpXteB6/eRW+t0L/mmnFL/vISF24EuGxGFosSYyp7IlLFtAhza95oCBj3FFx9nStz73k8gQtoFswt1C7Fit8N76cGp2YiXluiNrbOIJdhnS0FyZXAdfBA1w==
ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass
smtp.mailfrom=vub.be; dmarc=pass action=none header.from=vub.be; dkim=pass
header.d=vub.be; arc=none
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vub.be; s=selector1;
h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck;
bh=GwyNFupFV+l40W63bjde1tFbq/b2pHT7kda0IvrvfRo=;
b=A9RDiErlk3f+O0/i4J9oNGILFpY8fo1tcK1RPO8hoSdEwKHZv0xwzIc/C/ZUrhVG24UHhvaR6u5d2PxdFAqud0nqTEwiDfHhuWQOfvET5F/bQaUxWbmUFolvdisUGns8FKtY1tEJ5tSTVzjH4Apob/XnXivmBWgrJpDB3PQrynM=
Authentication-Results: dkim=none (message not signed)
header.d=none;dmarc=none action=none header.from=vub.be;
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.4.2
Content-Language: nl-BE
In-Reply-To: <tqmcbf$vdl$1@gioia.aioe.org>
X-ClientProxiedBy: AS4P191CA0022.EURP191.PROD.OUTLOOK.COM
(2603:10a6:20b:5d9::13) To AM0PR01MB4147.eurprd01.prod.exchangelabs.com
(2603:10a6:208:6a::12)
X-MS-PublicTrafficType: Email
X-MS-TrafficTypeDiagnostic: AM0PR01MB4147:EE_|AS4PR01MB9469:EE_
X-MS-Office365-Filtering-Correlation-Id: 66a8c380-4cb4-4f84-d77c-08daff01b888
X-MS-Exchange-SenderADCheck: 1
X-MS-Exchange-AntiSpam-Relay: 0
X-Microsoft-Antispam: BCL:0;
X-Microsoft-Antispam-Message-Info: 5XDAnqKe6Ux0PA4OrX1PiT9NdECsQkcPlnUoZMeG5ajnGIdzjZjM2OyqcVFtF0jtNau+NxW+gPjbl8eLnYBmAfjyQYpxwr7lZ9KCf9PLgmp8jLdKLtCj07gBG53/cN5cNd8D6X1i+txAmMqQIajSm27h2lzl6v4wpNc9T3R4X9tKfkF3kaHWOVzgH46uy99fYt+sV49fLe7X6O3gwJV9jNQDDrI7sz3QeTrwK1qc5EG/9++rboaSFVjNS5sEJSVn670ShHuVKxHAGH/JvKqhBA311gWXYx6zI6A8peEVSowvEqaDoknQhrNxQZL824vnuckOqxG3hqG0gSPjRqJMKAxBKD1+HIWQT93C4XT0IE3YWoO6WL13IjPCqJ66cbKc1E/lFt5PlRUAu7xkjDO2I40TwrNxo682fU9fWJWoDSfVNicye3iYiFVFDVi4+LfFgAa3A/jhYllGo54ubnEVPuVGfjDujTSHAwixojsde3OUPCG/YVg3iTlQrRCzAZ1Zr1Ed8WqaXHisc3qMSiQc4FHZKBmuvld3gXKTUkzxBdiikYmcPSMs00cyWaKrHPpaL/NS3XHDX9icXIAkVFhyKFqjqpr2KKSEDiYuFmO8fxzJ23B3oLXj9MzejweojNqEkeLZx2hewSU+ssBCP/ViTeCu+V8kuFlMrUh8aaUvn2S6avu0Jkm7TREcw+8ClQOu6TNcoWC7XKcPdY6wUPtwUg==
X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:;
IPV:NLI; SFV:NSPM; H:AM0PR01MB4147.eurprd01.prod.exchangelabs.com; PTR:;
CAT:NONE;
SFS:(13230025)(4636009)(366004)(376002)(136003)(39860400002)(396003)(346002)(451199018)(38100700002)(2616005)(6916009)(478600001)(66556008)(8676002)(66476007)(186003)(41320700001)(31686004)(6486002)(786003)(31696002)(66946007)(36756003)(2906002)(86362001)(41300700001)(4744005)(5660300002)(6506007)(316002)(6512007)(8936002)(44832011)(43740500002);
DIR:OUT; SFP:1101;
X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1
X-MS-Exchange-AntiSpam-MessageData-0: NzRUkCxB0cm8S4v9XMPXea/62IfxcxXz6Tj4dfNc0utYH
+17gDuXsp+0ENFo/z4mqabGH4XZOtX6crzW2/1YWP/IPS
QyXEjUx3JzP0IxITVVpQCCGWOKtSxL9uDe07l3U3nLpuX
QAiFvbnwxgcDFOkPEouDMxnfdqDW/QU/zELP9nyrN2XTj
nx5MPU8+f1X/lIujuq+bxSg4eOXGVEOJeObf46BnTNE+K
TYA6VXtYacBVOvftfdaqcTBW8EtRGtWiXNFMidQN9KtRE
l8YMCfLocR7o7iZf0DE175E4D/phx5DBULg5UWCd0gAeY
KayrcpA041biL0Zb2qozpIruso5ZoCDpthrW7FOHliqSG
fKmspwP20FNNnS2c4X53L/Km1ounzR3CKxOygx7F1fnAs
PkMO/DvKpRI2cLCY4WicWubqB6z34pse/1tpX8cm6EpVG
EIYKTUVlLq+SoLbtzxNDpwdeAyuYPesOUl/iv7IMHI/X8
+NmF4l4piNQnMGcrsKI3b8ehN9Ie90R7TSCtg7FYUSMwi
7g9MsONh5R76zt2WfBZZMyrmm324PwmrKmXlaikeZkQH+
TVWVUvjhV7/P48COykh70GAKcvQghx2HPQoTgaRLd5qpj
ozK43asR5ZJ8tTYAyYve6ZXuDz5NiLj2Asl9ppBAsj6aR
06mum2EHuR6KsEVRK9qhtv7QdfsIYUyyOzy4omPy8jf4c
phur/NN5D+iojaoLrCdaI8R9lDlrIT8FqhBypsJ37ZbFZ
8q8FvfUpUQxmVLN9no5FQLGPjTnKPzOOU5Hj4n2m2aaTK
IzQc9sBWa/OXZh50kjKDxJX4O9qtMXuYPQNUiaysYBWqT
vuTCvRiCS3ZGB3vZeYfD2uwG0eb/YXVCaDZ/+BpooVDWK
uEbS2+cS3aiIk8s1Wy7ogbc1eLI8wcaO7OLRLcPLP1RRr
zWGw71JsMNzz5649AGN+gExyz39+mBjBfT5syvN/OH26F
DbkwL0TcwIQ3LCdF/RwS5hvFRHFbrfYZlmkDs5M514JWi
C+kKuclp/iYEUrH3mSREMVnfDHIiV3gxEaGtCYkgX6LON
yAOb53TSbyHwB4eetaqp116fb5y7QhAElLor1lSqrMKrf
ftrXkfcbb2p6/EQSO9ryOngiinJ9DTG0bhkVAq1JUFKXZ
ZylN0c9WJwT4d5gPxqLlvSUViTzA6Dh0Box/Dx7OufpAi
G+46afc5LGBIWwbdpEVuJyyXsI4tV4Y4s3UCIKIDngvyg
Yq9thpZesqGuvxpQLl+eWdP6RBFzyUSxicRaQ4YLka8Dj
j2BivPlX7biHwcvXM+s38wnToNDezhO5fkmgSZjB2TNgm
x1gS2e3us3wgjQ+4H+RDiWGQ30RivO+rRSp+RxnUuW9j0
193trhwStWPfItUneY0c8YaKbiLIfLDOsFoYpOMjKkUif
TMqhtWFskMWooBHGIsJKEwKN+XZclUz+FaK3uhpmNA1qP
DUKFPtBOclRFg9S1qCL1sw1gdVuoaT3cpf9JO/Qewm+o2
hNRotlmpyr8Qw0THryzmQEinb2ZPZrV4NJ4sDS1fjkgqQ
wnyjDkk9xGsNcYcMAB3H1doBtj3853Qhmdnisbe2u0ZVy
cmGMam5YAX7gFqWTx+cW4V80fxzpzbX2+yKo7+UG4QjF1
gZgO7Ph3YRykh7tyejw5Q==
X-OriginatorOrg: vub.be
X-MS-Exchange-CrossTenant-Network-Message-Id: 66a8c380-4cb4-4f84-d77c-08daff01b888
X-MS-Exchange-CrossTenant-AuthSource: AM0PR01MB4147.eurprd01.prod.exchangelabs.com
X-MS-Exchange-CrossTenant-AuthAs: Internal
X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Jan 2023 18:26:48.4091 (UTC)
X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted
X-MS-Exchange-CrossTenant-Id: 695b7ca8-2da8-4545-a2da-42d03784e585
X-MS-Exchange-CrossTenant-MailboxType: HOSTED
X-MS-Exchange-CrossTenant-UserPrincipalName: 6LfpIPauR4ZsyD0+dzo4VZVaLWRLXIRu2rim7B4A5V1VKSiCo/HirVcykDeif5Efm9p0RjRuEa4Ju6zJFErW/A==
X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS4PR01MB9469
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: <d4a54200-1847-4fd2-c7e1-aa1189f63b78@vub.be>
X-Mailman-Original-References: <tqmcbf$vdl$1@gioia.aioe.org>
 by: Antoon Pardon - Wed, 25 Jan 2023 18:26 UTC

Op 23/01/2023 om 17:24 schreef Johannes Bauer:
> Hi there,
>
> is there an easy way to evaluate a string stored in a variable as if
> it were an f-string at runtime?
>
> I.e., what I want is to be able to do this:
>
> x = { "y": "z" }
> print(f"-> {x['y']}")
>
> This prints "-> z", as expected. But consider:
>
> x = { "y": "z" }
> s = "-> {x['y']}"
> print(s.format(x = x))
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> KeyError: "'y'"
>
> Even though
>
> s = "-> {x}"
> print(s.format(x = x))
>
> Prints the expected "-> {'y': 'z'}".
>
I am probably missing something but is there a reason why the following
wouldn't do what you want:

x = { "y": "z" }
s = "-> {target}"
print(s.format(target = x['y']))

Re: Evaluation of variable as f-string

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!news.szaf.org!fu-berlin.de!uni-berlin.de!not-for-mail
From: lis...@tompassin.net (Thomas Passin)
Newsgroups: comp.lang.python
Subject: Re: Evaluation of variable as f-string
Date: Wed, 25 Jan 2023 14:38:38 -0500
Lines: 45
Message-ID: <mailman.1613.1674675531.20444.python-list@python.org>
References: <tqmcbf$vdl$1@gioia.aioe.org>
<d4a54200-1847-4fd2-c7e1-aa1189f63b78@vub.be>
<5bb2546d-8c16-262d-9336-5b41cbf992b5@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 vR3DA6XZF0H1bQaPnRMdzwbiymkBuTt9HmSr0v/CUHKQ==
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=TWE2IDd7;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.000
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'this:': 0.03; 'def': 0.04;
'traceback': 0.04; '(most': 0.05; 'last):': 0.05; 'variable':
0.05; '"python': 0.07; 'string': 0.07; 'prints': 0.09; 'runtime?':
0.09; 'antoon': 0.16; 'expected.': 0.16; 'i.e.,': 0.16; 'pardon':
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; 'schreef': 0.16; 'subject:string':
0.16; 'subject:variable': 0.16; 'wrote:': 0.16; 'probably': 0.17;
'pm,': 0.19; 'to:addr:python-list': 0.20; 'header:User-Agent:1':
0.30; '"",': 0.32; 'received:10.0': 0.32;
'received:mailchannels.net': 0.32;
'received:relay.mailchannels.net': 0.32; "wouldn't": 0.32; 'but':
0.32; 'there': 0.33; 'able': 0.34; 'header:In-Reply-To:1': 0.34;
'following': 0.35; 'missing': 0.37; 'though': 0.37; 'file': 0.38;
'way': 0.38; 'something': 0.40; 'want': 0.40; 'search': 0.61;
'header:Received:6': 0.67; 'skip:e 20': 0.67; 'received:64': 0.67;
'easy': 0.74
X-Sender-Id: dreamhost|x-authsender|tpassin@tompassin.net
ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1674675520; a=rsa-sha256;
cv=none;
b=z9YLongDHzsWzck0J7ukEhhf72ohSTwgblP8h7B3DoPS0R71adZJbqrQ61tP2H78ZwAEjf
aODU2/DqpPftxNpBoNf2xaJDgtgkkzY7mko2q5q8i72GGT3W8UQC4UhcoqXCPYy6DpoWnb
20+YOKWVxs/f5W7wLPqbnFd7lAaM1RoZcNuSS+ay6nVtOel1OJnAUUUdWsbm3Ai8Ijoq9B
TMjrjfEjfDo6XLajKM8pxRWoph43OqjLuZd/ITtRKukiJZe+oMa6jC39XSTLpMsP9aw2bA
N/wfQZ8uKop495NdO9qm702Xmj6TH3H+RvzaS5l+A1eSIJuLGmyCIR3MFaxH6g==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed;
d=mailchannels.net; s=arc-2022; t=1674675520;
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=sa3wmBoSZoQjYVB3a6tZg1HORw+lh1184GB8OOOWGZ8=;
b=Qth0qld+PLNDSeX0Bd6f5rSRWGmpr2g5z2dtVRTMqlWynLSu16dN/PQI+L5KNlpW5+WUot
lYZOBaCpy8NA4AYXcLcsxy+yrXQfTCgFWMqP9z7xtvQsCCXtm8xVHjZudPBtUd+9vBHIcr
yR9qpWxtj3V+5qLtwanRh3OfXHoHj7HfvOAO0Cq69rJRaD7H38FJK6fTXIXmLrI27EoYqR
UsMHh4zpKsfVO91/TX6XLm9TxgePVmCPkbdPGvJM1PxP+YYWMFldGP6gXj/HE5HzxO5O+M
IdgSvZRKGxo0FWnYPSYRPgI268W9SDnu0e9Lu2QVDEhz5H7eoZiHO48zTLQPUA==
ARC-Authentication-Results: i=1; rspamd-6989874cc5-wpnhj;
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-Celery-Celery: 43ff71a34954c415_1674675520570_2288957715
X-MC-Loop-Signature: 1674675520570:1031338105
X-MC-Ingress-Time: 1674675520570
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tompassin.net;
s=dreamhost; t=1674675520;
bh=sa3wmBoSZoQjYVB3a6tZg1HORw+lh1184GB8OOOWGZ8=;
h=Date:Subject:To:From:Content-Type:Content-Transfer-Encoding;
b=TWE2IDd7PtnGLNFpn0B3bDodteZL57duf0ihvjLeEdmbXjvmghEPoIDAWEPfObZHN
3bJ4OBuHx7T5Sq60xGTi/EoGFlF0IxEoZfc+GtaKGG+NKeIM7xqdFp31AZuSj20fH/
/+RKqbBykvOF/cL/SFF7lbDQ6e1GzpF8OvNeBn/jwilyTM1KwGlxADVTEB2SIER3bc
QqLG0NDXob8psWy6r0T1p/9RUW7BVEk0qTh/PNcblP6VztUylYgwMqo5cDxY5y85ic
2vhUSaC9B/aNH2COOTtuC8YBT3V8HtmnA8Ty93aZrGq0UDgUKYKiBpoVs2xDwKruPi
/Uq5EcJvMd1Fw==
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.6.1
Content-Language: en-US
In-Reply-To: <d4a54200-1847-4fd2-c7e1-aa1189f63b78@vub.be>
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: <5bb2546d-8c16-262d-9336-5b41cbf992b5@tompassin.net>
X-Mailman-Original-References: <tqmcbf$vdl$1@gioia.aioe.org>
<d4a54200-1847-4fd2-c7e1-aa1189f63b78@vub.be>
 by: Thomas Passin - Wed, 25 Jan 2023 19:38 UTC

On 1/25/2023 1:26 PM, Antoon Pardon wrote:
> Op 23/01/2023 om 17:24 schreef Johannes Bauer:
>> Hi there,
>>
>> is there an easy way to evaluate a string stored in a variable as if
>> it were an f-string at runtime?
>>
>> I.e., what I want is to be able to do this:
>>
>> x = { "y": "z" }
>> print(f"-> {x['y']}")
>>
>> This prints "-> z", as expected. But consider:
>>
>> x = { "y": "z" }
>> s = "-> {x['y']}"
>> print(s.format(x = x))
>> Traceback (most recent call last):
>>   File "<stdin>", line 1, in <module>
>> KeyError: "'y'"
>>
>> Even though
>>
>> s = "-> {x}"
>> print(s.format(x = x))
>>
>> Prints the expected "-> {'y': 'z'}".
>>
> I am probably missing something but is there a reason why the following
> wouldn't do what you want:
>
> x = { "y": "z" }
> s = "-> {target}"
> print(s.format(target = x['y']))

Stack overflow to the rescue:

Search phrase: "python evaluate string as fstring"

https://stackoverflow.com/questions/47339121/how-do-i-convert-a-string-into-an-f-string

def effify(non_f_str: str):
return eval(f'f"""{non_f_str}"""')

print(effify(s)) # prints as expected: "-> z"

Re: Evaluation of variable as f-string

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: rob.cli...@btinternet.com (Rob Cliffe)
Newsgroups: comp.lang.python
Subject: Re: Evaluation of variable as f-string
Date: Thu, 26 Jan 2023 00:58:12 +0000
Lines: 33
Message-ID: <mailman.1666.1674841672.20444.python-list@python.org>
References: <tqmcbf$vdl$1@gioia.aioe.org>
<d4a54200-1847-4fd2-c7e1-aa1189f63b78@vub.be>
<5bb2546d-8c16-262d-9336-5b41cbf992b5@tompassin.net>
<5221ab5f-90fd-b7c9-4b84-e7a99e8320ff@btinternet.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de Dl94D+KLr2Y9jA04Fco9KQ6arpHXnz/HNs28H4MbR8SQ==
Return-Path: <rob.cliffe@btinternet.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=btinternet.com header.i=@btinternet.com header.b=hjhr92Cw;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.037
X-Spam-Evidence: '*H*': 0.93; '*S*': 0.00; 'this:': 0.03; 'def': 0.04;
'"python': 0.07; 'string': 0.07; 'parse': 0.09; 'prints': 0.09;
'quotes.': 0.16; 'subject:string': 0.16; 'subject:variable': 0.16;
'wrote:': 0.16; "can't": 0.17; 'round': 0.19; 'to:addr:python-
list': 0.20; 'else': 0.27; 'function': 0.27; 'thinking': 0.28;
'this?': 0.29; 'header:User-Agent:1': 0.30; 'module': 0.31;
'unless': 0.32; 'received:192.168.1': 0.32; 'there': 0.33; 'header
:In-Reply-To:1': 0.34; 'received:192.168': 0.37; 'though': 0.37;
'could': 0.38; 'put': 0.38; 'single': 0.39; 'double': 0.40;
'received:213': 0.40; 'wishes': 0.40; 'something': 0.40; 'best':
0.61; 'search': 0.61; 'skip:\xc2 10': 0.62; 'skip:e 20': 0.67;
'8bit%:100': 0.76; 'loc': 0.84; 'locals': 0.84; 'quotes': 0.84;
'quotes,': 0.84; 'rob': 0.84; 'stupid': 0.84
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=btinternet.com;
s=btmx201904; t=1674694705;
bh=ior2/9d2Gc39WE8uSutHeWjjiOH0ZAIV1/Hqzdv6tLo=;
h=Message-ID:Date:MIME-Version:Subject:To:References:From:In-Reply-To;
b=hjhr92CwueTgLcrieG5YURy3hafoXRZDN8K24f71x19ulEYFQ28y0SXTeB1j5B3aa0StEHEJVxaFfpE1OTtb7mx9aJZvCrm4O58uT67ilyxNkmw5iR2hJDuzouUBbAyNvYj7ZrcgXnumZP0yZw6Tt0Oj0QeHcxc26sKU5B/vbKa86UH90Ny/iS7Et0ucIjUPMMRTopg+6x2Uj9AV0G3GNFgR08869jxRCrHLwZCIqhWCrxDn84Q3LfmNILZEzkkQCaFphPTiwXC1WMJv1ml8ED5UI5ysNTwEiP8HmUOOlHXTs/yuzhQ6SqlVsTy6Vn9l6HFYuqM4tQtPyIZQWbVDBg==
Authentication-Results: btinternet.com;
auth=pass (PLAIN) smtp.auth=rob.cliffe@btinternet.com;
bimi=skipped
X-SNCR-Rigid: 613A8DE84C2F0F05
X-Originating-IP: [109.149.69.127]
X-OWM-Source-IP: 109.149.69.127 (GB)
X-OWM-Env-Sender: rob.cliffe@btinternet.com
X-VadeSecure-score: verdict=clean score=0/300, class=clean
X-RazorGate-Vade: gggruggvucftvghtrhhoucdtuddrgedvhedruddvfedgvdeiucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuueftkffvkffujffvgffngfevqffopdfqfgfvnecuuegrihhlohhuthemuceftddunecunecujfgurhepkfffgggfuffvfhfhjggtgfesthekredttdefjeenucfhrhhomheptfhosgcuvehlihhffhgvuceorhhosgdrtghlihhffhgvsegsthhinhhtvghrnhgvthdrtghomheqnecuggftrfgrthhtvghrnhepueeuvdduteffkeffvedtkeevgeeiudeuueeffeehieduteeuteefvdeugefhueefnecuffhomhgrihhnpehsthgrtghkohhvvghrfhhlohifrdgtohhmnecukfhppedutdelrddugeelrdeiledruddvjeenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhephhgvlhhopegludelvddrudeikedruddrvddtgegnpdhinhgvthepuddtledrudegledrieelrdduvdejpdhmrghilhhfrhhomheprhhosgdrtghlihhffhgvsegsthhinhhtvghrnhgvthdrtghomhdpnhgspghrtghpthhtohepuddprhgtphhtthhopehphihthhhonhdqlhhishhtsehphihthhhonhdrohhrgh
X-RazorGate-Vade-Verdict: clean 0
X-RazorGate-Vade-Classification: clean
X-SNCR-hdrdom: btinternet.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.6.1
Content-Language: en-GB
In-Reply-To: <5bb2546d-8c16-262d-9336-5b41cbf992b5@tompassin.net>
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: <5221ab5f-90fd-b7c9-4b84-e7a99e8320ff@btinternet.com>
X-Mailman-Original-References: <tqmcbf$vdl$1@gioia.aioe.org>
<d4a54200-1847-4fd2-c7e1-aa1189f63b78@vub.be>
<5bb2546d-8c16-262d-9336-5b41cbf992b5@tompassin.net>
 by: Rob Cliffe - Thu, 26 Jan 2023 00:58 UTC

On 25/01/2023 19:38, Thomas Passin wrote:
>
> Stack overflow to the rescue:
>
> Search phrase:  "python evaluate string as fstring"
>
> https://stackoverflow.com/questions/47339121/how-do-i-convert-a-string-into-an-f-string
>
>
> def effify(non_f_str: str):
>     return eval(f'f"""{non_f_str}"""')
>
> print(effify(s))  # prints as expected: "-> z"
Duh!  Am I the only one who feels stupid not thinking of this?
Although of course it won't work if the string already contains triple
double quotes.
I believe this could be got round with some ingenuity (having the effify
function parse the string and replace genuine (not embedded in
single-quotes) triple double quotes with triple single quotes, though
there are some complications).
And the effify function can't be put in its own module unless it can be
passed the globals and/or locals dictionaries as needed for eval to
use.  Something like this:

def effify(non_f_str, glob=None, loc=None):
    return eval(f'f"""{non_f_str}"""',
        glob if glob is not None else globals(),
        loc if loc is not None else locals())

Best wishes
Rob Cliffe

Re: Evaluation of variable as f-string

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: rob.cli...@btinternet.com (Rob Cliffe)
Newsgroups: comp.lang.python
Subject: Re: Evaluation of variable as f-string
Date: Wed, 25 Jan 2023 20:04:56 +0000
Lines: 40
Message-ID: <mailman.1668.1674844237.20444.python-list@python.org>
References: <tqmcbf$vdl$1@gioia.aioe.org>
<CAPTjJmpKEBbYFxowgm4cDfDn0bmieo9sHLK_sNZh8WcqQ=Wqhw@mail.gmail.com>
<ebb89997-603f-6283-742a-121128e55ae8@btinternet.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de 8rKWT2G2EP6bTSxIQ9nDiwo2yH0UALrKdARR4fso0X5g==
Return-Path: <rob.cliffe@btinternet.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=btinternet.com header.i=@btinternet.com header.b=qYHEO9XO;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.031
X-Spam-Evidence: '*H*': 0.94; '*S*': 0.00; 'variable': 0.05; '2023':
0.07; 'directly.': 0.07; 'string': 0.07; 'angelico': 0.09;
'namespace': 0.09; 'runtime?': 0.09; 'chrisa': 0.16; 'context.':
0.16; 'enough,': 0.16; 'evaluating': 0.16; 'for.': 0.16;
'inspired': 0.16; 'instead?': 0.16; 'relatively': 0.16; 'runtime':
0.16; 'subject:string': 0.16; 'subject:variable': 0.16; 'well-
defined': 0.16; '\xf0\x9f\x98\x8e': 0.16; 'wrote:': 0.16; 'tue,':
0.19; 'to:addr:python-list': 0.20; 'list,': 0.24; '(and': 0.25;
'behavior': 0.26; 'function': 0.27; 'chris': 0.28; 'thinking':
0.28; 'header:User-Agent:1': 0.30; 'code,': 0.31;
'received:192.168.1': 0.32; 'but': 0.32; 'there': 0.33; 'someone':
0.34; 'header:In-Reply-To:1': 0.34; 'trying': 0.35; 'encourage':
0.35; 'people': 0.36; 'really': 0.37; 'using': 0.37;
'received:192.168': 0.37; 'though': 0.37; 'way': 0.38; 'means':
0.38; 'use': 0.39; 'still': 0.40; 'received:213': 0.40; 'wishes':
0.40; 'want': 0.40; 'best': 0.61; 'method': 0.61; 'gain': 0.62;
'reasonable': 0.62; 'here': 0.62; 'security': 0.64; "you'd": 0.64;
'full': 0.64; 'less': 0.65; 'maintenance': 0.67; 'implications':
0.69; 'solutions': 0.70; 'easy': 0.74; 'need,': 0.76; 'treat':
0.76; '"no,': 0.84; 'rare': 0.84; 'rob': 0.84; 'supposedly': 0.84;
'secure.': 0.91; 'tend': 0.91
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=btinternet.com;
s=btmx201904; t=1674677109;
bh=5W5KG8ntlNKsSMKbFGPeCEFD082OVs6L9OL16xGd4aQ=;
h=Message-ID:Date:MIME-Version:Subject:To:References:From:In-Reply-To;
b=qYHEO9XOr+NsZPc5rhZdKorLOdMQwgrgUhPpe7x9A9/ISsqIMldtqkGIHAItLzZauktQ8d0DQgJJLo2LVdDYTw8W8HLNOaZLOdqQo3BE+/OXUmyQD+jjNLR9aAwWmcFCqco/qiomLMzBeMo0VkkSYUXQBdkHEnDIYoR6++Qa+t8oryfDcVta0jGPtoX9tiRAHicacGKb9VpEmm11sOyKMUZE79b2YDBHmqpGPLrOvNcRmLVDqb1QzD8PDIN5svaZ4eQTNYBXkM4Kr4QxgvbK0Wg5SNWwOCJ/51k3GqE9AziM7w9ajWHijDOQNxvfIqgdwmkKURGDZSNKiD7CvzKmNg==
Authentication-Results: btinternet.com;
auth=pass (PLAIN) smtp.auth=rob.cliffe@btinternet.com;
bimi=skipped
X-SNCR-Rigid: 613A91244C29F2D7
X-Originating-IP: [109.149.69.127]
X-OWM-Source-IP: 109.149.69.127 (GB)
X-OWM-Env-Sender: rob.cliffe@btinternet.com
X-VadeSecure-score: verdict=clean score=0/300, class=clean
X-RazorGate-Vade: gggruggvucftvghtrhhoucdtuddrgedvhedruddvvddgudefgecutefuodetggdotefrodftvfcurfhrohhfihhlvgemuceutffkvffkuffjvffgnffgvefqofdpqfgfvfenuceurghilhhouhhtmecufedtudenucenucfjughrpefkffggfgfuvfhfhfgjtgfgsehtkeertddtfeejnecuhfhrohhmpeftohgsucevlhhifhhfvgcuoehrohgsrdgtlhhifhhfvgessghtihhnthgvrhhnvghtrdgtohhmqeenucggtffrrghtthgvrhhnpedvteduhfetgfevgeeiffelledvuefgieekveevhedutefgveeijeeftdetvedtgeenucfkphepuddtledrudegledrieelrdduvdejnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehhvghloheplgduledvrdduieekrddurddvtdegngdpihhnvghtpedutdelrddugeelrdeiledruddvjedpmhgrihhlfhhrohhmpehrohgsrdgtlhhifhhfvgessghtihhnthgvrhhnvghtrdgtohhmpdhnsggprhgtphhtthhopedupdhrtghpthhtohepphihthhhohhnqdhlihhsthesphihthhhohhnrdhorhhg
X-RazorGate-Vade-Verdict: clean 0
X-RazorGate-Vade-Classification: clean
X-SNCR-hdrdom: btinternet.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.6.1
Content-Language: en-GB
In-Reply-To: <CAPTjJmpKEBbYFxowgm4cDfDn0bmieo9sHLK_sNZh8WcqQ=Wqhw@mail.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: <ebb89997-603f-6283-742a-121128e55ae8@btinternet.com>
X-Mailman-Original-References: <tqmcbf$vdl$1@gioia.aioe.org>
<CAPTjJmpKEBbYFxowgm4cDfDn0bmieo9sHLK_sNZh8WcqQ=Wqhw@mail.gmail.com>
 by: Rob Cliffe - Wed, 25 Jan 2023 20:04 UTC

On 23/01/2023 18:02, Chris Angelico wrote:
> On Tue, 24 Jan 2023 at 04:56, Johannes Bauer <dfnsonfsduifb@gmx.de> wrote:
>> Hi there,
>>
>> is there an easy way to evaluate a string stored in a variable as if it
>> were an f-string at runtime?
>>
>> ...
>>
>> This is supposedly for security reasons. However, when trying to emulate
>> this behavior that I wanted (and know the security implications of), my
>> solutions will tend to be less secure. Here is what I have been thinking
>> about:
> If you really want the full power of an f-string, then you're asking
> for the full power of eval(), and that means all the security
> implications thereof, not to mention the difficulties of namespacing.
> Have you considered using the vanilla format() method instead?
>
> But if you really REALLY know what you're doing, just use eval()
> directly. I don't really see what you'd gain from an f-string. At very
> least, work with a well-defined namespace and eval whatever you need
> in that context.
>
> Maybe, rather than asking for a way to treat a string as code, ask for
> what you ACTUALLY need, and we can help?
>
> ChrisA
Fair enough, Chris, but still ISTM that it is reasonable to ask (perhaps
for a different use-case) whether there is a way of evaluating a string
at runtime as if it were an f-string.  We encourage people to ask
questions on this list, even though the answer will not always be what
they're hoping for.
I appreciate that the answer may be "No, because it would be a lot of
work - and increase the maintenance burden - to support a relatively
rare requirement".
Perhaps someone will be inspired to write a function to do it. 😎
Best wishes
Rob Cliffe

Re: Evaluation of variable as f-string

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: ros...@gmail.com (Chris Angelico)
Newsgroups: comp.lang.python
Subject: Re: Evaluation of variable as f-string
Date: Sat, 28 Jan 2023 06:18:16 +1100
Lines: 61
Message-ID: <mailman.1669.1674849042.20444.python-list@python.org>
References: <tqmcbf$vdl$1@gioia.aioe.org>
<CAPTjJmpKEBbYFxowgm4cDfDn0bmieo9sHLK_sNZh8WcqQ=Wqhw@mail.gmail.com>
<ebb89997-603f-6283-742a-121128e55ae8@btinternet.com>
<CAPTjJmoxero1FVGW8BNB619tS1XA_LOqn=Lnoq3ogw1JPh52Jg@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Trace: news.uni-berlin.de JugiLNmkhOkNZBQkHsNd2Q5quOFNFGGuw5jepPnOswSw==
Return-Path: <rosuav@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=n7xeDkBQ;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.024
X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; 'variable': 0.05; '2023':
0.07; 'string': 0.07; 'angelico': 0.09; 'construct': 0.09;
'general,': 0.09; 'parse': 0.09; 'what,': 0.09; 'yes.': 0.09;
'problem.': 0.15; 'that.': 0.15; '*what': 0.16; 'chrisa': 0.16;
'enough,': 0.16; 'evaluating': 0.16; 'for.': 0.16;
'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16;
'inspired': 0.16; 'received:209.85.208.171': 0.16; 'received:mail-
lj1-f171.google.com': 0.16; 'relatively': 0.16; 'runtime': 0.16;
'seen.': 0.16; 'slow': 0.16; 'subject:string': 0.16;
'subject:variable': 0.16; 'variables,': 0.16; '\xf0\x9f\x98\x8e':
0.16; 'wrote:': 0.16; "can't": 0.17; 'to:addr:python-list': 0.20;
'sat,': 0.22; 'list,': 0.24; 'do,': 0.26; "isn't": 0.27;
'function': 0.27; 'done': 0.28; 'chris': 0.28; 'suggest': 0.28;
'asked': 0.29; 'code,': 0.31; "doesn't": 0.32; 'python-list':
0.32; 'message-id:@mail.gmail.com': 0.32; 'but': 0.32; 'there':
0.33; 'someone': 0.34; 'same': 0.34; 'requires': 0.34; 'header:In-
Reply-To:1': 0.34; 'received:google.com': 0.34; 'trying': 0.35;
'encourage': 0.35; 'from:addr:gmail.com': 0.35; 'people': 0.36;
'using': 0.37; "it's": 0.37; 'received:209.85': 0.37; 'hard':
0.37; 'though': 0.37; 'way': 0.38; 'received:209': 0.39; 'quite':
0.39; "we've": 0.39; 'received:209.85.208': 0.39; 'use': 0.39;
'still': 0.40; 'exact': 0.40; 'something': 0.40; 'tell': 0.60;
'best': 0.61; 'reasonable': 0.62; 'come': 0.62; 'pass': 0.64;
'our': 0.64; 'full': 0.64; 'thus': 0.64; 'your': 0.64; 'back':
0.67; 'maintenance': 0.67; 'right': 0.68; '"you': 0.69;
'instead,': 0.70; 'quote': 0.74; 'solutions.': 0.75; 'need,':
0.76; 'see,': 0.76; 'treat': 0.76; 'led': 0.81; '"no,': 0.84;
'*you': 0.84; 'horrible': 0.84; 'rare': 0.84; 'rob': 0.84; 'say,':
0.84; '"how': 0.91; '*we': 0.91; '*no': 0.93
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112;
h=content-transfer-encoding:to:subject:message-id:date:from
:in-reply-to:references:mime-version:from:to:cc:subject:date
:message-id:reply-to;
bh=AtkSlumbbsM9AQIEMrcBTRmVFb5jYdRDu1DSEZETFmk=;
b=n7xeDkBQQi8ugQ1y4qJ5jpilgfc/jjM4tNgkHi/VKMEjvc707/wOxfNG9eqN988ab6
vYSpsFp64AE2TslI+Gvh9f2vUQjc9Q5FFP3ovP+2TnDhpjTA8R2ZAXqHfqdkmSs5b0gD
m+9lbOkgubLC1dTcjJeNZO/q2JVOmbVmrA9fANvQFy1hxIJRkuekPnv/S/pLHMvgLk0c
FOrMkHWrG9unM2HrBeCO5ynxph/WNT9Y81BWcM57I4C9BrCQ+8CbH/x0rrtdF2r3cnte
dwCwdbuLjfOJtfPfaF0ejTlfBqfco/u+0bAhJmq1YGtdHHCjuyMucqy4Gis4e8zzONtN
XWFw==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20210112;
h=content-transfer-encoding:to:subject:message-id:date:from
:in-reply-to:references:mime-version:x-gm-message-state:from:to:cc
:subject:date:message-id:reply-to;
bh=AtkSlumbbsM9AQIEMrcBTRmVFb5jYdRDu1DSEZETFmk=;
b=w4FIWsilOGNaKFlx2zIvLDISdFuAOdth59vTjOmYIdK6Szx8EYyU+RzGT9AQFjBWVw
H1OTx2brz9kHtbT4XcYcWSHD9KsiIXQjZV3wtxgADzUzqznK9x0M6kPuud0ZQ4EkCpLR
8OV8Z1lMP8AAgm6AR76eMNzmUA30Js55Wrwjh+TXTNYq4C6Qt5cS0MxG7YAvTDtgbedC
xo6tavNltECWqTG318XH3v7rVePK7gDoWw80HiMVgnLMy+zibrPnEvUi/Hj7qx/zB34f
ULXCygg9nCWEjB8lMhpUSDEi9kvXGMzszYV4MswL1nUTPv2jb2va3bOE0quamYddlFur
fjQg==
X-Gm-Message-State: AFqh2kqeW8ooVP5WgaF43g1PJXjia9i1NvQLVQoy5qrVKu+jEsfBAquF
mTgIVD5sP0+lt8X0vHGWMSq3vsnFEIbaXeFztVUsVEBZUDQ=
X-Google-Smtp-Source: AK7set851D55PWQuvGS6SfnLLuzy6mP2Bs5V5+P9D2b+Lt1UDi6iY6+6wn9I1I1JRsqhCXlpWFIQ74ayvnDJLCWmdZI=
X-Received: by 2002:a17:906:dfef:b0:87e:fd9e:5c63 with SMTP id
lc15-20020a170906dfef00b0087efd9e5c63mr322599ejc.106.1674847107958; Fri, 27
Jan 2023 11:18:27 -0800 (PST)
In-Reply-To: <ebb89997-603f-6283-742a-121128e55ae8@btinternet.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: <CAPTjJmoxero1FVGW8BNB619tS1XA_LOqn=Lnoq3ogw1JPh52Jg@mail.gmail.com>
X-Mailman-Original-References: <tqmcbf$vdl$1@gioia.aioe.org>
<CAPTjJmpKEBbYFxowgm4cDfDn0bmieo9sHLK_sNZh8WcqQ=Wqhw@mail.gmail.com>
<ebb89997-603f-6283-742a-121128e55ae8@btinternet.com>
 by: Chris Angelico - Fri, 27 Jan 2023 19:18 UTC

On Sat, 28 Jan 2023 at 05:31, Rob Cliffe via Python-list
<python-list@python.org> wrote:
> On 23/01/2023 18:02, Chris Angelico wrote:
> > Maybe, rather than asking for a way to treat a string as code, ask for
> > what you ACTUALLY need, and we can help?
> >
> > ChrisA
> Fair enough, Chris, but still ISTM that it is reasonable to ask (perhaps
> for a different use-case) whether there is a way of evaluating a string
> at runtime as if it were an f-string. We encourage people to ask
> questions on this list, even though the answer will not always be what
> they're hoping for.

No, it's not, because that's the "how do I use X to do Y" problem.
Instead, just ask how to do *what you actually need*. If the best way
to do that is to eval an f-string, then someone will suggest that.
But, much much much more likely, the best way to do it would be
something completely different. What, exactly? That's hard to say,
because *we don't know what you actually need*. All you tell us is
what you're attempting to do, which there is *no good way to achieve*.

> I appreciate that the answer may be "No, because it would be a lot of
> work - and increase the maintenance burden - to support a relatively
> rare requirement".

What about: "No, because it's a terrible TERRIBLE idea, requires that
you do things horribly backwards, and we still don't even know what
you're trying to do"?

> Perhaps someone will be inspired to write a function to do it. 😎

See, we don't know what "it" is, so it's hard to write a function
that's any better than the ones we've seen. Using eval() to construct
an f-string and then parse it is TERRIBLE because:

1) It still doesn't work in general, and thus has caveats like "you
can't use this type of quote character"
2) You would have to pass it a dictionary of variables, which also
can't be done with full generality
3) These are the exact same problems, but backwards, that led to
f-strings in the first place
4) eval is extremely slow and horrifically inefficient.

For some reason, str.format() isn't suitable, but *you haven't said
why*, so we have to avoid that in our solutions. So, to come back to
your concern:

> We encourage people to ask
> questions on this list, even though the answer will not always be what
> they're hoping for.

Well, yes. If you asked "how can I do X", hoping the answer would be
"with a runtime-evaluated f-string", then you're quite right - the
answer might not be what you were hoping for. But since you asked "how
can I evaluate a variable as if it were an f-string", the only
possible answer is "you can't, and that's a horrible idea".

Don't ask how to use X to do Y. Ask how to do Y.

ChrisA

Re: Evaluation of variable as f-string

<tr1ca9$1r96f$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: dfnsonfs...@gmx.de (Johannes Bauer)
Newsgroups: comp.lang.python
Subject: Re: Evaluation of variable as f-string
Date: Fri, 27 Jan 2023 21:31:05 +0100
Organization: A noiseless patient Spider
Lines: 75
Message-ID: <tr1ca9$1r96f$1@dont-email.me>
References: <tqmcbf$vdl$1@gioia.aioe.org>
<CAPTjJmpKEBbYFxowgm4cDfDn0bmieo9sHLK_sNZh8WcqQ=Wqhw@mail.gmail.com>
<mailman.1575.1674496990.20444.python-list@python.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 27 Jan 2023 20:31:05 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="3525fdb799da898b02c959ec11411808";
logging-data="1942735"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+BqTee4lw8SyvmPhk6l8gN39K0ZMqjfwY="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.4.2
Cancel-Lock: sha1:kmNeGr3Wdfl1I9eqe3wHUlIJLUw=
In-Reply-To: <mailman.1575.1674496990.20444.python-list@python.org>
Content-Language: en-US
 by: Johannes Bauer - Fri, 27 Jan 2023 20:31 UTC

Am 23.01.23 um 19:02 schrieb Chris Angelico:

>> This is supposedly for security reasons. However, when trying to emulate
>> this behavior that I wanted (and know the security implications of), my
>> solutions will tend to be less secure. Here is what I have been thinking
>> about:
>
> If you really want the full power of an f-string, then you're asking
> for the full power of eval(),

Exactly.

> and that means all the security
> implications thereof,

Precisely, as I had stated myself.

> not to mention the difficulties of namespacing.

Not an issue in my case.

> Have you considered using the vanilla format() method instead?

Yes. It does not provide the functionality I want. Not even the utterly
trivial example that I gave. To quote myself again, let's say I have an
arbitrary dictionary x (with many nested data structures), I want an
expression to be evaluated that can access any members in there.

x = { "y": "z" }
s = "-> {x['y']}"
print(s.format(x = x))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: "'y'"

I also want to be able to say things like {'x' * 100}, which .format()
also does not do.

In other words: I want the evaluation of a variable as an f-string.

> But if you really REALLY know what you're doing, just use eval()
> directly.

I do, actually, but I hate it. Not because of the security issue, not
because of namespaces, but because it does not reliably work:

>>> s = "{\"x\" * 4}"
>>> eval("f'" + s + "'")
'xxxx'

As I mentioned, it depends on the exact quoting. Triple quotes only
shift the problem. Actually replacing/escaping the relevant quotation
marks is also not trivial.

> I don't really see what you'd gain from an f-string.

The full power of eval.

> At very
> least, work with a well-defined namespace and eval whatever you need
> in that context.

That's what I'm doing.

> Maybe, rather than asking for a way to treat a string as code, ask for
> what you ACTUALLY need, and we can help?

I want to render data from a template using an easily understandable
syntax (like an f-string), ideally using native Python. I want the
template to make use of Python code constructs AND formatting (e.g.
{x['time']['runtime']['seconds'] // 60:02d}).

Cheers,
Johannes

Re: Evaluation of variable as f-string

<tr1cc0$1r96f$2@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: dfnsonfs...@gmx.de (Johannes Bauer)
Newsgroups: comp.lang.python
Subject: Re: Evaluation of variable as f-string
Date: Fri, 27 Jan 2023 21:32:00 +0100
Organization: A noiseless patient Spider
Lines: 17
Message-ID: <tr1cc0$1r96f$2@dont-email.me>
References: <tqmcbf$vdl$1@gioia.aioe.org>
<s-20230123174218@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, 27 Jan 2023 20:32:00 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="3525fdb799da898b02c959ec11411808";
logging-data="1942735"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18iCYijq3xRMGAW+HPbVQ5hvB4MQFtbDGs="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.4.2
Cancel-Lock: sha1:UMO5gMncJh7fYtHUfq5nHhPhlYY=
Content-Language: en-US
In-Reply-To: <s-20230123174218@ram.dialup.fu-berlin.de>
 by: Johannes Bauer - Fri, 27 Jan 2023 20:32 UTC

Am 23.01.23 um 17:43 schrieb Stefan Ram:
> Johannes Bauer <dfnsonfsduifb@gmx.de> writes:
>> x = { "y": "z" }
>> s = "-> {x['y']}"
>> print(s.format(x = x))
>
> x = { "y": "z" }
> def s( x ): return '-> ' + x[ 'y' ]
> print( s( x = x ))

Except this is not at all what I asked for. The string "s" in my example
is just that, an example. I want to render *arbitrary* strings "s"
together with arbitrary dictionaries "x".

Cheers,
Johannes

Re: Evaluation of variable as f-string

<tr1ces$1r96f$3@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: dfnsonfs...@gmx.de (Johannes Bauer)
Newsgroups: comp.lang.python
Subject: Re: Evaluation of variable as f-string
Date: Fri, 27 Jan 2023 21:33:32 +0100
Organization: A noiseless patient Spider
Lines: 42
Message-ID: <tr1ces$1r96f$3@dont-email.me>
References: <tqmcbf$vdl$1@gioia.aioe.org>
<d4a54200-1847-4fd2-c7e1-aa1189f63b78@vub.be>
<5bb2546d-8c16-262d-9336-5b41cbf992b5@tompassin.net>
<mailman.1613.1674675531.20444.python-list@python.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 27 Jan 2023 20:33:32 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="3525fdb799da898b02c959ec11411808";
logging-data="1942735"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+Z0Wnb2KMHF6RH5YMosv7eA0pqiQmE57E="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.4.2
Cancel-Lock: sha1:7KL/eOHQVDeDskYi29No7p8ZJBI=
In-Reply-To: <mailman.1613.1674675531.20444.python-list@python.org>
Content-Language: en-US
 by: Johannes Bauer - Fri, 27 Jan 2023 20:33 UTC

Am 25.01.23 um 20:38 schrieb Thomas Passin:

>> x = { "y": "z" }
>> s = "-> {target}"
>> print(s.format(target = x['y']))
>
> Stack overflow to the rescue:

No.

> Search phrase:  "python evaluate string as fstring"
>
> https://stackoverflow.com/questions/47339121/how-do-i-convert-a-string-into-an-f-string
>
> def effify(non_f_str: str):
>     return eval(f'f"""{non_f_str}"""')
>
> print(effify(s))  # prints as expected: "-> z"

Great.

s = '"""'

> def effify(non_f_str: str):
> return eval(f'f"""{non_f_str}"""')
>
> print(effify(s)) # prints as expected: "-> z"

>>> print(effify(s))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in effify
File "<string>", line 1
f"""""""""
^
SyntaxError: unterminated triple-quoted string literal (detected at line 1)

This is literally the version I described myself, except using triple
quotes. It only modifies the underlying problem, but doesn't solve it.

Cheers,
Johannes

Re: Evaluation of variable as f-string

<tr1d0t$1r96f$4@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: dfnsonfs...@gmx.de (Johannes Bauer)
Newsgroups: comp.lang.python
Subject: Re: Evaluation of variable as f-string
Date: Fri, 27 Jan 2023 21:43:09 +0100
Organization: A noiseless patient Spider
Lines: 75
Message-ID: <tr1d0t$1r96f$4@dont-email.me>
References: <tqmcbf$vdl$1@gioia.aioe.org>
<CAPTjJmpKEBbYFxowgm4cDfDn0bmieo9sHLK_sNZh8WcqQ=Wqhw@mail.gmail.com>
<ebb89997-603f-6283-742a-121128e55ae8@btinternet.com>
<CAPTjJmoxero1FVGW8BNB619tS1XA_LOqn=Lnoq3ogw1JPh52Jg@mail.gmail.com>
<mailman.1669.1674849042.20444.python-list@python.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 27 Jan 2023 20:43:09 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="3525fdb799da898b02c959ec11411808";
logging-data="1942735"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18wBfAZr1HITM+TjfzzfCpVrVakVdT/Raw="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.4.2
Cancel-Lock: sha1:N+6CBabrC7R0z1rIFETAHKA+Cts=
Content-Language: en-US
In-Reply-To: <mailman.1669.1674849042.20444.python-list@python.org>
 by: Johannes Bauer - Fri, 27 Jan 2023 20:43 UTC

Am 27.01.23 um 20:18 schrieb Chris Angelico:

> All you tell us is
> what you're attempting to do, which there is *no good way to achieve*.

Fair enough, that is the answer. It's not possible.

>> Perhaps someone will be inspired to write a function to do it. 😎
>
> See, we don't know what "it" is, so it's hard to write a function
> that's any better than the ones we've seen. Using eval() to construct
> an f-string and then parse it is TERRIBLE because:
>
> 1) It still doesn't work in general, and thus has caveats like "you
> can't use this type of quote character"

Exactly my observation as well, which is why I was thinking there's
something else I missed.

> 2) You would have to pass it a dictionary of variables, which also
> can't be done with full generality

Nonsense. I only am passing a SINGLE variable to eval, called "x". That
is fully general.

> 3) These are the exact same problems, but backwards, that led to
> f-strings in the first place

I don't know what you mean by that.

> 4) eval is extremely slow and horrifically inefficient.

Let me worry about it.

> For some reason, str.format() isn't suitable,

I don't understand why you fully ignore literally the FIRST example I
gave in my original post and angrily claim that you solution works when
it does not:

x = { "y": "z" }
s = "-> {x['y']}"
print(s.format(x = x))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: "'y'"

This. Does. Not. Work.

I want to pass a single variable as a dictionary and access its members
inside the expression.

> but *you haven't said
> why*,

Yes I have, see above.

> Well, yes. If you asked "how can I do X", hoping the answer would be
> "with a runtime-evaluated f-string", then you're quite right - the
> answer might not be what you were hoping for. But since you asked "how
> can I evaluate a variable as if it were an f-string", the only
> possible answer is "you can't, and that's a horrible idea".

"You can't" would have been sufficient. Pity. Your judgement is
unnecessary and, frankly, uncalled for as well. Multiple instances you
claim that you have no idea what I am doing so how would you even begin
to judge a solution as fit or unfit?

> Don't ask how to use X to do Y. Ask how to do Y.

You don't have to be angry that my question does not have a solution. I
will manage and so might you.

Cheers,
Johannes

Re: Evaluation of variable as f-string

<dict-20230127223014@ram.dialup.fu-berlin.de>

  copy mid

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

  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: Evaluation of variable as f-string
Date: 27 Jan 2023 21:30:48 GMT
Organization: Stefan Ram
Lines: 29
Expires: 1 Jan 2024 11:59:58 GMT
Message-ID: <dict-20230127223014@ram.dialup.fu-berlin.de>
References: <tqmcbf$vdl$1@gioia.aioe.org> <s-20230123174218@ram.dialup.fu-berlin.de> <tr1cc0$1r96f$2@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de alPYdCWRIL001BqReL6Fow3AOXSzhdfgc51JfaC2FWyBS4
X-Copyright: (C) Copyright 2023 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
Accept-Language: de-DE, en-US, it, fr-FR
 by: Stefan Ram - Fri, 27 Jan 2023 21:30 UTC

Johannes Bauer <dfnsonfsduifb@gmx.de> writes:
>>Johannes Bauer <dfnsonfsduifb@gmx.de> writes:
>>>x = { "y": "z" }
>>>s = "-> {x['y']}"
>>>print(s.format(x = x))
>>x = { "y": "z" }
>>def s( x ): return '-> ' + x[ 'y' ]
>>print( s( x = x ))
>Except this is not at all what I asked for. The string "s" in my example
>is just that, an example. I want to render *arbitrary* strings "s"
>together with arbitrary dictionaries "x".

I take this to mean that you want to process a dictionary
name, a string and a dictionary that is only specified as
late as at run time.

import string

name = input( 'name of the dictionary? ' )
string_ = input( 'string? ' ) # "-> {x['y']}"
dictionary = eval( input( 'dictionary? ' ))
print( eval( 'f"""' + string_ + '"""', {name:dictionary} ))

name of the dictionary? x
string? -> {x['y']}
dictionary? { 'y': 'z' }
-> z

Re: Evaluation of variable as f-string

<tr1i5c$1saqj$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: aurio...@gmx.de (Christian Gollwitzer)
Newsgroups: comp.lang.python
Subject: Re: Evaluation of variable as f-string
Date: Fri, 27 Jan 2023 23:10:52 +0100
Organization: A noiseless patient Spider
Lines: 33
Message-ID: <tr1i5c$1saqj$1@dont-email.me>
References: <tqmcbf$vdl$1@gioia.aioe.org>
<CAPTjJmpKEBbYFxowgm4cDfDn0bmieo9sHLK_sNZh8WcqQ=Wqhw@mail.gmail.com>
<ebb89997-603f-6283-742a-121128e55ae8@btinternet.com>
<CAPTjJmoxero1FVGW8BNB619tS1XA_LOqn=Lnoq3ogw1JPh52Jg@mail.gmail.com>
<mailman.1669.1674849042.20444.python-list@python.org>
<tr1d0t$1r96f$4@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 27 Jan 2023 22:10:52 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="4339253d6a68068678ec248777bd6702";
logging-data="1977171"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19x+mpfTGlzGxCTPV9AOXAIoWl9G2Lux/g="
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0)
Gecko/20100101 Thunderbird/102.6.1
Cancel-Lock: sha1:iG3ZP22Ui86xx9KliqSAvFVA6bU=
In-Reply-To: <tr1d0t$1r96f$4@dont-email.me>
 by: Christian Gollwitzer - Fri, 27 Jan 2023 22:10 UTC

Am 27.01.23 um 21:43 schrieb Johannes Bauer:
> I don't understand why you fully ignore literally the FIRST example I
> gave in my original post and angrily claim that you solution works when
> it does not:
>
> x = { "y": "z" }
> s = "-> {x['y']}"
> print(s.format(x = x))
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> KeyError: "'y'"
>
> This. Does. Not. Work.

It's because "you're holding it wrong!". Notice the error message; it
says that the key 'y' does not exist.

(base) Apfelkiste:Abschlussmeeting chris$ ipython
Python 3.8.8 (default, Apr 13 2021, 12:59:45)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.22.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: x = { "y": "z" }

In [2]: s = "-> {x[y]}"

In [3]: print(s.format(x = x))
-> z

In [4]:

Christian

Re: Evaluation of variable as f-string

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: rob.cli...@btinternet.com (Rob Cliffe)
Newsgroups: comp.lang.python
Subject: Re: Evaluation of variable as f-string
Date: Fri, 27 Jan 2023 22:54:48 +0000
Lines: 95
Message-ID: <mailman.1675.1674860804.20444.python-list@python.org>
References: <tqmcbf$vdl$1@gioia.aioe.org>
<CAPTjJmpKEBbYFxowgm4cDfDn0bmieo9sHLK_sNZh8WcqQ=Wqhw@mail.gmail.com>
<ebb89997-603f-6283-742a-121128e55ae8@btinternet.com>
<CAPTjJmoxero1FVGW8BNB619tS1XA_LOqn=Lnoq3ogw1JPh52Jg@mail.gmail.com>
<8ebc73d2-e14a-b9ad-fa35-5078e5f9fd6c@btinternet.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de f/KLuabVE+NBzSXecJVUYgFRIIWBmYIbCAx/DhLey49w==
Return-Path: <rob.cliffe@btinternet.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=btinternet.com header.i=@btinternet.com header.b=Bd84P2Cv;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.023
X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; 'def': 0.04; 'fairly':
0.05; 'variable': 0.05; '2023': 0.07; 'partial': 0.07; 'string':
0.07; 'angelico': 0.09; 'confess': 0.09; 'construct': 0.09;
'general,': 0.09; 'parse': 0.09; 'string,': 0.09; 'what,': 0.09;
'yes.': 0.09; 'problem.': 0.15; 'that.': 0.15; '*what': 0.16;
'cases,': 0.16; 'chrisa': 0.16; 'dictionary,': 0.16; 'enough,':
0.16; 'evaluating': 0.16; 'for.': 0.16; 'indeed': 0.16;
'inspired': 0.16; 'relatively': 0.16; 'runtime': 0.16; 'seen.':
0.16; 'slow': 0.16; 'somewhat': 0.16; 'subject:string': 0.16;
'subject:variable': 0.16; 'variables,': 0.16; '\xf0\x9f\x98\x8e':
0.16; 'wrote:': 0.16; "can't": 0.17; 'to:addr:python-list': 0.20;
'i.e.': 0.22; 'maybe': 0.22; 'sat,': 0.22; 'list,': 0.24;
'again,': 0.26; 'do,': 0.26; 'suspect': 0.26; "isn't": 0.27;
'bit': 0.27; 'function': 0.27; 'done': 0.28; '>>>': 0.28; 'chris':
0.28; 'suggest': 0.28; 'asked': 0.29; 'header:User-Agent:1': 0.30;
'code,': 0.31; 'program': 0.31; 'think': 0.32; "doesn't": 0.32;
'language.': 0.32; 'python-list': 0.32; 'received:192.168.1':
0.32; 'but': 0.32; "i'm": 0.33; 'there': 0.33; 'someone': 0.34;
'same': 0.34; 'mean': 0.34; 'requires': 0.34; 'header:In-Reply-
To:1': 0.34; 'trying': 0.35; 'encourage': 0.35; 'addressed': 0.36;
'work,': 0.36; 'people': 0.36; 'using': 0.37; "it's": 0.37;
'hard': 0.37; 'this.': 0.37; 'received:192.168': 0.37; 'though':
0.37; 'way': 0.38; 'could': 0.38; 'means': 0.38; '8bit%:14': 0.38;
'quite': 0.39; "we've": 0.39; 'use': 0.39; 'still': 0.40; 'exact':
0.40; 'overflow:': 0.40; 'received:213': 0.40; 'wishes': 0.40;
'something': 0.40; 'tell': 0.60; 'best': 0.61; 'remember': 0.61;
'above': 0.62; 'reasonable': 0.62; 'come': 0.62; 'ever': 0.63;
'pass': 0.64; 'our': 0.64; 'full': 0.64; 'thus': 0.64; 'your':
0.64; 'his': 0.65; 'tool': 0.65; 'back': 0.67; 'maintenance':
0.67; 'skip:e 20': 0.67; 'right': 0.68; 'response.': 0.68; '"you':
0.69; 'acceptable': 0.69; 'important.': 0.69; 'instead,': 0.70;
'speed': 0.71; 'quote': 0.74; 'solutions.': 0.75; 'need,': 0.76;
'see,': 0.76; 'treat': 0.76; 'yes': 0.76; 'led': 0.81; 'points':
0.84; '"no,': 0.84; '*you': 0.84; 'horrible': 0.84; 'rare': 0.84;
'rob': 0.84; 'say,': 0.84; '"how': 0.91; '*we': 0.91; 'me:': 0.91;
'*no': 0.93
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=btinternet.com;
s=btmx201904; t=1674860103;
bh=pFGnAmSa0Wp2KclEb9Z53jz5E7Ca99c1D2HCOgDMQbw=;
h=Message-ID:Date:MIME-Version:Subject:To:References:From:In-Reply-To;
b=Bd84P2CvLqT9csw9X2UaA/i7WW+1DC22dPh4CitYSRm8GaHlw1IckDq64w6qin1JZJpZACeKglQGRCO3Mvgfq1IgOH6cEj/nxIqOiuwjESNZ7KVuPByKMHPVVsO24glqZrj5BHuJ0WbU6PaJHoAHKWnuBwWE2PYJWqzD1v06eo3b99Xfh7ZrCfIJ6QTzJDUSyBykHqakFTcgah992T+W6o9cR7B7UZRQiuyMTXFOa4JG1PwinG62m5Tr6WwXUKvpWAQDd8wJEKwACPjKl4MQcFjs0N5fBOhg9LMxKrtbuj3G8w+hn002s4S2J6HiePoKSPfFOQQ3FXu7RvAg5+W7Nw==
Authentication-Results: btinternet.com;
auth=pass (PLAIN) smtp.auth=rob.cliffe@btinternet.com;
bimi=skipped
X-SNCR-Rigid: 61A69BAC40C81390
X-Originating-IP: [109.149.69.127]
X-OWM-Source-IP: 109.149.69.127 (GB)
X-OWM-Env-Sender: rob.cliffe@btinternet.com
X-VadeSecure-score: verdict=clean score=0/300, class=clean
X-RazorGate-Vade: gggruggvucftvghtrhhoucdtuddrgedvhedruddvjedgtdefucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuueftkffvkffujffvgffngfevqffopdfqfgfvnecuuegrihhlohhuthemuceftddunecunecujfgurhepkfffgggfuffvfhfhjggtgfesthekredttdefjeenucfhrhhomheptfhosgcuvehlihhffhgvuceorhhosgdrtghlihhffhgvsegsthhinhhtvghrnhgvthdrtghomheqnecuggftrfgrthhtvghrnhepvdetudfhtefgveegieffleelvdeugfeikeevveehudetgfevieejfedtteevtdegnecukfhppedutdelrddugeelrdeiledruddvjeenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhephhgvlhhopegludelvddrudeikedruddrvddtgegnpdhinhgvthepuddtledrudegledrieelrdduvdejpdhmrghilhhfrhhomheprhhosgdrtghlihhffhgvsegsthhinhhtvghrnhgvthdrtghomhdpnhgspghrtghpthhtohepuddprhgtphhtthhopehphihthhhonhdqlhhishhtsehphihthhhonhdrohhrgh
X-RazorGate-Vade-Verdict: clean 0
X-RazorGate-Vade-Classification: clean
X-SNCR-hdrdom: btinternet.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.6.1
Content-Language: en-GB
In-Reply-To: <CAPTjJmoxero1FVGW8BNB619tS1XA_LOqn=Lnoq3ogw1JPh52Jg@mail.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: <8ebc73d2-e14a-b9ad-fa35-5078e5f9fd6c@btinternet.com>
X-Mailman-Original-References: <tqmcbf$vdl$1@gioia.aioe.org>
<CAPTjJmpKEBbYFxowgm4cDfDn0bmieo9sHLK_sNZh8WcqQ=Wqhw@mail.gmail.com>
<ebb89997-603f-6283-742a-121128e55ae8@btinternet.com>
<CAPTjJmoxero1FVGW8BNB619tS1XA_LOqn=Lnoq3ogw1JPh52Jg@mail.gmail.com>
 by: Rob Cliffe - Fri, 27 Jan 2023 22:54 UTC

Whoa! Whoa! Whoa!
I appreciate the points you are making, Chris, but I am a bit taken
aback by such forceful language.

On 27/01/2023 19:18, Chris Angelico wrote:
> On Sat, 28 Jan 2023 at 05:31, Rob Cliffe via Python-list
> <python-list@python.org> wrote:
>> On 23/01/2023 18:02, Chris Angelico wrote:
>>> Maybe, rather than asking for a way to treat a string as code, ask for
>>> what you ACTUALLY need, and we can help?
>>>
>>> ChrisA
>> Fair enough, Chris, but still ISTM that it is reasonable to ask (perhaps
>> for a different use-case) whether there is a way of evaluating a string
>> at runtime as if it were an f-string. We encourage people to ask
>> questions on this list, even though the answer will not always be what
>> they're hoping for.
> No, it's not, because that's the "how do I use X to do Y" problem.
> Instead, just ask how to do *what you actually need*. If the best way
> to do that is to eval an f-string, then someone will suggest that.
> But, much much much more likely, the best way to do it would be
> something completely different. What, exactly? That's hard to say,
> because *we don't know what you actually need*. All you tell us is
> what you're attempting to do, which there is *no good way to achieve*.
If the above is addressed to the OP, I can't answer for him.
If it's addressed to me:  How about if I wanted a program (a learning
tool) to allow the user to play with f-strings?
I.e. to type in a string, and then see what the result would be if it
had been an f-string?
I suspect there are other use cases, but I confess I can't think of one
right now.
>
>> I appreciate that the answer may be "No, because it would be a lot of
>> work - and increase the maintenance burden - to support a relatively
>> rare requirement".
> What about: "No, because it's a terrible TERRIBLE idea, requires that
> you do things horribly backwards, and we still don't even know what
> you're trying to do"?
>
>> Perhaps someone will be inspired to write a function to do it. 😎
> See, we don't know what "it" is, so it's hard to write a function
> that's any better than the ones we've seen.
Again, if this is addressed to the OP: I'm not his keeper. 😁
If it's addressed to me: "it" means a function that will take a string
and evaluate it at runtime as if it were an f-string.  Sure, with
caveats and limitations.  And indeed Thomas Passim found this partial
solution on Stack Overflow:
def effify(non_f_str: str):
    return eval(f'f"""{non_f_str}"""')
> Using eval() to construct
> an f-string and then parse it is TERRIBLE because:
>
> 1) It still doesn't work in general, and thus has caveats like "you
> can't use this type of quote character"
> 2) You would have to pass it a dictionary of variables, which also
> can't be done with full generality
> 3) These are the exact same problems, but backwards, that led to
> f-strings in the first place
> 4) eval is extremely slow and horrifically inefficient.
I understand these limitations.  Nonetheless I can conceive that there
may be scenarios where it is an acceptable solution (perhaps the
learning tool program I suggested above).
Addressing your points specifically:
    1) I believe the quote character limitation could be overcome. It
would need a fair amount of work, for which I haven't (yet) the time or
inclination.
    2) Yes in general you would have to pass it one dictionary, maybe
two.  I don't see this as an insuperable obstacle.  I am not sure what
you mean by "can't be done with full generality" and perhaps that's not
important.
    3) Not sure I understand this.
    4) On the fairly rare occasions that I have used eval(), I can't
remember speed ever being a consideration.
>
> For some reason, str.format() isn't suitable, but *you haven't said
> why*, so we have to avoid that in our solutions. So, to come back to
> your concern:
>
>> We encourage people to ask
>> questions on this list, even though the answer will not always be what
>> they're hoping for.
> Well, yes. If you asked "how can I do X", hoping the answer would be
> "with a runtime-evaluated f-string", then you're quite right - the
> answer might not be what you were hoping for. But since you asked "how
> can I evaluate a variable as if it were an f-string", the only
> possible answer is "you can't, and that's a horrible idea".
I hope that I have shown that this is a somewhat dogmatic response.
>
> Don't ask how to use X to do Y. Ask how to do Y.
Good advice.
Best wishes
Rob Cliffe
>
> ChrisA

Re: Evaluation of variable as f-string

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: ros...@gmail.com (Chris Angelico)
Newsgroups: comp.lang.python
Subject: Re: Evaluation of variable as f-string
Date: Sat, 28 Jan 2023 10:41:40 +1100
Lines: 119
Message-ID: <mailman.1677.1674862913.20444.python-list@python.org>
References: <tqmcbf$vdl$1@gioia.aioe.org>
<CAPTjJmpKEBbYFxowgm4cDfDn0bmieo9sHLK_sNZh8WcqQ=Wqhw@mail.gmail.com>
<ebb89997-603f-6283-742a-121128e55ae8@btinternet.com>
<CAPTjJmoxero1FVGW8BNB619tS1XA_LOqn=Lnoq3ogw1JPh52Jg@mail.gmail.com>
<8ebc73d2-e14a-b9ad-fa35-5078e5f9fd6c@btinternet.com>
<CAPTjJmqbqNUfRvUt8uwVJLD--faMZVUqTD2LnhD6Cao9Fyx0Yg@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-Trace: news.uni-berlin.de EftemyJr8Rk0aWcVPB4KMg+UlulaS6/m8Y6DpaydFkrA==
Return-Path: <rosuav@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=HLyq6nMx;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.010
X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'def': 0.04; 'variable':
0.05; '2023': 0.07; 'partial': 0.07; 'string': 0.07; '"just':
0.09; 'confess': 0.09; 'sometimes,': 0.09; 'string,': 0.09;
'yes.': 0.09; 'problem.': 0.15; '(except': 0.16; 'arbitrary':
0.16; 'cases,': 0.16; 'characters.': 0.16; 'chrisa': 0.16;
'dictionary,': 0.16; 'for.': 0.16; 'from:addr:rosuav': 0.16;
'from:name:chris angelico': 0.16; 'idea.': 0.16; 'indeed': 0.16;
'introduces': 0.16; 'limitations.': 0.16; 'print(x)': 0.16;
'received:209.85.218': 0.16; 'results,': 0.16; 'runtime': 0.16;
'somewhat': 0.16; 'string:': 0.16; 'subject:string': 0.16;
'subject:variable': 0.16; 'typing': 0.16; 'wrote:': 0.16; "can't":
0.17; 'solve': 0.19; 'to:addr:python-list': 0.20; 'language':
0.21; 'i.e.': 0.22; 'maybe': 0.22; 'sat,': 0.22; 'code': 0.23;
'goal': 0.23; 'anything': 0.25; 'saying': 0.25; 'suspect': 0.26;
'bit': 0.27; 'function': 0.27; 'done': 0.28; '>>>': 0.28;
'expect': 0.28; 'wrong': 0.28; 'asked': 0.29; 'program': 0.31;
'think': 0.32; "doesn't": 0.32; 'question': 0.32; 'do.': 0.32;
'here,': 0.32; 'language.': 0.32; 'python-list': 0.32; 'message-
id:@mail.gmail.com': 0.32; 'but': 0.32; 'there': 0.33; 'same':
0.34; 'mean': 0.34; 'header:In-Reply-To:1': 0.34;
'received:google.com': 0.34; 'definitely': 0.35;
'from:addr:gmail.com': 0.35; 'addressed': 0.36; 'work,': 0.36;
'people': 0.36; 'source': 0.36; "skip:' 10": 0.37; 'currently':
0.37; "it's": 0.37; 'received:209.85': 0.37; 'class': 0.37;
'this.': 0.37; 'though': 0.37; 'way': 0.38; 'could': 0.38;
'means': 0.38; 'received:209': 0.39; 'quite': 0.39; 'use': 0.39;
'seeing': 0.39; 'to.': 0.39; 'exact': 0.40; 'overflow:': 0.40;
'something': 0.40; "there's": 0.61; 'seen': 0.62; 'here': 0.62;
'simply': 0.63; 'pass': 0.64; "you'd": 0.64; 'full': 0.64; 'your':
0.64; 'required': 0.65; 'skip:e 20': 0.67; 'right': 0.68;
'exactly': 0.68; 'response.': 0.68; 'order': 0.69; '"you': 0.69;
'and,': 0.69; 'important.': 0.69; 'obvious': 0.69; 'skip:f 30':
0.71; 'quote': 0.74; '....': 0.76; 'yes': 0.76; 'discuss': 0.78;
'points': 0.84; 'exactly.': 0.84; 'extent': 0.84; 'horrible':
0.84; 'luck': 0.84; 'made,': 0.84; 'rob': 0.84; '"how': 0.91;
'me:': 0.91; 'two.': 0.91
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112;
h=to:subject:message-id:date:from:in-reply-to:references:mime-version
:from:to:cc:subject:date:message-id:reply-to;
bh=yG87tLNYerKcHeeAWixNTwBPHKmM9jvgEjdY8Y8V5tY=;
b=HLyq6nMxUNLYU8sYxxEkypmu+hbM9zqLqnuFZ+jjumHQVqgKLwUSRRx/Km55J/nblF
PYne0C5YIqlol4jRQ8G+lZ1PjSu+yYD2ZvBbn6z3y/SWs1qN2K9Z3m9inc1tRRaC7SDf
PfLZNEf3N8tddjQ7V6IUsk8Hm57ujmamfP1DswNLvUr8VYVmTR5FlwflhJnqU4P10Qgx
/hO3IfUK1h7/fiaUqfMG0SD3cFUwyZlaGJ24ny8w32BU+/r1m+jpSSIFilRyx/KVuSP5
JaVSPl+//e7SpK2n69l+T/NcTQz6b+/jd1JT9ZyAY90kbEezvAjOMoxygEI9StHAxUxh
UWoA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20210112;
h=to:subject:message-id:date:from:in-reply-to:references:mime-version
:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to;
bh=yG87tLNYerKcHeeAWixNTwBPHKmM9jvgEjdY8Y8V5tY=;
b=xpfRxd2J7Z+LsskrlIrcOI9eq1/aZxhtfHlDiY6NwWb/IRk46KPPIi3mP02LDagIt3
Tey/nojSy2PMGqMjxjG3i4yUCbMMOvAhElqFEr87PXFHRTF+hlJLr25h4rmXq0XIcG+H
AVE2AZNrkIYuj2NmS9r5HJYgYLV5403BKtguHJnjWRTvPv3spQ+6SoWqTmZujRqDFQZK
Tx+ltxzXR7jRezcIEkObnL0KRD1kMe02BUNfRwHbL43+oZn+7XslNKLTcpCqUuUK7Ic4
oVW7J8PAVow+fMZ4RiPWtb47Cj7ldRVVGLuA193mRe7p8F0E3l1vyQRhAcuJfDJqXxxY
Uqnw==
X-Gm-Message-State: AO0yUKWtE7u8+oK3fmIgnERLf7pzCpYgcfZywk9yBKKmTq4YIMYEqPQ8
ND2Yd8TsL2CPNspAPq2Rbz7PeyQy5K9cBDpk8GXDD0xc2ZM=
X-Google-Smtp-Source: AK7set+5M2iyWNStXZaW6kvNaUr0d0PUbSFczHrnlcmUD/AWm1Xs7DKq2uyEC7vFG5TbnP+cmv5rypJ56fSmJf9GNmk=
X-Received: by 2002:a17:906:6d07:b0:878:67ab:4fa4 with SMTP id
m7-20020a1709066d0700b0087867ab4fa4mr2192759ejr.28.1674862911691; Fri, 27 Jan
2023 15:41:51 -0800 (PST)
In-Reply-To: <8ebc73d2-e14a-b9ad-fa35-5078e5f9fd6c@btinternet.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: <CAPTjJmqbqNUfRvUt8uwVJLD--faMZVUqTD2LnhD6Cao9Fyx0Yg@mail.gmail.com>
X-Mailman-Original-References: <tqmcbf$vdl$1@gioia.aioe.org>
<CAPTjJmpKEBbYFxowgm4cDfDn0bmieo9sHLK_sNZh8WcqQ=Wqhw@mail.gmail.com>
<ebb89997-603f-6283-742a-121128e55ae8@btinternet.com>
<CAPTjJmoxero1FVGW8BNB619tS1XA_LOqn=Lnoq3ogw1JPh52Jg@mail.gmail.com>
<8ebc73d2-e14a-b9ad-fa35-5078e5f9fd6c@btinternet.com>
 by: Chris Angelico - Fri, 27 Jan 2023 23:41 UTC

On Sat, 28 Jan 2023 at 10:08, Rob Cliffe via Python-list
<python-list@python.org> wrote:
>
> Whoa! Whoa! Whoa!
> I appreciate the points you are making, Chris, but I am a bit taken
> aback by such forceful language.

The exact same points have already been made, but not listened to.
Sometimes, forceful language is required in order to get people to
listen.

> If it's addressed to me: How about if I wanted a program (a learning
> tool) to allow the user to play with f-strings?
> I.e. to type in a string, and then see what the result would be if it
> had been an f-string?
> I suspect there are other use cases, but I confess I can't think of one
> right now.

Use the REPL, which will happily evaluate f-strings in their original
context, just like any other code would. You're already eval'ing, so
it's exactly what you'd expect. This is not the same thing as "typing
in a string", though - it's typing in code and seeing what the result
would be. (Except to the extent that source code can be considered a
string.)

> If it's addressed to me: "it" means a function that will take a string
> and evaluate it at runtime as if it were an f-string. Sure, with
> caveats and limitations.

And that's what I am saying is a terrible terrible idea. It will
evaluate things in the wrong context, it has all the normal problems
of eval, and then it introduces its own unique problems with quote
characters.

> And indeed Thomas Passim found this partial
> solution on Stack Overflow:
> def effify(non_f_str: str):
> return eval(f'f"""{non_f_str}"""')

You can find anything on Stack Overflow. Just because you found it
there doesn't mean it's any good - even if it's been massively
upvoted.

> Addressing your points specifically:
> 1) I believe the quote character limitation could be overcome. It
> would need a fair amount of work, for which I haven't (yet) the time or
> inclination.

No problem. Here, solve it for this string:

eval_me = ' f"""{f\'\'\'{f"{f\'{1+2}\'}"}\'\'\'}""" '

F-strings can be nested, remember.

> 2) Yes in general you would have to pass it one dictionary, maybe
> two. I don't see this as an insuperable obstacle. I am not sure what
> you mean by "can't be done with full generality" and perhaps that's not
> important.

>>> def func():
.... x = 1
.... class cls:
.... y = 2
.... print(f"{x=} {y=}")
.... print(locals())
....
>>> func()
x=1 y=2
{'__module__': '__main__', '__qualname__': 'func.<locals>.cls', 'y': 2}

Maybe you don't care. Maybe you do. But locals() is not the same as
"all names currently available in this scope". And, this example is
definitely not something I would recommend, but good luck making this
work with eval:

>>> def func():
.... x = 1
.... print(f"{(x:=2)}")
.... print(x)
....
>>> func()
2 2
.... x = 1
.... print(eval("(x:=2)", globals(), locals()))
.... print(x)
....
>>> func()
2 1

> 3) Not sure I understand this.

Before f-strings existed, one of the big problems with "just use
str.format_map" was that you can't just pass it locals() to get all
the available names. You also can't eval arbitrary code and expect to
get the same results, even if you pass it globals and locals. And
various other considerations here - the exact issues seen here, but
flipped on their heads. So the obvious question is: why not just use
str.format_map?

> > Well, yes. If you asked "how can I do X", hoping the answer would be
> > "with a runtime-evaluated f-string", then you're quite right - the
> > answer might not be what you were hoping for. But since you asked "how
> > can I evaluate a variable as if it were an f-string", the only
> > possible answer is "you can't, and that's a horrible idea".
> I hope that I have shown that this is a somewhat dogmatic response.

And I hope that I have shown that it is fully justified.

> > Don't ask how to use X to do Y. Ask how to do Y.
> Good advice.

Exactly. As I have shown, asking how to use f-strings to achieve this
is simply not suitable, and there's no useful way to discuss other
than to argue semantics. If we had a GOAL to discuss, we could find
much better options.

ChrisA

Re: Evaluation of variable as f-string

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: lis...@tompassin.net (Thomas Passin)
Newsgroups: comp.lang.python
Subject: Re: Evaluation of variable as f-string
Date: Fri, 27 Jan 2023 18:38:58 -0500
Lines: 111
Message-ID: <mailman.1678.1674863350.20444.python-list@python.org>
References: <tqmcbf$vdl$1@gioia.aioe.org>
<CAPTjJmpKEBbYFxowgm4cDfDn0bmieo9sHLK_sNZh8WcqQ=Wqhw@mail.gmail.com>
<ebb89997-603f-6283-742a-121128e55ae8@btinternet.com>
<CAPTjJmoxero1FVGW8BNB619tS1XA_LOqn=Lnoq3ogw1JPh52Jg@mail.gmail.com>
<8ebc73d2-e14a-b9ad-fa35-5078e5f9fd6c@btinternet.com>
<42aedd2c-f4c6-1fd3-e40c-79fb547dc179@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 4VTM/jZkv/Apjqx+qV3caw2Z3TvKaIBQgYtE2tYnc9bw==
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=IOAO1GiO;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.003
X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'looks': 0.02; 'def': 0.04;
'"it': 0.05; 'fairly': 0.05; 'is.': 0.05; 'variable': 0.05;
'2023': 0.07; 'partial': 0.07; 'string': 0.07; 'angelico': 0.09;
'confess': 0.09; 'construct': 0.09; 'general,': 0.09; 'parse':
0.09; 'string,': 0.09; 'what,': 0.09; 'yes.': 0.09; 'problem.':
0.15; 'that.': 0.15; '*what': 0.16; '>>>>': 0.16; 'cases,': 0.16;
'chrisa': 0.16; 'dictionary,': 0.16; 'enough,': 0.16;
'evaluating': 0.16; 'for.': 0.16; 'indeed': 0.16; 'inspired':
0.16; 'intent': 0.16; 'it."': 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;
'relatively': 0.16; 'runtime': 0.16; 'seen.': 0.16; 'slow': 0.16;
'somewhat': 0.16; 'subject:string': 0.16; 'subject:variable':
0.16; 'templates,': 0.16; 'variables,': 0.16; '\xf0\x9f\x98\x8e':
0.16; 'wrote:': 0.16; "can't": 0.17; 'pm,': 0.19; 'to:addr:python-
list': 0.20; 'i.e.': 0.22; 'maybe': 0.22; 'sat,': 0.22; 'list,':
0.24; 'seems': 0.26; 'again,': 0.26; 'do,': 0.26; 'suspect': 0.26;
"isn't": 0.27; 'bit': 0.27; 'function': 0.27; 'done': 0.28; '>>>':
0.28; 'chris': 0.28; 'suggest': 0.28; 'asked': 0.29; 'header:User-
Agent:1': 0.30; 'code,': 0.31; 'program': 0.31; 'think': 0.32;
"doesn't": 0.32; 'language.': 0.32; 'python-list': 0.32;
'received:10.0': 0.32; 'received:mailchannels.net': 0.32;
'received:relay.mailchannels.net': 0.32; 'right,': 0.32; 'but':
0.32; "i'm": 0.33; 'there': 0.33; 'someone': 0.34; 'able': 0.34;
'same': 0.34; 'mean': 0.34; 'requires': 0.34; 'header:In-Reply-
To:1': 0.34; 'trying': 0.35; 'encourage': 0.35; 'addressed': 0.36;
'work,': 0.36; 'people': 0.36; 'using': 0.37; "it's": 0.37;
'hard': 0.37; 'this.': 0.37; 'though': 0.37; 'way': 0.38; 'could':
0.38; 'means': 0.38; '8bit%:14': 0.38; 'quite': 0.39; "we've":
0.39; 'use': 0.39; 'remember': 0.61; 'above': 0.62; 'reasonable':
0.62; 'come': 0.62; 'ever': 0.63; 'pass': 0.64; 'our': 0.64;
'clear': 0.64; 'full': 0.64; 'thus': 0.64; 'your': 0.64; 'his':
0.65; 'tool': 0.65; 'time.': 0.66; 'back': 0.67; 'generally':
0.67; 'maintenance': 0.67; 'header:Received:6': 0.67; 'skip:e 20':
0.67; 'received:64': 0.67; 'right': 0.68; 'response.': 0.68;
'"you': 0.69; 'acceptable': 0.69; 'important.': 0.69; 'instead,':
0.70; 'knowing': 0.71; 'speed': 0.71; 'quote': 0.74; 'solutions.':
0.75; 'need,': 0.76; 'see,': 0.76; 'treat': 0.76; 'yes': 0.76;
'led': 0.81; 'points': 0.84; '"no,': 0.84; '*you': 0.84;
'horrible': 0.84; 'rare': 0.84; 'rob': 0.84; 'say,': 0.84;
'strings': 0.84; '"how': 0.91; '*we': 0.91; 'me:': 0.91; '*no':
0.93
X-Sender-Id: dreamhost|x-authsender|tpassin@tompassin.net
ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1674862740; a=rsa-sha256;
cv=none;
b=fIzMR0u1NlKjjK3DXV4kvAgnmumrSp+ssiRVFbajR2+1IKfi195NanDXic4NX/GJ8tnPL+
pU1NeadGUzeX+xvwp2Fj6ooaF7yiFK9v/7QVOrCNyxOW81bVTOPcXRS2r2Ea1hJ9zGboid
AnrmlbLmGNS+zlSkrGyJA1QhqD1rRV8nnN0fgvtDan47u8M1uQHbWtqItq2y0KlokQDYQi
bKLwtx45oZgUVASg3+Ckc+F0Yzj36Q/Do5HgaD/OOSFi6uBz9iEn0gmDz38yVEDYRfKsRw
SkfqL8R8MiWOtckgj+MvBsT11oIa1u/Kx3lv9cGSXYVmQbivGOwe0V24v1nv+Q==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed;
d=mailchannels.net; s=arc-2022; t=1674862740;
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=FAPNtDsFfoxYb1SO90Y0Og7bFgKN/1Cn8hin6typhZY=;
b=OebHSPuoT3rc/He8lqv4Whqb3kYGtkRZPvb9R49WePaFwKd0PIc2FqUzms7RQAiypCx5WC
ppJbHNZW8+hP4Vjc36ypdNtW6QShtU5I8euAiYSZb7F6j0IepJgD+K7JqXc/HN/atUki14
dY6dO2HjjGu/twEkPZsNWWGs5ubUfBJskoddENBkBIGMPbYAHSq8ZPLL4ijw0pbmq3WXkt
cUaUZIl+c8ijtamlHSANcMo/heNXeCB3B/kuUzdojwz7RMIrE68VqZHVt+VARZbBLw53a7
ZmCVl0fqjUURuCX25QzyOtZACISwEt3uQh3tdvZ5I9Ikkw0XXLLpiIBiiX+m7w==
ARC-Authentication-Results: i=1; rspamd-5fb8f68d88-7bk62;
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-Towering-Whistle: 0f9dd7a879be382d_1674862740652_2702605540
X-MC-Loop-Signature: 1674862740652:3014544330
X-MC-Ingress-Time: 1674862740651
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tompassin.net;
s=dreamhost; t=1674862740;
bh=FAPNtDsFfoxYb1SO90Y0Og7bFgKN/1Cn8hin6typhZY=;
h=Date:Subject:To:From:Content-Type:Content-Transfer-Encoding;
b=IOAO1GiOc2EWM1iToTbgrUTfTLUOg5jWNk56YIKbhHhMZlEMDYvwvLgmMd7TnItHc
01afmWLdLGdl/3RWGR8vJH4Nu+mYzvz237SoPKfu7v3MLKY1AlE/OrlrDGw7GmZf5i
/LwH/rbDCvQGKpWwfIjHU89jmc/u8reFPT9PsCTkrozRsq2+Yo6Hh0kgl0eaFZX9en
lBB+Q9kzNyaun/tvqtE2f+42UVnhrhMUJAyLAU6u6MyF59CBqx5xgWuVzBRBC3caCk
E/UhpysadkqkAOPc6Lok9OiHcKJ6RR3zavDjKBOSIWjMtPS82tXlTh8kUEQoNJAali
hvhoBqHUltm6Q==
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.6.1
Content-Language: en-US
In-Reply-To: <8ebc73d2-e14a-b9ad-fa35-5078e5f9fd6c@btinternet.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: <42aedd2c-f4c6-1fd3-e40c-79fb547dc179@tompassin.net>
X-Mailman-Original-References: <tqmcbf$vdl$1@gioia.aioe.org>
<CAPTjJmpKEBbYFxowgm4cDfDn0bmieo9sHLK_sNZh8WcqQ=Wqhw@mail.gmail.com>
<ebb89997-603f-6283-742a-121128e55ae8@btinternet.com>
<CAPTjJmoxero1FVGW8BNB619tS1XA_LOqn=Lnoq3ogw1JPh52Jg@mail.gmail.com>
<8ebc73d2-e14a-b9ad-fa35-5078e5f9fd6c@btinternet.com>
 by: Thomas Passin - Fri, 27 Jan 2023 23:38 UTC

On 1/27/2023 5:54 PM, Rob Cliffe via Python-list wrote:
> Whoa! Whoa! Whoa!
> I appreciate the points you are making, Chris, but I am a bit taken
> aback by such forceful language.

I generally agree with asking for what the intent is. In this case it
seems pretty clear that the OP wants to use these string fragments as
templates, and he needs to be able to insert variables into them at
runtime, not compile time.

So I think a good response would have been roughly

"It looks like you want to use these strings as templates, is that
right? If not, please tell us what you are trying to do, because it's
hard to help without knowing that. If it's right, here's a way you
could go about it."

Short and amiable.

> On 27/01/2023 19:18, Chris Angelico wrote:
>> On Sat, 28 Jan 2023 at 05:31, Rob Cliffe via Python-list
>> <python-list@python.org> wrote:
>>> On 23/01/2023 18:02, Chris Angelico wrote:
>>>> Maybe, rather than asking for a way to treat a string as code, ask for
>>>> what you ACTUALLY need, and we can help?
>>>>
>>>> ChrisA
>>> Fair enough, Chris, but still ISTM that it is reasonable to ask (perhaps
>>> for a different use-case) whether there is a way of evaluating a string
>>> at runtime as if it were an f-string.  We encourage people to ask
>>> questions on this list, even though the answer will not always be what
>>> they're hoping for.
>> No, it's not, because that's the "how do I use X to do Y" problem.
>> Instead, just ask how to do *what you actually need*. If the best way
>> to do that is to eval an f-string, then someone will suggest that.
>> But, much much much more likely, the best way to do it would be
>> something completely different. What, exactly? That's hard to say,
>> because *we don't know what you actually need*. All you tell us is
>> what you're attempting to do, which there is *no good way to achieve*.
> If the above is addressed to the OP, I can't answer for him.
> If it's addressed to me:  How about if I wanted a program (a learning
> tool) to allow the user to play with f-strings?
> I.e. to type in a string, and then see what the result would be if it
> had been an f-string?
> I suspect there are other use cases, but I confess I can't think of one
> right now.
>>
>>> I appreciate that the answer may be "No, because it would be a lot of
>>> work - and increase the maintenance burden - to support a relatively
>>> rare requirement".
>> What about: "No, because it's a terrible TERRIBLE idea, requires that
>> you do things horribly backwards, and we still don't even know what
>> you're trying to do"?
>>
>>> Perhaps someone will be inspired to write a function to do it. 😎
>> See, we don't know what "it" is, so it's hard to write a function
>> that's any better than the ones we've seen.
> Again, if this is addressed to the OP: I'm not his keeper. 😁
> If it's addressed to me: "it" means a function that will take a string
> and evaluate it at runtime as if it were an f-string.  Sure, with
> caveats and limitations.  And indeed Thomas Passim found this partial
> solution on Stack Overflow:
> def effify(non_f_str: str):
>     return eval(f'f"""{non_f_str}"""')
>>   Using eval() to construct
>> an f-string and then parse it is TERRIBLE because:
>>
>> 1) It still doesn't work in general, and thus has caveats like "you
>> can't use this type of quote character"
>> 2) You would have to pass it a dictionary of variables, which also
>> can't be done with full generality
>> 3) These are the exact same problems, but backwards, that led to
>> f-strings in the first place
>> 4) eval is extremely slow and horrifically inefficient.
> I understand these limitations.  Nonetheless I can conceive that there
> may be scenarios where it is an acceptable solution (perhaps the
> learning tool program I suggested above).
> Addressing your points specifically:
>     1) I believe the quote character limitation could be overcome. It
> would need a fair amount of work, for which I haven't (yet) the time or
> inclination.
>     2) Yes in general you would have to pass it one dictionary, maybe
> two.  I don't see this as an insuperable obstacle.  I am not sure what
> you mean by "can't be done with full generality" and perhaps that's not
> important.
>     3) Not sure I understand this.
>     4) On the fairly rare occasions that I have used eval(), I can't
> remember speed ever being a consideration.
>>
>> For some reason, str.format() isn't suitable, but *you haven't said
>> why*, so we have to avoid that in our solutions. So, to come back to
>> your concern:
>>
>>> We encourage people to ask
>>> questions on this list, even though the answer will not always be what
>>> they're hoping for.
>> Well, yes. If you asked "how can I do X", hoping the answer would be
>> "with a runtime-evaluated f-string", then you're quite right - the
>> answer might not be what you were hoping for. But since you asked "how
>> can I evaluate a variable as if it were an f-string", the only
>> possible answer is "you can't, and that's a horrible idea".
> I hope that I have shown that this is a somewhat dogmatic response.
>>
>> Don't ask how to use X to do Y. Ask how to do Y.
> Good advice.
> Best wishes
> Rob Cliffe
>>
>> ChrisA
>

Re: Evaluation of variable as f-string

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: lis...@tompassin.net (Thomas Passin)
Newsgroups: comp.lang.python
Subject: Re: Evaluation of variable as f-string
Date: Fri, 27 Jan 2023 20:51:29 -0500
Lines: 49
Message-ID: <mailman.1683.1674870701.20444.python-list@python.org>
References: <tqmcbf$vdl$1@gioia.aioe.org>
<d4a54200-1847-4fd2-c7e1-aa1189f63b78@vub.be>
<5bb2546d-8c16-262d-9336-5b41cbf992b5@tompassin.net>
<mailman.1613.1674675531.20444.python-list@python.org>
<tr1ces$1r96f$3@dont-email.me>
<9db7a5e1-076c-a9aa-aee4-e044e45decb7@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 duYDFH+7WHWjY6fTlNbBnQy0oXiBKIllnsM5ab3qCTbg==
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=Vg20aOCI;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.000
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'def': 0.04; 'traceback':
0.04; '(most': 0.05; 'last):': 0.05; '"python': 0.07; 'string':
0.07; 'underlying': 0.07; 'ok,': 0.09; 'prints': 0.09;
'received:23.83.212': 0.09; 'received:elm.relay.mailchannels.net':
0.09; 'schrieb': 0.09; 'skip:\xc2 20': 0.09; 'are.': 0.16;
'great.': 0.16; 'modifies': 0.16; 'quotes.': 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; 'subject:string': 0.16;
'subject:variable': 0.16; 'syntaxerror:': 0.16; 'wrote:': 0.16;
'solve': 0.19; 'pm,': 0.19; 'to:addr:python-list': 0.20;
'problem,': 0.22; 'version': 0.23; 'cannot': 0.25; '>>>': 0.28;
'header:User-Agent:1': 0.30; "doesn't": 0.32; '"",': 0.32;
'received:10.0': 0.32; 'received:mailchannels.net': 0.32;
'received:relay.mailchannels.net': 0.32; 'but': 0.32; 'header:In-
Reply-To:1': 0.34; 'trying': 0.35; 'using': 0.37; 'file': 0.38;
'put': 0.38; 'to.': 0.39; 'explain': 0.40; 'search': 0.61; 'your':
0.64; 'earlier': 0.67; 'worked': 0.67; 'header:Received:6': 0.67;
'skip:e 20': 0.67; 'received:64': 0.67; 'management': 0.68;
'8bit%:100': 0.76; 'constraints': 0.84; 'literally': 0.84;
'quotes': 0.84; 'territory': 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=1674870691; a=rsa-sha256;
cv=none;
b=PpLylDtMTfLBswkapAQz1NQty4OJQkULNGit8Lmuvql6DDnCRdsWSQlvsWBPHXJzuJZwKq
eqk6n1G5tT7nUuaPLPDwlXJzLNS8H3UD1qYrgyan0nKATU47NWJUu4JHBc8ePX3hMyZOwu
Z7tsvNmJkDzKYOg1GdhW/UqV/EDlPzGkwtJb+cXsrB7LDNztvXtpsIcccepbYP0UqS8O4j
/WcAodX7l1WvcsAiF0695PAJjNOg+JmbFldOUFu7bCiJgs/IyxAf7gtFr/LIQ3SrIv9ncj
euoNrfzxng0HIHh2WCMFjR32OtXVCd00wU0sNYSyNRHOM0P+rxCWO7MqLwdT6g==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed;
d=mailchannels.net; s=arc-2022; t=1674870691;
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=AoiY9pLCkuN7Fj/6gPo5KE1r+JxGGNWJpNQypJ8MSuY=;
b=6sDECSdqPsLeAA/VtJ9Hc77zuc6GjPBxbQHFB1VDu5ObnunsSYiVTpXiQY/zFmfgNH+cTS
uaXslj0c3bBjwY8EQbuYz1mHHeeY1CqH2FJ2vlnzNBluQDXq0eSqlFLhdwzyWKAi9NW1Cn
flL/GVzGCtMcQXkKEBbc/n5MkUQUhi5ah3kWm5iKCizUyVAs/vfjrN9ABmxKGJI/U+j8YJ
ZB0tzVyI6gzZLA+59c4LYSQWSxyDbcqe+afnHAEUcgDf64tBNgBjCHl8MyVlM/2bRilPPS
x29bgcHr/2JUFNfLJm0c+jvVwIhBSdABdILuh/VxahAEzFFG5yM7NoJf7ehGlQ==
ARC-Authentication-Results: i=1; rspamd-5fb8f68d88-pxqvz;
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-Befitting-Hook: 657dcf533bb10d00_1674870691235_3070706426
X-MC-Loop-Signature: 1674870691235:3453855228
X-MC-Ingress-Time: 1674870691234
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tompassin.net;
s=dreamhost; t=1674870690;
bh=AoiY9pLCkuN7Fj/6gPo5KE1r+JxGGNWJpNQypJ8MSuY=;
h=Date:Subject:To:From:Content-Type:Content-Transfer-Encoding;
b=Vg20aOCIs0AG6hpOoh8gwiQwGFNmHrWEwRSiCBSG5MiKIZvURX6TICug814g0Oyuj
16t8gEGvKJHz4H52bK9RqP5YiFs/CiuFmZnegebFLrVPOi/0jdz++4oTdZ5ZK86dk5
iwpIQX3tuS+sbC45JGLgsSthd/YPxKbi13kDOD+1z786wsoeKjRTAKXT2UXldEBoAl
cumDCulxqG8F5T3wwd5ipMaBXGimFrvAxszxDkjFqprA55nT8gFYDRW3aGPLAoP+19
t5nQ2QJCckARdwKhch6I3kRrLYKpCRXWp9FJzC61pqkZ/zxSAOsAiRBtHDdOHFLhZv
qoz4IjOlFRNKQ==
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.6.1
Content-Language: en-US
In-Reply-To: <tr1ces$1r96f$3@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: <9db7a5e1-076c-a9aa-aee4-e044e45decb7@tompassin.net>
X-Mailman-Original-References: <tqmcbf$vdl$1@gioia.aioe.org>
<d4a54200-1847-4fd2-c7e1-aa1189f63b78@vub.be>
<5bb2546d-8c16-262d-9336-5b41cbf992b5@tompassin.net>
<mailman.1613.1674675531.20444.python-list@python.org>
<tr1ces$1r96f$3@dont-email.me>
 by: Thomas Passin - Sat, 28 Jan 2023 01:51 UTC

On 1/27/2023 3:33 PM, Johannes Bauer wrote:
> Am 25.01.23 um 20:38 schrieb Thomas Passin:
>
>>> x = { "y": "z" }
>>> s = "-> {target}"
>>> print(s.format(target = x['y']))
>>
>> Stack overflow to the rescue:
>
> No.
>
>> Search phrase:  "python evaluate string as fstring"
>>
>> https://stackoverflow.com/questions/47339121/how-do-i-convert-a-string-into-an-f-string
>>
>> def effify(non_f_str: str):
>>      return eval(f'f"""{non_f_str}"""')
>>
>> print(effify(s))  # prints as expected: "-> z"
>
> Great.
>
> s = '"""'
>
> > def effify(non_f_str: str):
> >      return eval(f'f"""{non_f_str}"""')
> >
> > print(effify(s))  # prints as expected: "-> z"
>
> >>> print(effify(s))
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "<stdin>", line 2, in effify
>   File "<string>", line 1
>     f"""""""""
>            ^
> SyntaxError: unterminated triple-quoted string literal (detected at line 1)
>
> This is literally the version I described myself, except using triple
> quotes. It only modifies the underlying problem, but doesn't solve it.

Ok, so now we are in the territory of "Tell us what you are trying to
accomplish". And part of that is why you cannot put some constraints on
what your string fragments are. The example I gave, copied out of your
earlier message, worked and now you are springing triple quotes on us.

Stop with the rock management already and explain (briefly if possible)
what you are up to.

Re: Evaluation of variable as f-string

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: lis...@tompassin.net (Thomas Passin)
Newsgroups: comp.lang.python
Subject: Re: Evaluation of variable as f-string
Date: Fri, 27 Jan 2023 20:56:49 -0500
Lines: 40
Message-ID: <mailman.1684.1674871016.20444.python-list@python.org>
References: <tqmcbf$vdl$1@gioia.aioe.org>
<CAPTjJmpKEBbYFxowgm4cDfDn0bmieo9sHLK_sNZh8WcqQ=Wqhw@mail.gmail.com>
<ebb89997-603f-6283-742a-121128e55ae8@btinternet.com>
<CAPTjJmoxero1FVGW8BNB619tS1XA_LOqn=Lnoq3ogw1JPh52Jg@mail.gmail.com>
<mailman.1669.1674849042.20444.python-list@python.org>
<tr1d0t$1r96f$4@dont-email.me> <tr1i5c$1saqj$1@dont-email.me>
<af913477-e2d9-df6e-7a56-27a525f9fc73@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 hgZuP5IOzQFrTq3tLcZCiAzZi9jzrTu1HCwdHOsNx5ig==
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=vMx58DUD;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.002
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'traceback': 0.04; '(most':
0.05; 'last):': 0.05; 'python.': 0.08; 'fails': 0.09;
'received:23.83.212': 0.09; 'received:elm.relay.mailchannels.net':
0.09; 'schrieb': 0.09; '8bit%:47': 0.16; '[1]:': 0.16; '[2]:':
0.16; 'gollwitzer': 0.16; 'ipython': 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;
'subject:string': 0.16; 'subject:variable': 0.16; 'wrote:': 0.16;
'python': 0.16; 'says': 0.17; 'pm,': 0.19; 'to:addr:python-list':
0.20; 'help.': 0.25; 'error': 0.29; 'header:User-Agent:1': 0.30;
'"",': 0.32; 'christian': 0.32; 'received:10.0': 0.32;
'received:mailchannels.net': 0.32;
'received:relay.mailchannels.net': 0.32; 'work.': 0.34; 'header
:In-Reply-To:1': 0.34; "it's": 0.37; 'this.': 0.37; 'file': 0.38;
'quite': 0.39; 'gave': 0.61; 'skip:\xc2 10': 0.62; 'key': 0.64;
'header:Received:6': 0.67; 'received:64': 0.67; 'claim': 0.71;
'ignore': 0.71; '"you\'re': 0.84; 'literally': 0.84; 'message;':
0.84; 'you:': 0.84
X-Sender-Id: dreamhost|x-authsender|tpassin@tompassin.net
ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1674871011; a=rsa-sha256;
cv=none;
b=GSIFLTnh7zKEdgUXq/Ne1q/cUHD33TwxP8dr3Sd/CW2dVj36Xyth8yZDwWc6BUMuujg0YF
wvygqbUTmiRLdrHWAn0mrx3dh239bHmYn7fZYLxuJihafnUPwjZCYbojYJXL3XPvC+1s6Z
0VHpxjl2HmIhH7kOHd9V/Fco7AVkdZx+JGaVpXLIGDOy5xoAcxNui12EvvvetxA4kSZ/A7
+stdQX2Zr9MAUEOfeJvnu/y55QHlkQ/kkSH4cQsFYQ7fiLgXAWKzuuusKAD6cSo8n2rGrU
GksnKcebghv7SQcmB00ysl18LjcxT6INZdp9ADYpgZEdAJhnc9gesXxDG2327g==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed;
d=mailchannels.net; s=arc-2022; t=1674871011;
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=nlygB9X65MbawIPFKiNF2/OwH7CX6ujfyl/XSu4GezQ=;
b=lyo3025B8BZz+sHNcXMHMWKIIlJWtyCJAuuFH8VgqrD1Cq+u7QHaHAlCxGIhpQqMwBfRxQ
C+xA7XFAvy3nprBXKPHbfhrE9/41WxcZkOpAWsFoTR8XqzS6SSctVrTRn1RDQnAYj7jVQS
ouf1QmA70WBV9WVAtxRgs09Ko3ZNk5ZuaBO+uXclBkkpQNwIwq884VE3Q8W7NvusvlREZw
XtC8I0x2GIq1UER1Dg0eu3UsNuQocYNR6R8GEuFST4ow1bRqqtTcrCBnJZOPM9r1aIYrYs
peTpuXSyVPw2cglREtB9/0EVF+BBU0WEtZUrFdHkOyvejXf7RhlM4HK8kRAV6g==
ARC-Authentication-Results: i=1; rspamd-544f66f495-8bgmt;
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-Chemical-Occur: 496128530c1aeb84_1674871011425_1535895578
X-MC-Loop-Signature: 1674871011425:332105847
X-MC-Ingress-Time: 1674871011425
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tompassin.net;
s=dreamhost; t=1674871011;
bh=nlygB9X65MbawIPFKiNF2/OwH7CX6ujfyl/XSu4GezQ=;
h=Date:Subject:To:From:Content-Type:Content-Transfer-Encoding;
b=vMx58DUDWyDrV/m3dJYjjcxJyPLdWydDgyOQymxM6/nzcuO67xH7xYi2WXVcHTbZW
k/rkg3XoSMCrGqERrneTn1PAyh+vOuR40Nc3dV8IeHiCUSgivuWMjteg3ZJi1USUgw
6dDCQaNALtPqvzE6CfRvyjnmPlWqPaUyIl0zrrepjMHonHoDKXdA+EZ9Ww7I8V3yhV
1TmOz7h0VyRCgUyFZFUNqAFAyJAeeY3IsVF22we+g7u9QcRXVdSYnqJnw4gAuEEktB
Hz9X7e4YHI4wn2kpeuGyLTgbdcvBPVP4AszGcY/Pvpo8xs7ly2STpvqlTWpTL9KOnV
SORwBnsfFaQXg==
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.6.1
Content-Language: en-US
In-Reply-To: <tr1i5c$1saqj$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: <af913477-e2d9-df6e-7a56-27a525f9fc73@tompassin.net>
X-Mailman-Original-References: <tqmcbf$vdl$1@gioia.aioe.org>
<CAPTjJmpKEBbYFxowgm4cDfDn0bmieo9sHLK_sNZh8WcqQ=Wqhw@mail.gmail.com>
<ebb89997-603f-6283-742a-121128e55ae8@btinternet.com>
<CAPTjJmoxero1FVGW8BNB619tS1XA_LOqn=Lnoq3ogw1JPh52Jg@mail.gmail.com>
<mailman.1669.1674849042.20444.python-list@python.org>
<tr1d0t$1r96f$4@dont-email.me> <tr1i5c$1saqj$1@dont-email.me>
 by: Thomas Passin - Sat, 28 Jan 2023 01:56 UTC

On 1/27/2023 5:10 PM, Christian Gollwitzer wrote:
> Am 27.01.23 um 21:43 schrieb Johannes Bauer:
>> I don't understand why you fully ignore literally the FIRST example I
>> gave in my original post and angrily claim that you solution works
>> when it does not:
>>
>> x = { "y": "z" }
>> s = "-> {x['y']}"
>> print(s.format(x = x))
>> Traceback (most recent call last):
>>    File "<stdin>", line 1, in <module>
>> KeyError: "'y'"
>>
>> This. Does. Not. Work.
>
> It's because "you're holding it wrong!". Notice the error message; it
> says that the key 'y' does not exist.
>
>
> (base) Apfelkiste:Abschlussmeeting chris$ ipython
> Python 3.8.8 (default, Apr 13 2021, 12:59:45)
> Type 'copyright', 'credits' or 'license' for more information
> IPython 7.22.0 -- An enhanced Interactive Python. Type '?' for help.
>
> In [1]: x = { "y": "z" }
>
> In [2]: s = "-> {x[y]}"
>
> In [3]: print(s.format(x = x))
> -> z
>
> In [4]:
>
>     Christian

Oops, that's not quite what he wrote.

You: s = "-> {x[y]}" # Works
Him: s = "-> {x['y']}" # Fails

RE: Evaluation of variable as f-string

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!news.szaf.org!fu-berlin.de!uni-berlin.de!not-for-mail
From:
Newsgroups: comp.lang.python
Subject: RE: Evaluation of variable as f-string
Date: Sat, 28 Jan 2023 00:19:21 -0500
Lines: 71
Message-ID: <mailman.1685.1674883167.20444.python-list@python.org>
References: <tqmcbf$vdl$1@gioia.aioe.org>
<s-20230123174218@ram.dialup.fu-berlin.de> <tr1cc0$1r96f$2@dont-email.me>
<dict-20230127223014@ram.dialup.fu-berlin.de>
<00c701d932d8$145d8be0$3d18a3a0$@gmail.com>
Mime-Version: 1.0
Content-Type: text/plain;
charset="iso-2022-jp"
Content-Transfer-Encoding: 7bit
X-Trace: news.uni-berlin.de KDNXn5SrAWBBLch/l9jkrA0R1/ogP0nhKTPQ2qrsJ4sg==
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=WxaOI4PB;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.015
X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'variable': 0.05; '2023':
0.07; '???': 0.07; 'explanation': 0.07; 'ram': 0.07; 'string':
0.07; '27,': 0.09; 'example.': 0.09; 'received:108': 0.09;
'situations': 0.09; 'writes:': 0.09; 'import': 0.15;
'url:mailman': 0.15; 'arbitrary': 0.16; 'context.': 0.16;
'evaluated': 0.16; 'evaluated.': 0.16; 'for.': 0.16; 'hint': 0.16;
'print(': 0.16; 'render': 0.16; 'skip:> 10': 0.16;
'subject:string': 0.16; 'subject:variable': 0.16; 'usable': 0.16;
'useful.': 0.16; "can't": 0.17; 'message-id:@gmail.com': 0.18;
'to:addr:python-list': 0.20; 'language': 0.21; 'run': 0.23;
'(and': 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; 'programming':
0.25; 'url-ip:188.166/16': 0.25; 'anyone': 0.25; 'again,': 0.26;
'friday,': 0.26; 'stefan': 0.26; 'function': 0.27; 'chris': 0.28;
'fact': 0.28; 'sense': 0.28; 'asked': 0.29; 'looked': 0.31;
'module': 0.31; 'received:209.85.160': 0.31; 'url-ip:188/8': 0.31;
'context': 0.32; 'do.': 0.32; 'python-list': 0.32; 'replies':
0.32; 'specified': 0.32; 'suitable': 0.32; 'but': 0.32; 'there':
0.33; 'mean': 0.34; 'header:In-Reply-To:1': 0.34;
'received:google.com': 0.34; 'trying': 0.35;
'from:addr:gmail.com': 0.35; 'people': 0.36; 'really': 0.37;
'received:209.85': 0.37; 'could': 0.38; 'received:209': 0.39;
'quite': 0.39; 'safe': 0.39; 'list': 0.39; 'use': 0.39;
'evaluation': 0.39; 'wants': 0.40; 'something': 0.40; 'want':
0.40; 'potential': 0.60; 'skip:o 10': 0.61; 'from:': 0.62; 'to:':
0.62; 'techniques': 0.62; 'here': 0.62; 'security': 0.64; 're:':
0.64; 'your': 0.64; 'let': 0.66; 'time.': 0.66; 'that,': 0.67;
'matter': 0.68; 'during': 0.69; 'functional': 0.69; 'below': 0.69;
'solutions': 0.70; 'too.': 0.70; 'january': 0.71; 'late': 0.73;
'name,': 0.75; 'languages,': 0.76; 'potentially': 0.76; 'sent:':
0.78; 'out.': 0.80; 'more.': 0.82; 'known': 0.84; 'points': 0.84;
'reasons': 0.84; 'allocated': 0.84; 'strings': 0.84
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112;
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=itNad9Okx6vlnFWQMxesu3z4B7QQspKIWqyXBj0pRXw=;
b=WxaOI4PByvG2FvGI/LKkxLf1UYvsZMp/m/IxUuLWIzjIQQl+bXz/6CcaB6lQmRyW+R
nr0aTz1jjCLrg4qj7SUuccB75/pEKWa0qR7+2030rvTd2xm3DaqcXZA9xnXgG0A7Eazu
0PCe45ExjVhlahqdEqpmkS0zyrLYIUx5eXo8r0gPplMS8dW6cn/bwqpIJhSlZCbFUTmM
ivsfOsFlUGNIXizqnE6kabzfQwtVDI+O3mHyrPQqjMvkFBLAcqk5Gxf+giHhFa0rDXJn
dRhi1pyyZIycBkVqjwXVcb4XigEVW8jpofirCRfoGw1Vb22RVy+cjfZ23WaBC36rfdcz
061Q==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20210112;
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=itNad9Okx6vlnFWQMxesu3z4B7QQspKIWqyXBj0pRXw=;
b=efIIuEKpjFjwHzXR3h3/ZFfUwAhKV+Ydq6gpHbqYK52V/Fg4VS7tJ8XZwCxnrkLptZ
RLoAd29fCLUPZ7QoJQeuLQzOzkRFL9DiD0u0wtK1poz5aQKht93mW8/22NLqvPh4uV9z
rHieIAjlF8I7suXbmLbh2FDnAccle+B4aRcr7On40dT0Ndtsg8IGjP3b1mMSBID8dM6V
ZMTyz4V60R1ROzG9xNsffe2JxNi8uDNQlOB2rZYTjdEZtu1+dEucyJ4M/rvO2MnHoFZV
xmxCBS1pFvgdaqVAxS0R/9a+CNebheMp6vFrOgzfNC2tmKzKcng7BwYjZjJmECr7HJhG
qyIg==
X-Gm-Message-State: AFqh2kpIo4sSVA1xB6kwp+TFLjI8UUuL6D5ltxNj456Z8R9w/nryBkzN
6qeNdfwmGOaxkQFtvdUQduUy0KURYsw=
X-Google-Smtp-Source: AMrXdXub8g4oO0ABmk+le+txDesMUk7nx3qA8m+h9aYfNqpXD5kKEI3xhQ8D6KYtjkIjF4u6bcuFdQ==
X-Received: by 2002:a05:622a:2593:b0:3b6:694f:d710 with SMTP id
cj19-20020a05622a259300b003b6694fd710mr56130930qtb.50.1674883164489;
Fri, 27 Jan 2023 21:19:24 -0800 (PST)
In-Reply-To: <dict-20230127223014@ram.dialup.fu-berlin.de>
X-Mailer: Microsoft Outlook 16.0
Thread-Index: AQH3wFQHbKRwob+V+9e7/c9WFiZ0OAHdJWbjAfY28jkCmH0EUK5Cy5kg
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: <00c701d932d8$145d8be0$3d18a3a0$@gmail.com>
X-Mailman-Original-References: <tqmcbf$vdl$1@gioia.aioe.org>
<s-20230123174218@ram.dialup.fu-berlin.de> <tr1cc0$1r96f$2@dont-email.me>
<dict-20230127223014@ram.dialup.fu-berlin.de>
 by: - Sat, 28 Jan 2023 05:19 UTC

May I point out that some dynamic situations can in a sense be normalized?

The example below posits a dynamically allocated dictionary during run time.
But why can't you have a placeholder variable name in place and make your
placeholder a link to the dictionary (or other item) before invoking the
existing f-string with the placeholder built-in, rather than trying to
evaluate an F♯ ???

Of course many situations may not have as much of a possible work-around.
But as so many have noted, we never got a really good explanation of what
the OP really wants to do. There have been replies that may be suitable
solutions and some clearly have potential to be security holes if you let
the users dynamically create strings to be evaluated.

In some languages, many of the facets of the language can be passed along as
a function with some name to be used in functional programming techniques
and this can be very useful. The "operator" module can be used for quite a
few things like operator.add or operator.__add__ or operator.concat and many
more. If the logic used to evaluate an f-string (and for that matter the
other string variants like b'..' and r'...') could be encapsulated in a
function like that, it would be potentially usable as in passing something
like dangerous_operator.f_string and a list of strings and having that
return a list of evaluated strings.

The fact that something like this is not known to the people here may hint
that it is not something considered safe to use by amateurs. But then again,
anyone who wants to can use eval() as Chris points out.

Of course, there may be other reasons too. An f-string is evaluated in a
context that may be different if a string is passed along and then looked at
in another context.

-----Original Message-----
From: Python-list <python-list-bounces+avi.e.gross=gmail.com@python.org> On
Behalf Of Stefan Ram
Sent: Friday, January 27, 2023 4:31 PM
To: python-list@python.org
Subject: Re: Evaluation of variable as f-string

Johannes Bauer <dfnsonfsduifb@gmx.de> writes:
>>Johannes Bauer <dfnsonfsduifb@gmx.de> writes:
>>>x = { "y": "z" }
>>>s = "-> {x['y']}"
>>>print(s.format(x = x))
>>x = { "y": "z" }
>>def s( x ): return '-> ' + x[ 'y' ]
>>print( s( x = x ))
>Except this is not at all what I asked for. The string "s" in my
>example is just that, an example. I want to render *arbitrary* strings "s"
>together with arbitrary dictionaries "x".

I take this to mean that you want to process a dictionary
name, a string and a dictionary that is only specified as
late as at run time.

import string

name = input( 'name of the dictionary? ' ) string_ = input( 'string? ' ) #
"-> {x['y']}"
dictionary = eval( input( 'dictionary? ' )) print( eval( 'f"""' + string_ +
'"""', {name:dictionary} ))

name of the dictionary? x
string? -> {x['y']}
dictionary? { 'y': 'z' }
-> z

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

Re: Evaluation of variable as f-string

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: hjp-pyt...@hjp.at (Peter J. Holzer)
Newsgroups: comp.lang.python
Subject: Re: Evaluation of variable as f-string
Date: Sat, 28 Jan 2023 14:28:47 +0100
Lines: 64
Message-ID: <mailman.1687.1674912536.20444.python-list@python.org>
References: <tqmcbf$vdl$1@gioia.aioe.org>
<CAPTjJmpKEBbYFxowgm4cDfDn0bmieo9sHLK_sNZh8WcqQ=Wqhw@mail.gmail.com>
<ebb89997-603f-6283-742a-121128e55ae8@btinternet.com>
<CAPTjJmoxero1FVGW8BNB619tS1XA_LOqn=Lnoq3ogw1JPh52Jg@mail.gmail.com>
<mailman.1669.1674849042.20444.python-list@python.org>
<tr1d0t$1r96f$4@dont-email.me> <tr1i5c$1saqj$1@dont-email.me>
<af913477-e2d9-df6e-7a56-27a525f9fc73@tompassin.net>
<20230128132847.fdc4d3hx7m7yedi4@hjp.at>
Mime-Version: 1.0
Content-Type: multipart/signed; micalg=pgp-sha512;
protocol="application/pgp-signature"; boundary="kouy2rrwlisrjof5"
X-Trace: news.uni-berlin.de XgSH+79/85T4EqXCYwto7QDU/MRsjeh5RfBCrEFv8wIA==
Return-Path: <hjp-python@hjp.at>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=none reason="no signature";
dkim-adsp=none (unprotected policy); dkim-atps=neutral
X-Spam-Status: OK 0.000
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'traceback': 0.04; '(most':
0.05; 'content-type:multipart/signed': 0.05; 'last):': 0.05;
'content-type:application/pgp-signature': 0.09; 'fails': 0.09;
'filename:fname piece:asc': 0.09; 'filename:fname
piece:signature': 0.09; 'filename:fname:signature.asc': 0.09;
'schrieb': 0.09; '"creative': 0.16; '[1]:': 0.16; '[2]:': 0.16;
'__/': 0.16; 'challenge!"': 0.16; 'from:addr:hjp-python': 0.16;
'from:addr:hjp.at': 0.16; 'from:name:peter j. holzer': 0.16;
'gollwitzer': 0.16; 'hjp@hjp.at': 0.16; 'holzer': 0.16;
'reality.': 0.16; 'stross,': 0.16; 'subject:string': 0.16;
'subject:variable': 0.16; 'url-ip:212.17.106.137/32': 0.16; 'url-
ip:212.17.106/24': 0.16; 'url-ip:212.17/16': 0.16; 'url:hjp':
0.16; '|_|_)': 0.16; 'wrote:': 0.16; 'says': 0.17; 'pm,': 0.19;
'to:addr:python-list': 0.20; 'sense': 0.28; 'error': 0.29; '"",':
0.32; 'christian': 0.32; 'work.': 0.34; 'header:In-Reply-To:1':
0.34; "it's": 0.37; 'this.': 0.37; 'file': 0.38; 'quite': 0.39;
'point.': 0.62; 'received:212': 0.62; 'key': 0.64;
'received:userid': 0.66; 'url-ip:212/8': 0.69;
'charset:iso-8859-1': 0.73; '"you\'re': 0.84; 'message;': 0.84;
'received:at': 0.84; 'you:': 0.84
Mail-Followup-To: python-list@python.org
Content-Disposition: inline
In-Reply-To: <af913477-e2d9-df6e-7a56-27a525f9fc73@tompassin.net>
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: <20230128132847.fdc4d3hx7m7yedi4@hjp.at>
X-Mailman-Original-References: <tqmcbf$vdl$1@gioia.aioe.org>
<CAPTjJmpKEBbYFxowgm4cDfDn0bmieo9sHLK_sNZh8WcqQ=Wqhw@mail.gmail.com>
<ebb89997-603f-6283-742a-121128e55ae8@btinternet.com>
<CAPTjJmoxero1FVGW8BNB619tS1XA_LOqn=Lnoq3ogw1JPh52Jg@mail.gmail.com>
<mailman.1669.1674849042.20444.python-list@python.org>
<tr1d0t$1r96f$4@dont-email.me>
<tr1i5c$1saqj$1@dont-email.me>
<af913477-e2d9-df6e-7a56-27a525f9fc73@tompassin.net>
 by: Peter J. Holzer - Sat, 28 Jan 2023 13:28 UTC
Attachments: signature.asc (application/pgp-signature)

On 2023-01-27 20:56:49 -0500, Thomas Passin wrote:
> On 1/27/2023 5:10 PM, Christian Gollwitzer wrote:
> > Am 27.01.23 um 21:43 schrieb Johannes Bauer:
> > > x = { "y": "z" }
> > > s = "-> {x['y']}"
> > > print(s.format(x = x))
> > > Traceback (most recent call last):
> > >    File "<stdin>", line 1, in <module>
> > > KeyError: "'y'"
> > >
> > > This. Does. Not. Work.
> >
> > It's because "you're holding it wrong!". Notice the error message; it
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> > says that the key 'y' does not exist.
[...]
> > In [1]: x = { "y": "z" }
> > In [2]: s = "-> {x[y]}"
> > In [3]: print(s.format(x = x))
> > -> z
> > In [4]:
>
> Oops, that's not quite what he wrote.
>
> You: s = "-> {x[y]}" # Works
> Him: s = "-> {x['y']}" # Fails

That was the point.

hp

--
_ | Peter J. Holzer | Story must make more sense than reality.
|_|_) | |
| | | hjp@hjp.at | -- Charles Stross, "Creative writing
__/ | http://www.hjp.at/ | challenge!"

Attachments: signature.asc (application/pgp-signature)
Re: Evaluation of variable as f-string

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: hjp-pyt...@hjp.at (Peter J. Holzer)
Newsgroups: comp.lang.python
Subject: Re: Evaluation of variable as f-string
Date: Sat, 28 Jan 2023 14:41:13 +0100
Lines: 63
Message-ID: <mailman.1688.1674913275.20444.python-list@python.org>
References: <tqmcbf$vdl$1@gioia.aioe.org>
<CAPTjJmpKEBbYFxowgm4cDfDn0bmieo9sHLK_sNZh8WcqQ=Wqhw@mail.gmail.com>
<ebb89997-603f-6283-742a-121128e55ae8@btinternet.com>
<CAPTjJmoxero1FVGW8BNB619tS1XA_LOqn=Lnoq3ogw1JPh52Jg@mail.gmail.com>
<mailman.1669.1674849042.20444.python-list@python.org>
<tr1d0t$1r96f$4@dont-email.me>
<20230128134113.l3awsy2dcflzeets@hjp.at>
Mime-Version: 1.0
Content-Type: multipart/signed; micalg=pgp-sha512;
protocol="application/pgp-signature"; boundary="hsa6b2dickdnmv7c"
X-Trace: news.uni-berlin.de 7qmNo25Jo7pdHua7oaxQrAV/w44CJLMhN9ScstKUdsUw==
Return-Path: <hjp-python@hjp.at>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=none reason="no signature";
dkim-adsp=none (unprotected policy); dkim-atps=neutral
X-Spam-Status: OK 0.000
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'traceback': 0.04; '(most':
0.05; 'content-type:multipart/signed': 0.05; 'last):': 0.05;
'variable': 0.05; 'template': 0.07; 'compiler': 0.09; 'content-
type:application/pgp-signature': 0.09; 'filename:fname piece:asc':
0.09; 'filename:fname piece:signature': 0.09;
'filename:fname:signature.asc': 0.09; '"creative': 0.16; '__/':
0.16; 'arbitrary': 0.16; 'challenge!"': 0.16; 'construct.': 0.16;
'expressions': 0.16; 'from:addr:hjp-python': 0.16;
'from:addr:hjp.at': 0.16; 'from:name:peter j. holzer': 0.16;
'hjp@hjp.at': 0.16; 'holzer': 0.16; 'jinja2': 0.16; 'reality.':
0.16; 'stross,': 0.16; 'subject:string': 0.16; 'subject:variable':
0.16; 'url-ip:212.17.106.137/32': 0.16; 'url-ip:212.17.106/24':
0.16; 'url-ip:212.17/16': 0.16; 'url:hjp': 0.16; '|_|_)': 0.16;
'wrote:': 0.16; 'python': 0.16; 'instead': 0.17; "can't": 0.17;
'implement': 0.19; 'to:addr:python-list': 0.20; 'cover': 0.26;
'sense': 0.28; 'suggest': 0.28; '"",': 0.32; 'but': 0.32; 'work.':
0.34; 'header:In-Reply-To:1': 0.34; 'cases': 0.36; 'this.': 0.37;
'file': 0.38; 'single': 0.39; 'enough': 0.39; '(see': 0.40;
'want': 0.40; 'format': 0.62; 'received:212': 0.62; 'pass': 0.64;
'in.': 0.64; 'received:userid': 0.66; 'interested': 0.68;
'latter': 0.69; 'url-ip:212/8': 0.69; 'interesting': 0.71;
'production': 0.71; 'skip:f 20': 0.75; 'received:at': 0.84;
'exists': 0.91
Mail-Followup-To: python-list@python.org
Content-Disposition: inline
In-Reply-To: <tr1d0t$1r96f$4@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: <20230128134113.l3awsy2dcflzeets@hjp.at>
X-Mailman-Original-References: <tqmcbf$vdl$1@gioia.aioe.org>
<CAPTjJmpKEBbYFxowgm4cDfDn0bmieo9sHLK_sNZh8WcqQ=Wqhw@mail.gmail.com>
<ebb89997-603f-6283-742a-121128e55ae8@btinternet.com>
<CAPTjJmoxero1FVGW8BNB619tS1XA_LOqn=Lnoq3ogw1JPh52Jg@mail.gmail.com>
<mailman.1669.1674849042.20444.python-list@python.org>
<tr1d0t$1r96f$4@dont-email.me>
 by: Peter J. Holzer - Sat, 28 Jan 2023 13:41 UTC
Attachments: signature.asc (application/pgp-signature)

On 2023-01-27 21:43:09 +0100, Johannes Bauer wrote:
> x = { "y": "z" }
> s = "-> {x['y']}"
> print(s.format(x = x))
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> KeyError: "'y'"
>
> This. Does. Not. Work.
>
> I want to pass a single variable as a dictionary and access its members
> inside the expression.

You can do that (see other responses), but you can't have arbitrary
Python expressions inside a format string.

It works with f-strings because f-strings are a compiler construct. The
f-string never exists at run-time. Instead the compiler transforms
f"-> {x['y']}"
into the equivalent of
"-> " + format_value(x["y"]) + ""

So either you need to pass it to the Python compiler (via eval), or you
need to implement enough of a Python parser/interpreter to cover the
cases you are interested in. The latter might be an interesting
exercise, but I would suggest looking at existing template engines like
Jinja2 for production purposes.

hp

--
_ | Peter J. Holzer | Story must make more sense than reality.
|_|_) | |
| | | hjp@hjp.at | -- Charles Stross, "Creative writing
__/ | http://www.hjp.at/ | challenge!"

Attachments: signature.asc (application/pgp-signature)
Re: Evaluation of variable as f-string

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: hjp-pyt...@hjp.at (Peter J. Holzer)
Newsgroups: comp.lang.python
Subject: Re: Evaluation of variable as f-string
Date: Sat, 28 Jan 2023 15:22:18 +0100
Lines: 48
Message-ID: <mailman.1689.1674915740.20444.python-list@python.org>
References: <tqmcbf$vdl$1@gioia.aioe.org>
<CAPTjJmpKEBbYFxowgm4cDfDn0bmieo9sHLK_sNZh8WcqQ=Wqhw@mail.gmail.com>
<mailman.1575.1674496990.20444.python-list@python.org>
<tr1ca9$1r96f$1@dont-email.me>
<20230128142218.atkybkdf7elzqrky@hjp.at>
Mime-Version: 1.0
Content-Type: multipart/signed; micalg=pgp-sha512;
protocol="application/pgp-signature"; boundary="55m4bnfujptjgnmd"
X-Trace: news.uni-berlin.de TezRtPb5oFTcqZ2GZQC2yAcsiC/BoT6G7Cz4T0l7vnvQ==
Return-Path: <hjp-python@hjp.at>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=none reason="no signature";
dkim-adsp=none (unprotected policy); dkim-atps=neutral
X-Spam-Status: OK 0.000
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'content-
type:multipart/signed': 0.05; 'directly.': 0.07; 'content-
type:application/pgp-signature': 0.09; 'filename:fname piece:asc':
0.09; 'filename:fname piece:signature': 0.09;
'filename:fname:signature.asc': 0.09; '"creative': 0.16; '__/':
0.16; 'challenge!"': 0.16; 'expected.': 0.16; 'from:addr:hjp-
python': 0.16; 'from:addr:hjp.at': 0.16; 'from:name:peter j.
holzer': 0.16; 'hjp@hjp.at': 0.16; 'holzer': 0.16; 'reality.':
0.16; 'stross,': 0.16; 'subject:string': 0.16; 'subject:variable':
0.16; 'url-ip:212.17.106.137/32': 0.16; 'url-ip:212.17.106/24':
0.16; 'url-ip:212.17/16': 0.16; 'url:hjp': 0.16; 'work:': 0.16;
'|_|_)': 0.16; 'wrote:': 0.16; 'to:addr:python-list': 0.20; 'do,':
0.26; '>>>': 0.28; 'sense': 0.28; 'but': 0.32; 'header:In-Reply-
To:1': 0.34; 'really': 0.37; 'use': 0.39; 'received:212': 0.62;
'security': 0.64; 'received:userid': 0.66; 'exactly': 0.68; 'url-
ip:212/8': 0.69; 'received:at': 0.84
Mail-Followup-To: python-list@python.org
Content-Disposition: inline
In-Reply-To: <tr1ca9$1r96f$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: <20230128142218.atkybkdf7elzqrky@hjp.at>
X-Mailman-Original-References: <tqmcbf$vdl$1@gioia.aioe.org>
<CAPTjJmpKEBbYFxowgm4cDfDn0bmieo9sHLK_sNZh8WcqQ=Wqhw@mail.gmail.com>
<mailman.1575.1674496990.20444.python-list@python.org>
<tr1ca9$1r96f$1@dont-email.me>
 by: Peter J. Holzer - Sat, 28 Jan 2023 14:22 UTC
Attachments: signature.asc (application/pgp-signature)

On 2023-01-27 21:31:05 +0100, Johannes Bauer wrote:
> > But if you really REALLY know what you're doing, just use eval()
> > directly.
>
> I do, actually, but I hate it. Not because of the security issue, not
> because of namespaces, but because it does not reliably work:
>
> >>> s = "{\"x\" * 4}"
> >>> eval("f'" + s + "'")
> 'xxxx'

That's exactly the result I expected. What did you expect?

hp

--
_ | Peter J. Holzer | Story must make more sense than reality.
|_|_) | |
| | | hjp@hjp.at | -- Charles Stross, "Creative writing
__/ | http://www.hjp.at/ | challenge!"

Attachments: signature.asc (application/pgp-signature)
Re: Evaluation of variable as f-string

<tr3uas$2buff$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: dfnsonfs...@gmx.de (Johannes Bauer)
Newsgroups: comp.lang.python
Subject: Re: Evaluation of variable as f-string
Date: Sat, 28 Jan 2023 20:50:52 +0100
Organization: A noiseless patient Spider
Lines: 34
Message-ID: <tr3uas$2buff$1@dont-email.me>
References: <tqmcbf$vdl$1@gioia.aioe.org>
<d4a54200-1847-4fd2-c7e1-aa1189f63b78@vub.be>
<5bb2546d-8c16-262d-9336-5b41cbf992b5@tompassin.net>
<mailman.1613.1674675531.20444.python-list@python.org>
<tr1ces$1r96f$3@dont-email.me>
<9db7a5e1-076c-a9aa-aee4-e044e45decb7@tompassin.net>
<mailman.1683.1674870701.20444.python-list@python.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 28 Jan 2023 19:50:52 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="de874888c2d413a4e82c8a3694ab67ff";
logging-data="2488815"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+DDPNXbBhL6UpWZ9xJJo9BrkuHBM7vJu8="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.4.2
Cancel-Lock: sha1:sR5RCsTm3k/qdoLzma2phZ0t+TI=
Content-Language: en-US
In-Reply-To: <mailman.1683.1674870701.20444.python-list@python.org>
 by: Johannes Bauer - Sat, 28 Jan 2023 19:50 UTC

Am 28.01.23 um 02:51 schrieb Thomas Passin:

>> This is literally the version I described myself, except using triple
>> quotes. It only modifies the underlying problem, but doesn't solve it.
>
> Ok, so now we are in the territory of "Tell us what you are trying to
> accomplish". And part of that is why you cannot put some constraints on
> what your string fragments are.  The example I gave, copied out of your
> earlier message, worked and now you are springing triple quotes on us.

It works in this particular case, yes. Just like the example I gave in
my original case:

eval("f'" + s + "'")

"works" if there are no apostrophes used. And just like

eval("f\"" + s + "\"")

"works" if there are no quotation marks used.

I don't want to have to care about what quotation is used inside the
string, as long as it could successfully evaluate using the f-string
grammar.

> Stop with the rock management already and explain (briefly if possible)
> what you are up to.

I have a string. I want to evaluate it as if it were an f-string. I.e.,
there *are* obviously restrictions that apply (namely, the syntax and
semantics of f-strings), but that's it.

Best,
Johannes

Pages:12
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor