Re: one Liner: Lisprint(x) --> (a, b, c) instead of ['a', 'b', 'c']

2023-02-27 Thread Greg Ewing via Python-list

On 28/02/23 4:24 pm, Hen Hanna wrote:

   is it poss. to peek at the Python-list's  messages 
without joining ?


It's mirrored to the comp.lang.python usenet group, or
you can read it through gmane with a news client.

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


RE: How to escape strings for re.finditer?

2023-02-27 Thread avi.e.gross
I think by now we have given all that is needed by the OP but Dave's answer
strikes me as being able to be a tad faster as a while loop if you are
searching  larger corpus such as an entire ebook or all books as you can do
on books.google.com

I think I mentioned earlier that some assumptions need to apply. The text
needs to be something like an ASCII encoding or seen as code points rather
than bytes. We assume a match should move forward by the length of the
match. And, clearly, there cannot be a match too close to the end.

So a while loop would begin with a variable set to zero to mark the current
location of the search. The condition for repeating the loop is that this
variable is less than or equal to len(searched_text) - len(key)

In the loop, each comparison is done the same way as David uses, or anything
similar enough but the twist is a failure increments the variable by 1 while
success increments by len(key).

Will this make much difference? It might as the simpler algorithm counts
overlapping matches and wastes some time hunting where perhaps it shouldn't.

And, of course, if you made something like this into a search function, you
can easily add features such as asking that you only return the first N
matches or the next N, simply by making it a generator.
So tying this into an earlier discussion, do you want the LAST match info
visible when the While loop has completed? If it was available, it opens up
possibilities for running the loop again but starting from where you left
off.



-Original Message-
From: Python-list  On
Behalf Of Thomas Passin
Sent: Monday, February 27, 2023 9:44 PM
To: python-list@python.org
Subject: Re: How to escape strings for re.finditer?

On 2/27/2023 9:16 PM, avi.e.gr...@gmail.com wrote:
> And, just for fun, since there is nothing wrong with your code, this minor
change is terser:
> 
 example = 'X - abc_degree + 1 + qq + abc_degree + 1'
 for match in re.finditer(re.escape('abc_degree + 1') , example):
> ... print(match.start(), match.end())
> ...
> ...
> 4 18
> 26 40

Just for more fun :) -

Without knowing how general your expressions will be, I think the following
version is very readable, certainly more readable than regexes:

example = 'X - abc_degree + 1 + qq + abc_degree + 1'
KEY = 'abc_degree + 1'

for i in range(len(example)):
 if example[i:].startswith(KEY):
 print(i, i + len(KEY))
# prints:
4 18
26 40

If you may have variable numbers of spaces around the symbols, OTOH, the
whole situation changes and then regexes would almost certainly be the best
approach.  But the regular expression strings would become harder to read.
--
https://mail.python.org/mailman/listinfo/python-list

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


Re: How to escape strings for re.finditer?

2023-02-27 Thread Thomas Passin

On 2/27/2023 9:16 PM, avi.e.gr...@gmail.com wrote:

And, just for fun, since there is nothing wrong with your code, this minor 
change is terser:


example = 'X - abc_degree + 1 + qq + abc_degree + 1'
for match in re.finditer(re.escape('abc_degree + 1') , example):

... print(match.start(), match.end())
...
...
4 18
26 40


Just for more fun :) -

Without knowing how general your expressions will be, I think the 
following version is very readable, certainly more readable than regexes:


example = 'X - abc_degree + 1 + qq + abc_degree + 1'
KEY = 'abc_degree + 1'

for i in range(len(example)):
if example[i:].startswith(KEY):
print(i, i + len(KEY))
# prints:
4 18
26 40

If you may have variable numbers of spaces around the symbols, OTOH, the 
whole situation changes and then regexes would almost certainly be the 
best approach.  But the regular expression strings would become harder 
to read.

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


RE: XXX XXX should stop sending me rude email messages.

2023-02-27 Thread avi.e.gross
Michael,

Although I appreciate much of what you say, I ask humbly and politely that
we change the Subject line for messages like this one. HH is out of range
for now, albeit I think he can still read what we say.

But keeping the name Michael Torrie in the subject line, should be sort of
XXX rated.

And I mean especially in this case where we have no idea what "he" was
writing but it was a private message not intended for the group.

Just kidding BTW. But since YOU are Michael Torrie, I guess you can
broadcast a request to stop mailing yourself if that pleases. I will just no
longer bring your name forth in this context. Feel free to send me email, if
the need arises.

Avi

-Original Message-
From: Python-list  On
Behalf Of Michael Torrie
Sent: Monday, February 27, 2023 9:08 PM
To: python-list@python.org
Subject: Re: Rob Cliffe should stop sending me rude email messages.

On 2/27/23 09:17, Grant Edwards wrote:
> On 2023-02-27, Michael Torrie  wrote:
> 
>> I've been putting off sending this message for days, but the list 
>> noise level is now to the point that it has to be said.
> 
> Ah, I've finially realized why some of those threads have seemed so 
> disjointed to me. Years ago, I plonked all posts which are (like Hen
> Hanna's) submitted via Googole Groups.
> 
> I highly recommend it.
> 
> FWIW, here's the "score" rule for doing that with srln:
> 
> Score:: =-
>   Message-ID: .*googlegroups.com

Thanks for the tip and reminder.  I'll add that to my gmail filter.

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

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


RE: How to escape strings for re.finditer?

2023-02-27 Thread avi.e.gross
Jen,

Can you see what SOME OF US see as ASCII text? We can help you better if we get 
code that can be copied and run as-is.

 What you sent is not terse. It is wrong. It will not run on any python 
interpreter because you somehow lost a carriage return and indent.

This is what you sent:

example = 'X - abc_degree + 1 + qq + abc_degree + 1'
find_string = re.escape('abc_degree + 1') for match in re.finditer(find_string, 
example):
print(match.start(), match.end())

This is code indentedproperly:

example = 'X - abc_degree + 1 + qq + abc_degree + 1'
find_string = re.escape('abc_degree + 1') 
for match in re.finditer(find_string, example):
print(match.start(), match.end())

Of course I am sure you wrote and ran code more like the latter version but 
somewhere in your copy/paste process, 

And, just for fun, since there is nothing wrong with your code, this minor 
change is terser:

>>> example = 'X - abc_degree + 1 + qq + abc_degree + 1'
>>> for match in re.finditer(re.escape('abc_degree + 1') , example):
... print(match.start(), match.end())
... 
... 
4 18
26 40

But note once you use regular expressions, and not in your case, you might 
match multiple things that are far from the same such as matching two repeated 
words of any kind in any case including "and and" and "so so" or finding words 
that have multiple doubled letter as in the  stereotypical bookkeeper. In those 
cases, you may want even more than offsets but also show the exact text that 
matched or even show some characters before and/or after for context.


-Original Message-
From: Python-list  On 
Behalf Of Jen Kris via Python-list
Sent: Monday, February 27, 2023 8:36 PM
To: Cameron Simpson 
Cc: Python List 
Subject: Re: How to escape strings for re.finditer?


I haven't tested it either but it looks like it would work.  But for this case 
I prefer the relative simplicity of:

example = 'X - abc_degree + 1 + qq + abc_degree + 1'
find_string = re.escape('abc_degree + 1') for match in re.finditer(find_string, 
example):
print(match.start(), match.end())

4 18
26 40

I don't insist on terseness for its own sake, but it's cleaner this way.  

Jen


Feb 27, 2023, 16:55 by c...@cskk.id.au:

> On 28Feb2023 01:13, Jen Kris  wrote:
>
>> I went to the re module because the specified string may appear more than 
>> once in the string (in the code I'm writing).
>>
>
> Sure, but writing a `finditer` for plain `str` is pretty easy (untested):
>
>  pos = 0
>  while True:
>  found = s.find(substring, pos)
>  if found < 0:
>  break
>  start = found
>  end = found + len(substring)
>  ... do whatever with start and end ...
>  pos = end
>
> Many people go straight to the `re` module whenever they're looking for 
> strings. It is often cryptic error prone overkill. Just something to keep in 
> mind.
>
> Cheers,
> Cameron Simpson 
> --
> https://mail.python.org/mailman/listinfo/python-list
>

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

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


Re: Rob Cliffe should stop sending me rude email messages.

2023-02-27 Thread Michael Torrie
On 2/27/23 09:17, Grant Edwards wrote:
> On 2023-02-27, Michael Torrie  wrote:
> 
>> I've been putting off sending this message for days, but the list noise
>> level is now to the point that it has to be said.
> 
> Ah, I've finially realized why some of those threads have seemed so
> disjointed to me. Years ago, I plonked all posts which are (like Hen
> Hanna's) submitted via Googole Groups.
> 
> I highly recommend it.
> 
> FWIW, here's the "score" rule for doing that with srln:
> 
> Score:: =-
>   Message-ID: .*googlegroups.com

Thanks for the tip and reminder.  I'll add that to my gmail filter.

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


RE: How to escape strings for re.finditer?

2023-02-27 Thread avi.e.gross
Jen,

What you just described is why that tool is not the right tool for the job, 
albeit it may help you confirm if whatever method you choose does work 
correctly and finds the same number of matches.

Sometimes you simply do some searching and roll your own.

Consider this code using a sort of list comprehension feature:

>>> short = "hello world"
>>> longer =  "hello world is how many programs start for novices but some use 
>>> hello world! to show how happy they are to say hello world"

>>> short in longer
True
>>> howLong = len(short)

>>> res = [(offset, offset + howLong)  for offset  in range(len(longer)) if 
>>> longer.startswith(short, offset)]
>>> res
[(0, 11), (64, 75), (111, 122)]
>>> len(res)
3

I could do a bit more but it seems to work. Did I get the offsets right? 
Checking:

>>> print( [ longer[res[index][0]:res[index][1]] for index in range(len(res))])
['hello world', 'hello world', 'hello world']

Seems to work but thrown together quickly so can likely be done much nicer.

But as noted, the above has flaws such as matching overlaps like:

>>> short = "good good"
>>> longer = "A good good good but not douple plus good good good goody"
>>> howLong = len(short)
>>> res = [(offset, offset + howLong)  for offset  in range(len(longer)) if 
>>> longer.startswith(short, offset)]
>>> res
[(2, 11), (7, 16), (37, 46), (42, 51), (47, 56)]

It matched five times as sometimes we had three of four good in a row. Some 
other method might match only three.

What some might do can get long and you clearly want one answer and not 
tutorials. For example, people can make a loop that finds a match and either 
sabotages the area by replacing or deleting it, or keeps track and searched 
again on a substring offset from the beginning. 

When you do not find a tool, consider making one. You can take (better) code 
than I show above and make it info a function and now you have a tool. Even 
better, you can make it return whatever you want.

-Original Message-
From: Python-list  On 
Behalf Of Jen Kris via Python-list
Sent: Monday, February 27, 2023 7:40 PM
To: Bob van der Poel 
Cc: Python List 
Subject: Re: How to escape strings for re.finditer?


string.count() only tells me there are N instances of the string; it does not 
say where they begin and end, as does re.finditer.  

Feb 27, 2023, 16:20 by bobmellow...@gmail.com:

> Would string.count() work for you then?
>
> On Mon, Feb 27, 2023 at 5:16 PM Jen Kris via Python-list <> 
> python-list@python.org> > wrote:
>
>>
>> I went to the re module because the specified string may appear more 
>> than once in the string (in the code I'm writing).  For example:
>>  
>>  a = "X - abc_degree + 1 + qq + abc_degree + 1"
>>   b = "abc_degree + 1"
>>   q = a.find(b)
>>  
>>  print(q)
>>  4
>>  
>>  So it correctly finds the start of the first instance, but not the 
>> second one.  The re code finds both instances.  If I knew that the substring 
>> occurred only once then the str.find would be best.
>>  
>>  I changed my re code after MRAB's comment, it now works.
>>  
>>  Thanks much.
>>  
>>  Jen
>>  
>>  
>>  Feb 27, 2023, 15:56 by >> c...@cskk.id.au>> :
>>  
>>  > On 28Feb2023 00:11, Jen Kris <>> jenk...@tutanota.com>> > wrote:
>>  >
>>  >> When matching a string against a longer string, where both 
>> strings have spaces in them, we need to escape the spaces.  >>  >> 
>> This works (no spaces):
>>  >>
>>  >> import re
>>  >> example = 'abcdefabcdefabcdefg'
>>  >> find_string = "abc"
>>  >> for match in re.finditer(find_string, example):
>>  >> print(match.start(), match.end())  >>  >> That gives me the 
>> start and end character positions, which is what I want.
>>  >>
>>  >> However, this does not work:
>>  >>
>>  >> import re
>>  >> example = re.escape('X - cty_degrees + 1 + qq')  >> find_string = 
>> re.escape('cty_degrees + 1')  >> for match in 
>> re.finditer(find_string, example):
>>  >> print(match.start(), match.end())  >>  >> I’ve tried several 
>> other attempts based on my reseearch, but still no match.
>>  >>
>>  >
>>  > You need to print those strings out. You're escaping the _example_ 
>> string, which would make it:
>>  >
>>  >  X - cty_degrees \+ 1 \+ qq
>>  >
>>  > because `+` is a special character in regexps and so `re.escape` escapes 
>> it. But you don't want to mangle the string you're searching! After all, the 
>> text above does not contain the string `cty_degrees + 1`.
>>  >
>>  > My secondary question is: if you're escaping the thing you're searching 
>> _for_, then you're effectively searching for a _fixed_ string, not a 
>> pattern/regexp. So why on earth are you using regexps to do your searching?
>>  >
>>  > The `str` type has a `find(substring)` function. Just use that! It'll be 
>> faster and the code simpler!
>>  >
>>  > Cheers,
>>  > Cameron Simpson <>> c...@cskk.id.au>> >  > --  > >> 
>> https://mail.python.org/mailman/listinfo/python-list
>>  >
>>  
>>  --
>>  >> https://mail.python.org/mailman/listinfo/python-list

Re: How to escape strings for re.finditer?

2023-02-27 Thread Jen Kris via Python-list

I haven't tested it either but it looks like it would work.  But for this case 
I prefer the relative simplicity of:

example = 'X - abc_degree + 1 + qq + abc_degree + 1'
find_string = re.escape('abc_degree + 1')
for match in re.finditer(find_string, example):
    print(match.start(), match.end())

4 18
26 40

I don't insist on terseness for its own sake, but it's cleaner this way.  

Jen


Feb 27, 2023, 16:55 by c...@cskk.id.au:

> On 28Feb2023 01:13, Jen Kris  wrote:
>
>> I went to the re module because the specified string may appear more than 
>> once in the string (in the code I'm writing).
>>
>
> Sure, but writing a `finditer` for plain `str` is pretty easy (untested):
>
>  pos = 0
>  while True:
>  found = s.find(substring, pos)
>  if found < 0:
>  break
>  start = found
>  end = found + len(substring)
>  ... do whatever with start and end ...
>  pos = end
>
> Many people go straight to the `re` module whenever they're looking for 
> strings. It is often cryptic error prone overkill. Just something to keep in 
> mind.
>
> Cheers,
> Cameron Simpson 
> -- 
> https://mail.python.org/mailman/listinfo/python-list
>

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


How to fix this issue

2023-02-27 Thread Arslan Mehmood
How I can remove python terminl, its again and again open during working in 
python. Please help me to resolve this issue.
Python 3.11.1 (tags/v3.11.1:a7a450f, Dec  6 2022, 19:58:39) [MSC v.1934 64 bit 
(AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to escape strings for re.finditer?

2023-02-27 Thread Cameron Simpson

On 28Feb2023 00:57, Jen Kris  wrote:
Yes, that's it.  I don't know how long it would have taken to find that 
detail with research through the voluminous re documentation.  Thanks 
very much. 


You find things like this by printing out the strings you're actually 
working with. Not the original strings, but the strings when you're 
invoking `finditer` i.e. in your case, escaped strings.


Then you might have seen that what you were searching no longer 
contained what you were searching for.


Don't underestimate the value of the debugging print call. It lets you 
see what your programme is actually working with, instead of what you 
thought it was working with.


Cheers,
Cameron Simpson 
--
https://mail.python.org/mailman/listinfo/python-list


Re: How to escape strings for re.finditer?

2023-02-27 Thread Cameron Simpson

On 28Feb2023 01:13, Jen Kris  wrote:
I went to the re module because the specified string may appear more 
than once in the string (in the code I'm writing).


Sure, but writing a `finditer` for plain `str` is pretty easy 
(untested):


pos = 0
while True:
found = s.find(substring, pos)
if found < 0:
break
start = found
end = found + len(substring)
... do whatever with start and end ...
pos = end

Many people go straight to the `re` module whenever they're looking for 
strings. It is often cryptic error prone overkill. Just something to 
keep in mind.


Cheers,
Cameron Simpson 
--
https://mail.python.org/mailman/listinfo/python-list


Re: How to escape strings for re.finditer?

2023-02-27 Thread Jen Kris via Python-list

string.count() only tells me there are N instances of the string; it does not 
say where they begin and end, as does re.finditer.  

Feb 27, 2023, 16:20 by bobmellow...@gmail.com:

> Would string.count() work for you then?
>
> On Mon, Feb 27, 2023 at 5:16 PM Jen Kris via Python-list <> 
> python-list@python.org> > wrote:
>
>>
>> I went to the re module because the specified string may appear more than 
>> once in the string (in the code I'm writing).  For example: 
>>  
>>  a = "X - abc_degree + 1 + qq + abc_degree + 1"
>>   b = "abc_degree + 1"
>>   q = a.find(b)
>>  
>>  print(q)
>>  4
>>  
>>  So it correctly finds the start of the first instance, but not the second 
>> one.  The re code finds both instances.  If I knew that the substring 
>> occurred only once then the str.find would be best.  
>>  
>>  I changed my re code after MRAB's comment, it now works.  
>>  
>>  Thanks much.  
>>  
>>  Jen
>>  
>>  
>>  Feb 27, 2023, 15:56 by >> c...@cskk.id.au>> :
>>  
>>  > On 28Feb2023 00:11, Jen Kris <>> jenk...@tutanota.com>> > wrote:
>>  >
>>  >> When matching a string against a longer string, where both strings have 
>> spaces in them, we need to escape the spaces. 
>>  >>
>>  >> This works (no spaces):
>>  >>
>>  >> import re
>>  >> example = 'abcdefabcdefabcdefg'
>>  >> find_string = "abc"
>>  >> for match in re.finditer(find_string, example):
>>  >>     print(match.start(), match.end())
>>  >>
>>  >> That gives me the start and end character positions, which is what I 
>> want. 
>>  >>
>>  >> However, this does not work:
>>  >>
>>  >> import re
>>  >> example = re.escape('X - cty_degrees + 1 + qq')
>>  >> find_string = re.escape('cty_degrees + 1')
>>  >> for match in re.finditer(find_string, example):
>>  >>     print(match.start(), match.end())
>>  >>
>>  >> I’ve tried several other attempts based on my reseearch, but still no 
>> match. 
>>  >>
>>  >
>>  > You need to print those strings out. You're escaping the _example_ 
>> string, which would make it:
>>  >
>>  >  X - cty_degrees \+ 1 \+ qq
>>  >
>>  > because `+` is a special character in regexps and so `re.escape` escapes 
>> it. But you don't want to mangle the string you're searching! After all, the 
>> text above does not contain the string `cty_degrees + 1`.
>>  >
>>  > My secondary question is: if you're escaping the thing you're searching 
>> _for_, then you're effectively searching for a _fixed_ string, not a 
>> pattern/regexp. So why on earth are you using regexps to do your searching?
>>  >
>>  > The `str` type has a `find(substring)` function. Just use that! It'll be 
>> faster and the code simpler!
>>  >
>>  > Cheers,
>>  > Cameron Simpson <>> c...@cskk.id.au>> >
>>  > -- 
>>  > >> https://mail.python.org/mailman/listinfo/python-list
>>  >
>>  
>>  -- 
>>  >> https://mail.python.org/mailman/listinfo/python-list
>>
>
>
> -- 
>  Listen to my CD at > http://www.mellowood.ca/music/cedars>  
> Bob van der Poel ** Wynndel, British Columbia, CANADA **
> EMAIL: > b...@mellowood.ca
> WWW:   > http://www.mellowood.ca
>

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


RE: How to escape strings for re.finditer?

2023-02-27 Thread avi.e.gross
Just FYI, Jen, there are times a sledgehammer works but perhaps is not the only 
way. These days people worry less about efficiency and more about programmer 
time and education and that can be fine.

But it you looked at methods available in strings or in some other modules, 
your situation is quite common. Some may use another RE front end called 
finditer().

I am NOT suggesting you do what I say next, but imagine writing a loop that 
takes a substring of what you are searching for of the same length as your 
search string. Near the end, it stops as there is too little left.

You can now simply test your searched for string against that substring for 
equality and it tends to return rapidly when they are not equal early on.

Your loop would return whatever data structure or results you want such as that 
it matched it three times at offsets a, b and c.

But do you allow overlaps? If not, your loop needs to skip len(search_str) 
after a match.

What you may want to consider is another form of pre-processing. Do you care if 
"abc_degree + 1" has missing or added spaces at the tart or end or anywhere in 
middle as in " abc_degree +1"?

Do you care if stuff is a different case like "Abc_Degree + 1"?

Some such searches can be done if both the pattern and searched string are 
first converted to a canonical format that maps to the same output. But that 
complicates things a bit and you may to display what you match differently.

And are you also willing to match this: "myabc_degree + 1"?

When using a crafter RE there is a way to ask for a word boundary so abc will 
only be matched if before that is a space or the start of the string and not 
"my".

So this may be a case where you can solve an easy version with the chance it 
can be fooled or overengineer it. If you are allowing the user to type in what 
to search for, as many programs including editors, do, you will often find such 
false positives unless the user knows RE syntax and applies it and you do not 
escape it. I have experienced havoc when doing a careless global replace that 
matched more than I expected, including making changes in comments or constant 
strings rather than just the name of a function. Adding a paren is helpful as 
is not replacing them all but one at a time and skipping any that are not 
wanted.

Good luck.

-Original Message-
From: Python-list  On 
Behalf Of Jen Kris via Python-list
Sent: Monday, February 27, 2023 7:14 PM
To: Cameron Simpson 
Cc: Python List 
Subject: Re: How to escape strings for re.finditer?


I went to the re module because the specified string may appear more than once 
in the string (in the code I'm writing).  For example:  

a = "X - abc_degree + 1 + qq + abc_degree + 1"
 b = "abc_degree + 1"
 q = a.find(b)

print(q)
4

So it correctly finds the start of the first instance, but not the second one.  
The re code finds both instances.  If I knew that the substring occurred only 
once then the str.find would be best.  

I changed my re code after MRAB's comment, it now works.  

Thanks much.  

Jen


Feb 27, 2023, 15:56 by c...@cskk.id.au:

> On 28Feb2023 00:11, Jen Kris  wrote:
>
>> When matching a string against a longer string, where both strings 
>> have spaces in them, we need to escape the spaces.
>>
>> This works (no spaces):
>>
>> import re
>> example = 'abcdefabcdefabcdefg'
>> find_string = "abc"
>> for match in re.finditer(find_string, example):
>> print(match.start(), match.end())
>>
>> That gives me the start and end character positions, which is what I 
>> want.
>>
>> However, this does not work:
>>
>> import re
>> example = re.escape('X - cty_degrees + 1 + qq') find_string = 
>> re.escape('cty_degrees + 1') for match in re.finditer(find_string, 
>> example):
>> print(match.start(), match.end())
>>
>> I’ve tried several other attempts based on my reseearch, but still no 
>> match.
>>
>
> You need to print those strings out. You're escaping the _example_ string, 
> which would make it:
>
>  X - cty_degrees \+ 1 \+ qq
>
> because `+` is a special character in regexps and so `re.escape` escapes it. 
> But you don't want to mangle the string you're searching! After all, the text 
> above does not contain the string `cty_degrees + 1`.
>
> My secondary question is: if you're escaping the thing you're searching 
> _for_, then you're effectively searching for a _fixed_ string, not a 
> pattern/regexp. So why on earth are you using regexps to do your searching?
>
> The `str` type has a `find(substring)` function. Just use that! It'll be 
> faster and the code simpler!
>
> Cheers,
> Cameron Simpson 
> --
> https://mail.python.org/mailman/listinfo/python-list
>

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

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


Re: How to escape strings for re.finditer?

2023-02-27 Thread Jen Kris via Python-list

I went to the re module because the specified string may appear more than once 
in the string (in the code I'm writing).  For example:  

a = "X - abc_degree + 1 + qq + abc_degree + 1"
 b = "abc_degree + 1"
 q = a.find(b)

print(q)
4

So it correctly finds the start of the first instance, but not the second one.  
The re code finds both instances.  If I knew that the substring occurred only 
once then the str.find would be best.  

I changed my re code after MRAB's comment, it now works.  

Thanks much.  

Jen


Feb 27, 2023, 15:56 by c...@cskk.id.au:

> On 28Feb2023 00:11, Jen Kris  wrote:
>
>> When matching a string against a longer string, where both strings have 
>> spaces in them, we need to escape the spaces. 
>>
>> This works (no spaces):
>>
>> import re
>> example = 'abcdefabcdefabcdefg'
>> find_string = "abc"
>> for match in re.finditer(find_string, example):
>>     print(match.start(), match.end())
>>
>> That gives me the start and end character positions, which is what I want. 
>>
>> However, this does not work:
>>
>> import re
>> example = re.escape('X - cty_degrees + 1 + qq')
>> find_string = re.escape('cty_degrees + 1')
>> for match in re.finditer(find_string, example):
>>     print(match.start(), match.end())
>>
>> I’ve tried several other attempts based on my reseearch, but still no match. 
>>
>
> You need to print those strings out. You're escaping the _example_ string, 
> which would make it:
>
>  X - cty_degrees \+ 1 \+ qq
>
> because `+` is a special character in regexps and so `re.escape` escapes it. 
> But you don't want to mangle the string you're searching! After all, the text 
> above does not contain the string `cty_degrees + 1`.
>
> My secondary question is: if you're escaping the thing you're searching 
> _for_, then you're effectively searching for a _fixed_ string, not a 
> pattern/regexp. So why on earth are you using regexps to do your searching?
>
> The `str` type has a `find(substring)` function. Just use that! It'll be 
> faster and the code simpler!
>
> Cheers,
> Cameron Simpson 
> -- 
> https://mail.python.org/mailman/listinfo/python-list
>

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


RE: it seems like a few weeks ago... but actually it was more like 30 years ago that i was programming in C, and

2023-02-27 Thread avi.e.gross
Yes, Greg, you are correct. After I posted, I encountered a later message
that suggested it was list comprehensions that had accidentally left a
variable behind in a context when theoretically you got ALL you asked for in
the resulting list, so it fixed eventually.

You live and learn till you don't.


-Original Message-
From: Python-list  On
Behalf Of Greg Ewing via Python-list
Sent: Monday, February 27, 2023 6:49 PM
To: python-list@python.org
Subject: Re: it seems like a few weeks ago... but actually it was more like
30 years ago that i was programming in C, and

On 28/02/23 7:40 am, avi.e.gr...@gmail.com wrote:
> inhahe  made the point that this may not have been the
original intent for python and may be a sort of bug that it is too late to
fix.

Guido has publically stated that it was a deliberate design choice.
The merits of that design choice can be debated, but it wasn't a bug or an
accident.

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

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


RE: How to escape strings for re.finditer?

2023-02-27 Thread avi.e.gross
MRAB makes a valid point. The regular expression compiled is only done on the 
pattern you are looking for and it it contains anything that might be a 
command, such as an ^ at the start or [12] in middle, you want that converted 
so NONE OF THAT is one. It will be compiled to something that looks for an ^, 
including later in the string, and look for a real [ then a real 1 and a real 2 
and a real ], not for one of the choices of 1 or 2. 

Your example was 'cty_degrees + 1' which can have a subtle bug introduced. The 
special character is "+" which means match greedily as many copies of the 
previous entity as possible. In this case, the previous entity was a single 
space. So the regular expression will match 'cty degrees' then match the single 
space it sees because it sees a space followed ny a plus  then not looking for 
a plus, hits a plus and fails. If your example is rewritten in whatever way 
re.escape uses, it might be 'cty_degrees \+ 1' and then it should work fine.

But converting what you are searching for just breaks that as the result will 
have a '\+" whish is being viewed as two unrelated symbols and the backslash 
breaks the match from going further.



-Original Message-
From: Python-list  On 
Behalf Of MRAB
Sent: Monday, February 27, 2023 6:46 PM
To: python-list@python.org
Subject: Re: How to escape strings for re.finditer?

On 2023-02-27 23:11, Jen Kris via Python-list wrote:
> When matching a string against a longer string, where both strings have 
> spaces in them, we need to escape the spaces.
> 
> This works (no spaces):
> 
> import re
> example = 'abcdefabcdefabcdefg'
> find_string = "abc"
> for match in re.finditer(find_string, example):
>  print(match.start(), match.end())
> 
> That gives me the start and end character positions, which is what I want.
> 
> However, this does not work:
> 
> import re
> example = re.escape('X - cty_degrees + 1 + qq') find_string = 
> re.escape('cty_degrees + 1') for match in re.finditer(find_string, 
> example):
>  print(match.start(), match.end())
> 
> I’ve tried several other attempts based on my reseearch, but still no match.
> 
> I don’t have much experience with regex, so I hoped a reg-expert might help.
> 
You need to escape only the pattern, not the string you're searching.
--
https://mail.python.org/mailman/listinfo/python-list

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


Re: How to escape strings for re.finditer?

2023-02-27 Thread Jen Kris via Python-list
Yes, that's it.  I don't know how long it would have taken to find that detail 
with research through the voluminous re documentation.  Thanks very much.  

Feb 27, 2023, 15:47 by pyt...@mrabarnett.plus.com:

> On 2023-02-27 23:11, Jen Kris via Python-list wrote:
>
>> When matching a string against a longer string, where both strings have 
>> spaces in them, we need to escape the spaces.
>>
>> This works (no spaces):
>>
>> import re
>> example = 'abcdefabcdefabcdefg'
>> find_string = "abc"
>> for match in re.finditer(find_string, example):
>>      print(match.start(), match.end())
>>
>> That gives me the start and end character positions, which is what I want.
>>
>> However, this does not work:
>>
>> import re
>> example = re.escape('X - cty_degrees + 1 + qq')
>> find_string = re.escape('cty_degrees + 1')
>> for match in re.finditer(find_string, example):
>>      print(match.start(), match.end())
>>
>> I’ve tried several other attempts based on my reseearch, but still no match.
>>
>> I don’t have much experience with regex, so I hoped a reg-expert might help.
>>
> You need to escape only the pattern, not the string you're searching.
> -- 
> https://mail.python.org/mailman/listinfo/python-list
>

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


Re: Python 3.10 Fizzbuzz

2023-02-27 Thread Rob Cliffe via Python-list



On 27/02/2023 21:04, Ethan Furman wrote:

On 2/27/23 12:20, rbowman wrote:

> "By using Black, you agree to cede control over minutiae of hand-
> formatting. In return, Black gives you speed, determinism, and freedom
> from pycodestyle nagging about formatting. You will save time and 
mental

> energy for more important matters."
>
> Somehow I don't think we would get along very well. I'm a little on the
> opinionated side myself.

I personally cannot stand Black.  It feels like every major choice it 
makes (and some minor ones) are exactly the opposite of the choice I 
make.


--
~Ethan~
I've never tried Black or any other code formatter, but I'm sure we 
wouldn't get on.

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


Re: Python 3.10 Fizzbuzz

2023-02-27 Thread Greg Ewing via Python-list

On 28/02/23 5:08 am, Thomas Passin wrote:

On 2/27/2023 11:01 AM, Mats Wichmann wrote:
If you intend to run Black on your code to ensure consistent 
formatting, you may as well learn to prefer double quotes, because 
it's going to convert single to double


I prefer single quotes because they are easier to type.


I tend to use the convention of double quotes for strings seen
by the outside world, and single quotes for internal constants
(such as enumerated types that happen to be represented by
strings).

I guess this means I can't use Black. :-(

--
Greg

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


Re: How to escape strings for re.finditer?

2023-02-27 Thread Cameron Simpson

On 28Feb2023 00:11, Jen Kris  wrote:

When matching a string against a longer string, where both strings have spaces 
in them, we need to escape the spaces. 

This works (no spaces):

import re
example = 'abcdefabcdefabcdefg'
find_string = "abc"
for match in re.finditer(find_string, example):
    print(match.start(), match.end())

That gives me the start and end character positions, which is what I want. 

However, this does not work:

import re
example = re.escape('X - cty_degrees + 1 + qq')
find_string = re.escape('cty_degrees + 1')
for match in re.finditer(find_string, example):
    print(match.start(), match.end())

I’ve tried several other attempts based on my reseearch, but still no 
match. 


You need to print those strings out. You're escaping the _example_ 
string, which would make it:


X - cty_degrees \+ 1 \+ qq

because `+` is a special character in regexps and so `re.escape` escapes 
it. But you don't want to mangle the string you're searching! After all, 
the text above does not contain the string `cty_degrees + 1`.


My secondary question is: if you're escaping the thing you're searching 
_for_, then you're effectively searching for a _fixed_ string, not a 
pattern/regexp. So why on earth are you using regexps to do your 
searching?


The `str` type has a `find(substring)` function. Just use that! It'll be 
faster and the code simpler!


Cheers,
Cameron Simpson 
--
https://mail.python.org/mailman/listinfo/python-list


Re: it seems like a few weeks ago... but actually it was more like 30 years ago that i was programming in C, and

2023-02-27 Thread Greg Ewing via Python-list

On 28/02/23 7:40 am, avi.e.gr...@gmail.com wrote:

inhahe  made the point that this may not have been the 
original intent for python and may be a sort of bug that it is too late to fix.


Guido has publically stated that it was a deliberate design choice.
The merits of that design choice can be debated, but it wasn't a
bug or an accident.

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


Re: How to escape strings for re.finditer?

2023-02-27 Thread MRAB

On 2023-02-27 23:11, Jen Kris via Python-list wrote:

When matching a string against a longer string, where both strings have spaces 
in them, we need to escape the spaces.

This works (no spaces):

import re
example = 'abcdefabcdefabcdefg'
find_string = "abc"
for match in re.finditer(find_string, example):
     print(match.start(), match.end())

That gives me the start and end character positions, which is what I want.

However, this does not work:

import re
example = re.escape('X - cty_degrees + 1 + qq')
find_string = re.escape('cty_degrees + 1')
for match in re.finditer(find_string, example):
     print(match.start(), match.end())

I’ve tried several other attempts based on my reseearch, but still no match.

I don’t have much experience with regex, so I hoped a reg-expert might help.


You need to escape only the pattern, not the string you're searching.
--
https://mail.python.org/mailman/listinfo/python-list


Re: Python 3.10 Fizzbuzz

2023-02-27 Thread Oscar Benjamin
On Mon, 27 Feb 2023 at 21:06, Ethan Furman  wrote:
>
> On 2/27/23 12:20, rbowman wrote:
>
>  > "By using Black, you agree to cede control over minutiae of hand-
>  > formatting. In return, Black gives you speed, determinism, and freedom
>  > from pycodestyle nagging about formatting. You will save time and mental
>  > energy for more important matters."
>  >
>  > Somehow I don't think we would get along very well. I'm a little on the
>  > opinionated side myself.
>
> I personally cannot stand Black.  It feels like every major choice it makes 
> (and some minor ones) are exactly the
> opposite of the choice I make.

I agree partially. There are two types of decisions black makes:

1. Leave the code alone because it seems okay or make small modifications.
2. Reformat the code because it violates some generic rule (like line
too long or something).

I've recently tried Black and mostly for my code it seems to go with 1
(code looks okay). There might be some minor changes like double vs
single quotes but I really don't care about those. In that sense me
and Black seem to agree.

However I have also reviewed code where it is clear that the author
has used black and their code came under case 2. In that case Black
seems to produce awful things. What I can't understand is someone
accepting the awful rewrite rather than just fixing the code. Treating
Black almost like a linter makes sense to me but accepting the
rewrites that it offers for bad code does not.

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


How to escape strings for re.finditer?

2023-02-27 Thread Jen Kris via Python-list
When matching a string against a longer string, where both strings have spaces 
in them, we need to escape the spaces.  

This works (no spaces):

import re
example = 'abcdefabcdefabcdefg'
find_string = "abc"
for match in re.finditer(find_string, example):
    print(match.start(), match.end())

That gives me the start and end character positions, which is what I want. 

However, this does not work:

import re
example = re.escape('X - cty_degrees + 1 + qq')
find_string = re.escape('cty_degrees + 1')
for match in re.finditer(find_string, example):
    print(match.start(), match.end())

I’ve tried several other attempts based on my reseearch, but still no match. 

I don’t have much experience with regex, so I hoped a reg-expert might help. 

Thanks,

Jen

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


[Python-announce] REMINDER - Call for Proposals for SciPy 2023!

2023-02-27 Thread Arliss Collins
[image: image.png]


Hello Folks,



[image: :mega:] *Call for Proposals for SciPy 2023 closes March 1st* [image:
:mega:]

*Call for Proposals*  for the 22nd
annual Scientific Computing with Python Conference — also known as SciPy
2023  — *CLOSES ON MARCH 1st*.

We would love to receive proposals from folks in your organization. We seek
submissions for Talks, Posters
, and Tutorials
. This year we are happy
to announce two highlighted tracks that run parallel to the general
conference track:  (1) Machine Learning, Data Science and Ethics in AI, and
(2) Tending Your Open Source Garden: Maintenance and Community.


You can learn more about this year's conference at SciPy 2023 | Home
. The full schedule will be published
later in May. In the meantime, please check out last year's program
 and videos of previous year's
talks and events

to better understand what SciPy conferences are like.

Please share this information with anyone you think would be interested.

SciPy 2023 is shaping up to be a fantastic conference. We hope you'll join
us in July!

Arliss

(On behalf of the SciPy 2023 Communications Committee)
Follow Us 

___
Python-announce-list mailing list -- python-announce-list@python.org
To unsubscribe send an email to python-announce-list-le...@python.org
https://mail.python.org/mailman3/lists/python-announce-list.python.org/
Member address: arch...@mail-archive.com


Re: Python 3.10 Fizzbuzz

2023-02-27 Thread Ethan Furman

On 2/27/23 12:20, rbowman wrote:

> "By using Black, you agree to cede control over minutiae of hand-
> formatting. In return, Black gives you speed, determinism, and freedom
> from pycodestyle nagging about formatting. You will save time and mental
> energy for more important matters."
>
> Somehow I don't think we would get along very well. I'm a little on the
> opinionated side myself.

I personally cannot stand Black.  It feels like every major choice it makes (and some minor ones) are exactly the 
opposite of the choice I make.


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


Re: Python 3.10 Fizzbuzz

2023-02-27 Thread rbowman
On Mon, 27 Feb 2023 11:08:22 -0500, Thomas Passin wrote:

> I prefer single quotes because they are easier to type.

There is that. JavaScript makes me lazy and C# slaps my knuckles with a 
steel edged ruler. 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python 3.10 Fizzbuzz

2023-02-27 Thread rbowman
On Mon, 27 Feb 2023 09:01:26 -0700, Mats Wichmann wrote:


> If you intend to run Black on your code to ensure consistent formatting,
> you may as well learn to prefer double quotes, because it's going to
> convert single to double (or: don't learn, and set your IDE to "convert
> on save" and don't think about it...)

I'd never heard of Black.

"By using Black, you agree to cede control over minutiae of hand-
formatting. In return, Black gives you speed, determinism, and freedom 
from pycodestyle nagging about formatting. You will save time and mental 
energy for more important matters."

Somehow I don't think we would get along very well. I'm a little on the 
opinionated side myself.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: TypeError: can only concatenate str (not "int") to str

2023-02-27 Thread Thomas Passin

On 2/27/2023 2:15 PM, avi.e.gr...@gmail.com wrote:

Karsten,

There are limits to the disruption a group should tolerate even from people
who may need some leeway.

I wonder if Hen Hanna has any idea that some of the people he is saying this
to lost most of their family in the Holocaust and had parents who barely
survived passing through multiple concentration camps, I doubt he would
change his words or attitude in the slightest as some of his other gems
indicate a paranoid view of the world at best.


This whole paragraph is about a person with speculation on his views. 
Please let's deprecate writing about people this way :)




It is disproportionate to call everyone a Nazi at the slightest imagined
slight. But we are not here in this forum to discuss world affairs or
politics or how to replace python with the same language they have been
using and likely abusing. Like every resource, it is best used as intended
and that tends to mean not treating all the recipients as being willing to
receive every thought you have had since breakfast followed by demanding
everyone stop responding to him privately or in public or disagreeing in any
way.

I apologize for my part in even bothering to try to help him as it clearly
is a thankless task and a huge waste of andwidth.


Again, this paragraph is commentary and speculation about another 
poster. It's so easy to write things like this without even realizing 
we're doing it (I'm not immune).



-Original Message-
From: Python-list  On
Behalf Of Karsten Hilbert
Sent: Monday, February 27, 2023 6:32 AM
To: python-list@python.org
Subject: Re: TypeError: can only concatenate str (not "int") to str

Am Sun, Feb 26, 2023 at 08:56:28AM -0800 schrieb Hen Hanna:


so far,  i think  Paul Rubin's post (in another thread) was esp.
concise, informative, --- but he's also made a comment
about   'shunting'  beginners  (questions) to a
concentration camp, and sounded  a bit  like a cold-hearted (or
warm-hearted)  Nazi  officer / scientist.


Now, I have a lot of sympathy -- not least from a professional point of view
-- and see quite some leeway for people acting neuro-atypically, but the
last line of the above really isn't necessary to be read on this list.

Best,
Karsten
--
GPG  40BE 5B0E C98E 1713 AFA6  5BC0 3BEA AC80 7D4F C89B
--
https://mail.python.org/mailman/listinfo/python-list



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


RE: TypeError: can only concatenate str (not "int") to str

2023-02-27 Thread avi.e.gross
Karsten,

There are limits to the disruption a group should tolerate even from people
who may need some leeway.

I wonder if Hen Hanna has any idea that some of the people he is saying this
to lost most of their family in the Holocaust and had parents who barely
survived passing through multiple concentration camps, I doubt he would
change his words or attitude in the slightest as some of his other gems
indicate a paranoid view of the world at best.

It is disproportionate to call everyone a Nazi at the slightest imagined
slight. But we are not here in this forum to discuss world affairs or
politics or how to replace python with the same language they have been
using and likely abusing. Like every resource, it is best used as intended
and that tends to mean not treating all the recipients as being willing to
receive every thought you have had since breakfast followed by demanding
everyone stop responding to him privately or in public or disagreeing in any
way.

I apologize for my part in even bothering to try to help him as it clearly
is a thankless task and a huge waste of andwidth.

-Original Message-
From: Python-list  On
Behalf Of Karsten Hilbert
Sent: Monday, February 27, 2023 6:32 AM
To: python-list@python.org
Subject: Re: TypeError: can only concatenate str (not "int") to str

Am Sun, Feb 26, 2023 at 08:56:28AM -0800 schrieb Hen Hanna:

> so far,  i think  Paul Rubin's post (in another thread) was esp. 
> concise, informative, --- but he's also made a comment
> about   'shunting'  beginners  (questions) to a
> concentration camp, and sounded  a bit  like a cold-hearted (or 
> warm-hearted)  Nazi  officer / scientist.

Now, I have a lot of sympathy -- not least from a professional point of view
-- and see quite some leeway for people acting neuro-atypically, but the
last line of the above really isn't necessary to be read on this list.

Best,
Karsten
--
GPG  40BE 5B0E C98E 1713 AFA6  5BC0 3BEA AC80 7D4F C89B
--
https://mail.python.org/mailman/listinfo/python-list

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


RE: it seems like a few weeks ago... but actually it was more like 30 years ago that i was programming in C, and

2023-02-27 Thread avi.e.gross
I am not a big fan of religions or philosophies that say a road to salvation is 
for the "I" to disappear.

But on a more serious note, as Roel said, there is NO RULE being violated 
unless the documentation of the language says it is supposed to do something 
different.

There are many excellent reasons to keep the final value of a loop variable 
around. On the other hand, there are also many good reasons to make such 
variables be totally kept within the context of the loop so they can mask a 
variable with the same name only temporarily within the loop.

Neither choice is wrong as long as it is applied consistently.

Now, having said that, does python allow you to in some way over-ride the 
behavior?

Well, first, you can simply choose an odd name like local__loopy___variable 
that is not used elsewhere in your code, except perhaps in the next loop 
downstream where it is re-initialized.

You can also use "del Variable" or reset it to null or something in every way 
you can exit the loop such as before a break or in an "else" clause if it 
bothers you.

inhahe  made the point that this may not have been the 
original intent for python and may be a sort of bug that it is too late to fix. 
Perhaps so, but insisting it be changed now is far from a simple request as I 
bet some people depend on the feature. True, it could be straightforward to 
recode any existing loops to update a secondary variable at the top of each 
loop that is declared before the loop and persists after the loop. 

Alas, that might force some to use the dreaded semicolon!

Final note is to look at something like the "with" statement in python as a 
context manager where it normally allows the resource to be closed or removed 
at the end. Of course you can set up an object that does not do the expected 
closure and preserves something, but generally what is wanted is to make sure 
the context exits gracefully.

Avi

-Original Message-
From: Python-list  On 
Behalf Of Roel Schroeven
Sent: Monday, February 27, 2023 3:51 AM
To: python-list@python.org
Subject: Re: it seems like a few weeks ago... but actually it was more like 30 
years ago that i was programming in C, and

Op 26/02/2023 om 6:53 schreef Hen Hanna:
> > There are some similarities between Python and Lisp-family 
> > languages, but really Python is its own thing.
>
>
> Scope (and extent ?) of   variables is one reminder that  Python is not 
> Lisp
>
>  fori in  range(5):  print( i )
>   .
>  print( i )
>
> ideally, after the FOR loop is done,  the (local) var  i should also 
> disappear.
> (this almost caused a bug for me)
I wouldn't say "i *should* also disappear". There is no big book of programming 
language design with rules like that that all languages have to follow. 
Different languages have different behavior. In some languages, for/if/while 
statements introduce a new scope, in other languages they don't. In Python, 
they don't. I won't say one is better than the other; they're just different.

--
"Most of us, when all is said and done, like what we like and make up reasons 
for it afterwards."
 -- Soren F. Petersen

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

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


Re: Python type system

2023-02-27 Thread Chris Angelico
On Tue, 28 Feb 2023 at 03:29, Weatherby,Gerard  wrote:
> Beyond Python, I’ve also found duck typing useful in real life. If it walks 
> like a troll, quacks like a troll …
>
... regenerates limbs like a troll...

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


Re: Hen Hanna & google groups

2023-02-27 Thread Thomas Passin

On 2/27/2023 12:35 PM, Ethan Furman wrote:

Greetings, all!

As has been stated, Hen Hanna is posting through Google Groups, over 
which the Python List moderators have zero control.


The only thing we can do, and which has now been done, is not allow 
those posts in to the Python List.


--
~Ethan~
Moderator


Thank you.

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


Hen Hanna & google groups

2023-02-27 Thread Ethan Furman

Greetings, all!

As has been stated, Hen Hanna is posting through Google Groups, over which the 
Python List moderators have zero control.

The only thing we can do, and which has now been done, is not allow those posts 
in to the Python List.

--
~Ethan~
Moderator
--
https://mail.python.org/mailman/listinfo/python-list


Python type system

2023-02-27 Thread Weatherby,Gerard
When I first started transitioning to Python as a Perl replacement, with my 
Java/C++ baggage, I thought Pythnon had some loosey-goosey type system. I 
thought int() and str() were casts, not constructors. I now realize Python has 
a great strong type system. Duck typing. If it walks like a duck, quacks like a 
duck, it’s probably a duck. (C++ added the auto keyword at some point to get 
analogous behavior).

Beyond Python, I’ve also found duck typing useful in real life. If it walks 
like a troll, quacks like a troll …


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


Re: Line continuation and comments

2023-02-27 Thread Robert Latest via Python-list
Robert Latest wrote:
> Paul Bryan wrote:
>> Adding to this, there should be no reason now in recent versions of
>> Python to ever use line continuation. Black goes so far as to state
>> "backslashes are bad and should never be used":
>>
>> https://black.readthedocs.io/en/stable/the_black_code_style/
>   future_style.html#using-backslashes-for-with-statements
>
> Then I wonder how Mr. Black would go about these long "dot chaining"
> expressions that packages like pandas and sqlalchemy require.

Just found out that parentheses work there, too.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Line continuation and comments

2023-02-27 Thread Robert Latest via Python-list
Paul Bryan wrote:
> Adding to this, there should be no reason now in recent versions of
> Python to ever use line continuation. Black goes so far as to state
> "backslashes are bad and should never be used":
>
> https://black.readthedocs.io/en/stable/the_black_code_style/
  future_style.html#using-backslashes-for-with-statements

Then I wonder how Mr. Black would go about these long "dot chaining"
expressions that packages like pandas and sqlalchemy require.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Line continuation and comments

2023-02-27 Thread Robert Latest via Python-list
Edmondo Giovannozzi wrote:
> Il giorno mercoledì 22 febbraio 2023 alle 09:50:14 UTC+1 Robert Latest ha
> scritto:
>> I found myself building a complicated logical condition with many ands and
>> ors which I made more manageable by putting the various terms on individual
>> lines and breaking them with the "\" line continuation character. In this
>> context it would have been nice to be able to add comments to lines terms
>> which of course isn't possible because the backslash must be the last
>> character on the line. 
>> 
>> Question: If the Python syntax were changed to allow comments after
>> line-ending 
>> backslashes, would it break any existing code? I can't think of an example.
>
> Well you can if you use parenthesis like in:
> x = 5
> a = (x > 3 and
> # x < 21 or
>  x > 100
>  )
> You don't need the "\" to continue a line in this case

I like that. Never thought of it.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Rob Cliffe should stop sending me rude email messages.

2023-02-27 Thread Grant Edwards
On 2023-02-27, Michael Torrie  wrote:

> I've been putting off sending this message for days, but the list noise
> level is now to the point that it has to be said.

Ah, I've finially realized why some of those threads have seemed so
disjointed to me. Years ago, I plonked all posts which are (like Hen
Hanna's) submitted via Googole Groups.

I highly recommend it.

FWIW, here's the "score" rule for doing that with srln:

Score:: =-
  Message-ID: .*googlegroups.com

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


Re: Python 3.10 Fizzbuzz

2023-02-27 Thread Thomas Passin

On 2/27/2023 11:01 AM, Mats Wichmann wrote:

On 2/26/23 14:07, Hen Hanna wrote:

On Monday, August 29, 2022 at 7:18:22 PM UTC-7, Paul Rubin wrote:

Just because.

from math import gcd



def fizz(n: int) -> str:
    match gcd(n, 15):
   case 3: return "Fizz"
   case 5: return "Buzz"
   case 15: return "FizzBuzz"
   case _: return str(n)

for i in range(1,101):
 print(fizz(i))



is there any reason to prefer    "    over    '   ?


If you intend to run Black on your code to ensure consistent formatting, 
you may as well learn to prefer double quotes, because it's going to 
convert single to double (or: don't learn, and set your IDE to "convert 
on save" and don't think about it...)


As has already been mentioned, syntactically there is no difference.


I prefer single quotes because they are easier to type.

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


Re: Python 3.10 Fizzbuzz

2023-02-27 Thread Mats Wichmann

On 2/26/23 14:07, Hen Hanna wrote:

On Monday, August 29, 2022 at 7:18:22 PM UTC-7, Paul Rubin wrote:

Just because.

from math import gcd



def fizz(n: int) -> str:
match gcd(n, 15):
   case 3: return "Fizz"
   case 5: return "Buzz"
   case 15: return "FizzBuzz"
   case _: return str(n)

for i in range(1,101):
 print(fizz(i))



is there any reason to prefer"over'   ?


If you intend to run Black on your code to ensure consistent formatting, 
you may as well learn to prefer double quotes, because it's going to 
convert single to double (or: don't learn, and set your IDE to "convert 
on save" and don't think about it...)


As has already been mentioned, syntactically there is no difference.



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


Re: Rob Cliffe should stop sending me rude email messages.

2023-02-27 Thread Michael Torrie
I've been putting off sending this message for days, but the list noise
level is now to the point that it has to be said.

Often it is better to contact someone directly and privately rather than
publicly embarrass them by calling them out. You've made it clear,
however, that publicly calling you out is necessary. No doubt you will
think my post rude as well, even though the tone is moderate and
deliberate.  Sometimes things need to said and others need to be asked
to make changes. That's just part of communication in real, grown-up life.

Everyone that's responded to you has patiently attempted to answer your
questions and engage with you despite your unorthodox and very difficult
communications style.  I can assure you that not one person who's
replied to you has been rude or insulting, yet I cannot say the same
about your own disparaging comments in reply.  The only ad homimems I've
seen have come from you.

We are frustrated and exasperated with your unwillingness to read,
learn, and understand, yes, definitely!  Although your posts are quite a
bit less frustrating than those trying to turn Python into Java.  We can
probably handle trying to turn Python into LISP! :)

Do you understand why your posts have been causing frustration?  This is
an existing community that you've chosen to join.  Many of the people
you've insulted here, including dn have been participating and helpfully
contributing to this list for many years.

Please stop posting messages about how you think people have been rude
to you. Besides being off-topic they are simply false.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: it seems like a few weeks ago... but actually it was more like 30 years ago that i was programming in C, and

2023-02-27 Thread Greg Ewing via Python-list

On 27/02/23 10:07 pm, Roel Schroeven wrote:
I'm guessing you're thinking about variables leaking out of list 
comprehensions. I seem to remember (but I could be wrong) it was a 
design mistake rather than a bug in the code, but in any case it's been 
fixed now (in the 2 to 3 transition, I think).


The semantics of list comprehensions was originally defined
in terms of nested for loops. A consequence was that the loop
variables ended up in the local scope just as with ordinary for
loops. Later it was decided to change that.

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


Re: TypeError: can only concatenate str (not "int") to str

2023-02-27 Thread Karsten Hilbert
Am Sun, Feb 26, 2023 at 08:56:28AM -0800 schrieb Hen Hanna:

> so far,  i think  Paul Rubin's post (in another thread) was
> esp. concise, informative, --- but he's also made a comment
> about   'shunting'  beginners  (questions) to a
> concentration camp, and sounded  a bit  like a cold-hearted
> (or warm-hearted)  Nazi  officer / scientist.

Now, I have a lot of sympathy -- not least from a
professional point of view -- and see quite some leeway for
people acting neuro-atypically, but the last line of the
above really isn't necessary to be read on this list.

Best,
Karsten
--
GPG  40BE 5B0E C98E 1713 AFA6  5BC0 3BEA AC80 7D4F C89B
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: it seems like a few weeks ago... but actually it was more like 30 years ago that i was programming in C, and

2023-02-27 Thread Roel Schroeven

Op 27/02/2023 om 9:56 schreef inhahe:

On Mon, Feb 27, 2023 at 3:52 AM Roel Schroeven 
wrote:

> Op 26/02/2023 om 6:53 schreef Hen Hanna:
> > > There are some similarities between Python and Lisp-family
> > > languages, but really Python is its own thing.
> >
> >
> > Scope (and extent ?) of   variables is one reminder that  Python is
> not Lisp
> >
> > fori in  range(5): print( i )
> >   .
> > print( i )
> >
> > ideally, after the FOR loop is done,  the (local) var  i should also
> disappear.
> > (this almost caused a bug for me)
> I wouldn't say "i *should* also disappear". There is no big book of
> programming language design with rules like that that all languages have
> to follow. Different languages have different behavior. In some
> languages, for/if/while statements introduce a new scope, in other
> languages they don't. In Python, they don't. I won't say one is better
> than the other; they're just different.
>
> --
>
>
I'm not sure, but I think I remember this was actually a bug in the
interpreter, and presumably they didn't fix it because they didn't want to
break backward compatibility?
I'm guessing you're thinking about variables leaking out of list 
comprehensions. I seem to remember (but I could be wrong) it was a 
design mistake rather than a bug in the code, but in any case it's been 
fixed now (in the 2 to 3 transition, I think).


For loops (and while loops, and if statements) not introducing a new 
scope is a deliberate decision and is not subject to change.


--
"Ever since I learned about confirmation bias, I've been seeing
it everywhere."
-- Jon Ronson

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


Re: it seems like a few weeks ago... but actually it was more like 30 years ago that i was programming in C, and

2023-02-27 Thread inhahe
On Mon, Feb 27, 2023 at 3:56 AM inhahe  wrote:

>
>
> On Mon, Feb 27, 2023 at 3:52 AM Roel Schroeven 
> wrote:
>
>> Op 26/02/2023 om 6:53 schreef Hen Hanna:
>> > > There are some similarities between Python and Lisp-family
>> > > languages, but really Python is its own thing.
>> >
>> >
>> > Scope (and extent ?) of   variables is one reminder that  Python is
>> not Lisp
>> >
>> > fori in  range(5): print( i )
>> >   .
>> > print( i )
>> >
>> > ideally, after the FOR loop is done,  the (local) var  i should also
>> disappear.
>> > (this almost caused a bug for me)
>> I wouldn't say "i *should* also disappear". There is no big book of
>> programming language design with rules like that that all languages have
>> to follow. Different languages have different behavior. In some
>> languages, for/if/while statements introduce a new scope, in other
>> languages they don't. In Python, they don't. I won't say one is better
>> than the other; they're just different.
>>
>> --
>>
>>
> I'm not sure, but I think I remember this was actually a bug in the
> interpreter, and presumably they didn't fix it because they didn't want to
> break backward compatibility?
>
>
Maybe I'm thinking of a variable scope leak after list comprehensions.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: it seems like a few weeks ago... but actually it was more like 30 years ago that i was programming in C, and

2023-02-27 Thread inhahe
On Mon, Feb 27, 2023 at 3:52 AM Roel Schroeven 
wrote:

> Op 26/02/2023 om 6:53 schreef Hen Hanna:
> > > There are some similarities between Python and Lisp-family
> > > languages, but really Python is its own thing.
> >
> >
> > Scope (and extent ?) of   variables is one reminder that  Python is
> not Lisp
> >
> > fori in  range(5): print( i )
> >   .
> > print( i )
> >
> > ideally, after the FOR loop is done,  the (local) var  i should also
> disappear.
> > (this almost caused a bug for me)
> I wouldn't say "i *should* also disappear". There is no big book of
> programming language design with rules like that that all languages have
> to follow. Different languages have different behavior. In some
> languages, for/if/while statements introduce a new scope, in other
> languages they don't. In Python, they don't. I won't say one is better
> than the other; they're just different.
>
> --
>
>
I'm not sure, but I think I remember this was actually a bug in the
interpreter, and presumably they didn't fix it because they didn't want to
break backward compatibility?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: it seems like a few weeks ago... but actually it was more like 30 years ago that i was programming in C, and

2023-02-27 Thread Roel Schroeven

Op 26/02/2023 om 6:53 schreef Hen Hanna:
> There are some similarities between Python and Lisp-family 
> languages, but really Python is its own thing. 



Scope (and extent ?) of   variables is one reminder that  Python is not Lisp

 fori in  range(5):  print( i )
  .
 print( i )

ideally, after the FOR loop is done,  the (local) var  i should also disappear.
(this almost caused a bug for me)
I wouldn't say "i *should* also disappear". There is no big book of 
programming language design with rules like that that all languages have 
to follow. Different languages have different behavior. In some 
languages, for/if/while statements introduce a new scope, in other 
languages they don't. In Python, they don't. I won't say one is better 
than the other; they're just different.


--
"Most of us, when all is said and done, like what we like and make up
reasons for it afterwards."
-- Soren F. Petersen

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