Re: Are there any easy-to-use Visual Studio C# WinForms-like GUI designers in the Python world for Tk?

2023-12-29 Thread Jach Feng via Python-list
Félix An 在 2023年12月29日 星期五下午2:05:24 [UTC+13] 的信中寫道:
> I'm used to C# WinForms, which has an easy-to-use drag-and-drop GUI 
> designer in Visual Studio. Is there anything similar for Tk? How about 
> Qt? What do you recommend as the easiest way to create GUI programs in 
> Python, similar to the ease of use of C# WinForms?
For tkinter, there is a GUI designer,
http://page.sourceforge.net/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Windows Gui Frontend

2023-04-03 Thread Jach Feng
Jim Schwartz 在 2023年4月1日 星期六晚上8:00:19 [UTC+8] 的信中寫道:
> I have another question. I have an app written in python, but I want to add 
> a windows GUI front end to it. Can this be done in python? What packages 
> would allow me to do that? 
> 
> 
> 
> Thanks.
There is a GUI Generator for tkinter
https://page.sourceforge.net/

--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?

2023-01-29 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
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: How to make argparse accept "-4^2+5.3*abs(-2-1)/2" string argument?

2023-01-28 Thread Jach Feng
Mark Bourne 在 2023年1月28日 星期六晚上10:00:01 [UTC+8] 的信中寫道:
> 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 
> > 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 which may make someone 
> > feel uneasy.
> If you can use it in the way you want, that's great. I thought you were 
> asking here because you *couldn't* use it the way you want. 
> 
> You're writing a command-line application. Your users are either 
> already familiar with the conventions of command-line applications, or 
> they'll soon need to be. 
> 
> If your application supports options beginning with a "-" (which is what 
> argparse gives you, even if only the default "-h" and "--help" options 
> are actually valid), and you also need to be able to pass positional 
> arguments which begin with a "-", you need some way for the user to 
> indicate whether any particular argument is an option or positional. 
> Using "--" to indicate that all subsequent arguments are positional, not 
> options, is a common convention. 
> 
> Some of your users might already be familiar with that convention from 
> other command-line tools. By using the same convention, you'd be making 
> it easier for them to use yours. Others might not already be familiar 
> with the convention, but by being consistent with other tools you'd 
> still be helping them when they eventually do come across it elsewhere. 
> You can always include an explanation of using "--" in your usage output 
> or other documentation. 
> 
> Apart from dealing with how to pass an argument beginning with "-", your 
> users will also likely have to deal with the features of whichever shell 
> they're using, which you have no control over. For example, it's quite 
> common to need to enclose an argument in quotes if it contains spaces. 
> It may also be necessary to use quotes if certain special characters are 
> used, such as "*", "?" or "$" (perhaps "%" on Windows).
> > 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, 
> > - 
> > e:\Works\Python>py infix2postfix.py 
> > usage: infix2postfix.py [-h] infix 
> > infix2postfix.py: error: the following arguments are required: infix 
> > 
> > e:\Works\Python>py infix2postfix.py -h 
> > usage: infix2postfix.py [-h] infix 
> > 
> > Convert infix notation to postfix 
> > 
> > positional arguments: 
> > infix Put equations in quote if there is space in it and separate each one 
> > with a comma, ie. 
> > "-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)))" 
> > 
> > optional arguments: 
> > -h, --help show this help message and exit 
> > - 
> > 
> > comparing with code using the argparse below, 
> > 
> > import argparse 
> > 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', 
> > help='Put equations in quote if there is space in it and separate each one 
> > with a comma, ie. "{}"'.format(sample))

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

2023-01-28 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 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 which may make someone feel 
uneasy.

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,
-
e:\Works\Python>py infix2postfix.py
usage: infix2postfix.py [-h] infix
infix2postfix.py: error: the following arguments are required: infix

e:\Works\Python>py infix2postfix.py -h
usage: infix2postfix.py [-h] infix

Convert infix notation to postfix

positional arguments:
  infix   Put equations in quote if there is space in it and separate each 
one with a comma, ie.
  "-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)))"

optional arguments:
  -h, --help  show this help message and exit
-

comparing with code using the argparse below,

import argparse
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', 
help='Put equations in quote if there is space in it and separate each one with 
a comma, ie. "{}"'.format(sample))

import sys
if len(sys.argv) > 1 and not '-h' in sys.argv:
sys.argv.insert(1, '--')
args = parser.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-25 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 appreciate every comment/suggestion in this thread even sometimes I may not 
fully understand what they mean. To me, argparse has been just a tool which I 
can use in a CLI app. I use it as long as the situation allows, even it's as 
simple as only one positional argument is needed. Now I understand some oppose 
this idea and saying that you shouldn't use a kitchen knife to cut a cake:-)
-- 
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-25 Thread Jach Feng
Chris Angelico 在 2023年1月25日 星期三下午1:16:25 [UTC+8] 的信中寫道:
> 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
I just don't get what you mean?

> You're still not really using argparse as an argument parser. Why not just do 
> your own -h checking?

Is a math equation not qualified as a command line "argument"? What criteria do 
you use when judging the quality of an "argument"?

> Stop trying to use argparse for what it's not designed for,

Even the author considers a positional argument begin with '-' is a legal 
argument. Below is a quote from its manual.

"If you have positional arguments that must begin with - and don’t look like 
negative numbers, you can insert the pseudo-argument '--' which tells 
parse_args() that everything after that is a positional argument"

> and then wondering why it isn't doing what you expect it to magically know."

I don't expect magic, I expect the consistency of a parser.
-- 
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: 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: 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-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 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 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


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

2023-01-22 Thread Jach Feng
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


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

2023-01-21 Thread Jach Feng
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 enter escape character in a positional string argument from the command line?

2022-12-21 Thread Jach Feng
Chris Angelico 在 2022年12月21日 星期三下午1:02:01 [UTC+8] 的信中寫道:
> On Wed, 21 Dec 2022 at 15:28, Jach Feng  wrote: 
> > That's what I am taking this path under Windows now, the ultimate solution 
> > before Windows has shell similar to bash:-)
> Technically, Windows DOES have a shell similar to bash. It's called 
> bash. :) The trouble is, most people use cmd.exe instead. 
> 
> ChrisA
Really? Where? I can't find it in my Windows 8.1
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to enter escape character in a positional string argument from the command line?

2022-12-20 Thread Jach Feng
ery...@gmail.com 在 2022年12月20日 星期二中午12:35:52 [UTC+8] 的信中寫道:
> On 12/19/22, Jach Feng  wrote: 
> > 
> > That's really good for Linux user! How about Windows?
> In CMD, typing the "^" escape character at the end of a line ignores 
> the newline and prompts for "more" input. If you press enter again, 
> you'll get another "more" prompt in which you can write the rest of 
> the command line. Command-line arguments are separated by spaces, so 
> you have to start the next line with a space if you want it to be a 
> new argument. Also, "^" is a literal character when it's in a 
> double-quoted string, which requires careful use of quotes. For 
> example: 
> 
> C:\>py -c "import sys; print(sys.orig_argv[3:])" spam^ 
> More? 
> More? eggs^ 
> More? 
> More? " and spam" 
> ['spam\n', 'eggs\n and spam'] 
> 
> The above is easier in PowerShell, which supports entering multiline 
> strings without having to escape the newline. The second-level prompt 
> is ">> ". For example: 
> 
> > py -c "import sys; print(sys.orig_argv[3:])" spam" 
> >> " eggs" 
> >> and spam" 
> ['spam\n', 'eggs\n and spam']
Thanks for the information. No idea Windows CMD can take such a trick to enter 
"\n" :-)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to enter escape character in a positional string argument from the command line?

2022-12-20 Thread Jach Feng
Thomas Passin 在 2022年12月20日 星期二上午11:36:41 [UTC+8] 的信中寫道:
> On 12/19/2022 9:24 PM, Jach Feng wrote: 
> > Mark Bourne 在 2022年12月20日 星期二凌晨4:49:13 [UTC+8] 的信中寫道: 
> >> Jach Feng wrote: 
> >>> I have a script using the argparse module. I want to enter the string 
> >>> "step\x0A" as one of its positional arguments. I expect this string has a 
> >>> length of 5, but it gives 8. Obviously the escape character didn't 
> >>> function correctly. How to do it? 
> >> That depends on the command-line shell you're calling your script from. 
> >> 
> >> In bash, you can include a newline in a quoted string: 
> >> ./your_script 'step 
> >> ' 
> >> (the closing quote is on the next line) 
> >> 
> >> Or if you want to do it on a single line (or use other escape 
> >> sequences), you can use e.g.: 
> >> ./your_script $'step\x0a' 
> >> (dollar sign before a single-quoted string which contains escape 
> >> sequences) 
> >> 
> >> -- 
> >> Mark. 
> > That's really good for Linux user! How about Windows?
> One way is to process the argument after it gets into Python rather than 
> before. How hard that will be depends on how general you need the 
> argument to be. For your actual example, the argument comes into Python 
> as if it were 
> 
> arg1 = r"step\x0A" # or "step\\x0a" 
> 
> You can see if there is an "\\x": 
> 
> pos = arg1.find('\\x') # 4 
> 
> Replace or use a regex to replace it: 
> 
> arg1_fixed = arg1.replace('\\x0A', '\n') 
> 
> Naturally, if "\\x0A" is only a special case and other combinations are 
> possible, you will need to figure out what you need and do some more 
> complicated processing.
That's what I am taking this path under Windows now, the ultimate solution 
before Windows has shell similar to bash:-)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to enter escape character in a positional string argument from the command line?

2022-12-19 Thread Jach Feng
Mark Bourne 在 2022年12月20日 星期二凌晨4:49:13 [UTC+8] 的信中寫道:
> Jach Feng wrote: 
> > I have a script using the argparse module. I want to enter the string 
> > "step\x0A" as one of its positional arguments. I expect this string has a 
> > length of 5, but it gives 8. Obviously the escape character didn't function 
> > correctly. How to do it?
> That depends on the command-line shell you're calling your script from. 
> 
> In bash, you can include a newline in a quoted string: 
> ./your_script 'step 
> ' 
> (the closing quote is on the next line) 
> 
> Or if you want to do it on a single line (or use other escape 
> sequences), you can use e.g.: 
> ./your_script $'step\x0a' 
> (dollar sign before a single-quoted string which contains escape sequences) 
> 
> -- 
> Mark.
That's really good for Linux user! How about Windows?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to convert a raw string r'\xdd' to '\xdd' more gracefully?

2022-12-12 Thread Jach Feng
moi 在 2022年12月12日 星期一下午5:38:50 [UTC+8] 的信中寫道:
> >>> ast.literal_eval("r'\x7a'") == ast.literal_eval("r'z'") 
> True 
> >>> ast.literal_eval("r'\xe0'") == ast.literal_eval("r'à'") 
> True 
> >>> ast.literal_eval("r'\x9c'") == ast.literal_eval("r'œ'") 
> False 
> 
> - 
> 
> 
> >>> print(codecs.decode(r'z', 'unicode-escape')) 
> z 
> >>> print(codecs.decode(r'g\hz', 'unicode-escape')) 
> g\hz 
> >>> print(codecs.decode(r'g\az', 'unicode-escape')) 
> g\u0007z 
> >>> print(codecs.decode(r'g\nz', 'unicode-escape')) 
> g 
> z 
> >>> 
> print(codecs.decode(r'abcü', 'unicode-escape')) 
> abcü 
> >>>
I have a different result:-)

>>> print(codecs.decode(r'g\hz', 'unicode-escape'))
:1: DeprecationWarning: invalid escape sequence '\h'
g\hz
>>> print(codecs.decode(r'g\az', 'unicode-escape'))
gz  # with a companioning bell
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to convert a raw string r'\xdd' to '\xdd' more gracefully?

2022-12-09 Thread Jach Feng
Weatherby,Gerard 在 2022年12月9日 星期五晚上9:36:18 [UTC+8] 的信中寫道:
> That’s actually more of a shell question than a Python question. How you pass 
> certain control characters is going to depend on the shell, operating system, 
> and possibly the keyboard you’re using. (e.g. https://www.alt-codes.net). 
> 
> Here’s a sample program. The dashes are to help show the boundaries of the 
> string 
> 
> #!/usr/bin/env python3 
> import argparse 
> import logging 
> 
> 
> parser = 
> argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
>  
> parser.add_argument('data') 
> args = parser.parse_args() 
> print(f'Input\n: -{args.data}- length {len(args.data)}') 
> for c in args.data: 
> print(f'{ord(c)} ',end='') 
> print() 
> 
> 
> Using bash on Linux: 
> 
> ./cl.py '^M 
> ' 
> Input 
> - 
> - length 3 
> 13 32 10
> From: Python-list  on 
> behalf of Jach Feng  
> Date: Thursday, December 8, 2022 at 9:31 PM 
> To: pytho...@python.org  
> Subject: Re: How to convert a raw string r'xdd' to 'xdd' more gracefully? 
> *** Attention: This is an external email. Use caution responding, opening 
> attachments or clicking on links. ***
> Jach Feng 在 2022年12月7日 星期三上午10:23:20 [UTC+8] 的信中寫道: 
> > s0 = r'\x0a' 
> > At this moment it was done by 
> > 
> > def to1byte(matchobj): 
> > return chr(int('0x' + matchobj.group(1), 16)) 
> > s1 = re.sub(r'\\x([0-9a-fA-F]{2})', to1byte, s0) 
> > 
> > But, is it that difficult on doing this simple thing? 
> > 
> > --Jach 
> The whold story is, 
> 
> I had a script which accepts an argparse's positional argument. I like this 
> argument may have control character embedded in when required. So I make a 
> post "How to enter escape character in a positional string argument from the 
> command line? on DEC05. But there is no response. I assume that there is no 
> way of doing it and I have to convert it later after I get the whole string 
> from the command line. 
> 
> I made this convertion using the chr(int(...)) method but not satisfied with. 
> That why this post came out. 
> 
> At this moment the conversion is done almost the same as Peter's 
> codecs.decode() method but without the need of importing codecs module:-) 
> 
> def to1byte(matchobj): 
> return matchobj.group(0).encode().decode("unicode-escape")
> -- 
> https://urldefense.com/v3/__https://mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!hcg9ULzmtVUzMJ87Emlfsf6PGAfC-MEzUs3QQNVzWwK4aWDEtePG34hRX0ZFVvWcqZXRcM67JkkIg-l-K9vB$<https://urldefense.com/v3/__https:/mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!hcg9ULzmtVUzMJ87Emlfsf6PGAfC-MEzUs3QQNVzWwK4aWDEtePG34hRX0ZFVvWcqZXRcM67JkkIg-l-K9vB$>

> That’s actually more of a shell question than a Python question. How you pass 
> certain control characters is going to depend on the shell, operating system, 
> and possibly the keyboard you’re using. (e.g. https://www.alt-codes.net).

You are right, that's why I found later that it's easier to enter it using a 
preferred pattern. But there is a case, as moi mentioned in his previous post, 
will cause failure when a Windows path in the form of \xdd just happen in the 
string:-(
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to convert a raw string r'\xdd' to '\xdd' more gracefully?

2022-12-09 Thread Jach Feng
moi 在 2022年12月9日 星期五晚上11:41:20 [UTC+8] 的信中寫道:
> PS C:\humour> py38 sysargwithliteral.py a\x0ab\x09c\x0a\x80uro\x0ax\x08z 
> cp1252 
> a 
> b c 
> €uro 
> z 
> 
> PS C:\humour> $a = py38 sysargwithliteral.py a\x0ab\x09c\x0a\x80uro\x0ax\x08z 
> cp1252 
> 
> PS C:\humour> licp($a) 
> a U+0061 
> b U+0062 
> U+0009 
> c U+0063 
> € U+20AC 
> u U+0075 
> r U+0072 
> o U+006F 
> x U+0078 
> U+0008 
> z U+007A 
> 
> PS C:\humour> 
> 
> PS C:\humour> py38 sysargwithliteral.py 
> a\u000ab\u0009c\u000a\u20acuro\u000ax\u0008z\u000aend\U0001f60a unicode 
> a 
> b c 
> €uro 
> z 
> end 
> 
> PS C:\humour> 
> 
> PS C:\humour> py38 sysargwithliteral.py a\x0ab\x09c\x0a\x80uro\x0ax\x08z 
> cp1252 | py38 -c "import sys; s = sys.stdin.read(); print(s.rstrip())" 
> a 
> b c 
> €uro 
> z 
> 
> PS C:\humour> 
> Note: In a terminal "\t" is correct.
Where is the sysargwithliteral.py?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to convert a raw string r'\xdd' to '\xdd' more gracefully?

2022-12-08 Thread Jach Feng
Jach Feng 在 2022年12月7日 星期三上午10:23:20 [UTC+8] 的信中寫道:
> s0 = r'\x0a' 
> At this moment it was done by 
> 
> def to1byte(matchobj): 
> return chr(int('0x' + matchobj.group(1), 16)) 
> s1 = re.sub(r'\\x([0-9a-fA-F]{2})', to1byte, s0) 
> 
> But, is it that difficult on doing this simple thing? 
> 
> --Jach
The whold story is,

I had a script which accepts an argparse's positional argument. I like this 
argument may have control character embedded in when required. So I make a post 
"How to enter escape character in a positional string argument from the command 
line? on DEC05. But there is no response. I assume that there is no way of 
doing it and I have to convert it later after I get the whole string from the 
command line.

I made this convertion using the chr(int(...)) method but not satisfied with. 
That why this post came out.

At this moment the conversion is done almost the same as Peter's 
codecs.decode() method but without the need of importing codecs module:-)

def to1byte(matchobj):
return matchobj.group(0).encode().decode("unicode-escape")
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to convert a raw string r'\xdd' to '\xdd' more gracefully?

2022-12-08 Thread Jach Feng
Jach Feng 在 2022年12月7日 星期三上午10:23:20 [UTC+8] 的信中寫道:
> s0 = r'\x0a' 
> At this moment it was done by 
> 
> def to1byte(matchobj): 
> return chr(int('0x' + matchobj.group(1), 16)) 
> s1 = re.sub(r'\\x([0-9a-fA-F]{2})', to1byte, s0) 
> 
> But, is it that difficult on doing this simple thing? 
> 
> --Jach
I find another answer on the web.

>>> s0 = r'\x0a'
>>> s0.encode('Latin-1').decode('unicode-escape')
'\n'
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to convert a raw string r'\xdd' to '\xdd' more gracefully?

2022-12-07 Thread Jach Feng
Peter Otten 在 2022年12月8日 星期四清晨5:17:59 [UTC+8] 的信中寫道:
> On 07/12/2022 03:23, Jach Feng wrote: 
> > s0 = r'\x0a' 
> > At this moment it was done by 
> > 
> > def to1byte(matchobj): 
> > return chr(int('0x' + matchobj.group(1), 16)) 
> > s1 = re.sub(r'\\x([0-9a-fA-F]{2})', to1byte, s0) 
> > 
> > But, is it that difficult on doing this simple thing?
> >>> import codecs 
> >>> codecs.decode(r"\x68\x65\x6c\x6c\x6f\x0a", "unicode-escape") 
> 'hello\n'
Thank you. What I really want to handle is to any r'\xdd'. The r'\x0a' is for 
example. Sorry, didn't describe it clearly:-)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to convert a raw string r'\xdd' to '\xdd' more gracefully?

2022-12-07 Thread Jach Feng
Roel Schroeven 在 2022年12月7日 星期三下午4:42:48 [UTC+8] 的信中寫道:
> Op 7/12/2022 om 4:37 schreef Jach Feng:
> > MRAB 在 2022年12月7日 星期三上午11:04:43 [UTC+8] 的信中寫道: 
> > > On 2022-12-07 02:23, Jach Feng wrote: 
> > > > s0 = r'\x0a' 
> > > > At this moment it was done by 
> > > > 
> > > > def to1byte(matchobj): 
> > > > return chr(int('0x' + matchobj.group(1), 16)) 
> > > > s1 = re.sub(r'\\x([0-9a-fA-F]{2})', to1byte, s0) 
> > > > 
> > > > But, is it that difficult on doing this simple thing? 
> > > > 
> > > You could try this: 
> > > 
> > > >>> s0 = r'\x0a' 
> > > >>> ast.literal_eval('"%s"' % s0) 
> > > '\n' 
> > Not work in my system:-( 
> > 
> > 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. 
> > >>> s0 = r'\x0a' 
> > >>> import ast 
> > >>> ast.literal_eval("%s" % s0) 
> > Traceback (most recent call last): 
> > File "", line 1, in  
> > File "C:\Users\Jach\AppData\Local\Programs\Python\Python38-32\lib\ast.py", 
> > line 59, in literal_eval 
> > node_or_string = parse(node_or_string, mode='eval') 
> > File "C:\Users\Jach\AppData\Local\Programs\Python\Python38-32\lib\ast.py", 
> > line 47, in parse 
> > return compile(source, filename, mode, flags, 
> > File "", line 1 
> > \x0a 
> > ^ 
> > SyntaxError: unexpected character after line continuation character
> You missed a pair of quotes. They are easily overlooked but very 
> important. The point is to wrap your string in another pair of quotes so 
> it becomes a valid Python string literal in a Python string which can 
> then be passed to ast.literal_eval(). Works for me: 
> 
> In [7]: s0 = r'\x0a' 
> 
> In [8]: import ast 
> 
> In [9]: ast.literal_eval('"%s"' % s0) 
> Out[9]: '\n' 
> 
> -- 
> "Experience is that marvelous thing that enables you to recognize a 
> mistake when you make it again." 
> -- Franklin P. Jones
Thank you for notifying me. I did notice those  ''' in MRAB's post, but didn't 
figure out what it is at that time:-(
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to convert a raw string r'\xdd' to '\xdd' more gracefully?

2022-12-07 Thread Jach Feng
Thomas Passin 在 2022年12月7日 星期三中午12:51:32 [UTC+8] 的信中寫道:
> On 12/6/2022 9:23 PM, Jach Feng wrote: 
> > s0 = r'\x0a' 
> > At this moment it was done by 
> > 
> > def to1byte(matchobj): 
> > return chr(int('0x' + matchobj.group(1), 16)) 
> > s1 = re.sub(r'\\x([0-9a-fA-F]{2})', to1byte, s0) 
> > 
> > But, is it that difficult on doing this simple thing? 
> > 
> > --Jach
> I'm not totally clear on what you are trying to do here. But: 
> 
> s1 = r'\xdd' # s1[2:] = 'dd' 
> n1 = int(s1[2:], 16) # = 221 decimal or 0xdd in hex 
> # So 
> chr(n1) == 'Ý' # True 
> # and 
> '\xdd' == 'Ý' # True 
> 
> So the conversion you want seems to be chr(int(s1[2:], 16)). 
> 
> Of course, this will only work if the input string is exactly four 
> characters long, and the first two characters are r'\x', and the 
> remaining two characters are going to be a hex string representation of 
> a number small enough to fit into a byte. 
> 
> If you know for sure that will be the case, then the conversion above 
> seems to be about as simple as it could be. If those conditions may not 
> always be met, then you need to work out exactly what strings you may 
> need to convert, and what they should be converted to.
Thank you for reminding that the '0x'+ in the to1byte() definition is 
redundant:-)

Just not sure if there is a better way than using chr(int(...)) to do it. 
Yes, for this specific case, slice is much simpler than re.sub().
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to convert a raw string r'\xdd' to '\xdd' more gracefully?

2022-12-06 Thread Jach Feng
MRAB 在 2022年12月7日 星期三上午11:04:43 [UTC+8] 的信中寫道:
> On 2022-12-07 02:23, Jach Feng wrote: 
> > s0 = r'\x0a' 
> > At this moment it was done by 
> > 
> > def to1byte(matchobj): 
> > return chr(int('0x' + matchobj.group(1), 16)) 
> > s1 = re.sub(r'\\x([0-9a-fA-F]{2})', to1byte, s0) 
> > 
> > But, is it that difficult on doing this simple thing? 
> >
> You could try this: 
> 
> >>> s0 = r'\x0a' 
> >>> ast.literal_eval('"%s"' % s0) 
> '\n'
Not work in my system:-(

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.
>>> s0 = r'\x0a'
>>> import ast
>>> ast.literal_eval("%s" % s0)
Traceback (most recent call last):
  File "", line 1, in 
  File "C:\Users\Jach\AppData\Local\Programs\Python\Python38-32\lib\ast.py", 
line 59, in literal_eval
node_or_string = parse(node_or_string, mode='eval')
  File "C:\Users\Jach\AppData\Local\Programs\Python\Python38-32\lib\ast.py", 
line 47, in parse
return compile(source, filename, mode, flags,
  File "", line 1
\x0a
   ^
SyntaxError: unexpected character after line continuation character
-- 
https://mail.python.org/mailman/listinfo/python-list


How to convert a raw string r'\xdd' to '\xdd' more gracefully?

2022-12-06 Thread Jach Feng
s0 = r'\x0a'
At this moment it was done by

def to1byte(matchobj):
return chr(int('0x' + matchobj.group(1), 16))
s1 = re.sub(r'\\x([0-9a-fA-F]{2})', to1byte, s0)

But, is it that difficult on doing this simple thing?

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


Re: Importlib behaves differently when importing pyd file

2022-11-18 Thread Jach Feng
Dieter Maurer 在 2022年11月17日 星期四凌晨1:12:20 [UTC+8] 的信中寫道:
> Jach Feng wrote at 2022-11-15 22:52 -0800: 
> >My working directory d:\Works\Python\ has a package 'fitz' looks like this: 
> > 
> >fitz\ 
> >__init__.py 
> >fitz.py 
> >utils.py 
> >_fitz.pyd 
> > 
> >There is a statement in fitz.py: 
> >return importlib.import_module('fitz._fitz') 
> > 
> >It works fine under Python 3.4 interpreter: 
> >>>> import fitz 
> >>>> 
> > 
> >But under Python 3.8 I get an exception: 
> >>>> import fitz 
> >Traceback(... 
> >... 
> >... 
> >ImportError: DLL load failed while importing _fitz 
> >>>>
> The Python C-API is Python version dependent. 
> Your `_fitz.pyd` may need to be recreated for Python 3.8.
It seems that I have to install pymupdf to use fitz. Thank you.
-- 
https://mail.python.org/mailman/listinfo/python-list


Importlib behaves differently when importing pyd file

2022-11-16 Thread Jach Feng
My working directory d:\Works\Python\ has a package 'fitz' looks like this:

fitz\
__init__.py
fitz.py
utils.py
_fitz.pyd

There is a statement in fitz.py:
return importlib.import_module('fitz._fitz')

It works fine under Python 3.4 interpreter:
>>> import fitz
>>>

But under Python 3.8 I get an exception:
>>> import fitz
Traceback(...
...
...
ImportError: DLL load failed while importing _fitz
>>>

I work under Windows7 64bit with Python 32bit. Can anyone help?


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


Re: for -- else: what was the motivation?

2022-10-10 Thread Jach Feng
Axy 在 2022年10月10日 星期一下午5:55:29 [UTC+8] 的信中寫道:
> On 09/10/2022 03:33, Jach Feng wrote: 
> > The else is always coming with the break, not the for. 
> However, the compiler does not complain. 
Sure, the compiler will not complain even in a IOCCC contest:-)

> > but the [for...else] is insane. 
> Not in Python. 
The confusion is always in human mind.

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


Re: for -- else: what was the motivation?

2022-10-10 Thread Jach Feng
Axy 在 2022年10月8日 星期六上午11:39:44 [UTC+8] 的信中寫道:
> Hi there, 
> 
> this is rather a philosophical question, but I assume I miss something. 
> I don't remember I ever used else clause for years I was with python and 
> my expectation was it executed only if the the main body was never run. 
> Ha-ha! I was caught by this mental trap. 
> 
> So, seriously, why they needed else if the following pieces produce same 
> result? Does anyone know or remember their motivation? 
> 
> Just curious. 
> 
> Axy. 
> 
> print('--- with else') 
> 
> 
> for i in [1,2,3]: 
> print(i) 
> else: 
> print(4) 
> 
> for i in []: 
> print(i) 
> else: 
> print(5) 
> 
> print('--- without else') 
> 
> for i in [1,2,3]: 
> print(i) 
> print(4) 
> 
> for i in []: 
> print(i) 
> print(5)
The else is always coming with the break, not the for. There are [for ...], 
[for...break...], and[for...break...else], but the [for...else] is insane.

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


Re: How to make a variable's late binding crosses the module boundary?

2022-09-01 Thread Jach Feng
ery...@gmail.com 在 2022年9月2日 星期五凌晨12:41:46 [UTC+8] 的信中寫道:
> On 8/31/22, Jach Feng  wrote: 
> > 
> > I found that using "from test import *" in test2.py makes test2's relation 
> > to "test" almost like the function's relation to the module where it is 
> > defined. You can refer to all the variables of the module
> Note that in general, if __all__ doesn't exist to explicitly define 
> the contents of a star import, then it imports everything except names 
> that begin with an underscore.
Thank you for pointing out these important difference:-)

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


Re: How to make a variable's late binding crosses the module boundary?

2022-09-01 Thread Jach Feng
Mark Bourne 在 2022年9月1日 星期四凌晨2:43:40 [UTC+8] 的信中寫道:
> Jach Feng wrote: 
> > Mark Bourne 在 2022年8月29日 星期一下午6:40:59 [UTC+8] 的信中寫道: 
> >> Jach Feng wrote: 
> >>> Chris Angelico 在 2022年8月29日 星期一下午1:58:58 [UTC+8] 的信中寫道: 
> >>>> On Mon, 29 Aug 2022 at 15:54, Jach Feng  wrote: 
> >>>>> 
> >>>>> Richard Damon 在 2022年8月29日 星期一上午10:47:08 [UTC+8] 的信中寫道: 
> >>>>>> On 8/27/22 7:42 AM, Mark Bourne wrote: 
> >>>>>>> Jach Feng wrote: 
> >>>>>>>> I have two files: test.py and test2.py 
> >>>>>>>> --test.py-- 
> >>>>>>>> x = 2 
> >>>>>>>> def foo(): 
> >>>>>>>> print(x) 
> >>>>>>>> foo() 
> >>>>>>>> 
> >>>>>>>> x = 3 
> >>>>>>>> foo() 
> >>>>>>>> 
> >>>>>>>> --test2.py-- 
> >>>>>>>> from test import * 
> >>>>>>>> x = 4 
> >>>>>>>> foo() 
> >>>>>>>> 
> >>>>>>>> - 
> >>>>>>>> Run test.py under Winows8.1, I get the expected result: 
> >>>>>>>> e:\MyDocument>py test.py 
> >>>>>>>> 2 
> >>>>>>>> 3 
> >>>>>>>> 
> >>>>>>>> But when run test2.py, the result is not my expected 2,3,4:-( 
> >>>>>>>> e:\MyDocument>py test2.py 
> >>>>>>>> 2 
> >>>>>>>> 3 
> >>>>>>>> 3 
> >>>>>>>> 
> >>>>>>>> What to do? 
> >>>>>>> 
> >>>>>>> `from test import *` does not link the names in `test2` to those in 
> >>>>>>> `test`. It just binds objects bound to names in `test` to the same 
> >>>>>>> names in `test2`. A bit like doing: 
> >>>>>>> 
> >>>>>>> import test 
> >>>>>>> x = test.x 
> >>>>>>> foo = test.foo 
> >>>>>>> del test 
> >>>>>>> 
> >>>>>>> Subsequently assigning a different object to `x` in one module does 
> >>>>>>> not affect the object assigned to `x` in the other module. So `x = 4` 
> >>>>>>> in `test2.py` does not affect the object assigned to `x` in `test.py` 
> >>>>>>> - that's still `3`. If you want to do that, you need to import `test` 
> >>>>>>> and assign to `test.x`, for example: 
> >>>>>>> 
> >>>>>>> import test 
> >>>>>>> test.x = 4 
> >>>>>>> test.foo() 
> >>>>>>> 
> >>>>>> Yes, fundamental issue is that the statement 
> >>>>>> 
> >>>>>> from x import y 
> >>>>>> 
> >>>>>> makes a binding in this module to the object CURRECTLY bound to x.y to 
> >>>>>> the name y, but if x.y gets rebound, this module does not track the 
> >>>>>> changes. 
> >>>>>> 
> >>>>>> You can mutate the object x.y and see the changes, but not rebind it. 
> >>>>>> 
> >>>>>> If you need to see rebindings, you can't use the "from x import y" 
> >>>>>> form, 
> >>>>>> or at a minimum do it as: 
> >>>>>> 
> >>>>>> 
> >>>>>> import x 
> >>>>>> 
> >>>>>> from x import y 
> >>>>>> 
> >>>>>> then later to get rebindings to x.y do a 
> >>>>>> 
> >>>>>> y = x.y 
> >>>>>> 
> >>>>>> to rebind to the current x.y object. 
> >>>>>> 
> >>>>>> -- 
> >>>>>> Richard Damon 
> >>>>> Yes, an extra "import x" will solve my problem too! Sometimes I am 
> >>>>> wondering why "from x import y" hides x? hum...can't figure out the 
> >>>>> reason:-) 
> >>>>> 
> >>>> "from x import y" doesn't hide x - it just grabs y. Python does what 
> >>&g

Re: How to make a variable's late binding crosses the module boundary?

2022-08-29 Thread Jach Feng
Mark Bourne 在 2022年8月29日 星期一下午6:40:59 [UTC+8] 的信中寫道:
> Jach Feng wrote: 
> > Chris Angelico 在 2022年8月29日 星期一下午1:58:58 [UTC+8] 的信中寫道: 
> >> On Mon, 29 Aug 2022 at 15:54, Jach Feng  wrote: 
> >>> 
> >>> Richard Damon 在 2022年8月29日 星期一上午10:47:08 [UTC+8] 的信中寫道: 
> >>>> On 8/27/22 7:42 AM, Mark Bourne wrote: 
> >>>>> Jach Feng wrote: 
> >>>>>> I have two files: test.py and test2.py 
> >>>>>> --test.py-- 
> >>>>>> x = 2 
> >>>>>> def foo(): 
> >>>>>> print(x) 
> >>>>>> foo() 
> >>>>>> 
> >>>>>> x = 3 
> >>>>>> foo() 
> >>>>>> 
> >>>>>> --test2.py-- 
> >>>>>> from test import * 
> >>>>>> x = 4 
> >>>>>> foo() 
> >>>>>> 
> >>>>>> - 
> >>>>>> Run test.py under Winows8.1, I get the expected result: 
> >>>>>> e:\MyDocument>py test.py 
> >>>>>> 2 
> >>>>>> 3 
> >>>>>> 
> >>>>>> But when run test2.py, the result is not my expected 2,3,4:-( 
> >>>>>> e:\MyDocument>py test2.py 
> >>>>>> 2 
> >>>>>> 3 
> >>>>>> 3 
> >>>>>> 
> >>>>>> What to do? 
> >>>>> 
> >>>>> `from test import *` does not link the names in `test2` to those in 
> >>>>> `test`. It just binds objects bound to names in `test` to the same 
> >>>>> names in `test2`. A bit like doing: 
> >>>>> 
> >>>>> import test 
> >>>>> x = test.x 
> >>>>> foo = test.foo 
> >>>>> del test 
> >>>>> 
> >>>>> Subsequently assigning a different object to `x` in one module does 
> >>>>> not affect the object assigned to `x` in the other module. So `x = 4` 
> >>>>> in `test2.py` does not affect the object assigned to `x` in `test.py` 
> >>>>> - that's still `3`. If you want to do that, you need to import `test` 
> >>>>> and assign to `test.x`, for example: 
> >>>>> 
> >>>>> import test 
> >>>>> test.x = 4 
> >>>>> test.foo() 
> >>>>> 
> >>>> Yes, fundamental issue is that the statement 
> >>>> 
> >>>> from x import y 
> >>>> 
> >>>> makes a binding in this module to the object CURRECTLY bound to x.y to 
> >>>> the name y, but if x.y gets rebound, this module does not track the 
> >>>> changes. 
> >>>> 
> >>>> You can mutate the object x.y and see the changes, but not rebind it. 
> >>>> 
> >>>> If you need to see rebindings, you can't use the "from x import y" form, 
> >>>> or at a minimum do it as: 
> >>>> 
> >>>> 
> >>>> import x 
> >>>> 
> >>>> from x import y 
> >>>> 
> >>>> then later to get rebindings to x.y do a 
> >>>> 
> >>>> y = x.y 
> >>>> 
> >>>> to rebind to the current x.y object. 
> >>>> 
> >>>> -- 
> >>>> Richard Damon 
> >>> Yes, an extra "import x" will solve my problem too! Sometimes I am 
> >>> wondering why "from x import y" hides x? hum...can't figure out the 
> >>> reason:-) 
> >>> 
> >> "from x import y" doesn't hide x - it just grabs y. Python does what 
> >> you tell it to. :) 
> >> 
> >> ChrisA 
> > But I had heard people say that "from x import y" did import the whole x 
> > module into memory, just as "import x" did, not "grabs y" only. Is this 
> > correct?
> `from x import y` does import the whole module x into memory, and adds 
> it to `sys.modules`. But it only binds the name `y` in the namespace of 
> module doing the import (and it binds it to the value of `x.y` at the 
> time the import is done - it doesn't magically keep them in sync if one 
> or the other is later reassigned). 
> 
> The point about the whole module being imported is that you don't save 
> any memory by using `from x import y` to avoid importing some very large 
> object `z` from `x`. Those other large objects might be needed by 
> functions which have been imported (e.g. your `foo` function still needs 
> `x` even if you haven't imported `x` - so it still needs to be loaded 
> into memory) or might be imported and used by other modules importing 
> `x`, so they still have to be loaded when any part of `x` is imported - 
> they just don't have to be bound to names in the importing module's 
> namespace. 
> 
> As Richard mentioned, if `x.y` is a mutable object (such as a list) you 
> can still mutate that object (e.g. add/remove items) and those changes 
> will be seen in both modules. That's because both are still bound to 
> the same object and you're mutating that existing object. If you assign 
> a new list to either, that won't be seen by the other. 
> 
> -- 
> Mark.
When using dot notation to change variable, no matter if 'x.y' is a mutable or 
immutable object, the change
will be seen in both modules except those early bindings.

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


Re: How to make a variable's late binding crosses the module boundary?

2022-08-29 Thread Jach Feng
Chris Angelico 在 2022年8月29日 星期一下午1:58:58 [UTC+8] 的信中寫道:
> On Mon, 29 Aug 2022 at 15:54, Jach Feng  wrote: 
> > 
> > Richard Damon 在 2022年8月29日 星期一上午10:47:08 [UTC+8] 的信中寫道: 
> > > On 8/27/22 7:42 AM, Mark Bourne wrote: 
> > > > Jach Feng wrote: 
> > > >> I have two files: test.py and test2.py 
> > > >> --test.py-- 
> > > >> x = 2 
> > > >> def foo(): 
> > > >> print(x) 
> > > >> foo() 
> > > >> 
> > > >> x = 3 
> > > >> foo() 
> > > >> 
> > > >> --test2.py-- 
> > > >> from test import * 
> > > >> x = 4 
> > > >> foo() 
> > > >> 
> > > >> - 
> > > >> Run test.py under Winows8.1, I get the expected result: 
> > > >> e:\MyDocument>py test.py 
> > > >> 2 
> > > >> 3 
> > > >> 
> > > >> But when run test2.py, the result is not my expected 2,3,4:-( 
> > > >> e:\MyDocument>py test2.py 
> > > >> 2 
> > > >> 3 
> > > >> 3 
> > > >> 
> > > >> What to do? 
> > > > 
> > > > `from test import *` does not link the names in `test2` to those in 
> > > > `test`. It just binds objects bound to names in `test` to the same 
> > > > names in `test2`. A bit like doing: 
> > > > 
> > > > import test 
> > > > x = test.x 
> > > > foo = test.foo 
> > > > del test 
> > > > 
> > > > Subsequently assigning a different object to `x` in one module does 
> > > > not affect the object assigned to `x` in the other module. So `x = 4` 
> > > > in `test2.py` does not affect the object assigned to `x` in `test.py` 
> > > > - that's still `3`. If you want to do that, you need to import `test` 
> > > > and assign to `test.x`, for example: 
> > > > 
> > > > import test 
> > > > test.x = 4 
> > > > test.foo() 
> > > > 
> > > Yes, fundamental issue is that the statement 
> > > 
> > > from x import y 
> > > 
> > > makes a binding in this module to the object CURRECTLY bound to x.y to 
> > > the name y, but if x.y gets rebound, this module does not track the 
> > > changes. 
> > > 
> > > You can mutate the object x.y and see the changes, but not rebind it. 
> > > 
> > > If you need to see rebindings, you can't use the "from x import y" form, 
> > > or at a minimum do it as: 
> > > 
> > > 
> > > import x 
> > > 
> > > from x import y 
> > > 
> > > then later to get rebindings to x.y do a 
> > > 
> > > y = x.y 
> > > 
> > > to rebind to the current x.y object. 
> > > 
> > > -- 
> > > Richard Damon 
> > Yes, an extra "import x" will solve my problem too! Sometimes I am 
> > wondering why "from x import y" hides x? hum...can't figure out the 
> > reason:-) 
> >
> "from x import y" doesn't hide x - it just grabs y. Python does what 
> you tell it to. :) 
> 
> ChrisA
But I had heard people say that "from x import y" did import the whole x module 
into memory, just as "import x" did, not "grabs y" only. Is this correct?

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


Re: How to make a variable's late binding crosses the module boundary?

2022-08-28 Thread Jach Feng
Richard Damon 在 2022年8月29日 星期一上午10:47:08 [UTC+8] 的信中寫道:
> On 8/27/22 7:42 AM, Mark Bourne wrote: 
> > Jach Feng wrote: 
> >> I have two files: test.py and test2.py 
> >> --test.py-- 
> >> x = 2 
> >> def foo(): 
> >>  print(x) 
> >> foo() 
> >> 
> >> x = 3 
> >> foo() 
> >> 
> >> --test2.py-- 
> >> from test import * 
> >> x = 4 
> >> foo() 
> >> 
> >> - 
> >> Run test.py under Winows8.1, I get the expected result: 
> >> e:\MyDocument>py test.py 
> >> 2 
> >> 3 
> >> 
> >> But when run test2.py, the result is not my expected 2,3,4:-( 
> >> e:\MyDocument>py test2.py 
> >> 2 
> >> 3 
> >> 3 
> >> 
> >> What to do? 
> > 
> > `from test import *` does not link the names in `test2` to those in 
> > `test`.  It just binds objects bound to names in `test` to the same 
> > names in `test2`.  A bit like doing: 
> > 
> > import test 
> > x = test.x 
> > foo = test.foo 
> > del test 
> > 
> > Subsequently assigning a different object to `x` in one module does 
> > not affect the object assigned to `x` in the other module. So `x = 4` 
> > in `test2.py` does not affect the object assigned to `x` in `test.py` 
> > - that's still `3`.  If you want to do that, you need to import `test` 
> > and assign to `test.x`, for example: 
> > 
> > import test 
> > test.x = 4 
> > test.foo() 
> >
> Yes, fundamental issue is that the statement 
> 
> from x import y 
> 
> makes a binding in this module to the object CURRECTLY bound to x.y to 
> the name y, but if x.y gets rebound, this module does not track the changes. 
> 
> You can mutate the object x.y and see the changes, but not rebind it. 
> 
> If you need to see rebindings, you can't use the "from x import y" form, 
> or at a minimum do it as: 
> 
> 
> import x 
> 
> from x import y 
> 
> then later to get rebindings to x.y do a 
> 
> y = x.y 
> 
> to rebind to the current x.y object. 
> 
> -- 
> Richard Damon
Yes, an extra "import x" will solve my problem too! Sometimes I am wondering 
why "from x import y" hides x? hum...can't figure out the reason:-)

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


Re: How to make a variable's late binding crosses the module boundary?

2022-08-28 Thread Jach Feng
Mark Bourne 在 2022年8月27日 星期六晚上7:42:37 [UTC+8] 的信中寫道:
> import test 
> test.x = 4 
> test.foo() 
> 
> -- 
> Mark.
Yes, you are right. But because of I am using "from test import *" which makes 
the name "test" disappear in the test2 scope, so this is not work.

I solved this problem by moving "x" to a separate config.py file, import it in 
both test.py and test2.py, and access it using config.x

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


Re: Behavior of the for-else construct

2022-03-03 Thread Jach Feng
I never feel confused by "else" because I always think it in "break...else", 
not "for...else". For those who always think in "for...else" deserves this 
confusion and it can't be just escaped by replacing with another magic word 
such as "then" or "finally" etc:-)

--Jach

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


Re: frozenset can be altered by |=

2021-11-21 Thread Jach Feng
Marco Sulla 在 2021年11月20日 星期六上午5:12:19 [UTC+8] 的信中寫道:
> (venv_3_10) marco@buzz:~$ python 
> Python 3.10.0 (heads/3.10-dirty:f6e8b80d20, Nov 18 2021, 19:16:18) 
> [GCC 10.1.1 20200718] on linux 
> Type "help", "copyright", "credits" or "license" for more information. 
> >>> a = frozenset((3, 4)) 
> >>> a 
> frozenset({3, 4}) 
> >>> a |= {5,} 
> >>> a 
> frozenset({3, 4, 5})
There is no CONSTANT in Pyhton as other languages does. The only way to have it 
is using special naming convention, such as in all capital letters, to remind 
yourself not to re-assign it:-)

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


Re: Definitive guide for Regex

2021-10-01 Thread Jach Feng
Shaozhong SHI 在 2021年9月30日 星期四下午7:29:47 [UTC+8] 的信中寫道:
> Dear All, 
> 
> I am trying to look for a definitive guide for Regex in Python. 
> Can anyone help? 
> 
> Regards, 
> 
> David
Try the rexegg.com which is a site dedicate to this subject and has many 
resources.

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


Re: Ask for help on using re

2021-08-07 Thread Jach Feng
jak 在 2021年8月6日 星期五下午4:10:05 [UTC+8] 的信中寫道:
> Il 05/08/2021 11:40, Jach Feng ha scritto: 
> > I want to distinguish between numbers with/without a dot attached: 
> > 
> >>>> text = 'ch 1. is\nch 23. is\nch 4 is\nch 56 is\n' 
> >>>> re.compile(r'ch \d{1,}[.]').findall(text) 
> > ['ch 1.', 'ch 23.'] 
> >>>> re.compile(r'ch \d{1,}[^.]').findall(text) 
> > ['ch 23', 'ch 4 ', 'ch 56 '] 
> > 
> > I can guess why the 'ch 23' appears in the second list. But how to get rid 
> > of it? 
> > 
> > --Jach 
> >
> import re
> t = 'ch 1. is\nch 23. is\nch 4 is\nch 56 is\n'
> r = re.compile(r'(ch +\d+\.)|(ch +\d+)', re.M) 
> 
> res = r.findall(t) 
> 
> dot = [x[1] for x in res if x[1] != ''] 
> udot = [x[0] for x in res if x[0] != ''] 
> 
> print(f"dot: {dot}") 
> print(f"undot: {udot}") 
> 
> out: 
> 
> dot: ['ch 4', 'ch 56'] 
> undot: ['ch 1.', 'ch 23.']
The result can be influenced by the order of re patterns?

>>> import re
>>> t = 'ch 1. is\nch 23. is\nch 4 is\nch 56 is\n'
>>> re.compile(r'(ch +\d+\.)|(ch +\d+)', re.M).findall(t)
[('ch 1.', ''), ('ch 23.', ''), ('', 'ch 4'), ('', 'ch 56')]

>>> re.compile(r'(ch +\d+)|(ch +\d+\.)', re.M).findall(t)
[('ch 1', ''), ('ch 23', ''), ('ch 4', ''), ('ch 56', '')]

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


Re: Ask for help on using re

2021-08-06 Thread Jach Feng
jak 在 2021年8月6日 星期五下午4:10:05 [UTC+8] 的信中寫道:
> Il 05/08/2021 11:40, Jach Feng ha scritto: 
> > I want to distinguish between numbers with/without a dot attached: 
> > 
> >>>> text = 'ch 1. is\nch 23. is\nch 4 is\nch 56 is\n' 
> >>>> re.compile(r'ch \d{1,}[.]').findall(text) 
> > ['ch 1.', 'ch 23.'] 
> >>>> re.compile(r'ch \d{1,}[^.]').findall(text) 
> > ['ch 23', 'ch 4 ', 'ch 56 '] 
> > 
> > I can guess why the 'ch 23' appears in the second list. But how to get rid 
> > of it? 
> > 
> > --Jach 
> >
> import re
> t = 'ch 1. is\nch 23. is\nch 4 is\nch 56 is\n'
> r = re.compile(r'(ch +\d+\.)|(ch +\d+)', re.M) 
> 
> res = r.findall(t) 
> 
> dot = [x[1] for x in res if x[1] != ''] 
> udot = [x[0] for x in res if x[0] != ''] 
> 
> print(f"dot: {dot}") 
> print(f"undot: {udot}") 
> 
> out: 
> 
> dot: ['ch 4', 'ch 56'] 
> undot: ['ch 1.', 'ch 23.']
> r = re.compile(r'(ch +\d+\.)|(ch +\d+)', re.M) 
That's an interest solution! Where the '|' operator in re.compile() was 
documented?

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


Re: Ask for help on using re

2021-08-06 Thread Jach Feng
ast 在 2021年8月5日 星期四下午11:29:15 [UTC+8] 的信中寫道:
> Le 05/08/2021 à 17:11, ast a écrit :
> > Le 05/08/2021 à 11:40, Jach Feng a écrit : 
> >> I want to distinguish between numbers with/without a dot attached: 
> >> 
> >>>>> text = 'ch 1. is\nch 23. is\nch 4 is\nch 56 is\n' 
> >>>>> re.compile(r'ch \d{1,}[.]').findall(text) 
> >> ['ch 1.', 'ch 23.'] 
> >>>>> re.compile(r'ch \d{1,}[^.]').findall(text) 
> >> ['ch 23', 'ch 4 ', 'ch 56 '] 
> >> 
> >> I can guess why the 'ch 23' appears in the second list. But how to get 
> >> rid of it? 
> >> 
> >> --Jach 
> >> 
> > 
> > >>> import re 
> > 
> > >>> text = 'ch 1. is\nch 23. is\nch 4 is\nch 56 is\n' 
> > 
> > >>> re.findall(r'ch \d+\.', text) 
> > ['ch 1.', 'ch 23.'] 
> > 
> > >>> re.findall(r'ch \d+(?!\.)', text) # (?!\.) for negated look ahead 
> > ['ch 2', 'ch 4', 'ch 56']
> import regex 
> 
> # regex is more powerful that re
> >>> text = 'ch 1. is\nch 23. is\nch 4 is\nch 56 is\n'
> >>> regex.findall(r'ch \d++(?!\.)', text) 
> 
> ['ch 4', 'ch 56'] 
> 
> ## ++ means "possessive", no backtrack is allowed
Can someone explain how the difference appear? I just can't figure it out:-(

>>> text = 'ch 1. is\nch 23. is\nch 4 is\nch 56 is\n'
>>> re.compile(r'ch \d+[^.]').findall(text)
['ch 23', 'ch 4 ', 'ch 56 ']
>>> re.compile(r'ch \d+[^.0-9]').findall(text)
['ch 4 ', 'ch 56 ']

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


Re: on slices, negative indices, which are the equivalent procedures?

2021-08-06 Thread Jach Feng
> > s = "Jack Brandom" 
> > s[3 : -13 : -1] 
> >> 'kcaJ' 
> >> I have no idea how to replace that -13 with a positive index. Is it 
> >> possible at all? 
That's not possible because a positive index is relative to the leftmost item 0

Below is some rules of slice usage which I collected sometimes ago.
-
slice s[i:j:k]
 The result is from start index i to stop index j (excluded) in step k 
(default is 1).  i, j, k can be positive or negative. For example:  s = [1,  2, 
 3,  4,  5]
1. Positive index is relative to the leftmost item (0), negative index is 
relative to the rightmost item (-1). Note: out-of-range index is valid.
   s[-4:5] == s[1:5] == [2,3,4,5]  # index 5 is out-of-range
   s[4:-6:-1] == [5,4,3,2,1]  # index -6 is out-of-range
2. The default index of i and j (When index is omitted) was decided by the sign 
of k. 
For positive k, the start index is 0 (the leftmost item) and the stop index 
is the one after the rightmost item.
   s[:3] == s[0:3] == [1,2,3]
   s[1:] == s[1:5] == [2,3,4,5]
   For negative k, the start index is -1 (the rightmost item) and the stop 
index is one ahead of the leftmost item 
   s[:2:-1] == s[-1:2:-1] == [5,4]
   s[3::-1] == s[3:-6:-1] == [4,3,2,1]
3. The items pick-up order was decided by the sign of k. For positive k, the 
direction is toward the right. For negative k, it is toward the left.
s[1:4] == [2,3,4]
s[3:0:-1] == [4,3,2]
4. Invalid slice will return an empty []
s[2:0] == []
-

--Jach

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


Re: Ask for help on using re

2021-08-05 Thread Jach Feng
Neil 在 2021年8月5日 星期四下午6:36:58 [UTC+8] 的信中寫道:
> Jach Feng  wrote: 
> > I want to distinguish between numbers with/without a dot attached: 
> > 
> >>>> text = 'ch 1. is\nch 23. is\nch 4 is\nch 56 is\n' 
> >>>> re.compile(r'ch \d{1,}[.]').findall(text) 
> > ['ch 1.', 'ch 23.'] 
> >>>> re.compile(r'ch \d{1,}[^.]').findall(text) 
> > ['ch 23', 'ch 4 ', 'ch 56 '] 
> > 
> > I can guess why the 'ch 23' appears in the second list. But how to get rid 
> > of it? 
> > 
> > --Jach
> Does 
> 
> >>> re.findall(r'ch\s+\d+(?![.\d])',text) 
> 
> do what you want? This matches "ch", then any nonzero number of 
> whitespaces, then any nonzero number of digits, provided this is not 
> followed by a dot or another digit.

Yes, the result is what I want. Thank you, Neil!

The solution is more complex than I expect. Have to digest it:-)

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


Ask for help on using re

2021-08-05 Thread Jach Feng
I want to distinguish between numbers with/without a dot attached:

>>> text = 'ch 1. is\nch 23. is\nch 4 is\nch 56 is\n'
>>> re.compile(r'ch \d{1,}[.]').findall(text)
['ch 1.', 'ch 23.']
>>> re.compile(r'ch \d{1,}[^.]').findall(text)
['ch 23', 'ch 4 ', 'ch 56 ']

I can guess why the 'ch 23' appears in the second list. But how to get rid of 
it?

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


Re: Tkinter problem

2021-06-19 Thread Jach Feng
Christian Gollwitzer 在 2021年6月19日 星期六下午1:54:46 [UTC+8] 的信中寫道:
> Am 19.06.21 um 07:16 schrieb Jach Feng:
> > Christian Gollwitzer 在 2021年6月19日 星期六下午12:27:54 [UTC+8] 的信中寫道: 
> >> Am 19.06.21 um 05:59 schrieb Jach Feng: 
> >>>>>> import tkinter as Tk 
> >>>>>> Tk 
> >>>  >>> 'C:\\Users\\jfong\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\tkinter\\__init__.py'>
> >>>  
> >>>>>> from tkinter import * 
> >>>>>> Tk 
> >>>  
> >>>>>> tkinter 
> >>> Traceback (most recent call last): 
> >>> File "", line 1, in  
> >>> NameError: name 'tkinter' is not defined 
> >>>>>> 
> >> What's the point? That has no relation to the question. 
> >> 
> >> "import A as B" does not define A. That's a feature, not a bug. 
> >> 
> >> Christian 
> > No, it's not. It's only because this line triggers my response:-) 
> >> label1 = tkinter.Label(master, text='Hello')
> You have posted this as an answer to Liya's question about Google Colab. 
> What you wrote has nothing to do with it and does not help with Liya's 
> problem. 
> 
> I guess you wanted to post another question? Then please open a new 
> thread. In addition, the question is unclear, you just posted a 
> transcript of three lines of Python. 
> 
> Christian
I posted to point out there is an error in Liya's script. It's not related to 
Colab, but it may relate to his problem. 

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


Re: Tkinter problem

2021-06-19 Thread Jach Feng
Christian Gollwitzer 在 2021年6月19日 星期六下午12:27:54 [UTC+8] 的信中寫道:
> Am 19.06.21 um 05:59 schrieb Jach Feng:
> >>>> import tkinter as Tk 
> >>>> Tk 
> >  > 'C:\\Users\\jfong\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\tkinter\\__init__.py'>
> >  
> >>>> from tkinter import * 
> >>>> Tk 
> >  
> >>>> tkinter 
> > Traceback (most recent call last): 
> > File "", line 1, in  
> > NameError: name 'tkinter' is not defined 
> >>>>
> What's the point? That has no relation to the question. 
> 
> "import A as B" does not define A. That's a feature, not a bug. 
> 
> Christian
No, it's not. It's only because this line triggers my response:-)
> label1 = tkinter.Label(master, text='Hello') 

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


Re: Tkinter problem

2021-06-19 Thread Jach Feng
liyaanns...@gmail.com 在 2021年6月18日 星期五下午2:28:35 [UTC+8] 的信中寫道:
> I am using Colab. How could solve this problem. 
> import tkinter as Tk 
> from tkinter import * 
> import sys 
> import os 
> #create main window 
> master = Tk() 
> master.title("tester") 
> master.geometry("300x100") 
> 
> 
> #make a label for the window 
> label1 = tkinter.Label(master, text='Hello') 
> # Lay out label 
> label1.pack() 
> 
> # Run forever! 
> master.mainloop() 
> The error shows that : 
>  in () 
> 9 
> 10 #create main window 
> ---> 11 master = Tk() 
> 12 master.title("tester") 
> 13 master.geometry("300x100") 
> 
> /usr/lib/python3.7/tkinter/__init__.py in __init__(self, screenName, 
> baseName, className, useTk, sync, use) 
> 2021 baseName = baseName + ext 
> 2022 interactive = 0 
> -> 2023 self.tk = _tkinter.create(screenName, baseName, className, 
> interactive, wantobjects, useTk, sync, use) 
> 2024 if useTk: 
> 2025 self._loadtk() 
> 
> TclError: couldn't connect to display ":0.0"

>>> import tkinter as Tk
>>> Tk

>>> from tkinter import *
>>> Tk

>>> tkinter
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'tkinter' is not defined
>>>

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


Re: Why the list creates in two different ways? Does it cause by the mutability of its elements? Where the Python document explains it?

2021-06-16 Thread Jach Feng
Greg Ewing 在 2021年6月16日 星期三上午7:11:35 [UTC+8] 的信中寫道:
> On 15/06/21 7:32 pm, Jach Feng wrote: 
> > But usually the list creation is not in simple way:-) for example: 
> >>>> a = [1,2] 
> >>>> m = [a for i in range(3)] 
> >>>> m 
> > [[1, 2], [1, 2], [1, 2]] 
> >>>> id(m[0]) == id(m[1]) == id(m[2]) 
> > True
> The first line is only executed once, so you just get one 
> list object [1, 2]. You then refer to that object three times 
> when you build the outer list. 
> 
> To get three different [1, 2] lists you would need something 
> like this:
> m = [[1,2] for i in range(3)]
> This executes the [1, 2] expression 3 times. Because lists are 
> mutable, you can be sure that this will give you 3 distinct 
> list objects. 
> 
> -- 
> Greg
Yes, I know. Here I just want to show another gutter I had found:-). Anyone 
else?

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


Re: Is there a way to get the following result in Python?

2021-06-15 Thread Jach Feng
Peter Otten 在 2021年6月15日 星期二下午2:48:07 [UTC+8] 的信中寫道:
> On 12/06/2021 04:02, Jach Feng wrote: 
> 
> >>>> def foo(): 
> > ... # do something 
> > ... 
> >>>> a = [] 
> >>>> for i in range(3): 
> > ... a.append(foo()) 
> > ... 
> >>>> a 
> > []
> The most natural way to achieve something similar is to replace append() 
> with extend(): 
> 
> >>> def foo(): return ()
> >>> a = [] 
> >>> for i in range(3):
> a.extend(foo()) 
> 
> 
> >>> a 
> []
Yes, return a list and using extend() to collect value is exactly what I need!
Thank you, Christian and Peter.

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


Re: Why the list creates in two different ways? Does it cause by the mutability of its elements? Where the Python document explains it?

2021-06-15 Thread Jach Feng
Greg Ewing 在 2021年6月15日 星期二下午3:01:46 [UTC+8] 的信中寫道:
> On 15/06/21 3:18 pm, Jach Feng wrote: 
> > From a user's point, I don't really care how Python creates thoseinstances, 
> > > either using an already exist one or create a new one, as
> > long as each element (and its sub-element) are independent from each 
> > other so a modification of one will not influence the other. The real 
> > problem is there are different methods can be used to build it and some 
> > will fail in this purpose. The * operator is one of them, but anyone 
> > else? I really like to know:-)
> This really has nothing to do with how the tuples are created. 
> It all depends on what you choose to put in them. 
> 
> When you use square brackets to build a list, you can be sure that 
> it will create a new list object each time. 
> 
> Also, if you create two tuples, by any means, containing distinct 
> elements, you can be sure that you will get two distinct tuple 
> objects. 
> 
> So, if you do this: 
> 
> a = [1, 2] 
> b = [1, 2] 
> c = [1, 2] 
> d = [1, 2] 
> 
> s = (a, b) 
> t = (c, d) 
> 
> then you are guaranteed to get four different list objects and 
> two diffferent tuple objects. 
> 
> Does that help to ease your fears? 
> 
> -- 
> Greg
But usually the list creation is not in simple way:-) for example:
>>> a = [1,2]
>>> m = [a for i in range(3)]
>>> m
[[1, 2], [1, 2], [1, 2]]
>>> id(m[0]) == id(m[1]) == id(m[2])
True

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


Re: Why the list creates in two different ways? Does it cause by the mutability of its elements? Where the Python document explains it?

2021-06-15 Thread Jach Feng
Chris Angelico 在 2021年6月15日 星期二上午5:23:12 [UTC+8] 的信中寫道:
> On Tue, Jun 15, 2021 at 7:11 AM Rob Cliffe via Python-list 
>  wrote: 
> > 
> > This puzzled me, so I played around with it a bit (Python 3.8.3): 
> > 
> > n = [] 
> > for i in range(3): 
> > n.append((1,7,-3,None,"x")) 
> > for i in range(3): 
> > n.append((1,7,-3,None,"x")) 
> > print([id(x) for x in n]) 
> > 
> > a = 4 
> > n = [] 
> > for i in range(3): 
> > n.append((1,7,-3,a,None,"x")) 
> > for i in range(3): 
> > n.append((1,7,-3,a,None,"x")) 
> > print([id(x) for x in n]) 
> > 
> > Output: 
> > 
> > [27164832, 27164832, 27164832, 27164832, 27164832, 27164832] 
> > [30065208, 30065496, 30237192, 30239976, 30240024, 30343928] 
> > 
> > Evidently the compiler is clever enough to pick out a constant tuple and 
> > create (or cause to get created) a single instance of it which is used 
> > when required. Indeed disassembling the code shows that LOAD_CONST is 
> > used to get the tuple. But it obviously can't do that when the tuple 
> > contains a variable.
> Correct. In theory, Python could intern the tuples (as can be done 
> with strings), noticing that it's constructing one that is identical 
> to one it already has, but the effort of doing that is hard to 
> justify. Simpler to just build a brand new tuple every time. 
> 
> ChrisA
From a user's point, I don't really care how Python creates those instances, 
either using an already exist one or create a new one, as long as each element 
(and its sub-element) are independent from each other so a modification of one 
will not influence the other. The real problem is there are different methods 
can be used to build it and some will fail in this purpose. The * operator is 
one of them, but anyone else? I really like to know:-)

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


Is there a way to get the following result in Python?

2021-06-14 Thread Jach Feng
>>> def foo():
... # do something
...
>>> a = []
>>> for i in range(3):
... a.append(foo())
...
>>> a
[]
>>>

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


Why the list creates in two different ways? Does it cause by the mutability of its elements? Where the Python document explains it?

2021-06-14 Thread Jach Feng
>>> n = [(1,2) for i in range(3)]
>>> n
[(1, 2), (1, 2), (1, 2)]
>>> id(n[0]) == id(n[1])  == id(n[2])
True
>>> m = [[1,2] for i in range(3)]
>>> m
[[1, 2], [1, 2], [1, 2]]
>>> id(m[0]) == id(m[1])  == id(m[2])
False
>>>

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


Re: How to debug python + curses? [was: RE: Applying winpdb_reborn]

2021-06-01 Thread Jach Feng
pjfa...@earthlink.net 在 2021年5月31日 星期一上午1:42:43 [UTC+8] 的信中寫道:
> I tried winpdb-reborn some time last year on my Win10 system (python 3.8.3 
> at that time), but could not figure out how to use it to debug a python 
> script that uses the curses module. 
> 
> Does anyone here know if winpdb-reborn or any other debugger can support 
> 2-window debugging for a python script that uses the curses module? It 
> seems to me that a 2-window debugging session is necessary for a python 
> script that uses the curses module because using curses takes over the 
> screen from which the script is started, so debugging output and script 
> output need to be in separate windows. 
> 
> I've been forced to use a logger to trace critical values and program flow 
> for errors in such a script. It works, but it is annoyingly slow to debug 
> that way. 
> 
> TIA for any advice or RTFM you can provide. 
> 
> Peter 
> --
I have no problem on using rpdb2 to debug a script which is based on curses.

My environment are:
Windows Vista, Python 3.4.4, rpdb2 of winpdb-1.4.8, 
curses-2.2-cp34-cp34m-win32.whl, snake.py

Everything are old, but it works:-)

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


Re: [ANN] Free Python tutorial with exercises, cscx.org

2021-04-07 Thread Jach Feng
Rudy Matela 在 2021年4月1日 星期四下午11:13:03 [UTC+8] 的信中寫道:
> Hello python-list members, 
> 
> I would like to announce the following educational project: 
> 
> Computer Science by Example https://cscx.org/ is a collection of short 
> programming exercises. The site can automatically grade students' 
> solutions and it accepts submissions in Python. 
> 
> The exercises start simple, then increase in difficulty and complexity 
> gradually. Here are some examples: 
> 
> * Print "Hello, World!": https://cscx.org/hello 
> * Add two numbers: https://cscx.org/add1 
> * Compute the factorial of a number: https://cscx.org/factorial 
> * Compute the GCD of two numbers: https://cscx.org/gcd 
> * Solve the change-making problem: https://cscx.org/cash 
> 
> The website has a tutorial section covering Python's basics. 
> 
> I tried to make the content easy to use by instructors/lecturers, feel free 
> to use this with your students. The backend of the website is open source 
> and you can find it on 
> https://github.com/rudymatela/udge 
> 
> -- Rudy

Is there any reason a student/beginner learn Python now start from Python2?

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


Re: What's the meaning the "backlog" in the socket.listen(backlog) is?

2021-02-16 Thread Jach Feng
Kushal Kumaran 在 2021年2月17日 星期三下午12:11:04 [UTC+8] 的信中寫道:
> On Tue, Feb 16 2021 at 07:24:30 PM, Jach Feng  wrote: 
> > I am experimenting with multithreading-socket these days. I build a 
> > server to handle each client in a separate thread. All are running on 
> > my local PC. It works fine except the listen() method. 
> > 
> > I set listen(2) and expect to see "error" when more clients than "the 
> > maximum number of queued connections" trying to connect the 
> > server. But, no error!! Even 4 clients can run normally without 
> > problem. 
> > 
> > Am I misunderstanding the meaning of this argument? 
> >
> The argument to listen specifies the maximum number of "outstanding" 
> connection requests. These are connections for which the TCP handshake 
> has completed, but for which the application has not called accept. 
> With your multi-threaded application, I assume each new connection 
> request gets picked up immediately by an accept call, so the queue does 
> not generally grow. 
> 
> If you want a limit on the number of concurrent clients you want to 
> handle, you'll have to implement that yourself. If you're using the 
> threading module, threading.Semaphore will let you implement a simple 
> system. 
> 
> -- 
> regards, 
> kushal

You are right! I do misunderstand its meaning:-(

After the server accepts the 1st connection and paused temperately, I saw the 
2nd and 3rd connection is in pending, and the 4th one was rejected immediately.

Thank you (and Jason) for clarifying this mystery:-)

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


What's the meaning the "backlog" in the socket.listen(backlog) is?

2021-02-16 Thread Jach Feng
I am experimenting with multithreading-socket these days. I build a server to 
handle each client in a separate thread. All are running on my local PC. It 
works fine except the listen() method.

I set listen(2) and expect to see "error" when more clients than "the maximum 
number of queued connections" trying to connect the server. But, no error!! 
Even 4 clients can run normally without problem.

Am I misunderstanding the meaning of this argument?

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


To whom he want to set breakpoint on negative line number in pdb

2020-12-12 Thread Jach Feng
I use .pdbrc file to help debugging. To avoid editing this file frequently when 
source was modified, I need this feature.

I modify the checkline function in pdb.py from
...
line = linecache.getline(filename, lineno, globs)
if not line:

to
...
lines = linecache.getlines(filename, globs)
if lineno < 0:  lineno = len(lines) + lineno + 1  # change to a valid 
positive number, or not
if 1 <= lineno <= len(lines):  line = lines[lineno-1]
else:  line = ''
if not line:
...

This modification is mostly the copy of the getline function body except the 
added line. It will not influence the original pdb usage in anyway.

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