Re: How to make argparse accept "-4^2+5.3*abs(-2-1)/2" string argument?
On 2023-01-29 at 16:51:20 +1100, Cameron Simpson wrote: > They're unrelated. As others have mentioned, "--" is _extremely_ common; > almost _all_ UNIX command like programmes which handle -* style options > honour the "--" convention. _argparse_ itself honours that convention, as > does getopt etc. And why do UNIX programs behave this way? Because POSIX says they should: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html -- https://mail.python.org/mailman/listinfo/python-list
RE: How to make argparse accept "-4^2+5.3*abs(-2-1)/2" string argument?
Although today you could say POSIX is the reason for many things including the use of "--" I hesitate to mention I and many others used that convention long before as a standard part of many UNIX utilities. Like many other such things, you build things first and by the time you standardize, ... -Original Message- From: Python-list On Behalf Of 2qdxy4rzwzuui...@potatochowder.com Sent: Sunday, January 29, 2023 7:12 AM To: python-list@python.org Subject: Re: How to make argparse accept "-4^2+5.3*abs(-2-1)/2" string argument? On 2023-01-29 at 16:51:20 +1100, Cameron Simpson wrote: > They're unrelated. As others have mentioned, "--" is _extremely_ > common; almost _all_ UNIX command like programmes which handle -* > style options honour the "--" convention. _argparse_ itself honours > that convention, as does getopt etc. And why do UNIX programs behave this way? Because POSIX says they should: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
RE: How to make argparse accept "-4^2+5.3*abs(-2-1)/2" string argument?
Cameron, You are technically correct but perhaps off the mark. Yes, a python program only sees what is handed to it by some shell if invoked a certain way. The issue here is what you tell people using your program about what they need to type to get it to work. That means if their shell is going to make changes in what they typed, they need to know how to avoid unintended changes. As one example not mentioned, whitespace disappears if not somehow protected as in quotes. What the OP is being told is that their Python program only controls what is fed to it. A user needs to know enough to avoid doing silly things like provide an unquoted string containing reserved symbols like a pipe symbol or odd things may happen and their program may not even be called. So the documentation of how to use the program may need to spell some things out alongside suggesting use of "--" ... -Original Message- From: Python-list On Behalf Of Cameron Simpson Sent: Sunday, January 29, 2023 12:51 AM To: python-list@python.org Subject: Re: How to make argparse accept "-4^2+5.3*abs(-2-1)/2" string argument? On 28Jan2023 18:55, Jach Feng wrote: >Mark Bourne 在 2023年1月28日 星期六晚上10:00:01 [UTC+8] 的信中寫道: >> I notice you explain the need to enclose the equation in quotes if it >> contains spaces. That's not even a feature of your application, but >> of the shell used to call it. So why so much objection to explaining >> the need for "--"? >> >> Depending on the shell, there are other cases where quoting might be >> needed, e.g. if the equation includes a "*" or "?" and happens to >> look like a pattern matching files in the current directory (most >> shells I've used pass the pattern unchanged if it doesn't match any >> files). In bash, if a "$" is used I'd need to enclose that in 'single quotes' >> (can't even use "double quotes" for that one). You can't really >> expect to document all that sort of thing, because it depends on >> which shell the user happens to run your application from - you just >> have to trust the user to know or learn how to use their shell. > >Thank you for detail explanation of the role the shell is involved in this >problem. I'm very appreciated! The shell has basicly _nothing_ to do with your problems. By the time you've got sys.argv in your Python programme you will have no idea whether quotes were used with an argument. (UNIX/POSIX, not Windows, where things are ... more complex.) This means you don't know if the use typed: -4.5 or "-4.5" You'll just get a string '4.5' in your Python programme both ways. All the quotes in the shell do is delimit what things should be kept together as a single argument versus several, or where variables should be interpolated when computing arguments etc. It's just _shell_ punctuation and the invoked programme doesn't see it. >It seems that a CLI app may become very complex when dealing with different >kind of shell, and may not be possible to solve its problem. It doesn't matter what shell is used. The just controls what punctuation the end user may need to use to invoke your programme. You programme doesn't need to care (and can't because it doesn't get the quotes etc, only their result). >> So why so much objection to explaining the need for "--"? >Because of using " to enclose a space separated string is a common >convention, and adding a "--" is not:-) They're unrelated. As others have mentioned, "--" is _extremely_ common; almost _all_ UNIX command like programmes which handle -* style options honour the "--" convention. _argparse_ itself honours that convention, as does getopt etc. The "--" convention has nothing to do with the shell. Cheers, Cameron Simpson -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: How to make argparse accept "-4^2+5.3*abs(-2-1)/2" string argument?
On 2023-01-29 07:59:21 -0500, avi.e.gr...@gmail.com wrote: > Although today you could say POSIX is the reason for many things including > the use of "--" I hesitate to mention I and many others used that convention > long before as a standard part of many UNIX utilities. I somewhat doubt that you are remembering correctly. The POSIX standard was published in 1988. I was starting to use Unix shortly before that and I don't think there was really a common way to indicate the end of the options. Some utilities used "-", some "--", some just stopped at the first argument which didn't look like an option ... The convergence to "--" happened later. My guess is that GNU getopt and the popularity of the GNU utilities had a lot to do with it. > Like many other such things, you build things first and by the time > you standardize, ... Right. But in this case lots of people were building different things and then *one* of them was standardized. Truth to be told, I don't know when that happened. It might have been in the original 1988 edition of POSIX, but I don't think so. I think it was in a later (possibly much later) edition, wennn "--" was already really common. But POSIX did lead the way in some cases and it might have been the case here, too. hp -- _ | Peter J. Holzer| Story must make more sense than reality. |_|_) || | | | h...@hjp.at |-- Charles Stross, "Creative writing __/ | http://www.hjp.at/ | challenge!" signature.asc Description: PGP signature -- https://mail.python.org/mailman/listinfo/python-list
Re: How to make argparse accept "-4^2+5.3*abs(-2-1)/2" string argument?
On 2023-01-27 21:03:39 -0800, Jach Feng wrote: > I have to admit that I don't know the background upon which the > argparse was built. The good side is that I don't carry its historical > knowledge ( or burden?), that's why I can use it in a new way I don't see what's new about that. > which may make someone feel uneasy. It seems that the person who feels most uneasy about it is yourself because it doesn't work like you want it to. > The reason I am still keep on using argparse on this "one positional > argument only" CLI app is that I can't see what advantage I can get > when writing code to handling sys.argv directly for the following two > situations, It would almost certainly be shorter and easier to understand, especially since you are *also* handling sys.argv directly in your code to prevent argparse to work as designed. But since you are probably the only person who will have to read your code in the future that's not our problem. hp -- _ | Peter J. Holzer| Story must make more sense than reality. |_|_) || | | | h...@hjp.at |-- Charles Stross, "Creative writing __/ | http://www.hjp.at/ | challenge!" signature.asc Description: PGP signature -- https://mail.python.org/mailman/listinfo/python-list
To gateway or not to gateway a specific person (was: Evaluation of variable as f-string)
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, but if > you're going to threaten python-list's owners with legal action for > daring to rebroadcast a public post, I would have to recommend that > you get promptly banned from the list in order to reduce liability. Stefan had been banned for years because of this issue. In mid-2021 (wow, that long ago? I thought that was a lot more recently) his posts started to appear again. I don't know if this was a concious decision of the moderators, a technical error or whether Stefan changed something which caused him to escape the filter. I don't think it matters. If someone posts to a Usenet group with the full knowledge that it is gatewayed to a mailing list it is unreasonable to expect the moderators of the list to jump through hoops to comply with wishes hidden away in a non-standard header. > What's so bad about mailing lists that you don't want your messages to > be seen on them? He's Stefan Ram :-). hp -- _ | Peter J. Holzer| Story must make more sense than reality. |_|_) || | | | h...@hjp.at |-- Charles Stross, "Creative writing __/ | http://www.hjp.at/ | challenge!" signature.asc Description: PGP signature -- https://mail.python.org/mailman/listinfo/python-list
Re: Usenet vs. Mailing-list
On 2023-01-29 02:09:28 -, Jon Ribbens via Python-list wrote: > I'm not aware of any significant period in the last twenty-one years > that [the gateway] > hasn't been working. Although sometimes it does feel like it isn't, in > that I reply to a post with an answer and then several other people > reply significantly later with the same answer, as if my one had never > existed... That's just because people don't read before they post. Happens in any usenet group or mailing list (and probably in web forums, too; but I don't really use those). I have to admit that I'm sometimes guilty of this behaviour, too. hp -- _ | Peter J. Holzer| Story must make more sense than reality. |_|_) || | | | h...@hjp.at |-- Charles Stross, "Creative writing __/ | http://www.hjp.at/ | challenge!" signature.asc Description: PGP signature -- https://mail.python.org/mailman/listinfo/python-list
Re: Evaluation of variable as f-string
Am 29.01.23 um 02:09 schrieb Chris Angelico: The exact same points have already been made, but not listened to. Sometimes, forceful language is required in order to get people to listen. An arrogant bully's rationale. Personally, I'm fine with it. I've been to Usenet for a long time, in which this way of "educating" people was considered normal. But I do think it creates a deterring, toxic environment and reflects back to you as a person negatively. Arrogant bully? Or someone who has tried *multiple times* to explain to you that what you're asking for is IMPOSSIBLE, and you need to ask a better question if you want a better answer? In literally your first answer you resorted to aggressive language and implied that what I asked wasn't what I actually wanted. It was. Also note that in your first answer you did not answer "sorry, this is not possible", which would have been completely sufficient as an answer. Instead you tried your best at guesswork, implying I didn't know what I was doing. So, yes, absolutely toxic behavior. I fully stand by that judgement of mine. I'll go a step further and again repeat that THIS sort of behavior is what gives open source forums a bad rep. There's always a Lennart Poettering, an Ulrich Drepper or maybe a Chris Angelico around who may have great technical skill but think they can treat people like shit. If that's "bullying", then fine, ban me for bullying, and go find somewhere else where you'll be coddled and told that your question is fine, it's awesome, and yes, wouldn't it be nice if magic were a thing. LOL, "ban you"? What the heck are you talking about, my friend? I don't need to be coddled by you. 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 possible* to "evaluate a string as if it were an f-string", other than by wrapping it in an f-string and evaluating it - with the consequences of that. Yeah that sucks, unfortunately. But I'll live. In other words, if there were a magic function: evalfstring(s, x = x) That would have been the ideal answer. There does not seem to be one, however. So I'm back to silly workarounds. Right. Exactly. Now if you'd asked for what you REALLY need, maybe there'd be a solution involving format_map, but no, you're so utterly intransigent that you cannot adjust your question to fit reality. Does format_map do exactly what f-strings can do? Can I pass arbitrary functions and Python expressions insode a format_map? No? Of course not. Then it does not answer the question. If that makes me a bad guy, then fine. I'll be the bad guy. Awww, it's adorable how you're trying to frame yourself as the victim. I'll be here if you need a hug, buddy. But you're not going to change the laws of physics. Yeah we'll have to disagree about the fact that it's the "laws of physics" preventing a specific implementation of a Python function. Cheers, Johannes -- https://mail.python.org/mailman/listinfo/python-list
Re: Usenet vs. Mailing-list
On Saturday, January 28, 2023 at 10:02:57 PM UTC-5, Ben Bacarisse wrote: > Jon Ribbens writes: > > > On 2023-01-29, Ben Bacarisse wrote: > >> "Peter J. Holzer" writes: > >> > >>> On 2023-01-27 21:04:58 +, Ben Bacarisse wrote: > mut...@dastardlyhq.com writes: > > > Hi > > It looks like you posted this question via Usenet. comp.lang.python is > essentially dead as a Usenet group. It exists, and gets NNTP versions > of mail sent to the mailing list, but nothing posted to the group via > NNTP get send on the mailing list. > >>> > >>> This is wrong. I did get Muttley's any your postings via the > >>> mailing-list. > >> > >> Ah, OK. I thought that was the case but I am obviously wrong. Has > >> there been a change, or have I been wrong for a long time!? > > > > I'm not aware of any significant period in the last twenty-one years > > that it hasn't been working. Although sometimes it does feel like it > > isn't, in that I reply to a post with an answer and then several > > other people reply significantly later with the same answer, as if > > my one had never existed... but whenever I check into it, my message > > has actually always made it to the list. > I have had the same experience and, as a result, I rarely post. Maybe > what I have to say is simply not interesting! > > -- > Ben. If I remember correctly, multiple regulars that use the mailing list mentioned that they "killfiled" posts originating from Google groups. This may contribute to such situations. -- https://mail.python.org/mailman/listinfo/python-list
Re: How to make argparse accept "-4^2+5.3*abs(-2-1)/2" string argument?
Jach Feng 在 2023年1月22日 星期日上午11:11:22 [UTC+8] 的信中寫道: > Fail on command line, > > e:\Works\Python>py infix2postfix.py "-4^2+5.3*abs(-2-1)/2" > usage: infix2postfix.py [-h] [infix] > infix2postfix.py: error: unrecognized arguments: -4^2+5.3*abs(-2-1)/2 > > Also fail in REPL, > > e:\Works\Python>py > Python 3.8.8 (tags/v3.8.8:024d805, Feb 19 2021, 13:08:11) [MSC v.1928 32 bit > (Intel)] on win32 > Type "help", "copyright", "credits" or "license" for more information. > >>> import argparse > >>> parser = argparse.ArgumentParser(description='Convert infix notation to > >>> postfix') > >>> parser.parse_args("-4^2+5.3*abs(-2-1)/2") > usage: [-h] > : error: unrecognized arguments: - 4 ^ 2 + 5 . 3 * a b s ( - 2 - 1 ) / 2 > > Just can't figure out where is wrong!? > > --Jach OK, I take a quick try to use argv directly. I write the code in a way even get rid of the -h option. import sys if len(sys.argv) == 1: infix = " " print("Usage: a math equation must follow!") else: infix = "".join(sys.argv[1:]) Simple enough, right? But unfortunately it didn't survive. The ^ symbol was lost! e:\Works\Python>py infix2postfix.py Usage: a math equation must follow! e:\Works\Python>py infix2postfix.py -4^2 +5.3 * abs(-2-1)/2 -42 5.3 -2 1 - abs * 2 / + Hmm... -- https://mail.python.org/mailman/listinfo/python-list
Re: Evaluation of variable as f-string
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: x = { "y": "z" } s = "-> {x['y']}" print(s.format(x = x)) Traceback (most recent call last): File "", line 1, in 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 You might want to reconsider why I could have possibly written this message Christian -- https://mail.python.org/mailman/listinfo/python-list
Re: How to make argparse accept "-4^2+5.3*abs(-2-1)/2" string argument?
Jach Feng wrote: Jach Feng 在 2023年1月22日 星期日上午11:11:22 [UTC+8] 的信中寫道: Fail on command line, e:\Works\Python>py infix2postfix.py "-4^2+5.3*abs(-2-1)/2" usage: infix2postfix.py [-h] [infix] infix2postfix.py: error: unrecognized arguments: -4^2+5.3*abs(-2-1)/2 Also fail in REPL, e:\Works\Python>py Python 3.8.8 (tags/v3.8.8:024d805, Feb 19 2021, 13:08:11) [MSC v.1928 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. import argparse parser = argparse.ArgumentParser(description='Convert infix notation to postfix') parser.parse_args("-4^2+5.3*abs(-2-1)/2") usage: [-h] : error: unrecognized arguments: - 4 ^ 2 + 5 . 3 * a b s ( - 2 - 1 ) / 2 Just can't figure out where is wrong!? --Jach OK, I take a quick try to use argv directly. I write the code in a way even get rid of the -h option. import sys if len(sys.argv) == 1: infix = " " print("Usage: a math equation must follow!") else: infix = "".join(sys.argv[1:]) Simple enough, right? But unfortunately it didn't survive. The ^ symbol was lost! e:\Works\Python>py infix2postfix.py Usage: a math equation must follow! e:\Works\Python>py infix2postfix.py -4^2 +5.3 * abs(-2-1)/2 -42 5.3 -2 1 - abs * 2 / + Hmm... I'm not certain, but I think that's a shell feature again. In Windows' command prompt, "^" can be used at the end of a line to indicate that the command continues on the next line. I'm not sure what happens if it's in the middle of a line (and not on Windows to be able to check), but it's quite possible that it just gets ignored. Enclosing your argument in quotes might help. Again, this is a feature of the shell, not your application, and other shells might behave differently. You probably don't want to go down the line of trying to document this kind of thing in your applications usage information, because it won't work like that for someone using e.g. the bash shell (which can be run on Windows). -- Mark. -- https://mail.python.org/mailman/listinfo/python-list
Re: Evaluation of variable as f-string
Am 29.01.23 um 05:27 schrieb Thomas Passin: Well, yes, we do see that. What we don't see is what you want to accomplish by doing it, and why you don't seem willing to accept some restrictions on the string fragments so that they will evaluate correctly. I'll have to accept the restrictions. That's a good enough answer for me, actually. I was just thinking that possibly there's something like (made-up code): x = { "foo": "bar" } fstr = string.fstring_compile(s) fstr.eval(x = x) Which I didn't know about. It would make sense to me, but possibly not enough of a usecase to make it into Python. The format() flavors do not IOW, perhaps there is a more practical way to accomplish what you want. Except that we don't know what that is. Well, I don't know. I pretty much want a generic Python mechanism that allows for 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 grammar): x = { "d": 12, "t": 12345, "dt": datetime.datetime, "td": datetime.timedelta } s = "{x['d']:09b} {'->' * (x['d'] // 3)} {(x['dt'](2000, 1, x['d']) + x['td'](120)).strftime('%y.%m.%d')} {'<-' * (x['d'] // 4)}" q = magic_function(s, x = x) and have "q" then be '01100 ->->->-> 00.05.11 <-<-<-' I believe the closest solution would be using a templating mechanism (like Mako), but that has slightly different syntax and doesn't do string formatting as nice as f-strings do. f-strings really are the perfect syntax for what I want to do. Cheers, Johannes -- https://mail.python.org/mailman/listinfo/python-list
Re: How to make argparse accept "-4^2+5.3*abs(-2-1)/2" string argument?
Jach Feng wrote: Thank you for detail explanation of the role the shell is involved in this problem. I'm very appreciated! It seems that a CLI app may become very complex when dealing with different kind of shell, and may not be possible to solve its problem. But the good thing in my app is that I need only to handle math equation:-) If you want to try to tell the user how to deal with their shell's requirements for quoting arguments, regardless of which shell they might be using, yes, that explanation would become very complicated. It doesn't affect the rest of the implementation of the application though - the user just needs to know how to use their shell to pass the arguments they want into the application. That's really something they should look up in their shell's documentation, rather than something your application should attempt to document. Since your application requires equations to be passed in, and they're quite likely to include characters handled specially by the shell (space, "*" and "^" have already come up in this thread, but there may be others), it may be worth highlighting that, but directing the user to consult the documentation for their shell rather than assuming a particular shell and attempting to cover all its features and limitations. So why so much objection to explaining the need for "--"? Because of using " to enclose a space separated string is a common convention, and adding a "--" is not:-) If you don't consider use of "--" to be a common convention, that seems even more reason to mention it in your application's documentation. Telling the user to use quotes around an argument containing spaces is nothing to do with your application, and might not even be applicable if they use a different shell to call your application. In most shells I've come across, there are also various other characters that need special handling (either quoting or escaping) - but exactly which characters again depends on the shell. Yet you seem quite happy to document that one particular case in your usage information. Using "--" is also a common convention (as I and others have mentioned), although perhaps not as common in Windows (where it's more common to use "/" rather than "-" for options anyway). But more to the point, it is a feature that is implemented under your application's control (if you don't want this feature, don't use argparse). Use of "--" is applicable regardless of which shell your user calls it from, and other applications might not use that convention even if called from the same shell, so it seems *more* in scope for your application to document than using quotes around spaces. -- Mark. -- https://mail.python.org/mailman/listinfo/python-list
Re: Usenet vs. Mailing-list
Igor Berger writes: > On Saturday, January 28, 2023 at 10:02:57 PM UTC-5, Ben Bacarisse wrote: >> Jon Ribbens writes: >> >> > On 2023-01-29, Ben Bacarisse wrote: >> >> "Peter J. Holzer" writes: >> >> >> >>> On 2023-01-27 21:04:58 +, Ben Bacarisse wrote: >> mut...@dastardlyhq.com writes: >> >> > Hi >> >> It looks like you posted this question via Usenet. comp.lang.python is >> essentially dead as a Usenet group. It exists, and gets NNTP versions >> of mail sent to the mailing list, but nothing posted to the group via >> NNTP get send on the mailing list. >> >>> >> >>> This is wrong. I did get Muttley's any your postings via the >> >>> mailing-list. >> >> >> >> Ah, OK. I thought that was the case but I am obviously wrong. Has >> >> there been a change, or have I been wrong for a long time!? >> > >> > I'm not aware of any significant period in the last twenty-one years >> > that it hasn't been working. Although sometimes it does feel like it >> > isn't, in that I reply to a post with an answer and then several >> > other people reply significantly later with the same answer, as if >> > my one had never existed... but whenever I check into it, my message >> > has actually always made it to the list. >> >> I have had the same experience and, as a result, I rarely post. Maybe >> what I have to say is simply not interesting! >> > > If I remember correctly, multiple regulars that use the mailing list > mentioned that they "killfiled" posts originating from Google groups. > This may contribute to such situations. Ah, that may explain most of my confusion. I know I stopped posting because there didn't seem to be much engagement, but maybe it was just many list members choosing not to see my posts rather than all list members not being able to see my posts. As I said, I did check to see if I could find a reply to a Usenet-originated post from a list member but my not being able to find one could just be down to many list members filtering Usenet posts. It's clearly not universal. -- Ben. -- https://mail.python.org/mailman/listinfo/python-list
Re: Usenet vs. Mailing-list
On 2023-01-29, Peter J. Holzer wrote: > On 2023-01-29 02:09:28 -, Jon Ribbens via Python-list wrote: >> I'm not aware of any significant period in the last twenty-one years >> that > [the gateway] >> hasn't been working. Although sometimes it does feel like it isn't, in >> that I reply to a post with an answer and then several other people >> reply significantly later with the same answer, as if my one had never >> existed... > > That's just because people don't read before they post. > > Happens in any usenet group or mailing list (and probably in web forums, > too; but I don't really use those). I have to admit that I'm sometimes > guilty of this behaviour, too. Well, let's just assume for a moment that I'm familiar with Usenet and with mailing lists ;-) This sort of replying-without-reading seems to happen on comp.lang.python/python-list more than usual. -- https://mail.python.org/mailman/listinfo/python-list
Re: How to make argparse accept "-4^2+5.3*abs(-2-1)/2" string argument?
This thread has run its course and seems to now be generating more heat than light. It is now closed (at least on the Python List side). Thank you everyone for your participation and understanding. -- ~Ethan~ Moderator -- https://mail.python.org/mailman/listinfo/python-list
Re: Evaluation of variable as f-string
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 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 "", line 1, in 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 You might want to reconsider why I could have possibly written this message I might .. or I might wish you had actually said what you wanted to convey ... -- https://mail.python.org/mailman/listinfo/python-list
Re: Evaluation of variable as f-string
On 2023-01-29 10:18:00 +0100, Johannes Bauer wrote: > Am 29.01.23 um 05:27 schrieb Thomas Passin: > > IOW, perhaps there is a more practical way to accomplish what you want. > > Except that we don't know what that is. > > Well, I don't know. I pretty much want 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 an > *arbitrary* dictionary x and a string s As I wrote before: An f-string isn't a string. It's a grammatical construct. So you want to execute Python code which is what eval and exec do. hp -- _ | Peter J. Holzer| Story must make more sense than reality. |_|_) || | | | h...@hjp.at |-- Charles Stross, "Creative writing __/ | http://www.hjp.at/ | challenge!" signature.asc Description: PGP signature -- https://mail.python.org/mailman/listinfo/python-list
Re: How to make argparse accept "-4^2+5.3*abs(-2-1)/2" string argument?
On 29Jan2023 07:12, 2qdxy4rzwzuui...@potatochowder.com <2qdxy4rzwzuui...@potatochowder.com> wrote: On 2023-01-29 at 16:51:20 +1100, Cameron Simpson wrote: They're unrelated. As others have mentioned, "--" is _extremely_ common; almost _all_ UNIX command like programmes which handle -* style options honour the "--" convention. _argparse_ itself honours that convention, as does getopt etc. And why do UNIX programs behave this way? Because POSIX says they should: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html Aye, but POSIX says they should because POSIX formalises a lot of existing practices so that people have a reference to let them _also_ use that practice in a consistent way. I used to lurk on the POSIX discussion list, and the length of the discussions around corner cases was quite wearing, even just to read. Cheers, Cameron Simpson -- https://mail.python.org/mailman/listinfo/python-list
Re: How to make argparse accept "-4^2+5.3*abs(-2-1)/2" string argument?
On 29Jan2023 08:10, avi.e.gr...@gmail.com wrote: You are technically correct but perhaps off the mark. Yes, a python program only sees what is handed to it by some shell if invoked a certain way. The issue here is what you tell people using your program about what they need to type to get it to work. That means if their shell is going to make changes in what they typed, they need to know how to avoid unintended changes. As one example not mentioned, whitespace disappears if not somehow protected as in quotes. Hmm, there is that. But the OP needs clarity on what happens in a shell and what happens in a programme once the shell has invoked it for a user. Cheers, Cameron Simpson -- https://mail.python.org/mailman/listinfo/python-list
Re: evaluation question
On 1/29/2023 4:15 PM, elvis-85...@notatla.org.uk wrote: On 2023-01-28, Louis Krupp wrote: On 1/27/2023 9:37 AM, mutt...@dastardlyhq.com wrote: eval("print(123)") 123 Does OP expect the text to come from the eval or from the print? x = print( [i for i in range(1, 10)] ) [1, 2, 3, 4, 5, 6, 7, 8, 9] x (nothing printed) Because print() returns nothing (i.e., the statement x is None is True). Other common constructs that return nothing are append(), sort(), and add(). It can be easy to forget this and write l2 = l1.sort() # l2 == None OTOH, you can (by slightly abusing the lambda) use this behavior to make a lambda expression print what it's receiving: >>> y = lambda x: print(f'Got {x}') or x**2 >>> z = y(3) Got 3 >>> z 9 >>> -- https://mail.python.org/mailman/listinfo/python-list