<div dir="ltr"><div dir="ltr"><div dir="ltr">Indeed there is a potential loss of precision:<div><br></div><div>_timedelta_to_microseconds(timedelta(0, 1, 1)) returns 1000000</div><div><br></div><div>where conversion function is defined according to the initial message in this thread</div></div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Feb 15, 2019 at 2:29 PM Paul Ganssle <<a href="mailto:paul@ganssle.io">paul@ganssle.io</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div bgcolor="#FFFFFF">
<p>I'm still with Alexander on this. I see functions like total_X as
basically putting one of the arguments directly in the function
name - it should be `total_duration(units)`, not `total_units()`,
because all of those functions do the same thing and only differ
in the units they use.<br>
<br>
But Alexander's approach of "divide it by the base unit" is <i>even
more general</i> than this, because it allows you to use
non-traditional units like weeks (timedelta(days=7)) or "two-day
periods" or whatever you want. If you use this idiom a lot and
want a simple "calculate the total" function, this should suffice:<br>
<br>
def total_duration(td, *args, **kwargs):<br>
return td / timedelta(*args, **kwargs)<br>
<br>
Then you can spell "x.total_microseconds()" as:<br>
<br>
total_duration(x, microseconds=1)<br>
<br>
Or you can write it like this:<br>
<br>
def total_duration(td, units='seconds'):<br>
return td / timedelta(**{units: 1})<br>
<br>
In which case it would be spelled:<br>
<br>
total_duration(x, units='microseconds')<br>
<br>
I don't see there being any compelling reason to add a bunch of
methods for a marginal (and I'd say arguable) gain in aesthetics.<br>
</p>
<div class="gmail-m_-7461548556104209815moz-cite-prefix">On 2/15/19 4:48 PM, Chris Barker via
Python-Dev wrote:<br>
</div>
<blockquote type="cite">
<div dir="ltr">
<div dir="ltr">On Fri, Feb 15, 2019 at 11:58 AM Rob Cliffe via
Python-Dev <<a href="mailto:python-dev@python.org" target="_blank">python-dev@python.org</a>> wrote:<br>
</div>
<div class="gmail_quote">
<blockquote class="gmail_quote">
<div> A function with "microseconds" in the name IMO
misleadingly suggests that it has something closer to
microsecond accuracy than a 1-second granularity.<br>
</div>
</blockquote>
<div><br>
</div>
<div>it sure does, but `delta.total_seconds()` is a float, so
ms accuracy is preserved.</div>
<div><br>
</div>
<div>However, if you DO want a "timedelta_to_microseconds"
function, it really should use the microseconds field in the
timedelta object. I haven't thought it through, but it makes
me nervous to convert to floating point, and then back again
-- for some large values of timedelta some precision may be
lost.</div>
<div><br>
</div>
<div>Also:</div>
<div><br>
</div>
<div>
<blockquote type="cite">
<div dir="ltr">
<div dir="ltr">
<div>_MICROSECONDS_PER_SECOND = 1000000</div>
</div>
</div>
</blockquote>
</div>
<div><br>
</div>
<div>really? why in the world would you define a constant for
something that simple that can never change? (and probably
isn't used in more than one place anyway</div>
<div> </div>
<div>As Alexander pointed out the canonical way to spell this
would be:</div>
<div><br>
</div>
<div>delta / timedelta(microseconds=1)</div>
<div><br>
</div>
<div>but I think that is less than obvious to the usual user,
so I think a:</div>
<div><br>
</div>
<div>delta.total_microseconds()</div>
<div><br>
</div>
<div>would be a reasonable addition.</div>
<div><br>
</div>
<div>I know I use .totalseconds() quite a bit, and would not
want to have to spell it:</div>
<div><br>
</div>
<div>delta / timedelta(seconds=1)<br>
</div>
<div><br>
</div>
<div>(and can't do that in py2 anyway)</div>
<div><br>
</div>
<div>-CHB</div>
<div><br>
</div>
</div>
-- <br>
<div dir="ltr" class="gmail-m_-7461548556104209815gmail_signature"><br>
Christopher Barker, Ph.D.<br>
Oceanographer<br>
<br>
Emergency Response Division<br>
NOAA/NOS/OR&R (206) 526-6959 voice<br>
7600 Sand Point Way NE (206) 526-6329 fax<br>
Seattle, WA 98115 (206) 526-6317 main reception<br>
<br>
<a href="mailto:Chris.Barker@noaa.gov" target="_blank">Chris.Barker@noaa.gov</a></div>
</div>
<br>
<fieldset class="gmail-m_-7461548556104209815mimeAttachmentHeader"></fieldset>
<pre class="gmail-m_-7461548556104209815moz-quote-pre">_______________________________________________
Python-Dev mailing list
<a class="gmail-m_-7461548556104209815moz-txt-link-abbreviated" href="mailto:Python-Dev@python.org" target="_blank">Python-Dev@python.org</a>
<a class="gmail-m_-7461548556104209815moz-txt-link-freetext" href="https://mail.python.org/mailman/listinfo/python-dev" target="_blank">https://mail.python.org/mailman/listinfo/python-dev</a>
Unsubscribe: <a class="gmail-m_-7461548556104209815moz-txt-link-freetext" href="https://mail.python.org/mailman/options/python-dev/paul%40ganssle.io" target="_blank">https://mail.python.org/mailman/options/python-dev/paul%40ganssle.io</a>
</pre>
</blockquote>
</div>
_______________________________________________<br>
Python-Dev mailing list<br>
<a href="mailto:Python-Dev@python.org" target="_blank">Python-Dev@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-dev" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/python-dev</a><br>
Unsubscribe: <a href="https://mail.python.org/mailman/options/python-dev/tahafut%40gmail.com" rel="noreferrer" target="_blank">https://mail.python.org/mailman/options/python-dev/tahafut%40gmail.com</a><br>
</blockquote></div>