[issue10073] calendar.isleap() not checking parameter type

Boštjan Mejak report at bugs.python.org
Mon Oct 18 20:12:48 CEST 2010


Boštjan Mejak <bostjan.mejak at gmail.com> added the comment:

Also, your "pedantic" version of isleap() is not pedantic at all.

return date(year, 3, 1) - date(year, 2, 1) == timedelta(29)
does not seem readable at all. Readability counts!

return date(year, 3, 1) is not understandable. What are the arguments 3 and
1 in the date() function for?

On Mon, Oct 18, 2010 at 8:06 PM, Boštjan Mejak <bostjan.mejak at gmail.com>wrote:

>     else:
> -->    ndays = (date(year, month + 1, 1) - first).days
>     return first.weekday(), ndays
>
> Oh my God! The line with a pointer is so ugly!
>
> On Mon, Oct 18, 2010 at 7:59 PM, Alexander Belopolsky <
> report at bugs.python.org> wrote:
>
>>
>> Alexander Belopolsky <belopolsky at users.sourceforge.net> added the
>> comment:
>>
>> The most pedantic implementation of calendar.isleap() would be
>>
>> from datetime import date, timedelta
>> def isleap(year):
>>    return date(year, 3, 1) - date(year, 2, 1) == timedelta(29)
>>
>> Since python calendar only supports years in the range [1, 9999], the
>> above will properly raise ValueError: year is out of range on negative or
>> zero year.  This also guarantees that calendar module's notion of leap year
>> is the same as that of datetime module.
>>
>> If this is found to be a worthwhile change, I would also rewrite
>> monthrange as
>>
>> def monthrange(year, month):
>>    first = date(year, month, 1)
>>    if month == 12:
>>        ndays = 31
>>    else:
>>        ndays = (date(year, month + 1, 1) - first).days
>>    return first.weekday(), ndays
>>
>> ----------
>>
>> _______________________________________
>> Python tracker <report at bugs.python.org>
>> <http://bugs.python.org/issue10073>
>> _______________________________________
>>
>
>

----------
Added file: http://bugs.python.org/file19267/unnamed

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue10073>
_______________________________________
-------------- next part --------------
Also, your &quot;pedantic&quot; version of isleap() is not pedantic at all.<div><br></div><div><span class="Apple-style-span" style="font-family: arial, sans-serif; font-size: 13px; border-collapse: collapse; ">return date(year, 3, 1) - date(year, 2, 1) == timedelta(29)</span></div>
<div><font class="Apple-style-span" face="arial, sans-serif"><span class="Apple-style-span" style="border-collapse: collapse;">does not seem readable at all. Readability counts!</span></font></div><div><font class="Apple-style-span" face="arial, sans-serif"><span class="Apple-style-span" style="border-collapse: collapse;"><br>
</span></font></div><div><font class="Apple-style-span" face="arial, sans-serif"><span class="Apple-style-span" style="border-collapse: collapse;"><span class="Apple-style-span" style="font-size: 13px; ">return date(year, 3, 1) is not understandable. What are the arguments 3 and 1 in the date() function for?</span><br>
</span></font><br></div><div><br><div class="gmail_quote">On Mon, Oct 18, 2010 at 8:06 PM, Boštjan Mejak <span dir="ltr">&lt;<a href="mailto:bostjan.mejak at gmail.com">bostjan.mejak at gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="im"><span style="font-family:arial, sans-serif;font-size:13px;border-collapse:collapse">    else:<br>--&gt;    ndays = (date(year, month + 1, 1) - first).days<br>    return first.weekday(), ndays</span><div>
<font face="arial, sans-serif"><span style="border-collapse:collapse"><br></span></font></div></div><div><font face="arial, sans-serif"><span style="border-collapse:collapse">Oh my God! The line with a pointer is so ugly!<br>

</span></font><div><div></div><div class="h5"><br><div class="gmail_quote">On Mon, Oct 18, 2010 at 7:59 PM, Alexander Belopolsky <span dir="ltr">&lt;<a href="mailto:report at bugs.python.org" target="_blank">report at bugs.python.org</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div><br>
Alexander Belopolsky &lt;<a href="mailto:belopolsky at users.sourceforge.net" target="_blank">belopolsky at users.sourceforge.net</a>&gt; added the comment:<br>
<br>
</div>The most pedantic implementation of calendar.isleap() would be<br>
<br>
from datetime import date, timedelta<br>
def isleap(year):<br>
    return date(year, 3, 1) - date(year, 2, 1) == timedelta(29)<br>
<br>
Since python calendar only supports years in the range [1, 9999], the above will properly raise ValueError: year is out of range on negative or zero year.  This also guarantees that calendar module&#39;s notion of leap year is the same as that of datetime module.<br>


<br>
If this is found to be a worthwhile change, I would also rewrite monthrange as<br>
<br>
def monthrange(year, month):<br>
    first = date(year, month, 1)<br>
    if month == 12:<br>
        ndays = 31<br>
    else:<br>
        ndays = (date(year, month + 1, 1) - first).days<br>
    return first.weekday(), ndays<br>
<div><div></div><div><br>
----------<br>
<br>
_______________________________________<br>
Python tracker &lt;<a href="mailto:report at bugs.python.org" target="_blank">report at bugs.python.org</a>&gt;<br>
&lt;<a href="http://bugs.python.org/issue10073" target="_blank">http://bugs.python.org/issue10073</a>&gt;<br>
_______________________________________<br>
</div></div></blockquote></div><br></div></div></div>
</blockquote></div><br></div>


More information about the Python-bugs-list mailing list