Re: Regular Expression bug?

2023-03-02 Thread jose isaias cabrera
On Thu, Mar 2, 2023 at 9:56 PM Alan Bawden  wrote:
>
> jose isaias cabrera  writes:
>
>On Thu, Mar 2, 2023 at 2:38 PM Mats Wichmann  wrote:
>
>This re is a bit different than the one I am used. So, I am trying to match
>everything after 'pn=':
>
>import re
>s = "pm=jose pn=2017"
>m0 = r"pn=(.+)"
>r0 = re.compile(m0)
>s0 = r0.match(s)
>>>> print(s0)
>None
>
> Assuming that you were expecting to match "pn=2017", then you probably
> don't want the 'match' method.  Read its documentation.  Then read the
> documentation for the _other_ methods that a Pattern supports.  Then you
> will be enlightened.

Yes. I need search. Thanks.

-- 

What if eternity is real?  Where will you spend it?  H...
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Regular Expression bug?

2023-03-02 Thread jose isaias cabrera
On Thu, Mar 2, 2023 at 8:35 PM  wrote:
>
> It is a well-known fact, Jose, that GIGO.
>
> The letters "n" and "m" are not interchangeable. Your pattern fails because 
> you have "pn" in one place and "pm" in the other.

It is not GIGO. pm=project manager. pn=project name. I needed search()
rather than match().

>
> >>> s = "pn=jose pn=2017"
> ...
> >>> s0 = r0.match(s)
> >>> s0
> 
>
>
>
> -Original Message-
> From: Python-list  On 
> Behalf Of jose isaias cabrera
> Sent: Thursday, March 2, 2023 8:07 PM
> To: Mats Wichmann 
> Cc: python-list@python.org
> Subject: Re: Regular Expression bug?
>
> On Thu, Mar 2, 2023 at 2:38 PM Mats Wichmann  wrote:
> >
> > On 3/2/23 12:28, Chris Angelico wrote:
> > > On Fri, 3 Mar 2023 at 06:24, jose isaias cabrera 
> wrote:
> > >>
> > >> Greetings.
> > >>
> > >> For the RegExp Gurus, consider the following python3 code:
> > >> 
> > >> import re
> > >> s = "pn=align upgrade sd=2023-02-"
> > >> ro = re.compile(r"pn=(.+) ")
> > >> r0=ro.match(s)
> > > print(r0.group(1))
> > >> align upgrade
> > >> 
> > >>
> > >> This is wrong. It should be 'align' because the group only goes up-to
> > >> the space. Thoughts? Thanks.
> > >>
> > >
> > > Not a bug. Find the longest possible match that fits this; as long as
> > > you can find a space immediately after it, everything in between goes
> > > into the .+ part.
> > >
> > > If you want to exclude spaces, either use [^ ]+ or .+?.
> >
> > https://docs.python.org/3/howto/regex.html#greedy-versus-non-greedy
>
> This re is a bit different than the one I am used. So, I am trying to match
> everything after 'pn=':
>
> import re
> s = "pm=jose pn=2017"
> m0 = r"pn=(.+)"
> r0 = re.compile(m0)
> s0 = r0.match(s)
> >>> print(s0)
> None
>
> Any help is appreciated.
> --
> https://mail.python.org/mailman/listinfo/python-list
>


-- 

What if eternity is real?  Where will you spend it?  H...
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Regular Expression bug?

2023-03-02 Thread jose isaias cabrera
On Thu, Mar 2, 2023 at 8:30 PM Cameron Simpson  wrote:
>
> On 02Mar2023 20:06, jose isaias cabrera  wrote:
> >This re is a bit different than the one I am used. So, I am trying to
> >match
> >everything after 'pn=':
> >
> >import re
> >s = "pm=jose pn=2017"
> >m0 = r"pn=(.+)"
> >r0 = re.compile(m0)
> >s0 = r0.match(s)
>
> `match()` matches at the start of the string. You want r0.search(s).
> - Cameron Simpson 

Thanks. Darn it! I knew it was something simple.


-- 

What if eternity is real?  Where will you spend it?  H...
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Regular Expression bug?

2023-03-02 Thread Alan Bawden
jose isaias cabrera  writes:

   On Thu, Mar 2, 2023 at 2:38 PM Mats Wichmann  wrote:

   This re is a bit different than the one I am used. So, I am trying to match
   everything after 'pn=':

   import re
   s = "pm=jose pn=2017"
   m0 = r"pn=(.+)"
   r0 = re.compile(m0)
   s0 = r0.match(s)
   >>> print(s0)
   None

Assuming that you were expecting to match "pn=2017", then you probably
don't want the 'match' method.  Read its documentation.  Then read the
documentation for the _other_ methods that a Pattern supports.  Then you
will be enlightened.

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


Re: Regular Expression bug?

2023-03-02 Thread Cameron Simpson

On 02Mar2023 20:06, jose isaias cabrera  wrote:
This re is a bit different than the one I am used. So, I am trying to 
match

everything after 'pn=':

import re
s = "pm=jose pn=2017"
m0 = r"pn=(.+)"
r0 = re.compile(m0)
s0 = r0.match(s)


`match()` matches at the start of the string. You want r0.search(s).
- Cameron Simpson 
--
https://mail.python.org/mailman/listinfo/python-list


RE: Regular Expression bug?

2023-03-02 Thread avi.e.gross
It is a well-known fact, Jose, that GIGO.

The letters "n" and "m" are not interchangeable. Your pattern fails because you 
have "pn" in one place and "pm" in the other.


>>> s = "pn=jose pn=2017"
...
>>> s0 = r0.match(s)
>>> s0




-Original Message-
From: Python-list  On 
Behalf Of jose isaias cabrera
Sent: Thursday, March 2, 2023 8:07 PM
To: Mats Wichmann 
Cc: python-list@python.org
Subject: Re: Regular Expression bug?

On Thu, Mar 2, 2023 at 2:38 PM Mats Wichmann  wrote:
>
> On 3/2/23 12:28, Chris Angelico wrote:
> > On Fri, 3 Mar 2023 at 06:24, jose isaias cabrera 
wrote:
> >>
> >> Greetings.
> >>
> >> For the RegExp Gurus, consider the following python3 code:
> >> 
> >> import re
> >> s = "pn=align upgrade sd=2023-02-"
> >> ro = re.compile(r"pn=(.+) ")
> >> r0=ro.match(s)
> > print(r0.group(1))
> >> align upgrade
> >> 
> >>
> >> This is wrong. It should be 'align' because the group only goes up-to
> >> the space. Thoughts? Thanks.
> >>
> >
> > Not a bug. Find the longest possible match that fits this; as long as
> > you can find a space immediately after it, everything in between goes
> > into the .+ part.
> >
> > If you want to exclude spaces, either use [^ ]+ or .+?.
>
> https://docs.python.org/3/howto/regex.html#greedy-versus-non-greedy

This re is a bit different than the one I am used. So, I am trying to match
everything after 'pn=':

import re
s = "pm=jose pn=2017"
m0 = r"pn=(.+)"
r0 = re.compile(m0)
s0 = r0.match(s)
>>> print(s0)
None

Any help is appreciated.
-- 
https://mail.python.org/mailman/listinfo/python-list

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


Re: Regular Expression bug?

2023-03-02 Thread jose isaias cabrera
On Thu, Mar 2, 2023 at 2:38 PM Mats Wichmann  wrote:
>
> On 3/2/23 12:28, Chris Angelico wrote:
> > On Fri, 3 Mar 2023 at 06:24, jose isaias cabrera 
wrote:
> >>
> >> Greetings.
> >>
> >> For the RegExp Gurus, consider the following python3 code:
> >> 
> >> import re
> >> s = "pn=align upgrade sd=2023-02-"
> >> ro = re.compile(r"pn=(.+) ")
> >> r0=ro.match(s)
> > print(r0.group(1))
> >> align upgrade
> >> 
> >>
> >> This is wrong. It should be 'align' because the group only goes up-to
> >> the space. Thoughts? Thanks.
> >>
> >
> > Not a bug. Find the longest possible match that fits this; as long as
> > you can find a space immediately after it, everything in between goes
> > into the .+ part.
> >
> > If you want to exclude spaces, either use [^ ]+ or .+?.
>
> https://docs.python.org/3/howto/regex.html#greedy-versus-non-greedy

This re is a bit different than the one I am used. So, I am trying to match
everything after 'pn=':

import re
s = "pm=jose pn=2017"
m0 = r"pn=(.+)"
r0 = re.compile(m0)
s0 = r0.match(s)
>>> print(s0)
None

Any help is appreciated.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python 3.10 Fizzbuzz

2023-03-02 Thread Keith Thompson
Greg Ewing  writes:
> On 2/03/23 10:59 am, gene heskett wrote:
>> Human skin always has the same color
>
> Um... no?

You took that out of context.  The assertion was that "Human skin
always has the same color" and "the difference is not the color,
but the brightness".  I offer no opinion on whether that's accurate.

-- 
Keith Thompson (The_Other_Keith) keith.s.thompso...@gmail.com
Working, but not speaking, for XCOM Labs
void Void(void) { Void(); } /* The recursive call of the void */
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: Which more Pythonic - self.__class__ or type(self)?

2023-03-02 Thread avi.e.gross
My understanding is that python created functions like type() and len() as a
general purpose way to get information and ALSO set up a protocol that
classes can follow by creating dunder methods. I think the most pythonic
things is to avoid directly calling the dunder methods with a few exceptions
that mainly happen when you are building or extending classes. I mean some
dunder methods are then called directly to avoid getting into infinite loops
that would be triggered.

And note in many cases, the protocol is more complex. Is a length built-in?
If not, can the object be iterated and you count the results? Calling the
function len() may get you more info as it can leverage such things. And it
means you can sometimes leave out some methods and your code still works.

Be warned that type() is a very special function in python and when called
with more arguments, does many relatively beautiful but unrelated things. It
has a special role in the class or type hierarchy. But used with a single
argument, it harmlessly return a result you want.


-Original Message-
From: Python-list  On
Behalf Of Thomas Passin
Sent: Thursday, March 2, 2023 6:43 PM
To: python-list@python.org
Subject: Re: Which more Pythonic - self.__class__ or type(self)?

On 3/2/2023 5:53 PM, Greg Ewing via Python-list wrote:
> On 3/03/23 9:54 am, Ian Pilcher wrote:
>> I haven't found
>> anything that talks about which form is considered to be more Pythonic
>> in those situations where there's no functional difference.
> 
> In such cases I'd probably go for type(x), because it looks less
> ugly.
> 
> x.__class__ *might* be slightly more efficient, as it avoids a
> global lookup and a function call. But as always, measurement
> would be required to be sure.

Except that we don't know if "efficiency" - whatever that might mean 
here - matters at all.

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

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


Re: Which more Pythonic - self.__class__ or type(self)?

2023-03-02 Thread Thomas Passin

On 3/2/2023 5:53 PM, Greg Ewing via Python-list wrote:

On 3/03/23 9:54 am, Ian Pilcher wrote:

I haven't found
anything that talks about which form is considered to be more Pythonic
in those situations where there's no functional difference.


In such cases I'd probably go for type(x), because it looks less
ugly.

x.__class__ *might* be slightly more efficient, as it avoids a
global lookup and a function call. But as always, measurement
would be required to be sure.


Except that we don't know if "efficiency" - whatever that might mean 
here - matters at all.


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


Re: How to escape strings for re.finditer?

2023-03-02 Thread Grant Edwards
On 2023-03-02, Peter J. Holzer  wrote:


> [1] Personally I'd say you shouldn't use Outlook if you are reading
> mails where line breaks (or other formatting) is important, but ...

I'd shorten that to

   "You shouldn't use Outlook if mail is important."

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


RE: How to escape strings for re.finditer?

2023-03-02 Thread avi.e.gross
Thanks, Peter. Excellent advice, even if only for any of us using Microsoft
Outlook as our mailer. I made the changes and we will see but they should
mainly impact what I see. I did tweak another parameter.

The problem for me was finding where they hid the options menu I needed.
Then, I started translating the menus back into German until I realized I
was being silly! Good practice though. LOL!

The truth is I generally can handle receiving mangled code as most of the
time I can re-edit it into shape, or am just reading it and not
copying/pasting.

What concerns me is to be able to send out the pure text content many seem
to need in a way that does not introduce the anomalies people see. Something
like a least-common denominator.

Or. I could switch mailers. But my guess is reading/responding from the
native gmail editor may also need options changes and yet still impact some
readers.

-Original Message-
From: Python-list  On
Behalf Of Peter J. Holzer
Sent: Thursday, March 2, 2023 3:09 PM
To: python-list@python.org
Subject: Re: How to escape strings for re.finditer?

On 2023-03-01 01:01:42 +0100, Peter J. Holzer wrote:
> On 2023-02-28 15:25:05 -0500, avi.e.gr...@gmail.com wrote:
> > I had no doubt the code you ran was indented properly or it would not
work.
> > 
> > I am merely letting you know that somewhere in the process of 
> > copying the code or the transition between mailers, my version is messed
up.
> 
> The problem seems to be at your end. Jen's code looks ok here.
[...]
> I have no idea why it would join only some lines but not others.

Actually I do have an idea now, since I noticed something similar at work
today: Outlook has an option "remove additional line breaks from text-only
messages" (translated from German) in the the "Email / Message Format"
section. You want to make sure this is off if you are reading mails where
line breaks might be important[1].

hp

[1] Personally I'd say you shouldn't use Outlook if you are reading mails
where line breaks (or other formatting) is important, but ...

-- 
   _  | Peter J. Holzer| Story must make more sense than reality.
|_|_) ||
| |   | h...@hjp.at |-- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |   challenge!"

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


Re: Which more Pythonic - self.__class__ or type(self)?

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

On 3/03/23 9:54 am, Ian Pilcher wrote:

I haven't found
anything that talks about which form is considered to be more Pythonic
in those situations where there's no functional difference.


In such cases I'd probably go for type(x), because it looks less
ugly.

x.__class__ *might* be slightly more efficient, as it avoids a
global lookup and a function call. But as always, measurement
would be required to be sure.

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


RE: Regular Expression bug?

2023-03-02 Thread avi.e.gross
José,

Matching can be greedy. Did it match to the last space?

What you want is a pattern that matches anything except a space (or whitespace) 
followed b matching a space or something similar.

Or use a construct that makes matching non-greedy.

Avi

-Original Message-
From: Python-list  On 
Behalf Of jose isaias cabrera
Sent: Thursday, March 2, 2023 2:23 PM
To: python-list@python.org
Subject: Regular Expression bug?

Greetings.

For the RegExp Gurus, consider the following python3 code:

import re
s = "pn=align upgrade sd=2023-02-"
ro = re.compile(r"pn=(.+) ")
r0=ro.match(s)
>>> print(r0.group(1))
align upgrade


This is wrong. It should be 'align' because the group only goes up-to the 
space. Thoughts? Thanks.

josé

-- 

What if eternity is real?  Where will you spend it?  H...
--
https://mail.python.org/mailman/listinfo/python-list

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


Re: Which more Pythonic - self.__class__ or type(self)?

2023-03-02 Thread Thomas Passin

On 3/2/2023 3:54 PM, Ian Pilcher wrote:

Seems like an FAQ, and I've found a few things on StackOverflow that
discuss the technical differences in edge cases, but I haven't found
anything that talks about which form is considered to be more Pythonic
in those situations where there's no functional difference.

Is there any consensus?


For what purpose do you want to get it?

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


[Python-announce] PyCA cryptography 39.0.2 released

2023-03-02 Thread Paul Kehrer
PyCA cryptography 39.0.2 has been released to PyPI. cryptography
includes both high level recipes and low level interfaces to common
cryptographic algorithms such as symmetric ciphers, asymmetric
algorithms, message digests, X509, key derivation functions, and much
more. We support Python 3.6+, and PyPy3.

Changelog (https://cryptography.io/en/latest/changelog/#v39-0-2)

* Fixed a bug where the content type header was not properly encoded
for PKCS7 signatures when using the ``Text`` option and ``SMIME``
encoding.

-Paul Kehrer (reaperhulk)
___
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


Which more Pythonic - self.__class__ or type(self)?

2023-03-02 Thread Ian Pilcher

Seems like an FAQ, and I've found a few things on StackOverflow that
discuss the technical differences in edge cases, but I haven't found
anything that talks about which form is considered to be more Pythonic
in those situations where there's no functional difference.

Is there any consensus?

--

Google  Where SkyNet meets Idiocracy

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


Re: How to escape strings for re.finditer?

2023-03-02 Thread Peter J. Holzer
On 2023-03-01 01:01:42 +0100, Peter J. Holzer wrote:
> On 2023-02-28 15:25:05 -0500, avi.e.gr...@gmail.com wrote:
> > I had no doubt the code you ran was indented properly or it would not work.
> > 
> > I am merely letting you know that somewhere in the process of copying
> > the code or the transition between mailers, my version is messed up.
> 
> The problem seems to be at your end. Jen's code looks ok here.
[...]
> I have no idea why it would join only some lines but not others.

Actually I do have an idea now, since I noticed something similar at
work today: Outlook has an option "remove additional line breaks from
text-only messages" (translated from German) in the the "Email / Message
Format" section. You want to make sure this is off if you are reading
mails where line breaks might be important[1].

hp

[1] Personally I'd say you shouldn't use Outlook if you are reading
mails where line breaks (or other formatting) is important, but ...

-- 
   _  | Peter J. Holzer| Story must make more sense than reality.
|_|_) ||
| |   | h...@hjp.at |-- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |   challenge!"


signature.asc
Description: PGP signature
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Regular Expression bug?

2023-03-02 Thread jose isaias cabrera
On Thu, Mar 2, 2023 at 2:32 PM <2qdxy4rzwzuui...@potatochowder.com> wrote:
>
> On 2023-03-02 at 14:22:41 -0500,
> jose isaias cabrera  wrote:
>
> > For the RegExp Gurus, consider the following python3 code:
> > 
> > import re
> > s = "pn=align upgrade sd=2023-02-"
> > ro = re.compile(r"pn=(.+) ")
> > r0=ro.match(s)
> > >>> print(r0.group(1))
> > align upgrade
> > 
> >
> > This is wrong. It should be 'align' because the group only goes up-to
> > the space. Thoughts? Thanks.
>
> The bug is in your regular expression; the plus modifier is greedy.
>
> If you want to match up to the first space, then you'll need something
> like [^ ] (i.e., everything that isn't a space) instead of that dot.

Thanks. I appreciate your wisdom.

josé
-- 

What if eternity is real?  Where will you spend it?  H...
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Packing Problem

2023-03-02 Thread Weatherby,Gerard
Haven’t look at it all in detail, but I’d replace the list comprehensions with 
a loop:
CopyOfWords = list(Words)
Words = [(w[1:] if w[0] == ch else w) for w in Words]
Words = [w for w in Words if w != '']
nextWords = []
for w in CopyOfWords:
if w[0] != ch:
nextWords.append(w)
elif len(w) > 1:
nextWords.append(w[1:])
assert Words == nextWords

From: Python-list  on 
behalf of Rob Cliffe via Python-list 
Date: Thursday, March 2, 2023 at 2:12 PM
To: python-list@python.org 
Subject: Re: Packing Problem
*** Attention: This is an external email. Use caution responding, opening 
attachments or clicking on links. ***

Slightly improved version (deals with multiple characters together
instead of one at a time):

# Pack.py
def Pack(Words):
 if not Words:
 return ''
 # The method is to build up the result by adding letters at the
beginning
 # and working forward, and by adding letters at the end, working
backwards,
 # meanwhile shrinking the words in the list.
 Words = list(Words) # Don't mutate the original
 Initial = ''
 Final = ''
 while True:
 # It is safe to add an initial letter (of one or more of the
words) if
 # EITHERThere is no word that contains it as
 # a non-initial letter but not as an initial letter.
 #  OR   All words start with it.
 while True:
 FirstLetters = set(w[0] for w in Words)
 FirstLettersSafe = sorted(ch for ch in FirstLetters if
 all(w[0]==ch for w in Words)
 or not any(ch in w[1:] and w[0]!=ch for w in Words))
 # sorted() to make the answer deterministic
 # (not dependent on set ordering)
 if not FirstLettersSafe:
 break
 AnyProgress = True
 Initial += ''.join(FirstLettersSafe)   # Build up the
answer from the beginning
 Words = [ (w[1:] if w[0] in FirstLettersSafe else w) for w
in Words ]
 Words = [ w for w in Words if w != '']
 if not Words:
 return Initial + Final
 # It is safe to add a final letter (of one or more of the words) of
 # EITHERThere is no word that contains it as
 # a non-final letter but not as a final letter.
 #  OR   All words end with it.
 while True:
 LastLetters = set(w[-1] for w in Words)
 LastLettersSafe = sorted(ch for ch in LastLetters if
 all(w[-1]==ch for w in Words)
 or not any(ch in w[:-1] and w[-1]!=ch for w in Words))
 # sorted() to make the answer deterministic
 # (not dependent on set ordering)
 if not LastLettersSafe:
 break
 Final = ''.join(LastLettersSafe) + Final   # Build up the
answer from the end
 Words = [ (w[:-1] if w[-1] in LastLettersSafe else w) for w
in Words ]
 Words = [ w for w in Words if w != '']
 if not Words:
 return Initial + Final
 if not (FirstLettersSafe or LastLettersSafe):
 break # stuck
 # Try all the possibilities for the next letter to add at the
beginning,
 # with recursive calls, and pick one that gives a shortest answer:
 BestResult = None
 for ch in FirstLetters:
 Words2 = list( (w[1:] if w[0] == ch else w) for w in Words )
 Words2 = [ w for w in Words2 if w != '' ]
 res = ch + Pack(Words2)
 if BestResult is None or len(res) < len(BestResult):
 BestResult = res
 return Initial + BestResult + Final

print(Pack(['APPLE', 'PIE', 'APRICOT', 'BANANA', 'CANDY']))

Rob Cliffe
--
https://urldefense.com/v3/__https://mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!l3ysx0BUPZBdKdwb9F8mw4BAE2UIflvNqWeZLfALY2kjEo9e4KTy6fEYoGCTileOUtYe0yp6nL18ymdOguC3TGagEA$
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Regular Expression bug?

2023-03-02 Thread Mats Wichmann

On 3/2/23 12:28, Chris Angelico wrote:

On Fri, 3 Mar 2023 at 06:24, jose isaias cabrera  wrote:


Greetings.

For the RegExp Gurus, consider the following python3 code:

import re
s = "pn=align upgrade sd=2023-02-"
ro = re.compile(r"pn=(.+) ")
r0=ro.match(s)

print(r0.group(1))

align upgrade


This is wrong. It should be 'align' because the group only goes up-to
the space. Thoughts? Thanks.



Not a bug. Find the longest possible match that fits this; as long as
you can find a space immediately after it, everything in between goes
into the .+ part.

If you want to exclude spaces, either use [^ ]+ or .+?.



https://docs.python.org/3/howto/regex.html#greedy-versus-non-greedy

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


Re: Regular Expression bug?

2023-03-02 Thread 2QdxY4RzWzUUiLuE
On 2023-03-02 at 14:22:41 -0500,
jose isaias cabrera  wrote:

> For the RegExp Gurus, consider the following python3 code:
> 
> import re
> s = "pn=align upgrade sd=2023-02-"
> ro = re.compile(r"pn=(.+) ")
> r0=ro.match(s)
> >>> print(r0.group(1))
> align upgrade
> 
> 
> This is wrong. It should be 'align' because the group only goes up-to
> the space. Thoughts? Thanks.

The bug is in your regular expression; the plus modifier is greedy.

If you want to match up to the first space, then you'll need something
like [^ ] (i.e., everything that isn't a space) instead of that dot.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Regular Expression bug?

2023-03-02 Thread Chris Angelico
On Fri, 3 Mar 2023 at 06:24, jose isaias cabrera  wrote:
>
> Greetings.
>
> For the RegExp Gurus, consider the following python3 code:
> 
> import re
> s = "pn=align upgrade sd=2023-02-"
> ro = re.compile(r"pn=(.+) ")
> r0=ro.match(s)
> >>> print(r0.group(1))
> align upgrade
> 
>
> This is wrong. It should be 'align' because the group only goes up-to
> the space. Thoughts? Thanks.
>

Not a bug. Find the longest possible match that fits this; as long as
you can find a space immediately after it, everything in between goes
into the .+ part.

If you want to exclude spaces, either use [^ ]+ or .+?.

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


Regular Expression bug?

2023-03-02 Thread jose isaias cabrera
Greetings.

For the RegExp Gurus, consider the following python3 code:

import re
s = "pn=align upgrade sd=2023-02-"
ro = re.compile(r"pn=(.+) ")
r0=ro.match(s)
>>> print(r0.group(1))
align upgrade


This is wrong. It should be 'align' because the group only goes up-to
the space. Thoughts? Thanks.

josé

-- 

What if eternity is real?  Where will you spend it?  H...
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Packing Problem

2023-03-02 Thread Rob Cliffe via Python-list
Slightly improved version (deals with multiple characters together 
instead of one at a time):


# Pack.py
def Pack(Words):
    if not Words:
    return ''
    # The method is to build up the result by adding letters at the 
beginning
    # and working forward, and by adding letters at the end, working 
backwards,

    # meanwhile shrinking the words in the list.
    Words = list(Words) # Don't mutate the original
    Initial = ''
    Final = ''
    while True:
    # It is safe to add an initial letter (of one or more of the 
words) if

    # EITHER    There is no word that contains it as
    # a non-initial letter but not as an initial letter.
    #  OR   All words start with it.
    while True:
    FirstLetters = set(w[0] for w in Words)
    FirstLettersSafe = sorted(ch for ch in FirstLetters if
    all(w[0]==ch for w in Words)
    or not any(ch in w[1:] and w[0]!=ch for w in Words))
    # sorted() to make the answer deterministic
    # (not dependent on set ordering)
    if not FirstLettersSafe:
    break
    AnyProgress = True
    Initial += ''.join(FirstLettersSafe)   # Build up the 
answer from the beginning
    Words = [ (w[1:] if w[0] in FirstLettersSafe else w) for w 
in Words ]

    Words = [ w for w in Words if w != '']
    if not Words:
    return Initial + Final
    # It is safe to add a final letter (of one or more of the words) of
    # EITHER    There is no word that contains it as
    # a non-final letter but not as a final letter.
    #  OR   All words end with it.
    while True:
    LastLetters = set(w[-1] for w in Words)
    LastLettersSafe = sorted(ch for ch in LastLetters if
    all(w[-1]==ch for w in Words)
    or not any(ch in w[:-1] and w[-1]!=ch for w in Words))
    # sorted() to make the answer deterministic
    # (not dependent on set ordering)
    if not LastLettersSafe:
    break
    Final = ''.join(LastLettersSafe) + Final   # Build up the 
answer from the end
    Words = [ (w[:-1] if w[-1] in LastLettersSafe else w) for w 
in Words ]

    Words = [ w for w in Words if w != '']
    if not Words:
    return Initial + Final
    if not (FirstLettersSafe or LastLettersSafe):
    break # stuck
    # Try all the possibilities for the next letter to add at the 
beginning,

    # with recursive calls, and pick one that gives a shortest answer:
    BestResult = None
    for ch in FirstLetters:
    Words2 = list( (w[1:] if w[0] == ch else w) for w in Words )
    Words2 = [ w for w in Words2 if w != '' ]
    res = ch + Pack(Words2)
    if BestResult is None or len(res) < len(BestResult):
    BestResult = res
    return Initial + BestResult + Final

print(Pack(['APPLE', 'PIE', 'APRICOT', 'BANANA', 'CANDY']))

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


Packing Problem

2023-03-02 Thread Rob Cliffe via Python-list
I found Hen Hanna's "packing" problem to be an intriguing one: Given a 
list of words:

    ['APPLE', 'PIE', 'APRICOT', 'BANANA', 'CANDY']
find a string (in general non-unique) as short as possible which 
contains the letters of each of these words, in order, as a subsequence.
It struck me as being rather hard for a homework problem, unless I'm 
missing something blindingly obvious.
Here is what I came up with (I could have done with 
removeprefix/removesuffix but I'm stuck on Python 3.8 for now ):


# Pack.py
def Pack(Words):
    if not Words:
    return ''
    # The method is to build up the result by adding letters at the 
beginning
    # and working forward, and by adding letters at the end, working 
backwards,

    # meanwhile shrinking the words in the list.
    Words = list(Words) # Don't mutate the original
    Initial = ''
    Final = ''
    while True:
    AnyProgress = False
    # It is safe to add an initial letter (of one or more of the 
words) if

    # EITHER    There is no word that contains it as
    # a non-initial letter but not as an initial letter.
    #  OR   All words start with it.
    while True:
    FirstLetters = ''.join(w[0] for w in Words)
    FirstLetters = [ ch for ch in FirstLetters if
    all(w[0]==ch for w in Words)
    or not any(ch in w[1:] and w[0]!=ch for w in Words) ]
    if not FirstLetters:
    break
    AnyProgress = True
    ch = FirstLetters[0]    # Pick one
    Initial += ch   # Build up the answer from the 
beginning

    Words = [ (w[1:] if w[0]==ch else w) for w in Words ]
    Words = [ w for w in Words if w != '']
    if not Words:
    return Initial + Final
    # It is safe to add a final letter (of one or more of the words) of
    # EITHER    There is no word that contains it as
    # a non-final letter but not as a final letter.
    #  OR   All words end with it.
    while True:
    LastLetters = ''.join(w[-1] for w in Words)
    LastLetters = [ ch for ch in LastLetters if
    all(w[-1]==ch for w in Words)
    or not any(ch in w[:-1] and w[-1]!=ch for w in Words) ]
    if not LastLetters:
    break
    AnyProgress = True
    ch = LastLetters[0] # Pick one
    Final = ch + Final  # Build up the answer from the end
    Words = [ (w[:-1] if w[-1]==ch else w) for w in Words ]
    Words = [ w for w in Words if w != '']
    if not Words:
    return Initial + Final
    if not AnyProgress:
    break
    # Try all the possibilities for the next letter to add at the 
beginning,

    # with recursive calls, and pick one that gives a shortest answer:
    BestResult = None
    for ch in set(w[0] for w in Words):
    Words2 = list( (w[1:] if w[0] == ch else w) for w in Words )
    Words2 = [ w for w in Words2 if w != '' ]
    res = ch + Pack(Words2)
    if BestResult is None or len(res) < len(BestResult):
    BestResult = res
    return Initial + BestResult + Final

print(Pack(['APPLE', 'PIE', 'APRICOT', 'BANANA', 'CANDY']))

The output:
BAPPRICNANADYOTLE
which has the same length as the answer I came up with trying to solve 
it with my unaided brain, which may or may not be reassuring ,

and also contains a much-needed BRANDY.
I expect there are simpler and more efficient solutions.
Best wishes
Rob Cliffe
--
https://mail.python.org/mailman/listinfo/python-list


Re: Look free ID genertion (was: Is there a more efficient threading lock?)

2023-03-02 Thread Dennis Lee Bieber
On Thu, 2 Mar 2023 12:45:50 +1100, Chris Angelico 
declaimed the following:

>
>As have all CPUs since; it's the only way to implement locks (push the
>locking all the way down to the CPU level).
>

Xerox Sigma (circa 1970): Modify and Test (byte/halfword/word)

Granted, that was a "mainframe" system, not a microprocessor.

Looks like Intel didn't catch the boat until 1985 and the i386.



-- 
Wulfraed Dennis Lee Bieber AF6VN
wlfr...@ix.netcom.comhttp://wlfraed.microdiversity.freeddns.org/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python 2.7 range Function provokes a Memory Error

2023-03-02 Thread Jon Ribbens via Python-list
On 2023-03-02, Stephen Tucker  wrote:
> The range function in Python 2.7 (and yes, I know that it is now
> superseded), provokes a Memory Error when asked to deiliver a very long
> list of values.
>
> I assume that this is because the function produces a list which it then
> iterates through.
>
> 1. Does the  range  function in Python 3.x behave the same way?

No, in Python 3 it is an iterator which produces the next number in the
sequence each time.

> 2. Is there any equivalent way that behaves more like a  for loop (that is,
> without producing a list)?

Yes, 'xrange' in Python 2 behaves like 'range' in Python 3.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python 2.7 range Function provokes a Memory Error

2023-03-02 Thread Chris Angelico
On Thu, 2 Mar 2023 at 22:27, Stephen Tucker  wrote:
>
> Hi,
>
> The range function in Python 2.7 (and yes, I know that it is now
> superseded), provokes a Memory Error when asked to deiliver a very long
> list of values.
>
> I assume that this is because the function produces a list which it then
> iterates through.
>
> 1. Does the  range  function in Python 3.x behave the same way?

No, but list(range(x)) might, for the same reason. In Py2, range
returns a list, which means it needs a gigantic collection of integer
objects. In Py3, a range object just defines its start/stop/step, but
if you call list() on it, you get the same sort of

> 2. Is there any equivalent way that behaves more like a  for loop (that is,
> without producing a list)?
>
> To get round the problem I have written my own software that is used in a
> for  loop.

xrange is an iterator in Py2, so that's the easiest way to handle it.
Obviously migrating to Py3 would be the best way, but in the meantime,
xrange will probably do what you need.

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


Re: Python 2.7 range Function provokes a Memory Error

2023-03-02 Thread 2QdxY4RzWzUUiLuE
On 2023-03-02 at 11:25:49 +,
Stephen Tucker  wrote:

> The range function in Python 2.7 (and yes, I know that it is now
> superseded), provokes a Memory Error when asked to deiliver a very long
> list of values.
> 
> I assume that this is because the function produces a list which it then
> iterates through.
> 
> 1. Does the  range  function in Python 3.x behave the same way?

No.

> 2. Is there any equivalent way that behaves more like a  for loop (that is,
> without producing a list)?

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


Python 2.7 range Function provokes a Memory Error

2023-03-02 Thread Stephen Tucker
Hi,

The range function in Python 2.7 (and yes, I know that it is now
superseded), provokes a Memory Error when asked to deiliver a very long
list of values.

I assume that this is because the function produces a list which it then
iterates through.

1. Does the  range  function in Python 3.x behave the same way?

2. Is there any equivalent way that behaves more like a  for loop (that is,
without producing a list)?

To get round the problem I have written my own software that is used in a
for  loop.

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