Re: How to make argparse accept "-4^2+5.3*abs(-2-1)/2" string argument?

2023-01-23 Thread Cameron Simpson

On 23Jan2023 17:58, Jach Feng  wrote:

parser.parse_args(['--', 'infix2postfix.py', '-4.3+5'])

usage: [-h] infix
: error: unrecognized arguments: -4.3+5


This error doesn't look like "-4.3+5 looks like an option" but instead 
"we don't expect any arguments after "infix".


Not personally a fan of argparse myself, but then I have my own 
elaborate command line framework which generally uses getopt for the 
option stuff.


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?

2023-01-23 Thread Jach Feng
Greg Ewing 在 2023年1月24日 星期二清晨7:33:43 [UTC+8] 的信中寫道:
> >> On 2023-01-22 at 18:19:13 -0800, 
> >> Jach Feng  wrote: 
> >>> 1) Modify the sys.argv by inserting an item '--' before parsing it, ie. 
> >>> sys.argv.insert(1, '--') 
> >>> args = parser.parse_args()
> If you do that, you'll never be able to have any actual options, so 
> using argparse seems like overkill. Just pull the argument out of 
> argv directly. 
> 
> -- 
> Greg
Any easy way to "pull the argument out of argv directly" before parse_args()?:-)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to make argparse accept "-4^2+5.3*abs(-2-1)/2" string argument?

2023-01-23 Thread Chris Angelico
On Tue, 24 Jan 2023 at 13:09, Jach Feng  wrote:
>
> Chris Angelico 在 2023年1月24日 星期二清晨5:00:27 [UTC+8] 的信中寫道:
> > On Tue, 24 Jan 2023 at 07:47, Cameron Simpson  wrote:
> > >
> > > But for Jach Feng: the "--" is really expected as something the user
> > > does when they invoke your programme, _explicitly_ saying that what
> > > follows from here is not an argument. So the user is expected to type:
> > >
> > > your_script -x -y -- "-4^2+5.3*abs(-2-1)/2"
> > >
> > > where there are -x and -y options, then end of options, then an
> > > argument, which would look like an option if there wasn't the "--"
> > > argument.
> > And if you DON'T expect the user to enter the "--", then why use
> > argparse? You can just check argv directly to get your arguments.
> >
> > This entire thread is a massive "how can I use X to do Y?" problem.
> >
> > ChrisA
> The '--' requirement makes its usage less instinctive, and handling argv 
> directly makes me loss the benefit of using '-h':-)

if "-h" in sys.argv: usage()
else: do_stuff_with(sys.argv[1:])

What is argparse really doing for you?

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


Re: How to make argparse accept "-4^2+5.3*abs(-2-1)/2" string argument?

2023-01-23 Thread Jach Feng
Chris Angelico 在 2023年1月24日 星期二清晨5:00:27 [UTC+8] 的信中寫道:
> On Tue, 24 Jan 2023 at 07:47, Cameron Simpson  wrote: 
> > 
> > But for Jach Feng: the "--" is really expected as something the user 
> > does when they invoke your programme, _explicitly_ saying that what 
> > follows from here is not an argument. So the user is expected to type: 
> > 
> > your_script -x -y -- "-4^2+5.3*abs(-2-1)/2" 
> > 
> > where there are -x and -y options, then end of options, then an 
> > argument, which would look like an option if there wasn't the "--" 
> > argument.
> And if you DON'T expect the user to enter the "--", then why use 
> argparse? You can just check argv directly to get your arguments. 
> 
> This entire thread is a massive "how can I use X to do Y?" problem. 
> 
> ChrisA
The '--' requirement makes its usage less instinctive, and handling argv 
directly makes me loss the benefit of using '-h':-)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to make argparse accept "-4^2+5.3*abs(-2-1)/2" string argument?

2023-01-23 Thread Jach Feng
2qdxy4rz...@potatochowder.com 在 2023年1月24日 星期二凌晨2:47:12 [UTC+8] 的信中寫道:
> On 2023-01-22 at 18:19:13 -0800,
> Jach Feng  wrote: 
> 
> > 1) Modify the sys.argv by inserting an item '--' before parsing it, ie. 
> > sys.argv.insert(1, '--') 
> > args = parser.parse_args()
> Please don't do that. :-) 
> 
> In my mind, sys.argv belongs to Python, not the application. Instead, 
> pass a newly created argument list to parse_args: 
> 
> args = parser.parse_args(['--'] + sys.argv) 
> 
> This approach (adjusting the actual arguments) will work until your 
> program actually has options.
> > 2) By adding an extra space character before the leading '-' sign, ie. 
> > e:\Works\Python>py infix2postfix.py " -4^2+5.3*abs(-2-1)/2" 
> > -4 2 ^ 5.3 -2 1 - abs * 2 / + 
> > 
> > But no idea how it works? and if it can survive in a newer argparse 
> > version?:-)
> It works because argparse checks the first character of each argument, 
> and *doesn't* strip/trim whitespace. So "-x" looks like an option, and 
> " -x" looks an argument.
More pathonic, but don't work. The '--' must be at index 1:-)

>>> parser.parse_args(['--', 'infix2postfix.py', '-4.3+5'])
usage: [-h] infix
: error: unrecognized arguments: -4.3+5
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to make argparse accept "-4^2+5.3*abs(-2-1)/2" string argument?

2023-01-23 Thread Greg Ewing

On 2023-01-22 at 18:19:13 -0800,
Jach Feng  wrote:

1) Modify the sys.argv by inserting an item '--' before parsing it, ie.
sys.argv.insert(1, '--')
args = parser.parse_args()


If you do that, you'll never be able to have any actual options, so
using argparse seems like overkill. Just pull the argument out of
argv directly.

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


Re: How to make argparse accept "-4^2+5.3*abs(-2-1)/2" string argument?

2023-01-23 Thread Chris Angelico
On Tue, 24 Jan 2023 at 07:47, Cameron Simpson  wrote:
>
> But for Jach Feng: the "--" is really expected as something the user
> does when they invoke your programme, _explicitly_ saying that what
> follows from here is not an argument. So the user is expected to type:
>
>  your_script -x -y -- "-4^2+5.3*abs(-2-1)/2"
>
> where there are -x and -y options, then end of options, then an
> argument, which would look like an option if there wasn't the "--"
> argument.

And if you DON'T expect the user to enter the "--", then why use
argparse? You can just check argv directly to get your arguments.

This entire thread is a massive "how can I use X to do Y?" problem.

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


Re: How to make argparse accept "-4^2+5.3*abs(-2-1)/2" string argument?

2023-01-23 Thread Cameron Simpson

On 23Jan2023 13:46, 2qdxy4rzwzuui...@potatochowder.com 
<2qdxy4rzwzuui...@potatochowder.com> wrote:

On 2023-01-22 at 18:19:13 -0800,
Jach Feng  wrote:
1) Modify the sys.argv by inserting an item '--' before parsing it, 
ie.

sys.argv.insert(1, '--')
args = parser.parse_args()


Please don't do that.  :-)


Well... We routine mmodify argv when parsing a command line. It's just a 
list. It does assume I'm the only user of it.



In my mind, sys.argv belongs to Python, not the application.  Instead,
pass a newly created argument list to parse_args:

args = parser.parse_args(['--'] + sys.argv)


I do sometimes copy it:

def main(argv):
argv = list(argv)

for exactly the reasons you're alluding to. Then I'm free to modify the 
copy.


But for Jach Feng: the "--" is really expected as something the user 
does when they invoke your programme, _explicitly_ saying that what 
follows from here is not an argument. So the user is expected to type:


your_script -x -y -- "-4^2+5.3*abs(-2-1)/2"

where there are -x and -y options, then end of options, then an 
argument, which would look like an option if there wasn't the "--" 
argument.


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?

2023-01-23 Thread 2QdxY4RzWzUUiLuE
On 2023-01-22 at 18:19:13 -0800,
Jach Feng  wrote:

> 1) Modify the sys.argv by inserting an item '--' before parsing it, ie.
> sys.argv.insert(1, '--')
> args = parser.parse_args()

Please don't do that.  :-)

In my mind, sys.argv belongs to Python, not the application.  Instead,
pass a newly created argument list to parse_args:

args = parser.parse_args(['--'] + sys.argv)

This approach (adjusting the actual arguments) will work until your
program actually has options.

> 2) By adding an extra space character before the leading '-' sign, ie.
> e:\Works\Python>py infix2postfix.py " -4^2+5.3*abs(-2-1)/2"
> -4 2 ^ 5.3 -2 1 - abs * 2 / +
> 
> But no idea how it works? and if it can survive in a newer argparse 
> version?:-)

It works because argparse checks the first character of each argument,
and *doesn't* strip/trim whitespace.  So "-x" looks like an option, and
" -x" looks an argument.
-- 
https://mail.python.org/mailman/listinfo/python-list


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 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
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to make argparse accept "-4^2+5.3*abs(-2-1)/2" string argument?

2023-01-23 Thread Jach Feng
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
Thank you for all your suggestions. From it, I get two solutions for my problem.

1) Modify the sys.argv by inserting an item '--' before parsing it, ie.
sys.argv.insert(1, '--')
args = parser.parse_args()

It works, and maybe more reliable.
e:\Works\Python>py infix2postfix.py "-4^2+5.3*abs(-2-1)/2"
-4 2 ^ 5.3 -2 1 - abs * 2 / +

2) By adding an extra space character before the leading '-' sign, ie.
e:\Works\Python>py infix2postfix.py " -4^2+5.3*abs(-2-1)/2"
-4 2 ^ 5.3 -2 1 - abs * 2 / +

But no idea how it works? and if it can survive in a newer argparse version?:-)
-- 
https://mail.python.org/mailman/listinfo/python-list


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 consider:

x = { "y": "z" }
s = "-> {x['y']}"
print(s.format(x = x))
Traceback (most recent call last):
  File "", line 1, in 
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
--
https://mail.python.org/mailman/listinfo/python-list


Re: How to make argparse accept "-4^2+5.3*abs(-2-1)/2" string argument?

2023-01-23 Thread Grant Edwards
On 2023-01-22, Weatherby,Gerard  wrote:
> Argparse is for parsing command line arguments and options.
>
> If you just want to evaluate an Python expression, use eval( )

Only use eval() if the expression is always under your control (and
you don't make mistakes when typing).

Don't use eval() on strings that come from the outside world.

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