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

2023-01-24 Thread Cameron Simpson

On 25Jan2023 16:15, Chris Angelico  wrote:

On Wed, 25 Jan 2023 at 14:42, Jach Feng  wrote:

I was happy working with argparse during implement my script. To save the 
typing, I used a default equation for testing.


Sure, but what benefit was it bringing you? Just the usage (help) 
message?  Did you have many options to handle?



sample = "-4^2+5.3*abs(-2-1)/2, abs(Abc)*(B+C)/D, (-3) * sqrt(1-(x1/7)*(y1/7)) * 
sqrt(abs((x0-4.5)/(y0-4)))"
parser = argparse.ArgumentParser(description='Convert infix notation to 
postfix')
parser.add_argument('infix', nargs='?', default=sample, help="")


If this was the whole thing, I don't see what argparse was doing for you 
which was better than just handling the arguments yourself - there's 
only one after all.



You're still not really using argparse as an argument parser. Why not
just do your own -h checking? Stop trying to use argparse for what
it's not designed for, and then wondering why it isn't doing what you
expect it to magically know.


I'm with Chris here.

As someone who pretty well _never_ uses argparse, and occasionally uses 
getopt, I'd do something like this:


usage = '''Usage: infix
Parse the argument infix as an expression, print the result.'''

badopts = False
cmd = argv.pop(0)
if not argv:
print(f'{cmd}: missing infix argument', file=sys.stderr)
badopts = True
else:
infix = argv.pop(0)
if infix in ('-h', '--help'):
print(usage)
exit(0)
if argv:
print(f'{cmd}: extra arguments after infix: {argv!r}', 
file=sys.stderr)
badopts = True
if badopts:
print(usage, file=sys.stderr)
exit(2)
... work with infix as desired ...

This:
- avoids trying to shoehorn argparse into behaving in a way it was not 
  designed for
- avoids users needing to know the standard UNIX/POSIX "--" idiom for 
  marking off the end of options

- supports a -h or --help leading option

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-24 Thread Chris Angelico
On Wed, 25 Jan 2023 at 14:42, Jach Feng  wrote:
> I was happy working with argparse during implement my script. To save the 
> typing, I used a default equation for testing.
>
> sample = "-4^2+5.3*abs(-2-1)/2, abs(Abc)*(B+C)/D, (-3) * 
> sqrt(1-(x1/7)*(y1/7)) * sqrt(abs((x0-4.5)/(y0-4)))"
> parser = argparse.ArgumentParser(description='Convert infix notation to 
> postfix')
> parser.add_argument('infix', nargs='?', default=sample, help="")
>

You're still not really using argparse as an argument parser. Why not
just do your own -h checking? Stop trying to use argparse for what
it's not designed for, and then wondering why it isn't doing what you
expect it to magically know.

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-24 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
I was happy working with argparse during implement my script. To save the 
typing, I used a default equation for testing.

sample = "-4^2+5.3*abs(-2-1)/2, abs(Abc)*(B+C)/D, (-3) * sqrt(1-(x1/7)*(y1/7)) 
* sqrt(abs((x0-4.5)/(y0-4)))"
parser = argparse.ArgumentParser(description='Convert infix notation to 
postfix')
parser.add_argument('infix', nargs='?', default=sample, help="")

The argparse has no complain at all, even I enter it in the command line.
e:\Works\Python>py infix2postfix.py "-4^2+5.3*abs(-2-1)/2, abs(Abc)*(B+C)/D, 
(-3) * sqrt(1-(x1/7)*(y1/7)) * sqrt(abs((x0
-4.5)/(y0-4)))"
-4 2 ^ 5.3 -2 1 - abs * 2 / +
Abc abs B C + * D /
-3 1 x1 7 / y1 7 / * - sqrt * x0 4.5 - y0 4 - / abs sqrt *

But the happiness ends when this day comes,
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

Then, I know I am in trouble, and You know the rest of the story:-)
-- 
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-24 Thread Jach Feng
avi.e...@gmail.com 在 2023年1月25日 星期三清晨6:39:13 [UTC+8] 的信中寫道:
> If I understood the issue, the problem is the unary minus at the start of the 
> expression. 
> 
> So if you know the expression is in that argument, would it make sense to pad 
> it in one of many ways that are otherwise harmless? 
> 
> Could you put parens on both sides of "-4^2+5.3*abs(-2-1)/2": 
> 
> "(-4^2+5.3*abs(-2-1)/2)" 
> 
> Or perhaps place a zero in front as in the awkward case where it begins with 
> a minus sign: 
> 
> "0 -4^2+5.3*abs(-2-1)/2" 
> 
> Some people suggest you ask the user to modify what they type in and such 
> changes may work. Your program could also potentially diddle with argv and 
> recognize it albeit as you allow calling a function like abs() I can easily 
> imagine a potentially valid looking "-abs(...)..." that could look like -a 
> followed by something. 
> 
> I see a deeper issue with interactions at the command shell level if parts of 
> the arithmetic expression are evaluated or changed before python is even 
> invoked. 
> 
> Then again, I may be misunderstanding the issue.
> -Original Message- 
> From: Python-list  On 
> Behalf Of Jach Feng 
> Sent: Tuesday, January 24, 2023 2:21 AM 
> To: pytho...@python.org 
> Subject: Re: How to make argparse accept "-4^2+5.3*abs(-2-1)/2" string 
> argument?
> cameron...@gmail.com 在 2023年1月24日 星期二下午2:05:33 [UTC+8] 的信中寫道: 
> > 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  
> 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
The user may not know any unix idiom, or even don't know Python. Ask them to 
add extra characters in their equations to avoid the argparse conflict is 
strange to me.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: bool and int

2023-01-24 Thread rbowman
On Wed, 25 Jan 2023 12:14:50 +1100, Chris Angelico wrote:


> So the problem isn't that I'm trying to write Python in C#, but that I'm
> trying to write code that would work on pretty much *any other C-family
> language*, but doesn't work on C#. I could use those techniques in
> plenty of C-derived and C-inspired languages, but no not in C#,
> despite looking very much C-inspired. Unfortunately the truth is that C#
> is not *actually* C-inspired; it's really Microsoft Java, so it has all
> the stupidities of Java:

I have the installation media for Visual J++ around here someplace. It 
lasted for a few years before Sun sued for non-compliance. There 
definitely is some of its DNA in C#. 

I prefer C# to C++ but it does have annoyances. It's only a warning that 
can be pragma'd but

string foo;  

complaining that foo may be null unless you shut it up with

string? foo;

can be frustrating. It has a germ of a good idea for structs (classes) 
where some of the members are optional. 

The bool hoops definitely are another sore point after a few decades of C.

I can switch gears for Python but the first day or two is painful. I was 
getting a bit frustrated until I remembered the range() thing. 
-- 
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-24 Thread Jach Feng
Michael Torrie 在 2023年1月25日 星期三凌晨3:05:44 [UTC+8] 的信中寫道:
> On 1/23/23 18:58, Jach Feng wrote: 
> > More pathonic, but don't work. The '--' must be at index 1:-)
> I'm very confused. Why are you even using argparse, since if you put -- 
> at index 1 then argparse wont't do any argument parsing at all. If all 
> you want is the expression on the command line, just access it directly. 
> If it's spread out with spaces, you can do something like this to put 
> it back together: 
> 
> expression = " ".join(sys.argv[1:] 
> 
> Otherwise the standard unix way of doing this is to require the user to 
> either provide the -- himself, or put the expression in quotes so it's 
> one unit.
Maybe a condition is required before the modification,
if len(sys.argv) > 1 and not '-h' in sys.argv:
sys.argv.insert(1, '--')
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: bool and int

2023-01-24 Thread Grant Edwards
On 2023-01-25, Mike Baskin via Python-list  wrote:

> Will all of you please stop sending me emails

Oh dear.

You might want to try unsubscribing from the list.

Telling everybody to stop using the mailing list and newsgroup is a bit silly.

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


Re: bool and int

2023-01-24 Thread Chris Angelico
On Wed, 25 Jan 2023 at 12:43,  wrote:
> Python has a different philosophy than some other languages with strong
> typing. In some of those, you would not be allowed to add or multiply at
> random but would need to convert parts of your calculation to all be the
> same, such as a 32-bit integer. You could still do things like I mention
> above but only after consciously mapping your Boolean to an actual zero or
> one of the kind wanted.

Python is strongly dynamically typed. You may be thinking of "static
typing" rather than "strong typing" here, and there are plenty of
strongly statically typed languages that allow you to do arithmetic on
booleans (with them usually behaving as if False is 0 and True is 1,
although not always).

> I worry a tad more about the other direction where something like an integer
> containing a number like 12 is used in a context where it gets downgraded to
> a True/False and later may inadvertently be used as a "1" as the conversion
> is not anticipated. There is data loss there more than in the case of a
> Boolean becoming a 1.

Well, yes, but that's no different from a float like 6.25 getting
downgraded to an integer 6. There's a reason that most languages
upcast silently but don't downcast without being asked to.

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


RE: bool and int

2023-01-24 Thread avi.e.gross
Python yet again is being asked why something is the way it is and not as
someone insists it should be. It is a tool to be used the way it SAYS it
works so the bug is perhaps in the user and their expectations.

It is what it is and would break lots of things if changed without much
thought. Every other language I know has similar detractors asking why it is
the way it is.

A useful example where the use of Booleans as 0/1 is commonly used is to
find how many of something satisfy some criteria, such as how many items in
a list are numbers greater than 65 representing test scores that "passed".
It could also be a numpy type of array or a column of a data.frame and so
on. The common method is to create a Boolean data structure (often
implicitly) that is then viewed as a bunch of 0's and 1's and you can count
them to see how many are True (meaning they qualify) or calculate a
percentage simply by taking a mean of the 0/1 values.

There are other ways, but some also use sums of Booleans to calculate things
like any() or all() and I am sure many other things. Yes, all these can be
done another way if Booleans were not allowed to be viewed as very small
integers.

So only use a Boolean in relatively pure situations where arithmetic as a
zero or one is meaningful. In other places, maybe don't. The reality though
is that if you have 10 apples and you get another one if you flip a coin and
it lands on heads. Then 10 + Boolean would indeed be 11. If you have five
losing flips (or tickets or whatever) then 5 * Boolean indeed is a zero. 

Python has a different philosophy than some other languages with strong
typing. In some of those, you would not be allowed to add or multiply at
random but would need to convert parts of your calculation to all be the
same, such as a 32-bit integer. You could still do things like I mention
above but only after consciously mapping your Boolean to an actual zero or
one of the kind wanted. 

I worry a tad more about the other direction where something like an integer
containing a number like 12 is used in a context where it gets downgraded to
a True/False and later may inadvertently be used as a "1" as the conversion
is not anticipated. There is data loss there more than in the case of a
Boolean becoming a 1.

-Original Message-
From: Python-list  On
Behalf Of 2qdxy4rzwzuui...@potatochowder.com
Sent: Tuesday, January 24, 2023 6:31 PM
To: python-list@python.org
Subject: Re: bool and int

On 2023-01-25 at 08:58:06 +1100,
Chris Angelico  wrote:

> On Wed, 25 Jan 2023 at 08:22, MRAB  wrote:
> > For backwards compatibility, bool was made a subclass of int.
> 
> Plus, it's really REALLY handy in quite a lot of situations.
> 
> > > C# is pickier, which I guess is a good thing.
> >
> 
> Nope, not a good thing. Actually a highly frustrating thing on those 
> occasions when I have to write C# code.

The usual complaint is that some people write FORTRAN no matter what
language they're actually using.  Are you writing Python in C#?  ;-)

There's a sweet spot somewhere that includes dynamic typing, high powered
global type inference and optimization systems, a thriving community, and a
metric [boatload] of rock solid libraries.

And an alomost fanatical devotion to the Pope.  :-/
--
https://mail.python.org/mailman/listinfo/python-list

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


Re: bool and int

2023-01-24 Thread David
On Wed, 25 Jan 2023 at 12:19, Mike Baskin via Python-list
 wrote:

> Will all of you please stop sending me emails

Hi. We don't have the power to do that.
Because this is a public list, which works by
people adding and removing themselves.
You, or perhaps someone messing with you,
added your email address to it.
So you have to remove yourself. The link to do
that is at the bottom of every message. See here:
  https://mail.python.org/mailman/listinfo/python-list
Go to the bottom, enter your email address, click
the button next to it named "Unsubscribe or edit options"
and follow the instructions.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: bool and int

2023-01-24 Thread Chris Angelico
On Wed, 25 Jan 2023 at 12:20, Mike Baskin via Python-list
 wrote:
>
> Will all of you please stop sending me emails

Learn to manage your own mailing list subscription. HINT: Look at the
*ENTIRE* email, not just the bit up the top that makes you angry.

> Sent from my iPhone

Ahh, I see the problem here. You probably can't even read the entire
email. Good luck. If you can find the link, you can unsubscribe from
the mailing list, although you still probably won't be able to stop
yourself from being a constant advertisement bragging "Oh I am the
greatest, I use default iPhone apps, ain't I awesome".

Forgive me for thinking that it's utterly lame. There's probably a
good reason for bragging about iPhone usage on EVERY SINGLE EMAIL, but
I've just never seen it.

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


Re: bool and int

2023-01-24 Thread Mike Baskin via Python-list
Will all of you please stop sending me emails

Sent from my iPhone

> On Jan 24, 2023, at 2:59 PM, rbowman  wrote:
> 
> On Mon, 23 Jan 2023 23:22:00 -0500, Dino wrote:
> 
>> $ python Python 3.8.10 (default, Mar 15 2022, 12:22:08)
>> [GCC 9.4.0] on linux Type "help", "copyright", "credits" or "license"
>> for more information.
> b = True isinstance(b,bool)
>> True
> isinstance(b,int)
>> True
> 
> 
>> WTF!
> 
> 
 b = True
 isinstance(b, bool)
> True
 isinstance(b, int)
> True
 c = b + 10
 print(c)
> 11
 b = False
 c = b + 10
 print(c)
> 10
> 
> 
> bool is a subtype of integer. I never dug that deep into Python's guts but 
> I assume it goes back to boolean being an afterthought in C. Some people 
> fancy it up with #defines but I always use int.  0 is false, anything else 
> is true.
> 
> C# is pickier, which I guess is a good thing. 
> -- 
> https://mail.python.org/mailman/listinfo/python-list

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


Re: bool and int

2023-01-24 Thread Chris Angelico
On Wed, 25 Jan 2023 at 10:32, <2qdxy4rzwzuui...@potatochowder.com> wrote:
>
> On 2023-01-25 at 08:58:06 +1100,
> Chris Angelico  wrote:
>
> > On Wed, 25 Jan 2023 at 08:22, MRAB  wrote:
> > > For backwards compatibility, bool was made a subclass of int.
> >
> > Plus, it's really REALLY handy in quite a lot of situations.
> >
> > > > C# is pickier, which I guess is a good thing.
> > >
> >
> > Nope, not a good thing. Actually a highly frustrating thing on those
> > occasions when I have to write C# code.
>
> The usual complaint is that some people write FORTRAN no matter what
> language they're actually using.  Are you writing Python in C#?  ;-)

Well, let's see. If I were writing C code, I would write:

if (state & PRELAUNCH)

If I were writing Python, it would probably be very different, but it
depends on the API. Could be:

if "PreLaunch" in state:

But the way I have to write it in C# is a messed-up version of C:

if ((state & StartState.PreLaunch) > 0) {

because bool and int are fundamentally different. Using the C style results in:

VelocimeterModule.cs(37,8): error CS0029: Cannot implicitly convert
type `PartModule.StartState' to `bool'

Here's another example. If I were writing C, I would write:

if (TimeWarp_CurrentRateIndex)

Python?

if TimeWarp.CurrentRateIndex:

C#?

if (TimeWarp.CurrentRateIndex > 0)

And, again, if I do it C style, I get:

VelocimeterModule.cs(261,17): error CS0029: Cannot implicitly convert
type `int' to `bool'

I'm pretty sure I've had a case where I wanted to use a boolean in an
arithmetic context, too, but it's less obvious from the final code, so
I can't give an example. So this is synthetic:

autothrust_last_dv *= AT_mode == AT.Idle;

VelocimeterModule.cs(252,4): error CS0019: Operator `*=' cannot be
applied to operands of type `double' and `bool'

So the problem isn't that I'm trying to write Python in C#, but that
I'm trying to write code that would work on pretty much *any other
C-family language*, but doesn't work on C#. I could use those
techniques in plenty of C-derived and C-inspired languages, but no
not in C#, despite looking very much C-inspired. Unfortunately the
truth is that C# is not *actually* C-inspired; it's really Microsoft
Java, so it has all the stupidities of Java:

int x = 3 + (args.length > 1);
test.java:4: error: bad operand types for binary operator '+'

if (args.length) System.out.println("There are args!");
test.java:6: error: incompatible types: int cannot be converted to boolean

But this is hardly a Python-versus-C# thing; it's Java versus most of
the rest of the world, and C# feigns to be part of the C style while
retaining the limitations of Java.

(My apologies if the Java entries look synthetic. It's because they
are, and that's a consequence of me not having ANY reason to write
Java code in, like, ever. In fact, I had to go and install a JDK just
to confirm that Java really did have these limitations.)

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


Re: bool and int

2023-01-24 Thread 2QdxY4RzWzUUiLuE
On 2023-01-25 at 08:58:06 +1100,
Chris Angelico  wrote:

> On Wed, 25 Jan 2023 at 08:22, MRAB  wrote:
> > For backwards compatibility, bool was made a subclass of int.
> 
> Plus, it's really REALLY handy in quite a lot of situations.
> 
> > > C# is pickier, which I guess is a good thing.
> >
> 
> Nope, not a good thing. Actually a highly frustrating thing on those
> occasions when I have to write C# code.

The usual complaint is that some people write FORTRAN no matter what
language they're actually using.  Are you writing Python in C#?  ;-)

There's a sweet spot somewhere that includes dynamic typing, high
powered global type inference and optimization systems, a thriving
community, and a metric [boatload] of rock solid libraries.

And an alomost fanatical devotion to the Pope.  :-/
-- 
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-24 Thread Dennis Lee Bieber
On Tue, 24 Jan 2023 15:13:33 + (UTC), Mike Baskin
 declaimed the following:

>Can you stop please
>

Stop what?

You appear to be subscribed to the Python mailing list -- as such you
WILL receive all traffic that appears on that list. There is nothing we can
do. It is not a personal "you post, and only direct replies to you show
up".

BTW: the mailing list is gatewayed to newsgroup comp.lang.python...




-- 
Wulfraed Dennis Lee Bieber AF6VN
wlfr...@ix.netcom.comhttp://wlfraed.microdiversity.freeddns.org/
-- 
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-24 Thread avi.e.gross
If I understood the issue, the problem is the unary minus at the start of the 
expression. 

So if you know the expression is in that argument, would it make sense to pad 
it in one of many ways that are otherwise harmless?

Could you put parens on both sides of "-4^2+5.3*abs(-2-1)/2":

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

Or perhaps place a zero in front as in the awkward case where it begins with a 
minus sign:

"0 -4^2+5.3*abs(-2-1)/2"

Some people suggest you ask the user to modify what they type in and such 
changes may work. Your program could also potentially diddle with argv and 
recognize it albeit as you allow calling a function like abs() I can easily 
imagine a potentially valid looking "-abs(...)..." that could look like -a 
followed by something.

I see a deeper issue with interactions at the command shell level if parts of 
the arithmetic expression are evaluated or changed before python is even 
invoked.

Then again, I may be misunderstanding the issue.



-Original Message-
From: Python-list  On 
Behalf Of Jach Feng
Sent: Tuesday, January 24, 2023 2:21 AM
To: python-list@python.org
Subject: Re: How to make argparse accept "-4^2+5.3*abs(-2-1)/2" string argument?

cameron...@gmail.com 在 2023年1月24日 星期二下午2:05:33 [UTC+8] 的信中寫道:
> 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 
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

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


Re: bool and int

2023-01-24 Thread Chris Angelico
On Wed, 25 Jan 2023 at 08:22, MRAB  wrote:
> For backwards compatibility, bool was made a subclass of int.

Plus, it's really REALLY handy in quite a lot of situations.

> > C# is pickier, which I guess is a good thing.
>

Nope, not a good thing. Actually a highly frustrating thing on those
occasions when I have to write C# code.

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


Re: bool and int

2023-01-24 Thread MRAB

On 2023-01-24 20:32, Weatherby,Gerard wrote:

https://peps.python.org/pep-0285/

From: Python-list  on behalf of 
rbowman 
Date: Tuesday, January 24, 2023 at 3:01 PM
To: python-list@python.org 
Subject: Re: bool and int


bool is a subtype of integer. I never dug that deep into Python's guts but
I assume it goes back to boolean being an afterthought in C. Some people
fancy it up with #defines but I always use int.  0 is false, anything else
is true.

bool was introduced early in Python 2. Before then 0 and 1 were used for 
false and true, like in C, which also gained 'false' and 'true'.


For backwards compatibility, bool was made a subclass of int.


C# is pickier, which I guess is a good thing.


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


Re: bool and int

2023-01-24 Thread Weatherby,Gerard
https://peps.python.org/pep-0285/

From: Python-list  on 
behalf of rbowman 
Date: Tuesday, January 24, 2023 at 3:01 PM
To: python-list@python.org 
Subject: Re: bool and int


bool is a subtype of integer. I never dug that deep into Python's guts but
I assume it goes back to boolean being an afterthought in C. Some people
fancy it up with #defines but I always use int.  0 is false, anything else
is true.

C# is pickier, which I guess is a good thing.
--
https://urldefense.com/v3/__https://mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!hdxuMNsprOXvH5ouxGfbGLLq6wuXs-_gOESRVYUxDsHYCmlrpv9ru-WYMziYU4FRdum02bS6DfRnNDnCNQ$
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: bool and int

2023-01-24 Thread Weatherby,Gerard
Booleans work exactly the way the language documentation says they work:

Booleans (bool)
These represent the truth values False and True. The two objects representing 
the values False and True are the only Boolean objects. The Boolean type is a 
subtype of the integer type, and Boolean values behave like the values 0 and 1, 
respectively, in almost all contexts, the exception being that when converted 
to a string, the strings "False" or "True"are returned, respectively.

https://docs.python.org/3/reference/datamodel.html#the-standard-type-hierarchy

From: Python-list  on 
behalf of Dino 
Date: Tuesday, January 24, 2023 at 3:04 PM
To: python-list@python.org 
Subject: bool and int
*** Attention: This is an external email. Use caution responding, opening 
attachments or clicking on links. ***

$ python
Python 3.8.10 (default, Mar 15 2022, 12:22:08)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
 >>> b = True
 >>> isinstance(b,bool)
True
 >>> isinstance(b,int)
True
 >>>

WTF!

--
https://urldefense.com/v3/__https://mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!jPbvUX9ZXFGYt-q850YI6aQ7ET7BwbF-LIT4XT7MKKwF9OSOgqnaHdM4MbQ7p6YaRCYJYXZ4-XiT3Sko$
-- 
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-24 Thread Mike Baskin via Python-list
Can you stop please


Sent from Yahoo Mail for iPhone


On Tuesday, January 24, 2023, 10:12 AM, Thomas Passin  
wrote:

On 1/23/2023 9:12 PM, Chris Angelico wrote:
> 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?

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


bool and int

2023-01-24 Thread Dino



$ python
Python 3.8.10 (default, Mar 15 2022, 12:22:08)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> b = True
>>> isinstance(b,bool)
True
>>> isinstance(b,int)
True
>>>

WTF!

--
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-24 Thread Mike Baskin via Python-list
Stop please


Sent from Yahoo Mail for iPhone


On Tuesday, January 24, 2023, 1:05 AM, Cameron Simpson  wrote:

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



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


Re: bool and int

2023-01-24 Thread rbowman
On Mon, 23 Jan 2023 23:22:00 -0500, Dino wrote:

> $ python Python 3.8.10 (default, Mar 15 2022, 12:22:08)
> [GCC 9.4.0] on linux Type "help", "copyright", "credits" or "license"
> for more information.
>  >>> b = True isinstance(b,bool)
> True
>  >>> isinstance(b,int)
> True
>  >>>
>  >>>
> WTF!


>>> b = True
>>> isinstance(b, bool)
True
>>> isinstance(b, int)
True
>>> c = b + 10
>>> print(c)
11
>>> b = False
>>> c = b + 10
>>> print(c)
10


bool is a subtype of integer. I never dug that deep into Python's guts but 
I assume it goes back to boolean being an afterthought in C. Some people 
fancy it up with #defines but I always use int.  0 is false, anything else 
is true.

C# is pickier, which I guess is a good thing. 
-- 
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-24 Thread Jach Feng
cameron...@gmail.com 在 2023年1月24日 星期二下午2:05:33 [UTC+8] 的信中寫道:
> 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 
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?

2023-01-24 Thread Michael Torrie
On 1/23/23 18:58, Jach Feng wrote:
> More pathonic, but don't work. The '--' must be at index 1:-)

I'm very confused.  Why are you even using argparse, since if you put --
at index 1 then argparse wont't do any argument parsing at all.  If all
you want is the expression on the command line, just access it directly.
 If it's spread out with spaces, you can do something like this to put
it back together:

expression = " ".join(sys.argv[1:]

Otherwise the standard unix way of doing this is to require the user to
either provide the -- himself, or put the expression in quotes so it's
one unit.



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


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

2023-01-24 Thread Weatherby,Gerard
I understand we all want to be helpful and friendly, but it seems to me that 
helping someone use the wrong tool for the job isn’t really helpful in the long 
run.

argparse is for parsing command line arguments.  It’s the wrong tool for this 
job.

As Chris Angelico already said: This entire thread is a massive "how can I use 
X to do Y?" problem.



From: Python-list  on 
behalf of Thomas Passin 
Date: Tuesday, January 24, 2023 at 10:23 AM
To: Mike Baskin , 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. ***

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 
>   >
>
> On Tuesday, January 24, 2023, 10:12 AM, Thomas Passin
>  wrote:
>
> On 1/23/2023 9:12 PM, Chris Angelico wrote:
>  > 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?
>
> 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://urldefense.com/v3/__https://mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!g3YRQIW4wminyTVpjV9kATW-QRZ1YhvDi82oJvatyhNe_OABSPTU9c8KIOlztBNofs690OPwUdIykR5ILj4$
> 
>   >
>

--
https://urldefense.com/v3/__https://mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!g3YRQIW4wminyTVpjV9kATW-QRZ1YhvDi82oJvatyhNe_OABSPTU9c8KIOlztBNofs690OPwUdIykR5ILj4$
-- 
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-24 Thread Thomas Passin

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 

On Tuesday, January 24, 2023, 10:12 AM, Thomas Passin 
 wrote:


On 1/23/2023 9:12 PM, Chris Angelico wrote:
 > On Tue, 24 Jan 2023 at 13:09, Jach Feng mailto:jf...@ms4.hinet.net>> wrote:
 >>
 >> Chris Angelico 在 2023年1月24日 星期二清晨5:00:27 [UTC+8] 的信中
寫道:
 >>> On Tue, 24 Jan 2023 at 07:47, Cameron Simpson 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


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

2023-01-24 Thread Thomas Passin

On 1/23/2023 9:12 PM, Chris Angelico wrote:

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?


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