Mailing List Archive

How to make argparse accept "-4^2+5.3*abs(-2-1)/2" string argument?
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
--
https://mail.python.org/mailman/listinfo/python-list
Re: How to make argparse accept "-4^2+5.3*abs(-2-1)/2" string argument? [ In reply to ]
On 1/21/2023 10:11 PM, Jach Feng wrote:
> 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!?

It just doesn't work like that. If you download the package, there is
only one python file, __init__.py. This file contains one class. It
has a demo at the end, commented out. If you uncomment those lines and
run the file, you get a result printed.

These remarks are based on downloading the link for the source
distribution from Pypi
(https://pypi.org/project/infix2postfix/). When I installed it with
pip, nothing seems to have gotten installed although pip went through
the motions and claimed it was. So I just downloaded the source package.

The test expression is "-(a*b)+(c+d)-(a+b+c+d)". The test output for
this is "ab*-cd++ab+c+d+-".

If you substitute your expression, the result is

abs1-2-*2/3.5+2^4-

This may or may not be correct. I'm not sure but I think it's as
intended except for reversing "3.5". But maybe that's right, I'm not
too sure. Notice that this file is in its first release, version 0.0.1
- the metadata that says it's 'Development Status :: 5 -
Production/Stable' seems to be bogus. So it may very well be buggy.

At any rate, if you want to use it in a program that can accept
arguments, you will have to write that part yourself. And the
expression you feed it would need to be a single string, meaning it has
to be quoted on the command line as you have done (although on Windows
you should be using double quotes instead of single quotes).

As for argparse, it isn't doing what you want because you haven't told
it what to do with the arguments.
--
https://mail.python.org/mailman/listinfo/python-list
Re: How to make argparse accept "-4^2+5.3*abs(-2-1)/2" string argument? [ In reply to ]
Thomas Passin ? 2023?1?22? ?????1:30:39 [UTC+8] ??????
> On 1/21/2023 10:11 PM, Jach Feng wrote:
> > 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!?
> It just doesn't work like that. If you download the package, there is
> only one python file, __init__.py. This file contains one class. It
> has a demo at the end, commented out. If you uncomment those lines and
> run the file, you get a result printed.
>
> These remarks are based on downloading the link for the source
> distribution from Pypi
> (https://pypi.org/project/infix2postfix/). When I installed it with
> pip, nothing seems to have gotten installed although pip went through
> the motions and claimed it was. So I just downloaded the source package.
>
> The test expression is "-(a*b)+(c+d)-(a+b+c+d)". The test output for
> this is "ab*-cd++ab+c+d+-".
>
> If you substitute your expression, the result is
>
> abs1-2-*2/3.5+2^4-
>
> This may or may not be correct. I'm not sure but I think it's as
> intended except for reversing "3.5". But maybe that's right, I'm not
> too sure. Notice that this file is in its first release, version 0.0.1
> - the metadata that says it's 'Development Status :: 5 -
> Production/Stable' seems to be bogus. So it may very well be buggy.
>
> At any rate, if you want to use it in a program that can accept
> arguments, you will have to write that part yourself. And the
> expression you feed it would need to be a single string, meaning it has
> to be quoted on the command line as you have done (although on Windows
> you should be using double quotes instead of single quotes).
>
> As for argparse, it isn't doing what you want because you haven't told
> it what to do with the arguments.
Sorry to cause confusion here. I don't know there is a Pypi project with the same name infix2postfix.py:-(

Nevertheless, Is there anyway to make parse_args works?
>>> parser.parse_args("-4^2+5.3*abs(-2-1)/2")
--
https://mail.python.org/mailman/listinfo/python-list
Re: How to make argparse accept "-4^2+5.3*abs(-2-1)/2" string argument? [ In reply to ]
Jach Feng wrote:
> 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!?

First, you need to add an argument to the parser, so that it expects an
argument:
>>> parser.add_argument("expression")

Secondly, `parse_args` expects a list of arguments. By passing a
string, it interprets each character as a separate argument (since
iterating over a string yields each character in turn). Here, I
intentionally leave off the initial hyphen because that's the next problem:
>>> parser.parse_args(["4^2+5.3*abs(-2-1)/2"])
Namespace(expression='4^2+5.3*abs(-2-1)/2')

Thirdly, an initial hyphen indicates an optional argument so, for
example if you pass "-l" it will expect a "-l" argument to be defined as
one of the valid options, and also complain that you haven't specified
the required expression:
>>> parser.parse_args(["-4^2+5.3*abs(-2-1)/2"])
usage: [-h] expression
: error: the following arguments are required: expression

If you need to pass a value starting with a "-" there are a couple of
options...

Perhaps it would be acceptable to represent it as "0-...":
>>> parser.parse_args(["0-4^2+5.3*abs(-2-1)/2"])
Namespace(expression='0-4^2+5.3*abs(-2-1)/2')

While mathematically equivalent, that might have different meaning for
whatever you're trying to do. Alternatively, a double hyphen indicates
that there are no more options and that anything else is positional
arguments even if they begin with a hyphen:
>>> parser.parse_args(["--", "-4^2+5.3*abs(-2-1)/2"])
Namespace(expression='-4^2+5.3*abs(-2-1)/2')

You wouldn't usually explicitly pass a list of arguments to `parse_args`
like that, but it can be useful for testing and experimentation.
Usually, you'd call `parse_args()` without any arguments, and it would
parse the arguments passed on the command-line when calling your script.
e.g. you'd call (from a Windows command prompt / Linux shell / etc.):
> ./convert_infix.py -- '-4^2+5.3*abs(-2-1)/2'
(it's probably a good idea to quote the expression, in case it includes
any characters which would be interpreted specially by the shell - e.g.
"*" without quotes usually expands to all matching files in the current
directory)

--
Mark.
--
https://mail.python.org/mailman/listinfo/python-list
Re: How to make argparse accept "-4^2+5.3*abs(-2-1)/2" string argument? [ In reply to ]
On 2023-01-22 00:30:07 -0500, Thomas Passin wrote:
> On 1/21/2023 10:11 PM, Jach Feng wrote:
> > 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
[...]
> > > > > 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!?
>
> It just doesn't work like that.
[...]
> If you substitute your expression, the result is
>
> abs1-2-*2/3.5+2^4-
>
> This may or may not be correct. I'm not sure but I think it's as intended
> except for reversing "3.5". But maybe that's right, I'm not too sure.

It depends on the intended meaning. But I'm pretty sure that "5.3" is
supposed to be a floating point number and not (5 operatordot 3), so
it's wrong to split that apart. (Also it seems that the code treats
digits as operators, not operands?)

Also "abs(...)" is almost certainly intended to be a function call, so
it should be invoked after its parameters.

I think the correct output (using newlines as delimiters) would be:

-4
2
^
5.3
-2
1
-
abs
*
2
/
+

> Notice that this file is in its first release, version 0.0.1 - the metadata
> that says it's 'Development Status :: 5 - Production/Stable' seems to be
> bogus. So it may very well be buggy.

It is at least too incomplete to be useful. It handles only single
letters as operands and treats everything else (except parentheses) as an operator, handling
only +, -, *, / and ^ correctly (actually, ^ is typically right
associative, so that's arguably wrong, too).

hp

--
_ | Peter J. Holzer | Story must make more sense than reality.
|_|_) | |
| | | hjp@hjp.at | -- Charles Stross, "Creative writing
__/ | http://www.hjp.at/ | challenge!"
Re: How to make argparse accept "-4^2+5.3*abs(-2-1)/2" string argument? [ In reply to ]
On 1/22/2023 10:45 AM, Peter J. Holzer wrote:
>> Notice that this file is in its first release, version 0.0.1 - the metadata
>> that says it's 'Development Status :: 5 - Production/Stable' seems to be
>> bogus. So it may very well be buggy.
> It is at least too incomplete to be useful. It handles only single
> letters as operands and treats everything else (except parentheses) as an operator, handling
> only +, -, *, / and ^ correctly (actually, ^ is typically right
> associative, so that's arguably wrong, too).

This script is basically useless without some documentation on the
intended inputs and outputs, and how to invoke it. The documentation
could (in the minimal case) have been included in docstrings and
comments within the script. As it is, the script seems tantalizing but
unusable.

Please document your work, even if it's just for yourself!

--
https://mail.python.org/mailman/listinfo/python-list
Re: How to make argparse accept "-4^2+5.3*abs(-2-1)/2" string argument? [ In reply to ]
Argparse is for parsing command line arguments and options.

If you just want to evaluate an Python expression, use eval( )

Your string isn$B!G(Bt valid Python due to order of operations, but -(4^2)+5.3*abs(-2-1)/2 is.

From: Python-list <python-list-bounces+gweatherby=uchc.edu@python.org> on behalf of Jach Feng <jfong@ms4.hinet.net>
Date: Sunday, January 22, 2023 at 11:24 AM
To: python-list@python.org <python-list@python.org>
Subject: Re: How to make argparse accept "-4^2+5.3*abs(-2-1)/2" string argument?
*** Attention: This is an external email. Use caution responding, opening attachments or clicking on links. ***

Thomas Passin $B:_(B 2023$BG/(B1$B7n(B22$BF|(B $B@14|F|2<8a(B1:30:39 [UTC+8] $BE*?.CfUmF;!'(B
> On 1/21/2023 10:11 PM, Jach Feng wrote:
> > 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!?
> It just doesn't work like that. If you download the package, there is
> only one python file, __init__.py. This file contains one class. It
> has a demo at the end, commented out. If you uncomment those lines and
> run the file, you get a result printed.
>
> These remarks are based on downloading the link for the source
> distribution from Pypi
> (https://urldefense.com/v3/__https://pypi.org/project/infix2postfix/__;!!Cn_UX_p3!gqKmYLlyUndAzxmJsqCB429izQ-2-KMbpGP2eVzp_iDKtbgQXfrCu21UBvepq-F9EXb4SJwP516MHeUFMBtW$ ). When I installed it with
> pip, nothing seems to have gotten installed although pip went through
> the motions and claimed it was. So I just downloaded the source package.
>
> The test expression is "-(a*b)+(c+d)-(a+b+c+d)". The test output for
> this is "ab*-cd++ab+c+d+-".
>
> If you substitute your expression, the result is
>
> abs1-2-*2/3.5+2^4-
>
> This may or may not be correct. I'm not sure but I think it's as
> intended except for reversing "3.5". But maybe that's right, I'm not
> too sure. Notice that this file is in its first release, version 0.0.1
> - the metadata that says it's 'Development Status :: 5 -
> Production/Stable' seems to be bogus. So it may very well be buggy.
>
> At any rate, if you want to use it in a program that can accept
> arguments, you will have to write that part yourself. And the
> expression you feed it would need to be a single string, meaning it has
> to be quoted on the command line as you have done (although on Windows
> you should be using double quotes instead of single quotes).
>
> As for argparse, it isn't doing what you want because you haven't told
> it what to do with the arguments.
Sorry to cause confusion here. I don't know there is a Pypi project with the same name infix2postfix.py:-(

Nevertheless, Is there anyway to make parse_args works?
>>> parser.parse_args("-4^2+5.3*abs(-2-1)/2")
--
https://urldefense.com/v3/__https://mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!gqKmYLlyUndAzxmJsqCB429izQ-2-KMbpGP2eVzp_iDKtbgQXfrCu21UBvepq-F9EXb4SJwP516MHezHygV-$<https://urldefense.com/v3/__https:/mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!gqKmYLlyUndAzxmJsqCB429izQ-2-KMbpGP2eVzp_iDKtbgQXfrCu21UBvepq-F9EXb4SJwP516MHezHygV-$>
--
https://mail.python.org/mailman/listinfo/python-list
Re: How to make argparse accept "-4^2+5.3*abs(-2-1)/2" string argument? [ In reply to ]
On 21Jan2023 19:11, Jach Feng <jfong@ms4.hinet.net> wrote:
>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

The usual convention for having "nonoption" arguments beginning with a
dash/minus is to explicitly _end_ the option arguments, eg:

py infix2postfix.py -- "-4^2+5.3*abs(-2-1)/2"

That "--" indicates the end of the options, and that what follows should
not be treated as an option. _However_, it requires support in the
script parsing the options. I'm pretty sure argparse gives that support
for free, as does getopt and probably any others implementing "normal
UNIXish options".

SO try adding a "--" argument and see how it behaves.

>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

This is a different error. `parse_args()` expects a list of arguments,
not a single argument. So it has iterated over what you gave it, which
produces a series of single character strings which it has taken to be
individual arguments. Try this:

parser.parse_args(["-4^2+5.3*abs(-2-1)/2"])

and of course:

parser.parse_args(["--", "-4^2+5.3*abs(-2-1)/2"])

You can see this behaviour of strings as:

print(list("abc"))

or:

for s in "abc":
print("s =", repr(s))

Cheers,
Cameron Simpson <cs@cskk.id.au>
--
https://mail.python.org/mailman/listinfo/python-list
Re: How to make argparse accept "-4^2+5.3*abs(-2-1)/2" string argument? [ In reply to ]
On 1/22/23 11:44, Stefan Ram wrote:
> Jach Feng <jfong@ms4.hinet.net> writes:
>> e:\Works\Python>py infix2postfix.py "-4^2+5.3*abs(-2-1)/2"
>
> Well, it's a nice exercise! But I only made it work for the
> specific example given. I have not tested whether it always
> works.

Haha. Yes a nice exercise, but has nothing to do with the original
question which is how to convince argparse to accept a string like that
without thinking it's a switch. Many unix utilities treat "--" as a
special argument that turns off argument parsing for the rest of the
command line. Maybe argparse follows this convention too; I don't know.
But if it did you'd do:

infix2postfix.py -- "-4^2+5.3*abs(-2-1)/2"
--
https://mail.python.org/mailman/listinfo/python-list
Re: How to make argparse accept "-4^2+5.3*abs(-2-1)/2" string argument? [ In reply to ]
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
Re: How to make argparse accept "-4^2+5.3*abs(-2-1)/2" string argument? [ In reply to ]
On 2023-01-22, Weatherby,Gerard <gweatherby@uchc.edu> 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
Re: How to make argparse accept "-4^2+5.3*abs(-2-1)/2" string argument? [ In reply to ]
On 2023-01-22 at 18:19:13 -0800,
Jach Feng <jfong@ms4.hinet.net> 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: How to make argparse accept "-4^2+5.3*abs(-2-1)/2" string argument? [ In reply to ]
On 23Jan2023 13:46, 2QdxY4RzWzUUiLuE@potatochowder.com <2QdxY4RzWzUUiLuE@potatochowder.com> wrote:
>On 2023-01-22 at 18:19:13 -0800,
>Jach Feng <jfong@ms4.hinet.net> 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 <cs@cskk.id.au>
--
https://mail.python.org/mailman/listinfo/python-list
Re: How to make argparse accept "-4^2+5.3*abs(-2-1)/2" string argument? [ In reply to ]
On Tue, 24 Jan 2023 at 07:47, Cameron Simpson <cs@cskk.id.au> 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? [ In reply to ]
>> On 2023-01-22 at 18:19:13 -0800,
>> Jach Feng <jfong@ms4.hinet.net> 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? [ In reply to ]
2QdxY4Rz...@potatochowder.com ? 2023?1?24? ?????2:47:12 [UTC+8] ??????
> On 2023-01-22 at 18:19:13 -0800,
> Jach Feng <jf...@ms4.hinet.net> 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? [ In reply to ]
Chris Angelico ? 2023?1?24? ?????5:00:27 [UTC+8] ??????
> On Tue, 24 Jan 2023 at 07:47, Cameron Simpson <c...@cskk.id.au> 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? [ In reply to ]
Greg Ewing ? 2023?1?24? ?????7:33:43 [UTC+8] ??????
> >> On 2023-01-22 at 18:19:13 -0800,
> >> Jach Feng <jf...@ms4.hinet.net> 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? [ In reply to ]
On Tue, 24 Jan 2023 at 13:09, Jach Feng <jfong@ms4.hinet.net> wrote:
>
> Chris Angelico ? 2023?1?24? ?????5:00:27 [UTC+8] ??????
> > On Tue, 24 Jan 2023 at 07:47, Cameron Simpson <c...@cskk.id.au> 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? [ In reply to ]
On 23Jan2023 17:58, Jach Feng <jfong@ms4.hinet.net> 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 <cs@cskk.id.au>
--
https://mail.python.org/mailman/listinfo/python-list
Re: How to make argparse accept "-4^2+5.3*abs(-2-1)/2" string argument? [ In reply to ]
cameron...@gmail.com ? 2023?1?24? ?????2:05:33 [UTC+8] ??????
> On 23Jan2023 17:58, Jach Feng <jf...@ms4.hinet.net> 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 <c...@cskk.id.au>
Hmm, good to you. During experiments in these days, I had found argparse shows some strange/unstable behaviors in dealing with the leading '-' situation.
--
https://mail.python.org/mailman/listinfo/python-list
Re: How to make argparse accept "-4^2+5.3*abs(-2-1)/2" string argument? [ In reply to ]
Stop please


Sent from Yahoo Mail for iPhone


On Tuesday, January 24, 2023, 1:05 AM, Cameron Simpson <cs@cskk.id.au> wrote:

On 23Jan2023 17:58, Jach Feng <jfong@ms4.hinet.net> 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 <cs@cskk.id.au>
--
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? [ In reply to ]
On 1/23/2023 9:12 PM, Chris Angelico wrote:
> On Tue, 24 Jan 2023 at 13:09, Jach Feng <jfong@ms4.hinet.net> wrote:
>>
>> Chris Angelico ? 2023?1?24? ?????5:00:27 [UTC+8] ??????
>>> On Tue, 24 Jan 2023 at 07:47, Cameron Simpson <c...@cskk.id.au> 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?

I second this. "if '-h' in sys.argv:" is usually what I do.

Alternatively, you could use "--arg=" syntax and place your string
"-4^2+5.3*abs(-2-1)/2" its right-hand side":

infix2postfix [options] "--infix=-4^2+5.3*abs(-2-1)/2"

This shouldn't be too hard for a user to work with. You could scan the
argument list for the presence of "--infix=" and display the help
message if it isn't there.

--
https://mail.python.org/mailman/listinfo/python-list
Re: How to make argparse accept "-4^2+5.3*abs(-2-1)/2" string argument? [ In reply to ]
Can you stop please


Sent from Yahoo Mail for iPhone


On Tuesday, January 24, 2023, 10:12 AM, Thomas Passin <list1@tompassin.net> wrote:

On 1/23/2023 9:12 PM, Chris Angelico wrote:
> On Tue, 24 Jan 2023 at 13:09, Jach Feng <jfong@ms4.hinet.net> wrote:
>>
>> Chris Angelico ? 2023?1?24? ?????5:00:27 [UTC+8] ??????
>>> On Tue, 24 Jan 2023 at 07:47, Cameron Simpson <c...@cskk.id.au> 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?

I second this.  "if '-h' in sys.argv:"  is usually what I do.

Alternatively, you could use "--arg=" syntax and place your string
"-4^2+5.3*abs(-2-1)/2" its right-hand side":

infix2postfix [options] "--infix=-4^2+5.3*abs(-2-1)/2"

This shouldn't be too hard for a user to work with.  You could scan the
argument list for the presence of "--infix=" and display the help
message if it isn't there.

--
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? [ In reply to ]
On 1/24/2023 10:13 AM, Mike Baskin wrote:
> Can you stop please

It's way past time, isn't it!

> Sent from Yahoo Mail for iPhone <https://overview.mail.yahoo.com/?.src=iOS>
>
> On Tuesday, January 24, 2023, 10:12 AM, Thomas Passin
> <list1@tompassin.net> wrote:
>
> On 1/23/2023 9:12 PM, Chris Angelico wrote:
> > On Tue, 24 Jan 2023 at 13:09, Jach Feng <jfong@ms4.hinet.net
> <mailto:jfong@ms4.hinet.net>> wrote:
> >>
> >> Chris Angelico ? 2023?1?24? ?????5:00:27 [UTC+8] ???
> ???
> >>> On Tue, 24 Jan 2023 at 07:47, Cameron Simpson <c...@cskk.id.au
> <mailto:c...@cskk.id.au>> 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?
>
> I second this.  "if '-h' in sys.argv:"  is usually what I do.
>
> Alternatively, you could use "--arg=" syntax and place your string
> "-4^2+5.3*abs(-2-1)/2" its right-hand side":
>
> infix2postfix [options] "--infix=-4^2+5.3*abs(-2-1)/2"
>
> This shouldn't be too hard for a user to work with.  You could scan the
> argument list for the presence of "--infix=" and display the help
> message if it isn't there.
>
>
> --
> https://mail.python.org/mailman/listinfo/python-list
> <https://mail.python.org/mailman/listinfo/python-list>
>

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

1 2 3  View All