On Wed, Jun 24, 2020 at 7:40 AM Ethan Furman ethan@stoneleaf.us wrote:
Okay, I took Paul Moore's advice and went looking in existing code for some examples, and came across the following:
if isinstance(v, int): # v is an offset elif isinstance(v, str): # v is a docstring elif isinstance(v, tuple) and len(v) in (2, 3) and isinstance(v[0],
baseinteger) and isinstance(v[1], (basestring, NoneType)): # v is an offset, a docstring, and (maybe) a default elif isinstance(v, tuple) and len(v) in (1, 2) and isinstance(v[0], (basestring, NoneType)): # v is a docstring and (maybe) a default
That seems like it would be a perfect match (hah) for the new syntax, but I am not confident in my efforts.
This is what I started with:
match v: # goal here is to turn v into an (offset, docstring,
default value) case int: v = v, None, None case str: v = None, v, None case (str, ): # how to combine with above case? v = None, v[0], None case (int, str): v += (None, ) case (int, str, default): pass case (str, default): v = None, v[0], v[1]
Which got me to here:
match v: # goal here is to turn v into an (offset, docstring,
default value) case int(offset): v = offset, None, None case str(doc) | (str(doc), ): v = None, doc, None case (int(offset), str(doc)): v = offset, doc, None case (int(offset), str(doc), default): # already correct pass case (str(doc), default): v = None, doc, default
Is this correct?
Yes.
Side note: I would much rather read "case str(doc) or (str(doc), )" instead
of a |.
Duly noted, we'll come back to this.