<div dir="ltr">Hey Chris,<div><br></div><div>Thanks for bringing that up! Before submitting this, I actually had the syntax for multiple matches for one arm being separated by or. And frankly I just didn't like how that looked for more than 3 items:</div><div><br></div><div>'1' or '2' or '3' or '4' or '5' vs '1', '2', '3', '4', '5'</div><div><br>But you're right. The syntax should be for tuples instead.</div><div><br></div><div><br></div><div>Here's my revised syntax, using a guard instead for the moment:</div><div><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(0, 0, 0);"><br></span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(0, 0, 136);">def</span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(0, 0, 0);"> convert_time_to_timedelta_<wbr>with_match</span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(102, 102, 0);">(</span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(0, 0, 0);">unit</span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(102, 102, 0);">:</span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(0, 0, 0);">str</span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(102, 102, 0);">,</span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(0, 0, 0);"> amount</span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(102, 102, 0);">:</span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(0, 0, 136);">int</span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(102, 102, 0);">,</span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(0, 0, 0);"> now</span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(102, 102, 0);">:</span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(0, 0, 0);">date</span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(102, 102, 0);">):</span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(0, 0, 0);"><br> </span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(0, 0, 136);">return</span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(0, 0, 0);"> match unit</span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(102, 102, 0);">:</span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(0, 0, 0);"><br> </span><span style="font-family: monospace; background-color: rgb(250, 250, 250);"><font color="#008800">x if x in (</font></span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(0, 136, 0);">'days'</span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(102, 102, 0);">,</span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(0, 0, 0);"> </span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(0, 136, 0);">'hours'</span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(102, 102, 0);">,</span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(0, 0, 0);"> </span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(0, 136, 0);">'weeks'</span><span style="font-family: monospace; background-color: rgb(250, 250, 250);"><font color="#008800">)</font></span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(0, 0, 0);"> </span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(102, 102, 0);">=></span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(0, 0, 0);"> timedelta</span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(102, 102, 0);">(**{</span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(0, 0, 0);">unit</span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(102, 102, 0);">:</span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(0, 0, 0);"> amount</span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(102, 102, 0);">})</span></div><div><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(0, 0, 0);"> </span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(0, 136, 0);">'months'</span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(0, 0, 0);"> </span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(102, 102, 0);">=></span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(0, 0, 0);"> timedelta</span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(102, 102, 0);">(</span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(0, 0, 0);">days</span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(102, 102, 0);">=</span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(0, 102, 102);">30</span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(0, 0, 0);"> </span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(102, 102, 0);">*</span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(0, 0, 0);"> amount</span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(102, 102, 0);">)</span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(0, 0, 0);"><br> </span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(0, 136, 0);">'years'</span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(0, 0, 0);"> </span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(102, 102, 0);">=></span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(0, 0, 0);"> timedelta</span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(102, 102, 0);">(</span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(0, 0, 0);">days</span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(102, 102, 0);">=</span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(0, 102, 102);">365</span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(0, 0, 0);"> </span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(102, 102, 0);">*</span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(0, 0, 0);"> amount</span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(102, 102, 0);">)</span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(0, 0, 0);"><br> </span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(0, 136, 0);">'cal_years'</span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(0, 0, 0);"> </span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(102, 102, 0);">=></span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(0, 0, 0);"> now </span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(102, 102, 0);">-</span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(0, 0, 0);"> now</span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(102, 102, 0);">.</span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(0, 0, 0);">replace</span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(102, 102, 0);">(</span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(0, 0, 0);">year</span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(102, 102, 0);">=</span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(0, 0, 0);">now</span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(102, 102, 0);">.</span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(0, 0, 0);">year </span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(102, 102, 0);">-</span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(0, 0, 0);"> amount</span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(102, 102, 0);">)</span><span style="font-family: monospace; background-color: rgb(250, 250, 250); color: rgb(0, 0, 0);"><br></span></div><div><br><br><br>On Thursday, May 3, 2018 at 2:54:24 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 4:36 AM, Robert Roskam <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="ykQHOrCqBAAJ" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">raider...@gmail.com</a>> wrote:
<br>> Hey Chris,
<br>>
<br>> So I started extremely generally with my syntax, but it seems like I should
<br>> provide a lot more examples of real use. Examples are hard. Here's my
<br>> hastily put together example from an existing piece of production code:
<br>>
<br>>
<br>> # New Syntax for same problem
<br>>
<br>>
<br>> def convert_time_to_timedelta_<wbr>with_match(unit:str, amount:int, now:date):
<br>> return match unit:
<br>> 'days', 'hours', 'weeks' => timedelta(**{unit: amount})
<br>> 'months' => timedelta(days=30 * amount)
<br>> 'years' => timedelta(days=365 * amount)
<br>> 'cal_years' => now - now.replace(year=now.year - amount)
<br>
<br>Okay, here we may have a problem. You're expecting a comma separated
<br>set of values to indicate "any of these", but elsewhere, pattern
<br>matching against a list of values is making an assertion about a
<br>tuple. So if you have any pattern matching that isn't based on
<br>equality, you're going to need to clearly stipulate how your syntax
<br>works.
<br>
<br>If you are NOT going to support tuple pattern matching (but only
<br>dict), you'll need to make this VERY clear, because people are going
<br>to expect it.
<br>
<br>ChrisA
<br>______________________________<wbr>_________________
<br>Python-ideas mailing list
<br><a href="javascript:" target="_blank" gdf-obfuscated-mailto="ykQHOrCqBAAJ" 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>