Re: Some pattern annoyance

Steve Holden <[email protected]> Wed, 2 Aug 2023 21:30:36 +0100
Newsgroups gmane.comp.python.devel
Message-ID <CAMofdRB+rRCpWOCORjxm+znB574MKiEajzkqDaNybo+ZjsRsjw@mail.gmail.com>
--===============8917413804798509957==
Content-Type: multipart/alternative; boundary="000000000000dbd4080601f68933"

--000000000000dbd4080601f68933
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Hi Chris,

Nice to see you on the list.

While this is definitely off-topic, I trust I might be given license by the
list's few remaining readers to point out that the match-case construct is
for _structural_ pattern matching. As I wrote in the latest Nutshell:
"Resist the temptation to use match unless there is a need to analyse the
_structure_ of an object."

I don't believe it's accidental that match-case sequence patterns won't
match str, bytes or bytearrray objects - regexen are the tool already
optimised for that purpose, so it's quite impressive that you are
managing to approach the same level of performance!

Kind regards,
Steve


On Wed, 2 Aug 2023 at 18:26, Christian Tismer-Sperling <[email protected]=
m>
wrote:

> On 02.08.23 18:30, Paul Moore wrote:
> > On Wed, 2 Aug 2023 at 15:24, Stephen J. Turnbull
> > <[email protected]
> > <mailto:[email protected]>> wrote:
> >
> >     Partly because that's where the other discussants are (the network
> >     externality is undeniably powerful), and partly (I believe) because
> >     effective use of email is a skill that requires effort to acquire.
> >     Popular mail clients are designed to be popular, not to make that
> >     expertise easy to acquire and exercise.  Clunky use of email makes
> >     lists much less pleasant for everyone than they could be.
> >
> >     I guess that's sad (I am, after all, a GNU Mailman developer), but
> >     it's reality.
> >
> >
> > Personally, I'm sad because some people whose contributions I enjoy (yo=
u
> > being one of them :-)) didn't move to Discourse. But like you say, it's
> > how things are.
> >
> > Christian - you can make named constants using class attributes (or an
> > enum):
> >
> > class A:
> >      M =3D "M"
> >
> > match seq:
> >      case A.M, A.M, A.M, A.M, *r:
> >          return 4*1000, r
> >
> > Basically, the "names are treated as variables to assign to" rule
> > doesn't apply to attributes.
> >
> > I'm not sure how helpful that is (it's not particularly *shorter*) but =
I
> > think the idea was that most uses of named constants in a match
> > statement would be enums or module attributes. And compromises had to b=
e
> > made.
> >
> > Cheers,
> > Paul
>
> Thanks a lot, everybody!
>
> I have tried a lot now, using classes which becomes more readable
> but - funnily - slower! Using the clumsy if-guards felt slow but isn't.
>
> Then I generated functions even, with everything as constants,
> and now the SPM version in fact out-performs the regex slightly!
>
> But at last, I found an even faster and correct algorithm
> by a different approach, which ends now this story :)
>
> Going to the Discourse tite, now.
>
> Cheers -- Chris
> --
> Christian Tismer-Sperling    :^)   [email protected]
> Software Consulting          :     http://www.stackless.com/
> Strandstra=C3=9Fe 37              :     https://github.com/PySide
> 24217 Sch=C3=B6nberg              :     GPG key -> 0xFB7BEE0E
> phone +49 173 24 18 776  fax +49 (30) 700143-0023
>
> _______________________________________________
> Python-Dev mailing list -- [email protected]
> To unsubscribe send an email to [email protected]
> https://mail.python.org/mailman3/lists/python-dev.python.org/
> Message archived at
> https://mail.python.org/archives/list/[email protected]/message/OFLAU=
34KWAKREKG4H2M5GES3PGT6VBAU/
> Code of Conduct: http://python.org/psf/codeofconduct/
>

--000000000000dbd4080601f68933
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div class=3D"gmail_default" style=3D"font-size:small">Hi =
Chris,</div><div class=3D"gmail_default" style=3D"font-size:small"><br></di=
v><div class=3D"gmail_default" style=3D"font-size:small">Nice to see you on=
 the list.</div><div class=3D"gmail_default" style=3D"font-size:small"><br>=
</div><div class=3D"gmail_default" style=3D"font-size:small">While this is =
definitely off-topic, I trust I might be given license by the list&#39;s fe=
w remaining readers to point out that the match-case construct is for _stru=
ctural_ pattern matching. As I wrote in the latest Nutshell: &quot;Resist t=
he temptation to use match unless there is a need to analyse the _structure=
_ of an object.&quot;</div><div class=3D"gmail_default" style=3D"font-size:=
small"><br></div><div class=3D"gmail_default" style=3D"font-size:small">I d=
on&#39;t believe it&#39;s accidental that match-case sequence patterns won&=
#39;t match str, bytes or bytearrray=C2=A0objects - regexen=C2=A0are the to=
ol already optimised for that purpose, so it&#39;s quite impressive that yo=
u are managing=C2=A0to approach the same level of performance!</div><div cl=
ass=3D"gmail_default" style=3D"font-size:small"><br></div><div><div dir=3D"=
ltr" class=3D"gmail_signature" data-smartmail=3D"gmail_signature"><div dir=
=3D"ltr"><div><div dir=3D"ltr">Kind regards,</div><div dir=3D"ltr">Steve<br=
></div></div></div></div></div><br></div><br><div class=3D"gmail_quote"><di=
v dir=3D"ltr" class=3D"gmail_attr">On Wed, 2 Aug 2023 at 18:26, Christian T=
ismer-Sperling &lt;<a href=3D"mailto:[email protected]">tismer@stackless=
.com</a>&gt; wrote:<br></div><blockquote class=3D"gmail_quote" style=3D"mar=
gin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1=
ex">On 02.08.23 18:30, Paul Moore wrote:<br>
&gt; On Wed, 2 Aug 2023 at 15:24, Stephen J. Turnbull <br>
&gt; &lt;<a href=3D"mailto:[email protected]" target=3D"_=
blank">[email protected]</a> <br>
&gt; &lt;mailto:<a href=3D"mailto:[email protected]" targ=
et=3D"_blank">[email protected]</a>&gt;&gt; wrote:<br>
&gt; <br>
&gt;=C2=A0 =C2=A0 =C2=A0Partly because that&#39;s where the other discussan=
ts are (the network<br>
&gt;=C2=A0 =C2=A0 =C2=A0externality is undeniably powerful), and partly (I =
believe) because<br>
&gt;=C2=A0 =C2=A0 =C2=A0effective use of email is a skill that requires eff=
ort to acquire.<br>
&gt;=C2=A0 =C2=A0 =C2=A0Popular mail clients are designed to be popular, no=
t to make that<br>
&gt;=C2=A0 =C2=A0 =C2=A0expertise easy to acquire and exercise.=C2=A0 Clunk=
y use of email makes<br>
&gt;=C2=A0 =C2=A0 =C2=A0lists much less pleasant for everyone than they cou=
ld be.<br>
&gt; <br>
&gt;=C2=A0 =C2=A0 =C2=A0I guess that&#39;s sad (I am, after all, a GNU Mail=
man developer), but<br>
&gt;=C2=A0 =C2=A0 =C2=A0it&#39;s reality.<br>
&gt; <br>
&gt; <br>
&gt; Personally, I&#39;m sad because some people whose contributions I enjo=
y (you <br>
&gt; being one of them :-)) didn&#39;t move to Discourse. But like you say,=
 it&#39;s <br>
&gt; how things are.<br>
&gt; <br>
&gt; Christian - you can make named constants using class attributes (or an=
 <br>
&gt; enum):<br>
&gt; <br>
&gt; class A:<br>
&gt;=C2=A0 =C2=A0 =C2=A0 M =3D &quot;M&quot;<br>
&gt; <br>
&gt; match seq:<br>
&gt;=C2=A0 =C2=A0 =C2=A0 case A.M, A.M, A.M, A.M, *r:<br>
&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 return 4*1000, r<br>
&gt; <br>
&gt; Basically, the &quot;names are treated as variables to assign to&quot;=
 rule <br>
&gt; doesn&#39;t apply to attributes.<br>
&gt; <br>
&gt; I&#39;m not sure how helpful that is (it&#39;s not particularly *short=
er*) but I <br>
&gt; think the idea was that most uses of named constants in a match <br>
&gt; statement would be enums or module attributes. And compromises had to =
be <br>
&gt; made.<br>
&gt; <br>
&gt; Cheers,<br>
&gt; Paul<br>
<br>
Thanks a lot, everybody!<br>
<br>
I have tried a lot now, using classes which becomes more readable<br>
but - funnily - slower! Using the clumsy if-guards felt slow but isn&#39;t.=
<br>
<br>
Then I generated functions even, with everything as constants,<br>
and now the SPM version in fact out-performs the regex slightly!<br>
<br>
But at last, I found an even faster and correct algorithm<br>
by a different approach, which ends now this story :)<br>
<br>
Going to the Discourse tite, now.<br>
<br>
Cheers -- Chris<br>
-- <br>
Christian Tismer-Sperling=C2=A0 =C2=A0 :^)=C2=A0 =C2=A0<a href=3D"mailto:ti=
[email protected]" target=3D"_blank">[email protected]</a><br>
Software Consulting=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 :=C2=A0 =C2=A0 =C2=A0=
<a href=3D"http://www.stackless.com/" rel=3D"noreferrer" target=3D"_blank">=
http://www.stackless.com/</a><br>
Strandstra=C3=9Fe 37=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 :=C2=
=A0 =C2=A0 =C2=A0<a href=3D"https://github.com/PySide" rel=3D"noreferrer" t=
arget=3D"_blank">https://github.com/PySide</a><br>
24217 Sch=C3=B6nberg=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 :=C2=
=A0 =C2=A0 =C2=A0GPG key -&gt; 0xFB7BEE0E<br>
phone +49 173 24 18 776=C2=A0 fax +49 (30) 700143-0023<br>
<br>
_______________________________________________<br>
Python-Dev mailing list -- <a href=3D"mailto:[email protected]" target=
=3D"_blank">[email protected]</a><br>
To unsubscribe send an email to <a href=3D"mailto:[email protected]=
rg" target=3D"_blank">[email protected]</a><br>
<a href=3D"https://mail.python.org/mailman3/lists/python-dev.python.org/" r=
el=3D"noreferrer" target=3D"_blank">https://mail.python.org/mailman3/lists/=
python-dev.python.org/</a><br>
Message archived at <a href=3D"https://mail.python.org/archives/list/python=
[email protected]/message/OFLAU34KWAKREKG4H2M5GES3PGT6VBAU/" rel=3D"noreferre=
r" target=3D"_blank">https://mail.python.org/archives/list/python-dev@pytho=
n.org/message/OFLAU34KWAKREKG4H2M5GES3PGT6VBAU/</a><br>
Code of Conduct: <a href=3D"http://python.org/psf/codeofconduct/" rel=3D"no=
referrer" target=3D"_blank">http://python.org/psf/codeofconduct/</a><br>
</blockquote></div>

--000000000000dbd4080601f68933--

--===============8917413804798509957==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline