Re: Is there a better way? [combining f-string, thousands separator, right align]

2024-08-26 Thread dn via Python-list
On 26/08/24 23:00, Dan Sommers via Python-list wrote: On 2024-08-26 at 20:42:32 +1200, dn via Python-list wrote: and if we really want to go over-board: RIGHT_JUSTIFIED = ">" THOUSANDS_SEPARATOR = "," s_format = F"{RIGHT_JUSTIFIED}{S_FIELD_WIDTH}{THOUSANDS_SEPARATOR}" or (better) because r

Re: Is there a better way? [combining f-string, thousands separator, right align]

2024-08-26 Thread Dan Sommers via Python-list
On 2024-08-26 at 20:42:32 +1200, dn via Python-list wrote: > and if we really want to go over-board: > > >>> RIGHT_JUSTIFIED = ">" > >>> THOUSANDS_SEPARATOR = "," > >>> s_format = F"{RIGHT_JUSTIFIED}{S_FIELD_WIDTH}{THOUSANDS_SEPARATOR}" > > or (better) because right-justification is the default

Re: Is there a better way? [combining f-string, thousands separator, right align]

2024-08-26 Thread dn via Python-list
On 26/08/24 03:12, Gilmeh Serda via Python-list wrote: Subject explains it, or ask. This is a bloody mess: s = "123456789" # arrives as str f"{f'{int(s):,}': >20}" ' 123,456,789' With recent improvements to the expressions within F-strings, we can separate the string from the form

Re: Is there a better way? [combining f-string, thousands separator, right align]

2024-08-25 Thread Pierre Fortin via Python-list
On Sun, 25 Aug 2024 15:12:20 GMT Gilmeh Serda via Python-list wrote: >Subject explains it, or ask. > >This is a bloody mess: > s = "123456789" # arrives as str f"{f'{int(s):,}': >20}" >' 123,456,789' > f"{s:>20}" -- https://mail.python.org/mailman/listinfo/python-list

Re: Is there a better way? [combining f-string, thousands separator, right align]

2024-08-25 Thread MRAB via Python-list
On 2024-08-25 16:12, Gilmeh Serda via Python-list wrote: Subject explains it, or ask. This is a bloody mess: s = "123456789" # arrives as str f"{f'{int(s):,}': >20}" ' 123,456,789' You don't need to format twice; you can combine them: >>> s = "123456789" >>> f'{int(s): >20,}' '

Re: Is there a better way? [combining f-string, thousands separator, right align]

2024-08-25 Thread Pierre Fortin via Python-list
On Sun, 25 Aug 2024 15:12:20 GMT Gilmeh Serda via Python-list wrote: >Subject explains it, or ask. > >This is a bloody mess: > s = "123456789" # arrives as str f"{f'{int(s):,}': >20}" >' 123,456,789' > Oops.. forgot comma f"{int(s):>20,}" -- https://mail.python.org/mailman/li

Re: f-string error message

2023-08-30 Thread Random832 via Python-list
On Sun, Aug 27, 2023, at 17:19, Rob Cliffe via Python-list wrote: > I understand that this is an error: I'm telling the f-string to expect > an integer when in fact I'm giving it a Decimal. > And indeed f"{x:3}" gives ' 42' whether x is an int or a Decim

f-string error message

2023-08-30 Thread Rob Cliffe via Python-list
at string I understand that this is an error: I'm telling the f-string to expect an integer when in fact I'm giving it a Decimal. And indeed f"{x:3}" gives ' 42' whether x is an int or a Decimal. However, to my mind it is not the format string that is invalid, but th

Re: f-string syntax deficiency?

2023-06-06 Thread Mark Bourne via Python-list
Roel Schroeven wrote: Op 6/06/2023 om 16:08 schreef Chris Angelico: On Wed, 7 Jun 2023 at 00:06, Neal Becker wrote: > > The following f-string does not parse and gives syntax error on 3.11.3: > > f'thruput/{"user" if opt.return else "cell"} vs. elevation\

Re: f-string syntax deficiency?

2023-06-06 Thread Roel Schroeven via Python-list
Op 6/06/2023 om 16:48 schreef Chris Angelico via Python-list: On Wed, 7 Jun 2023 at 00:42, Roel Schroeven wrote: > (Recently there has been an effort to provide clearer and more useful > error messages; this seems to be a case where there is still room for > improvement: "SyntaxError: invalid sy

Re: f-string syntax deficiency?

2023-06-06 Thread Chris Angelico via Python-list
On Wed, 7 Jun 2023 at 00:42, Roel Schroeven wrote: > (Recently there has been an effort to provide clearer and more useful > error messages; this seems to be a case where there is still room for > improvement: "SyntaxError: invalid syntax" doesn't immediately remind me > of that fact that 'return'

Re: f-string syntax deficiency?

2023-06-06 Thread Roel Schroeven
Op 6/06/2023 om 16:41 schreef Roel Schroeven: 'return' being a keyowrd is definitely going to be the problem. *keyword -- "Don't Panic." -- Douglas Adams, The Hitchhiker's Guide to the Galaxy -- https://mail.python.org/mailman/listinfo/python-list

Re: f-string syntax deficiency?

2023-06-06 Thread Roel Schroeven
Op 6/06/2023 om 16:08 schreef Chris Angelico: On Wed, 7 Jun 2023 at 00:06, Neal Becker wrote: > > The following f-string does not parse and gives syntax error on 3.11.3: > > f'thruput/{"user" if opt.return else "cell"} vs. elevation\n' > > Howe

Re: f-string syntax deficiency?

2023-06-06 Thread Chris Angelico
On Wed, 7 Jun 2023 at 00:06, Neal Becker wrote: > > The following f-string does not parse and gives syntax error on 3.11.3: > > f'thruput/{"user" if opt.return else "cell"} vs. elevation\n' > > However this expression, which is similar does par

f-string syntax deficiency?

2023-06-06 Thread Neal Becker
The following f-string does not parse and gives syntax error on 3.11.3: f'thruput/{"user" if opt.return else "cell"} vs. elevation\n' However this expression, which is similar does parse correctly: f'thruput/{"user" if True else "c

Re: Evaluation of variable as f-string

2023-02-07 Thread Chris Angelico
On Wed, 8 Feb 2023 at 05:30, Stefan Ram wrote: > > Rob Cliffe writes: > >Does that mean that it is not possible to have a (built-in) function > >that would construct and return a dictionary of all available variables > >and their values? If it were possible, it could be useful, and there > >woul

Re: Evaluation of variable as f-string

2023-02-07 Thread Chris Angelico
On Wed, 8 Feb 2023 at 02:12, Rob Cliffe wrote: > > [re-sending this to both the list and to Chris, as a prior send to the > list only was bounced back] > On 31/01/2023 22:33, Chris Angelico wrote: > > > >> Thanks for clarifying. > >> Hm. So 'x' is neither in locals() nor in globals(). Which star

Re: Evaluation of variable as f-string

2023-02-07 Thread Rob Cliffe via Python-list
[re-sending this to both the list and to Chris, as a prior send to the list only was bounced back] On 31/01/2023 22:33, Chris Angelico wrote: Thanks for clarifying. Hm. So 'x' is neither in locals() nor in globals(). Which starts me wondering (to go off on a tangent): Should there be a nonlo

Re: Evaluation of variable as f-string

2023-01-31 Thread Chris Angelico
On Wed, 1 Feb 2023 at 09:14, Rob Cliffe via Python-list wrote: > With great respect, Chris, isn't it for the OP (or anyone else) to > decide - having been warned of the various drawbacks and limitations - > to decide if it's a terrible idea *for him*? He's entitled to decide > that it's just what

Re: Evaluation of variable as f-string

2023-01-31 Thread Rob Cliffe via Python-list
. 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 su

Re: Evaluation of variable as f-string

2023-01-29 Thread Peter J. Holzer
a generic Python mechanism that > allows for exactly what f-strings do: execute arbitrary Python snippets of > code That exists. Use eval (or exec). > and format them in one go. Include an f-string in the code you eval. > In other words, I want to be able to do things like that, given a

Re: Evaluation of variable as f-string

2023-01-29 Thread Thomas Passin
On 1/29/2023 6:09 AM, Christian Gollwitzer wrote: Am 28.01.23 um 02:56 schrieb Thomas Passin: 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 angril

Re: Evaluation of variable as f-string

2023-01-29 Thread Johannes Bauer
exactly what f-strings do: execute arbitrary Python snippets of code and format them in one go. In other words, I want to be able to do things like that, given an *arbitrary* dictionary x and a string s (which has the only restriction that its content needs to be vald f-string gramma

Re: Evaluation of variable as f-string

2023-01-29 Thread Christian Gollwitzer
Am 28.01.23 um 02:56 schrieb Thomas Passin: 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:

Re: Evaluation of variable as f-string

2023-01-29 Thread Johannes Bauer
I'm trying to give you the favor of honest feedback, which is that you sound like an utter bully. If you don't care, that is totally fine by me. They're not different things, because what you asked for is NOT POSSIBLE without the caveats that I gave. It is *fundamentally not possi

To gateway or not to gateway a specific person (was: Evaluation of variable as f-string)

2023-01-29 Thread Peter J. Holzer
On 2023-01-29 15:47:47 +1100, Chris Angelico wrote: > On Sun, 29 Jan 2023 at 14:36, Stefan Ram wrote: > > (This message was written for Usenet. If you read it in a > > mailing list or the Web, it has been stolen from Usenet.) > > I'm curious as to the copyright protections available to you, b

Re: Evaluation of variable as f-string

2023-01-28 Thread Thomas Passin
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 *ar

Re: Evaluation of variable as f-string

2023-01-28 Thread Chris Angelico
On Sun, 29 Jan 2023 at 14:36, Stefan Ram wrote: > > Johannes Bauer writes: > >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'

Re: Evaluation of variable as f-string

2023-01-28 Thread Chris Angelico
hat your question is fine, it's awesome, and yes, wouldn't it be nice if magic were a thing. > Exactly. This is precisely what I want to avoid. Essentially, proper > quotation of such a string requires to write a fully fledged f-string > parser, in which case the whole problem solve

Re: Evaluation of variable as f-string

2023-01-28 Thread Chris Angelico
On Sun, 29 Jan 2023 at 11:53, Johannes Bauer wrote: > 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. > Not possible. An f-string can contain other f-strings, and it is entirel

Re: Evaluation of variable as f-string

2023-01-28 Thread Johannes Bauer
Am 27.01.23 um 23:10 schrieb Christian Gollwitzer: 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

Re: Evaluation of variable as f-string

2023-01-28 Thread Johannes Bauer
Here, solve it for this string: eval_me = ' f"""{f\'\'\'{f"{f\'{1+2}\'}"}\'\'\'}""" ' F-strings can be nested, remember. Exactly. This is precisely what I want to avoid. Essentially, proper quotation of such a st

Re: Evaluation of variable as f-string

2023-01-28 Thread Johannes Bauer
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

Re: Evaluation of variable as f-string

2023-01-28 Thread Peter J. Holzer
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}

Re: Evaluation of variable as f-string

2023-01-28 Thread Peter J. Holzer
> 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 a

Re: Evaluation of variable as f-string

2023-01-28 Thread Peter J. Holzer
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 "", line 1, in

RE: Evaluation of variable as f-string

2023-01-27 Thread avi.e.gross
oking 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 tha

Re: Evaluation of variable as f-string

2023-01-27 Thread Thomas Passin
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.

Re: Evaluation of variable as f-string

2023-01-27 Thread Thomas Passin
g" 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 = '""&

Re: Evaluation of variable as f-string

2023-01-27 Thread Christian Gollwitzer
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):

Re: Evaluation of variable as f-string

2023-01-27 Thread Johannes Bauer
hat "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 q

Re: Evaluation of variable as f-string

2023-01-27 Thread Johannes Bauer
Am 23.01.23 um 17:43 schrieb Stefan Ram: Johannes Bauer 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 exampl

Re: Evaluation of variable as f-string

2023-01-27 Thread Johannes Bauer
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/question

Re: Evaluation of variable as f-string

2023-01-27 Thread Johannes Bauer
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 va

Re: Evaluation of variable as f-string

2023-01-27 Thread Thomas Passin
rote: 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 a

Re: Evaluation of variable as f-string

2023-01-27 Thread Chris Angelico
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

Re: Evaluation of variable as f-string

2023-01-27 Thread Rob Cliffe via Python-list
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

Re: Evaluation of variable as f-string

2023-01-27 Thread Chris Angelico
ris, 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

Re: Evaluation of variable as f-string

2023-01-27 Thread Rob Cliffe via Python-list
On 23/01/2023 18:02, Chris Angelico wrote: On Tue, 24 Jan 2023 at 04:56, Johannes Bauer 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

Re: Evaluation of variable as f-string

2023-01-27 Thread Rob Cliffe via Python-list
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""

Re: Evaluation of variable as f-string

2023-01-25 Thread Thomas Passin
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

Re: Evaluation of variable as f-string

2023-01-25 Thread Antoon Pardon
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 p

Re: Evaluation of variable as f-string

2023-01-23 Thread Chris Angelico
On Tue, 24 Jan 2023 at 04:56, Johannes Bauer 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 be

Evaluation of variable as f-string

2023-01-23 Thread 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 cons

Re: F-string usage in a print()

2022-05-24 Thread Cameron Simpson
On 25May2022 00:13, Kevin M. Wilson wrote: >Cameron, I have a misunderstanding here, the 'f-string' is used when >the str() is not...isn't it! No, f-strings (format strings) are just a convenient way to embed values in a string. The result is a string. In days of yore

Re: F-string usage in a print()

2022-05-24 Thread Cameron Simpson
> # future_value += monthly_interest_amount > # display the result > print(f"Year = ", years + f"Future value = \n", future_value) > >When joining a string with a number, use an f-string otherwise, code a >str() because a implicit convert of an int to str causes a >

Re: F-string usage in a print()

2022-05-24 Thread Mats Wichmann
thly_interest_rate ># future_value += monthly_interest_amount ># display the result >print(f"Year = ", years + f"Future value = \n", future_value)When joining > a string with a number, use an f-string otherwise, code a str() because a > implicit convert of an int to

Re: F-string usage in a print()

2022-05-24 Thread Paul Bryan
;    future_value = round(future_value, 2) >    # monthly_interest_amount = future_value * monthly_interest_rate >    # future_value += monthly_interest_amount >    # display the result >    print(f"Year = ", years + f"Future value = \n", future_value)When > joining a s

Re: F-string usage in a print()

2022-05-24 Thread MRAB
+= monthly_interest_amount # display the result print(f"Year = ", years + f"Future value = \n", future_value)When joining a string with a number, use an f-string otherwise, code a str() because a implicit convert of an int to str causes a TypeError!Well...WTF! Am I no

F-string usage in a print()

2022-05-24 Thread Kevin M. Wilson via Python-list
;Year = ", years + f"Future value = \n", future_value)When joining a string with a number, use an f-string otherwise, code a str() because a implicit convert of an int to str causes a TypeError!Well...WTF! Am I not using the f-string function correctly...in the above line of cod

Re: Unpacking lists in a f string

2022-02-09 Thread Kirill Ratkin
Hi. Try this: f"foo {','.join([f'{a} {b}' for a,b in list(zip(l1,l2))])} bar" 09.02.2022 21:13, Paulo da Silva пишет: Às 02:17 de 09/02/22, Paulo da Silva escreveu: Hi! Let's say I have two lists of equal length but with a variable number of elements. For ex.: l1=['a','b','c'] l2=['j','k'

Re: Unpacking lists in a f string

2022-02-09 Thread Paulo da Silva
Às 02:17 de 09/02/22, Paulo da Silva escreveu: Hi! Let's say I have two lists of equal length but with a variable number of elements. For ex.: l1=['a','b','c'] l2=['j','k','l'] I want to build a string like this "foo a j, b k, c l bar" Is it possible to achieve this with f strings or any ot

Re: Unpacking lists in a f string

2022-02-09 Thread David Lowry-Duda
l list of things that want to be done (and natural ways to perform them) 1. pair items in the lists (using list comprehension), 2. format the pairs (using an f-string), 3. separate the pairs with commas (using join), and 4. incorporate surrounding "foo" "bar" text. (using an f-st

Re: Unpacking lists in a f string

2022-02-09 Thread Python
Paulo da Silva wrote: Hi! Let's say I have two lists of equal length but with a variable number of elements. For ex.: l1=['a','b','c'] l2=['j','k','l'] I want to build a string like this "foo a j, b k, c l bar" Is it possible to achieve this with f strings or any other simple/efficient way

Unpacking lists in a f string

2022-02-09 Thread Paulo da Silva
Hi! Let's say I have two lists of equal length but with a variable number of elements. For ex.: l1=['a','b','c'] l2=['j','k','l'] I want to build a string like this "foo a j, b k, c l bar" Is it possible to achieve this with f strings or any other simple/efficient way? Thanks for any help

Re: f-string not referenced in library

2019-02-20 Thread Calvin Spealman
more can be done to guide them to the right place. On Wed, Feb 20, 2019 at 8:34 AM Caridad Díaz Rivero < caridad.diaz.riv...@gmail.com> wrote: > Hi, > I don´t have too much experience using the official python documentation, > and I was looking about f-string and I didn´t find an

f-string not referenced in library

2019-02-20 Thread Caridad Díaz Rivero
Hi, I don´t have too much experience using the official python documentation, and I was looking about f-string and I didn´t find anythings in /3/library/string.html <https://docs.python.org/3/library/string.html> and I find it in 3/reference/lexical_analysis.html#f-strings

Re: f-string anomaly

2018-05-14 Thread Lele Gaifax
Ken Kundert writes: > Lele, > I am using Python3.6. d has to be an object of mydict. My bad, sorry, I completely missed the premise :-|. ciao, lele. -- nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia. l...@m

Re: f-string anomaly

2018-05-14 Thread Ken Kundert
, template) return ', '.join(template.format(v, k=k, v=v) for k, v in self.items()) d = mydict(bob='239-8402', ted='371-8567', carol='891-5810', alice='552-2219') print('Using format():') print('Email: {0:{{k}}: {{v}}}&#x

Re: f-string anomaly

2018-05-14 Thread MRAB
On 2018-05-14 20:24, Lele Gaifax wrote: Ken Kundert writes: Lele, I'm afraid I was unclear. The ... in the code snippet was intended to imply that these lines were appended to the end of the original code, where d was defined. Ok, but then I get a different behaviour: Python 3.6.5

Re: f-string anomaly

2018-05-14 Thread Lele Gaifax
Ken Kundert writes: > Lele, > I'm afraid I was unclear. The ... in the code snippet was intended > to imply that these lines were appended to the end of the original code, > where d was defined. Ok, but then I get a different behaviour: Python 3.6.5 (default, May 11 2018, 13:30:17)

Re: f-string anomaly

2018-05-14 Thread Ken Kundert
Lele, I'm afraid I was unclear. The ... in the code snippet was intended to imply that these lines were appended to the end of the original code, where d was defined. -Ken On 05/14/2018 12:30 AM, Lele Gaifax wrote: > Ken Kundert writes: > >> I tried adding k and v to the local namespace: >>

Re: f-string anomaly

2018-05-14 Thread Thomas Jollans
  print('Template:', template) >> return ', '.join(template.format(v, k=k, v=v) for k, v in >> self.items()) >> >> >> d = mydict(bob='239-8402', ted='371-8567', carol='891-5810', >> alice=

Re: f-string anomaly

2018-05-14 Thread Lele Gaifax
Ken Kundert writes: > I tried adding k and v to the local namespace: > > ... > k = 6 > v = 9 > print(f'Email: {d:{{k}} {{v}}}') > > I still got: > > NameError: name 'k' is not defined This is not what I get: Python 3.6.5 (default, May 11 2018, 13:30:17) [GCC 7.3.0]

Re: f-string anomaly

2018-05-13 Thread Ken Kundert
specification in the f-string documentation does not allow, and so it should be a syntax error. But it does not generate a syntax error. So the question becomes, how does it interpret {{k}} and {{v}} in the format spec. I was expecting it to treat the double braces as escaped braces, and that is what the

Re: f-string anomaly

2018-05-13 Thread Terry Reedy
'239-8402', ted='371-8567', carol='891-5810', alice='552-2219') print('Using format():') print('Email: {0:{{k}}: {{v}}}'.format(d)) print() print('Using f-string:') print(f'Email: {d:{{k}} {{v}}}&#x

f-string anomaly

2018-05-13 Thread Ken Kundert
, template) return ', '.join(template.format(v, k=k, v=v) for k, v in self.items()) d = mydict(bob='239-8402', ted='371-8567', carol='891-5810', alice='552-2219') print('Using format():') print('Email: {0:{

f-string anomaly

2018-05-13 Thread Ken Kundert
, template) return ', '.join(template.format(v, k=k, v=v) for k, v in self.items()) d = mydict(bob='239-8402', ted='371-8567', carol='891-5810', alice='552-2219') print('Using format():') print('Email: {0:{{k}}: {{v}}}

Re: Python 3.6: How to expand f-string literals read from a file vs inline statement

2018-03-23 Thread Steven D'Aprano
On Fri, 23 Mar 2018 10:39:05 -0600, Malcolm Greene wrote: >> Perhaps it doesn't need to be said, but just to be sure: don't use eval >> if you don't trust the people writing the configuration file. They can >> do nearly unlimited damage to your environment.  They are writing code >> that you are

Re: Python 3.6: How to expand f-string literals read from a file vs inline statement

2018-03-23 Thread Ned Batchelder
On 3/23/18 12:39 PM, Malcolm Greene wrote: Perhaps it doesn't need to be said, but just to be sure: don't use eval if you don't trust the people writing the configuration file. They can do nearly unlimited damage to your environment.  They are writing code that you are running. Of course! Sc

Re: Python 3.6: How to expand f-string literals read from a file vs inline statement

2018-03-23 Thread Malcolm Greene
> Perhaps it doesn't need to be said, but just to be sure: don't use eval if > you don't trust the people writing the configuration file. They can do nearly > unlimited damage to your environment.  They are writing code that you are > running. Of course! Script and config file are running in a

Re: Python 3.6: How to expand f-string literals read from a file vs inline statement

2018-03-23 Thread Ned Batchelder
On 3/23/18 4:30 AM, Malcolm Greene wrote: Looking for advice on how to expand f-string literal strings whose values I'm reading from a configuration file vs hard coding into my script as statements. I'm using f-strings as a very simple template language. I'm currently usin

Re: Python 3.6: How to expand f-string literals read from a file vs inline statement

2018-03-23 Thread Chris Angelico
On Fri, Mar 23, 2018 at 7:37 PM, Malcolm Greene wrote: > My original post reformatted for text mode: > > Looking for advice on how to expand f-string literal strings whose values I'm > reading from a configuration file vs hard coding into > my script as statements. I'm

Re: Python 3.6: How to expand f-string literals read from a file vs inline statement

2018-03-23 Thread Malcolm Greene
My original post reformatted for text mode: Looking for advice on how to expand f-string literal strings whose values I'm reading from a configuration file vs hard coding into my script as statements. I'm using f-strings as a very simple template language. I'm currently usin

Python 3.6: How to expand f-string literals read from a file vs inline statement

2018-03-23 Thread Malcolm Greene
Looking for advice on how to expand f-string literal strings whose values I'm reading from a configuration file vs hard coding into my script as statements. I'm using f-strings as a very simple template language. I'm currently using the following technique to expand these f-stri

Re: f-string

2017-12-08 Thread Chris Angelico
On Tue, Dec 5, 2017 at 6:37 PM, Ned Batchelder wrote: > On 12/5/17 7:16 PM, Steve D'Aprano wrote: >> compile('f"{spam} {eggs}"', '', 'single') > > $ python3.6 > Python 3.6.3 (default, Octâ 4 2017, 06:03:25) > [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.37)] on darwin > Typ

Re: f-string

2017-12-08 Thread Steve D'Aprano
On Wed, 6 Dec 2017 11:54 am, John Pote wrote: [...] > Ran above test file and got, > >>python36 compiletest.py > at 0x02120E40, file "", line 1> > > > SPAM scrambled Thanks everyone, that's what I wanted to see. -- Steve â £Cheer up,â Ø they said, â £things could be worse.â Ø So I ch

Re: f-string

2017-12-08 Thread Chris Angelico
m and eggs as. >> >> ChrisA >> *ducking for cover* > > Possibly duck eggs :-) > > > I was thinking of responding to your comment on Python-Ideas that said there > is no way to represent an unevaluated f-string in Python, but then I decided > that even for me t

Re: f-string

2017-12-08 Thread Steve D'Aprano
;>> What does compile('f"{spam} {eggs}"', '', 'single') return? [...] > I think Steve just wanted to see what we'd all define spam and eggs as. > > ChrisA > *ducking for cover* Possibly duck eggs :-) I was thinking of responding t

Re: f-string

2017-12-08 Thread John Pote
On 06/12/2017 00:16, Steve D'Aprano wrote: > Anyone got a handy copy of Python 3.6 available to test something for me? > > What does compile('f"{spam} {eggs}"', '', 'single') return? > > What does eval()'ing the above compiled object do? If necessary, you may have > to define spam and eggs first.

Re: f-string

2017-12-08 Thread Ned Batchelder
On 12/5/17 7:16 PM, Steve D'Aprano wrote: > compile('f"{spam} {eggs}"', '', 'single') $ python3.6 Python 3.6.3 (default, Octâ 4 2017, 06:03:25) [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.37)] on darwin Type "help", "copyright", "credits" or "license" for more information.

Re: f-string

2017-12-08 Thread MRAB
On 2017-12-06 00:16, Steve D'Aprano wrote: > Anyone got a handy copy of Python 3.6 available to test something for me? > > What does compile('f"{spam} {eggs}"', '', 'single') return? > > What does eval()'ing the above compiled object do? If necessary, you may have > to define spam and eggs first. >

Re: f-string

2017-12-08 Thread Chris Angelico
On Wed, Dec 6, 2017 at 11:54 AM, John Pote wrote: > > On 06/12/2017 00:16, Steve D'Aprano wrote: >> >> Anyone got a handy copy of Python 3.6 available to test something for me? >> >> What does compile('f"{spam} {eggs}"', '', 'single') return? >> >> What does eval()'ing the above compiled object do

Re: f-string

2017-12-08 Thread Chris Angelico
On Wed, Dec 6, 2017 at 11:16 AM, Steve D'Aprano wrote: > Anyone got a handy copy of Python 3.6 available to test something for me? > > What does compile('f"{spam} {eggs}"', '', 'single') return? > > What does eval()'ing the above compiled object do? If necessary, you may have > to define spam and

f-string

2017-12-08 Thread Steve D'Aprano
Anyone got a handy copy of Python 3.6 available to test something for me? What does compile('f"{spam} {eggs}"', '', 'single') return? What does eval()'ing the above compiled object do? If necessary, you may have to define spam and eggs first. Thanks in advance. -- Steve â £Cheer up,â Ø they s

Re: f-string

2017-12-06 Thread Chris Angelico
m and eggs as. >> >> ChrisA >> *ducking for cover* > > Possibly duck eggs :-) > > > I was thinking of responding to your comment on Python-Ideas that said there > is no way to represent an unevaluated f-string in Python, but then I decided > that even for me

Re: f-string

2017-12-06 Thread Steve D'Aprano
;>> What does compile('f"{spam} {eggs}"', '', 'single') return? [...] > I think Steve just wanted to see what we'd all define spam and eggs as. > > ChrisA > *ducking for cover* Possibly duck eggs :-) I was thinking of responding

Re: f-string

2017-12-05 Thread Steve D'Aprano
On Wed, 6 Dec 2017 11:54 am, John Pote wrote: [...] > Ran above test file and got, > >>python36 compiletest.py > at 0x02120E40, file "", line 1> > > > SPAM scrambled Thanks everyone, that's what I wanted to see. -- Steve “Cheer up,” they said, “things could be worse.” So I cheered

Re: f-string

2017-12-05 Thread Chris Angelico
On Wed, Dec 6, 2017 at 11:54 AM, John Pote wrote: > > On 06/12/2017 00:16, Steve D'Aprano wrote: >> >> Anyone got a handy copy of Python 3.6 available to test something for me? >> >> What does compile('f"{spam} {eggs}"', '', 'single') return? >> >> What does eval()'ing the above compiled object do

Re: f-string

2017-12-05 Thread John Pote
On 06/12/2017 00:16, Steve D'Aprano wrote: Anyone got a handy copy of Python 3.6 available to test something for me? What does compile('f"{spam} {eggs}"', '', 'single') return? What does eval()'ing the above compiled object do? If necessary, you may have to define spam and eggs first. Thanks

  1   2   >