Re: [Python-Dev] datetime.timedelta total_microseconds
Looks like timedelta has a microseconds property. Would this work for your needs? In [12]: d Out[12]: datetime.timedelta(0, 3, 398407) In [13]: d.microseconds Out[13]: 398407 On Wed, Feb 13, 2019 at 9:08 PM Richard Belleville via Python-Dev < python-dev@python.org> wrote: > In a recent code review, the following snippet was called out as > reinventing the > wheel: > > _MICROSECONDS_PER_SECOND = 100 > > > def _timedelta_to_microseconds(delta): > return int(delta.total_seconds() * _MICROSECONDS_PER_SECOND) > > > The reviewer thought that there must already exist a standard library > function > that fulfills this functionality. After we had both satisfied ourselves > that we > hadn't simply missed something in the documentation, we decided that we had > better raise the issue with a wider audience. > > Does this functionality already exist within the standard library? If not, > would > a datetime.timedelta.total_microseconds function be a reasonable addition? > I > would be happy to submit a patch for such a thing. > > Richard Belleville > ___ > Python-Dev mailing list > Python-Dev@python.org > https://mail.python.org/mailman/listinfo/python-dev > Unsubscribe: > https://mail.python.org/mailman/options/python-dev/tahafut%40gmail.com > ___ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] datetime.timedelta total_microseconds
Oops. That isn't the TOTAL microseconds, but just the microseconds portion. Sorry for the confusion. On Wed, Feb 13, 2019 at 9:23 PM Henry Chen wrote: > Looks like timedelta has a microseconds property. Would this work for your > needs? > > In [12]: d > Out[12]: datetime.timedelta(0, 3, 398407) > > In [13]: d.microseconds > Out[13]: 398407 > > On Wed, Feb 13, 2019 at 9:08 PM Richard Belleville via Python-Dev < > python-dev@python.org> wrote: > >> In a recent code review, the following snippet was called out as >> reinventing the >> wheel: >> >> _MICROSECONDS_PER_SECOND = 100 >> >> >> def _timedelta_to_microseconds(delta): >> return int(delta.total_seconds() * _MICROSECONDS_PER_SECOND) >> >> >> The reviewer thought that there must already exist a standard library >> function >> that fulfills this functionality. After we had both satisfied ourselves >> that we >> hadn't simply missed something in the documentation, we decided that we >> had >> better raise the issue with a wider audience. >> >> Does this functionality already exist within the standard library? If >> not, would >> a datetime.timedelta.total_microseconds function be a reasonable >> addition? I >> would be happy to submit a patch for such a thing. >> >> Richard Belleville >> ___ >> Python-Dev mailing list >> Python-Dev@python.org >> https://mail.python.org/mailman/listinfo/python-dev >> Unsubscribe: >> https://mail.python.org/mailman/options/python-dev/tahafut%40gmail.com >> > ___ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] datetime.timedelta total_microseconds
Indeed there is a potential loss of precision: _timedelta_to_microseconds(timedelta(0, 1, 1)) returns 100 where conversion function is defined according to the initial message in this thread On Fri, Feb 15, 2019 at 2:29 PM Paul Ganssle wrote: > 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. > > But Alexander's approach of "divide it by the base unit" is *even more > general* 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: > > def total_duration(td, *args, **kwargs): > return td / timedelta(*args, **kwargs) > > Then you can spell "x.total_microseconds()" as: > > total_duration(x, microseconds=1) > > Or you can write it like this: > > def total_duration(td, units='seconds'): > return td / timedelta(**{units: 1}) > > In which case it would be spelled: > > total_duration(x, units='microseconds') > > 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. > On 2/15/19 4:48 PM, Chris Barker via Python-Dev wrote: > > On Fri, Feb 15, 2019 at 11:58 AM Rob Cliffe via Python-Dev < > python-dev@python.org> wrote: > >> A function with "microseconds" in the name IMO misleadingly suggests that >> it has something closer to microsecond accuracy than a 1-second granularity. >> > > it sure does, but `delta.total_seconds()` is a float, so ms accuracy is > preserved. > > 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. > > Also: > > _MICROSECONDS_PER_SECOND = 100 > > > 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 > > As Alexander pointed out the canonical way to spell this would be: > > delta / timedelta(microseconds=1) > > but I think that is less than obvious to the usual user, so I think a: > > delta.total_microseconds() > > would be a reasonable addition. > > I know I use .totalseconds() quite a bit, and would not want to have to > spell it: > > delta / timedelta(seconds=1) > > (and can't do that in py2 anyway) > > -CHB > > -- > > Christopher Barker, Ph.D. > Oceanographer > > Emergency Response Division > NOAA/NOS/OR&R(206) 526-6959 voice > 7600 Sand Point Way NE (206) 526-6329 fax > Seattle, WA 98115 (206) 526-6317 main reception > > chris.bar...@noaa.gov > > ___ > Python-Dev mailing > listPython-Dev@python.orghttps://mail.python.org/mailman/listinfo/python-dev > Unsubscribe: > https://mail.python.org/mailman/options/python-dev/paul%40ganssle.io > > ___ > Python-Dev mailing list > Python-Dev@python.org > https://mail.python.org/mailman/listinfo/python-dev > Unsubscribe: > https://mail.python.org/mailman/options/python-dev/tahafut%40gmail.com > ___ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] datetime.timedelta total_microseconds
+1 on the improved docs solution: no new code to maintain and big return on investment in preventing future bugs / confusion :) On Sat, Feb 16, 2019 at 9:40 AM Nick Coghlan wrote: > On Sun, 17 Feb 2019 at 03:20, Paul Ganssle wrote: > > I think if we add such a function, it will essentially be just a slower > version of something that already exists. I suspect the main reason the > "divide the timedelta by the interval" thing isn't a common enough idiom > that people see it all the time is that it's only supported in Python 3. As > more code drops Python 2, I think the "td / interval" idiom will hopefully > become common enough that it will obviate the need for a total_duration > function. > > And personally, the total_seconds() case has always been enough for me. > > > That said, if people feel very strongly that a total_duration function > would be useful, maybe the best thing to do would be for me to add it to > dateutil.utils? In that case it would at least be available in Python 2, so > people who find it more readable and people still writing polyglot code > would be able to use it, without the standard library unnecessarily > providing two ways to do the exact same thing. > > I'm now thinking a slight documentation improvement would have > addressed my own confusion (and I suspect the OPs as well): > > * In the "Supported Operations" section of > https://docs.python.org/3/library/datetime.html#timedelta-objects, > change "Division (3) of t2 by t3." to "Division (3) of overall > duration t2 by interval unit t3." > * In the total_seconds() documentation, add a sentence "For interval > units other than seconds, use the division form directly (e.g. `td / > timedelta(microseconds=1)`)" > > Cheers, > Nick. > > -- > Nick Coghlan | ncogh...@gmail.com | Brisbane, Australia > ___ > Python-Dev mailing list > Python-Dev@python.org > https://mail.python.org/mailman/listinfo/python-dev > Unsubscribe: > https://mail.python.org/mailman/options/python-dev/tahafut%40gmail.com > ___ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Compact ordered set
If sets were ordered, then what ought pop() return - first, last, or nevertheless an arbitrary element? I lean toward arbitrary because in existing code, set.pop often implies that which particular element is immaterial. On Wed, Feb 27, 2019 at 2:18 PM Barry Warsaw wrote: > On Feb 27, 2019, at 13:54, Chris Barker via Python-Dev < > python-dev@python.org> wrote: > > > > A mapping and a set type really don't have much to do with each other > other than implementation -- anyone that isn't familiar with python C code, > or hash tables in general, wouldn't likely have any expectation of them > having anything to do with each other. > > I’m just relaying a data point. Some Python folks I’ve worked with do > make the connection between dicts and sets, and have questions about the > ordering guarantees of then (and how they relate). > > -Barry > > ___ > Python-Dev mailing list > Python-Dev@python.org > https://mail.python.org/mailman/listinfo/python-dev > Unsubscribe: > https://mail.python.org/mailman/options/python-dev/tahafut%40gmail.com > ___ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com