<div dir="ltr">Hey Chris,<div><br></div><div>So I started extremely generally with my syntax, but it seems like I should provide a lot more examples of real use. Examples are hard. Here's my hastily put together example from an existing piece of production code:</div><div><br></div><div><br></div><div class="prettyprint" style="background-color: rgb(250, 250, 250); border-color: rgb(187, 187, 187); border-style: solid; border-width: 1px; word-wrap: break-word;"><code class="prettyprint"><div class="subprettyprint"><span style="color: #800;" class="styled-by-prettify"># Existing Production Code</span><span style="color: #000;" class="styled-by-prettify"><br></span><span style="color: #008;" class="styled-by-prettify">from</span><span style="color: #000;" class="styled-by-prettify"> datetime </span><span style="color: #008;" class="styled-by-prettify">import</span><span style="color: #000;" class="styled-by-prettify"> timedelta</span><span style="color: #660;" class="styled-by-prettify">,</span><span style="color: #000;" class="styled-by-prettify"> date<br></span><span style="color: #008;" class="styled-by-prettify">from</span><span style="color: #000;" class="styled-by-prettify"> django</span><span style="color: #660;" class="styled-by-prettify">.</span><span style="color: #000;" class="styled-by-prettify">utils </span><span style="color: #008;" class="styled-by-prettify">import</span><span style="color: #000;" class="styled-by-prettify"> timezone<br><br><br></span><span style="color: #008;" class="styled-by-prettify">def</span><span style="color: #000;" class="styled-by-prettify"> convert_time_to_timedelta</span><span style="color: #660;" class="styled-by-prettify">(</span><span style="color: #000;" class="styled-by-prettify">unit</span><span style="color: #660;" class="styled-by-prettify">:</span><span style="color: #000;" class="styled-by-prettify">str</span><span style="color: #660;" class="styled-by-prettify">,</span><span style="color: #000;" class="styled-by-prettify"> amount</span><span style="color: #660;" class="styled-by-prettify">:</span><span style="color: #008;" class="styled-by-prettify">int</span><span style="color: #660;" class="styled-by-prettify">,</span><span style="color: #000;" class="styled-by-prettify"> now</span><span style="color: #660;" class="styled-by-prettify">:</span><span style="color: #000;" class="styled-by-prettify">date</span><span style="color: #660;" class="styled-by-prettify">):</span><span style="color: #000;" class="styled-by-prettify"><br> </span><span style="color: #008;" class="styled-by-prettify">if</span><span style="color: #000;" class="styled-by-prettify"> unit </span><span style="color: #008;" class="styled-by-prettify">in</span><span style="color: #000;" class="styled-by-prettify"> </span><span style="color: #660;" class="styled-by-prettify">[</span><span style="color: #080;" class="styled-by-prettify">'days'</span><span style="color: #660;" class="styled-by-prettify">,</span><span style="color: #000;" class="styled-by-prettify"> </span><span style="color: #080;" class="styled-by-prettify">'hours'</span><span style="color: #660;" class="styled-by-prettify">,</span><span style="color: #000;" class="styled-by-prettify"> </span><span style="color: #080;" class="styled-by-prettify">'weeks'</span><span style="color: #660;" class="styled-by-prettify">]:</span><span style="color: #000;" class="styled-by-prettify"><br> </span><span style="color: #008;" class="styled-by-prettify">return</span><span style="color: #000;" class="styled-by-prettify"> timedelta</span><span style="color: #660;" class="styled-by-prettify">(**{</span><span style="color: #000;" class="styled-by-prettify">unit</span><span style="color: #660;" class="styled-by-prettify">:</span><span style="color: #000;" class="styled-by-prettify"> amount</span><span style="color: #660;" class="styled-by-prettify">})</span><span style="color: #000;" class="styled-by-prettify"><br> </span><span style="color: #008;" class="styled-by-prettify">elif</span><span style="color: #000;" class="styled-by-prettify"> unit </span><span style="color: #660;" class="styled-by-prettify">==</span><span style="color: #000;" class="styled-by-prettify"> </span><span style="color: #080;" class="styled-by-prettify">'months'</span><span style="color: #660;" class="styled-by-prettify">:</span><span style="color: #000;" class="styled-by-prettify"><br> </span><span style="color: #008;" class="styled-by-prettify">return</span><span style="color: #000;" class="styled-by-prettify"> timedelta</span><span style="color: #660;" class="styled-by-prettify">(</span><span style="color: #000;" class="styled-by-prettify">days</span><span style="color: #660;" class="styled-by-prettify">=</span><span style="color: #066;" class="styled-by-prettify">30</span><span style="color: #000;" class="styled-by-prettify"> </span><span style="color: #660;" class="styled-by-prettify">*</span><span style="color: #000;" class="styled-by-prettify"> amount</span><span style="color: #660;" class="styled-by-prettify">)</span><span style="color: #000;" class="styled-by-prettify"><br> </span><span style="color: #008;" class="styled-by-prettify">elif</span><span style="color: #000;" class="styled-by-prettify"> unit </span><span style="color: #660;" class="styled-by-prettify">==</span><span style="color: #000;" class="styled-by-prettify"> </span><span style="color: #080;" class="styled-by-prettify">'years'</span><span style="color: #660;" class="styled-by-prettify">:</span><span style="color: #000;" class="styled-by-prettify"><br> </span><span style="color: #008;" class="styled-by-prettify">return</span><span style="color: #000;" class="styled-by-prettify"> timedelta</span><span style="color: #660;" class="styled-by-prettify">(</span><span style="color: #000;" class="styled-by-prettify">days</span><span style="color: #660;" class="styled-by-prettify">=</span><span style="color: #066;" class="styled-by-prettify">365</span><span style="color: #000;" class="styled-by-prettify"> </span><span style="color: #660;" class="styled-by-prettify">*</span><span style="color: #000;" class="styled-by-prettify"> amount</span><span style="color: #660;" class="styled-by-prettify">)</span><span style="color: #000;" class="styled-by-prettify"><br> </span><span style="color: #008;" class="styled-by-prettify">elif</span><span style="color: #000;" class="styled-by-prettify"> unit </span><span style="color: #660;" class="styled-by-prettify">==</span><span style="color: #000;" class="styled-by-prettify"> </span><span style="color: #080;" class="styled-by-prettify">'cal_years'</span><span style="color: #660;" class="styled-by-prettify">:</span><span style="color: #000;" class="styled-by-prettify"><br> </span><span style="color: #008;" class="styled-by-prettify">return</span><span style="color: #000;" class="styled-by-prettify"> now </span><span style="color: #660;" class="styled-by-prettify">-</span><span style="color: #000;" class="styled-by-prettify"> now</span><span style="color: #660;" class="styled-by-prettify">.</span><span style="color: #000;" class="styled-by-prettify">replace</span><span style="color: #660;" class="styled-by-prettify">(</span><span style="color: #000;" class="styled-by-prettify">year</span><span style="color: #660;" class="styled-by-prettify">=</span><span style="color: #000;" class="styled-by-prettify">now</span><span style="color: #660;" class="styled-by-prettify">.</span><span style="color: #000;" class="styled-by-prettify">year </span><span style="color: #660;" class="styled-by-prettify">-</span><span style="color: #000;" class="styled-by-prettify"> amount</span><span style="color: #660;" class="styled-by-prettify">)</span><span style="color: #000;" class="styled-by-prettify"><br><br><br><br><br></span><span style="color: #800;" class="styled-by-prettify"># New Syntax for same problem</span><span style="color: #000;" class="styled-by-prettify"><br><br><br></span><span style="color: #008;" class="styled-by-prettify">def</span><span style="color: #000;" class="styled-by-prettify"> convert_time_to_timedelta_with_match</span><span style="color: #660;" class="styled-by-prettify">(</span><span style="color: #000;" class="styled-by-prettify">unit</span><span style="color: #660;" class="styled-by-prettify">:</span><span style="color: #000;" class="styled-by-prettify">str</span><span style="color: #660;" class="styled-by-prettify">,</span><span style="color: #000;" class="styled-by-prettify"> amount</span><span style="color: #660;" class="styled-by-prettify">:</span><span style="color: #008;" class="styled-by-prettify">int</span><span style="color: #660;" class="styled-by-prettify">,</span><span style="color: #000;" class="styled-by-prettify"> now</span><span style="color: #660;" class="styled-by-prettify">:</span><span style="color: #000;" class="styled-by-prettify">date</span><span style="color: #660;" class="styled-by-prettify">):</span><span style="color: #000;" class="styled-by-prettify"><br> </span><span style="color: #008;" class="styled-by-prettify">return</span><span style="color: #000;" class="styled-by-prettify"> match unit</span><span style="color: #660;" class="styled-by-prettify">:</span><span style="color: #000;" class="styled-by-prettify"><br> </span><span style="color: #080;" class="styled-by-prettify">'days'</span><span style="color: #660;" class="styled-by-prettify">,</span><span style="color: #000;" class="styled-by-prettify"> </span><span style="color: #080;" class="styled-by-prettify">'hours'</span><span style="color: #660;" class="styled-by-prettify">,</span><span style="color: #000;" class="styled-by-prettify"> </span><span style="color: #080;" class="styled-by-prettify">'weeks'</span><span style="color: #000;" class="styled-by-prettify"> </span><span style="color: #660;" class="styled-by-prettify">=></span><span style="color: #000;" class="styled-by-prettify"> timedelta</span><span style="color: #660;" class="styled-by-prettify">(**{</span><span style="color: #000;" class="styled-by-prettify">unit</span><span style="color: #660;" class="styled-by-prettify">:</span><span style="color: #000;" class="styled-by-prettify"> amount</span><span style="color: #660;" class="styled-by-prettify">})</span><span style="color: #000;" class="styled-by-prettify"><br> </span><span style="color: #080;" class="styled-by-prettify">'months'</span><span style="color: #000;" class="styled-by-prettify"> </span><span style="color: #660;" class="styled-by-prettify">=></span><span style="color: #000;" class="styled-by-prettify"> timedelta</span><span style="color: #660;" class="styled-by-prettify">(</span><span style="color: #000;" class="styled-by-prettify">days</span><span style="color: #660;" class="styled-by-prettify">=</span><span style="color: #066;" class="styled-by-prettify">30</span><span style="color: #000;" class="styled-by-prettify"> </span><span style="color: #660;" class="styled-by-prettify">*</span><span style="color: #000;" class="styled-by-prettify"> amount</span><span style="color: #660;" class="styled-by-prettify">)</span><span style="color: #000;" class="styled-by-prettify"><br> </span><span style="color: #080;" class="styled-by-prettify">'years'</span><span style="color: #000;" class="styled-by-prettify"> </span><span style="color: #660;" class="styled-by-prettify">=></span><span style="color: #000;" class="styled-by-prettify"> timedelta</span><span style="color: #660;" class="styled-by-prettify">(</span><span style="color: #000;" class="styled-by-prettify">days</span><span style="color: #660;" class="styled-by-prettify">=</span><span style="color: #066;" class="styled-by-prettify">365</span><span style="color: #000;" class="styled-by-prettify"> </span><span style="color: #660;" class="styled-by-prettify">*</span><span style="color: #000;" class="styled-by-prettify"> amount</span><span style="color: #660;" class="styled-by-prettify">)</span><span style="color: #000;" class="styled-by-prettify"><br> </span><span style="color: #080;" class="styled-by-prettify">'cal_years'</span><span style="color: #000;" class="styled-by-prettify"> </span><span style="color: #660;" class="styled-by-prettify">=></span><span style="color: #000;" class="styled-by-prettify"> now </span><span style="color: #660;" class="styled-by-prettify">-</span><span style="color: #000;" class="styled-by-prettify"> now</span><span style="color: #660;" class="styled-by-prettify">.</span><span style="color: #000;" class="styled-by-prettify">replace</span><span style="color: #660;" class="styled-by-prettify">(</span><span style="color: #000;" class="styled-by-prettify">year</span><span style="color: #660;" class="styled-by-prettify">=</span><span style="color: #000;" class="styled-by-prettify">now</span><span style="color: #660;" class="styled-by-prettify">.</span><span style="color: #000;" class="styled-by-prettify">year </span><span style="color: #660;" class="styled-by-prettify">-</span><span style="color: #000;" class="styled-by-prettify"> amount</span><span style="color: #660;" class="styled-by-prettify">)</span><span style="color: #000;" class="styled-by-prettify"><br><br></span></div></code></div><div><br><br></div><div><br></div><div><br></div><div><br><br>On Thursday, May 3, 2018 at 2:02:54 PM UTC-4, Chris Angelico wrote:<blockquote class="gmail_quote" style="margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">On Fri, May 4, 2018 at 3:18 AM, Ed Kellett <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="mdMkoeCnBAAJ" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">e+pytho...@kellett.im</a>> wrote:
<br>> I believe the intention in the example you quoted is syntax something like:
<br>>
<br>> <match-case> ::= <pattern>
<br>> | <pattern> "if" <expression>
<br>>
<br>> where the expression is a guard expression evaluated in the context of
<br>> the matched pattern.
<br>>
<br>> IOW, it could be written like this, too:
<br>>
<br>> number = match x:
<br>> 1 if True => "one"
<br>> y if isinstance(y, str) => f'The string is {y}'
<br>> _ if True => "anything"
<br>>
<br>> I do see a lot of room for bikeshedding around the specific spelling.
<br>> I'm going to try to resist the temptation ;)
<br>
<br>Okay, let me try to tease apart your example.
<br>
<br>1) A literal matches anything that compares equal to that value.
<br>2) A name matches anything at all, and binds it to that name.
<br>2a) An underscore matches anything at all. It's just a name, and
<br>follows a common convention.
<br>3) "if cond" modifies the prior match; if the condition evaluates as
<br>falsey, the match does not match.
<br>4) As evidenced below, a comma-separated list of comparisons matches a
<br>tuple with as many elements, and each element must match.
<br>
<br>Ultimately, this has to be a series of conditions, so this is
<br>effectively a syntax for an elif tree as an expression.
<br>
<br>For another example, here's a way to use inequalities to pick a
<br>numeric formatting:
<br>
<br>display = match number:
<br> x if x < 1e3: f"{number}"
<br> x if x < 1e6: f"{number/1e3} thousand"
<br> x if x < 1e9: f"** {number/1e6} million **"
<br> x if x < 1e12: f"an incredible {number/1e9} billion"
<br> _: "way WAY too many"
<br>
<br>I guarantee you that people are going to ask for this to be spelled
<br>simply "< 1e3" instead of having the "x if x" part. :)
<br>
<br>> How about this?
<br>>
<br>> def hyperop(n, a, b):
<br>> return match (n, a, b):
<br>> (0, _, b) => b + 1
<br>> (1, a, 0) => a
<br>> (2, _, 0) => 0
<br>> (_, _, 0) => 1
<br>> (n, a, b) => hyperop(n-1, a, hyperop(n, a, b-1))
<br>>
<br>> versus:
<br>>
<br>> def hyperop(n, a, b):
<br>> if n == 0:
<br>> return b + 1
<br>> if n == 1 and b == 0:
<br>> return a
<br>> if n == 2 and b == 0:
<br>> return 0
<br>> if b == 0:
<br>> return 1
<br>> return hyperop(n-1, a, hyperop(n, a, b-1))
<br>
<br>I have no idea what this is actually doing, and it looks like a port
<br>of Haskell code. I'd want to rewrite it as a 'while' loop with maybe
<br>one level of recursion in it, instead of two. (Zero would be better,
<br>but I think that's not possible. Maybe?) Is this something that you do
<br>a lot of? Is the tuple (n, a, b) meaningful as a whole, or are the
<br>three values independently of interest?
<br>
<br>Not sure how this is useful without a lot more context.
<br>
<br>ChrisA
<br>______________________________<wbr>_________________
<br>Python-ideas mailing list
<br><a href="javascript:" target="_blank" gdf-obfuscated-mailto="mdMkoeCnBAAJ" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">Python...@python.org</a>
<br><a href="https://mail.python.org/mailman/listinfo/python-ideas" target="_blank" rel="nofollow" onmousedown="this.href='https://www.google.com/url?q\x3dhttps%3A%2F%2Fmail.python.org%2Fmailman%2Flistinfo%2Fpython-ideas\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFj1EaNHnVmh20FnFPoUi4J-MpfQw';return true;" onclick="this.href='https://www.google.com/url?q\x3dhttps%3A%2F%2Fmail.python.org%2Fmailman%2Flistinfo%2Fpython-ideas\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFj1EaNHnVmh20FnFPoUi4J-MpfQw';return true;">https://mail.python.org/<wbr>mailman/listinfo/python-ideas</a>
<br>Code of Conduct: <a href="http://python.org/psf/codeofconduct/" target="_blank" rel="nofollow" onmousedown="this.href='http://www.google.com/url?q\x3dhttp%3A%2F%2Fpython.org%2Fpsf%2Fcodeofconduct%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHJOrArSUDKkjrnthO6_CznMzkPsA';return true;" onclick="this.href='http://www.google.com/url?q\x3dhttp%3A%2F%2Fpython.org%2Fpsf%2Fcodeofconduct%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHJOrArSUDKkjrnthO6_CznMzkPsA';return true;">http://python.org/psf/<wbr>codeofconduct/</a>
<br></blockquote></div></div>