Raymond Hettinger Profile picture
Jun 5 8 tweets 3 min read
Here's a PDF for my #Python #PyConIT2022 talk: Structural Pattern Matching in the Real World: New tooling, real code, problems solved.

This is intermediate and advanced level Structural Pattern Matching.

tl;dr The “good stuff” is in section 1.2

dropbox.com/s/w1bs8ckekki9…
You'll find #Python pattern matching recipes for:

* Replacing literals with variables and named constants
* Replacing literals with regexes
* Replacing literals with function calls
* Replacing literals with set membership tests
We draw key learning points from real #Python applications:

* Datetime formatting mini language
* Language tokenizer
* SQL logic for JSON lines
* SQL logic for two level JSON
* Traversing arbitrary depth JSON trees
* Marshal style data serializer
There is a problems and solutions section:

* Inverting case logic
* Matching sets and frozensets
* Exact type matches
* Order of overlapping cases
* Converting hasattr() duck typed cases
* Handling difficult cases
* Fooling aggressive linters
* Floating point special values
I'll post a link to the presentation video when it is available, but I only had 30 minutes.

Perhaps another conference has an open back to back timeslots for 60 to 75 minutes.

This is a super interesting topic.
#Python 's Structural pattern matching is amazing.

But when you apply it to real world code, all kinds of interesting problems arise and the solutions aren't always obvious.
The PDF isn't pretty. My slides are generated by Sphinx in a read-tuhe-docs format. When I get a chance (and figure-out how), I'll post the beautiful version of the slides on Github pages.

Hopefully, by then I'll have a video link to post as well.

Enjoy!
The other conference hash tag is #PyConItalia

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Raymond Hettinger

Raymond Hettinger Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @raymondh

Mar 11
#Python tip: The default_factory feature of a defaultdict is only useful when building up a dictionary (automatically adding missing keys).

However, that feature is a menace when doing lookups (risking accidental dict mutation).

Consider converting back to a regular dict.

1/
# During build-up, we want the factory magic.
d = defaultdict(list)
for elem in data:
d[feature(elem)].append(elem)

# Magic is no longer useful.
d = dict(d)

# Lookups are now safe.
print(d[some_feature])

2/
Converting back to a regular dictionary is cheap.

The hash values are reused rather than recomputed. The new dict is presized in a single step. The key and value pointers are copied rather than inserted. The only slow part is updating all the reference counts.

3/
Read 4 tweets
Feb 19
#Python structural pattern matching factlet:

Class patterns with positional arguments match by attribute lookup on the names in __match_args__.

Accordingly, they match normal attributes, descriptors, and virtual attributes implemented with __getattribute__ or __getattr__.

1/
class V:
'Virtual attribute'
__match_args__ = ('a',)
def __getattribute__(self, attr):
return 10 if attr == 'a' else 0

>>> match V():
... case V(10):
... print('hit')
...
hit

2/
class D:
'Descriptor'
__match_args__ = ('a',)
@property
def a(self):
return 20

>>> match D():
... case D(10):
... print('hit')
...
hit

3/
Read 5 tweets
Feb 8
#Python news: It was always awkward to write a type annotation for methods that returned self (an instance of the current class). As of yesterday, typing.Self was added to make this much easier and more readable.

It is a big win.

1/
This works great for methods that return self.

def set_scale(self, scale: float) -> Self:
self.scale = scale
return self

2/
The __enter__ method for context managers is sometimes a beneficiary:

def __enter__(self) -> Self:
return self

3/
Read 8 tweets
Jan 31
We are often burdened by preconceived ideas, ideas that we invented, acquired on related projects, or heard about in class. Sometimes we undertake a project in order to try out a favorite idea. Such ideas may not be derived from our requirements by a rational process -— Parnas
This thought is equally interesting if you switch the judgment from "burdened" to "empowered".

Either way, the core idea remains than programs have a human dimension that transcends requirements and "rational design".
Read 13 tweets
Jan 15
Argh! Who thought Black should be automatically applied to lines in the IPython CLI?

The makes it less useful for education purposes, less useful for interactive math, and annoying when it rewrites your input across multiple lines.
In a #Python course, if you want to demonstrate that print('hello') and print("hello") are the same, then too bad. The CLI rewrites both to use double quotes and the students can't see what you were demonstrating.
When doing math, you improve readability by grouping your terms as shown in PEP 8:

3*x**2 - 5*x + 10

However, the new #Ipython CLI immediately expands it to:

3 * x ** 2 - 5 * x + 10
Read 12 tweets
Nov 18, 2021
#Python success: I was finally able to make a type annotated pure python version of max().

All it took was a protocol bound to a typevar, a custom sentinel class, unions, overloads, isinstance checks, casts, repeated arguments, and the / and * notation.

gist.github.com/rhettinger/beb…
I normally use the "is" operator to test for sentinel values but mypy needs an instance() check to distinguish that case.

Checking ininstance(x, object) always matches, so a custom Sentinel class was required.
MyPy has a known issue picking apart unions in the form: Union[T, Iterable[T].

So, overloads were needed as a work-around.

# github.com/python/mypy/is…
Read 10 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(