Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #65072

Re: Another surprise from the datetime module

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <cameron@cskk.homeip.net>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; '(at': 0.04; 'parameters': 0.04; 'modified': 0.07; 'modify': 0.07; 'modifying': 0.07; 'meantime,': 0.09; 'objects,': 0.09; 'subject:module': 0.09; 'python': 0.11; 'factory': 0.16; 'from:addr:cs': 0.16; 'from:addr:zip.com.au': 0.16; 'from:name:cameron simpson': 0.16; 'hmm.': 0.16; 'message-id:@cskk.homeip.net': 0.16; 'need:': 0.16; 'none.': 0.16; 'optional': 0.16; 'properties,': 0.16; 'received:211.29': 0.16; 'received:211.29.132': 0.16; 'received:cskk.homeip.net': 0.16; 'received:homeip.net': 0.16; 'received:optusnet.com.au': 0.16; 'received:syd.optusnet.com.au': 0.16; 'roy': 0.16; 'seconds.': 0.16; 'simpson': 0.16; 'wrote:': 0.18; 'normally': 0.19; 'print': 0.22; 'header:User-Agent:1': 0.23; 'finally,': 0.24; 'fraction': 0.24; 'replace': 0.24; 'initial': 0.24; 'cheers,': 0.24; 'least': 0.26; 'header:In-Reply- To:1': 0.27; 'instruction': 0.29; 'properties': 0.29; "doesn't": 0.30; 'specified': 0.30; '(used': 0.31; 'bunch': 0.31; 'another': 0.32; 'skip:d 20': 0.34; 'subject:the': 0.34; 'subject:from': 0.34; 'but': 0.35; 'there': 0.35; 'received:com.au': 0.36; 'method': 0.36; 'charset:us-ascii': 0.36; 'expressed': 0.37; 'received:211': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'how': 0.40; 'read': 0.60; 'then,': 0.60; 'most': 0.60; 'new': 0.61; 'content-disposition:inline': 0.62; 'real': 0.63; 'such': 0.63; 'smith': 0.68; 'wish': 0.70; 'car': 0.72; 'discover': 0.82; 'beside': 0.84; 'locked': 0.84; "shouldn't,": 0.84; 'verb,': 0.84; 'wanting': 0.93
Date Fri, 31 Jan 2014 11:06:31 +1100
From Cameron Simpson <cs@zip.com.au>
To python-list@python.org
Subject Re: Another surprise from the datetime module
MIME-Version 1.0
Content-Type text/plain; charset=us-ascii
Content-Disposition inline
In-Reply-To <lce63h$c88$1@ger.gmane.org>
User-Agent Mutt/1.5.21 (2010-09-15)
References <lce63h$c88$1@ger.gmane.org>
X-Optus-CM-Score 0
X-Optus-CM-Analysis v=2.1 cv=HZAtEE08 c=1 sm=1 tr=0 a=YuQlxtEQCowy2cfE5kc7TA==:117 a=YuQlxtEQCowy2cfE5kc7TA==:17 a=ZtCCktOnAAAA:8 a=PO7r1zJSAAAA:8 a=LcaDllckn3IA:10 a=fP9v0Dwv92kA:10 a=kj9zAlcOel0A:10 a=vrnE16BAAAAA:8 a=8AHkEIZyAAAA:8 a=3iwo6vZIqkoA:10 a=VUfPOBp7AAAA:8 a=lEvSqVbsq0iV60O1iZcA:9 a=CjuIK1q_8ugA:10 a=5hK03km2n30A:10
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.6182.1391127922.18130.python-list@python.org> (permalink)
Lines 63
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1391127922 news.xs4all.nl 2942 [2001:888:2000:d::a6]:60698
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:65072

Show key headers only | View raw


On 30Jan2014 18:36, Neil Cerutti <neilc@norwich.edu> wrote:
> On 2014-01-30, Roy Smith <roy@panix.com> wrote:
> > I was astounded just now to discover that datetime.timedelta
> > doesn't have a replace() method (at least not in Python 2.7).
> > Is there some fundamental reason why it shouldn't, or is this
> > just an oversight?
> >
> > My immediate use case was wanting to print a timedelta without
> > the fractions of seconds.  The most straight-forward is:
> >
> > print td.replace(microseconds=0)
> 
> That would be nice.
> 
> In the meantime, this works for your use case:
> 
> td -= td % timedelta(seconds=1)

Hmm. I do not like the replace() as suggested.

Firstly, replace is a verb, and I would normally read
td.replace(microseconds=0) as an instruction to modify td in place.
Traditionally, such methods in python return None.
So you would need:

  td.replace(microseconds=0)
  print td

Then, if the intent is to modify td in place, I would far prefer a system of
properties on timedelta objects, eg:

  # print the microseconds part
  print td.microseconds

  # set the microseconds part to zero
  td.microseconds = 0

  # print the modified timedelta
  print td

Also, clearly, such a system needs definition: is "microseconds"
the sub-millisecond fraction or the sub-second fraction, expressed
in microsecond units?

Alternatively, if td.replace() is intened to return a new timedelta
with the specified properties, perhaps another factory would be
cleaner:

  td2 = datetime.timedelta(td, microseconds=0)

with a bunch of optional parameters like microseconds for modifying
the initial value (used at call, as above).

Finally, how much of Roy's original wish is addressable by format
strings to print with a specified precision? No good for arithmetic,
but perhaps ok for presentation.

Cheers,
-- 
Cameron Simpson <cs@zip.com.au>

I knew I was a real biker when I pulled up beside a car at a stoplight and
the people inside locked all the doors.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Another surprise from the datetime module Cameron Simpson <cs@zip.com.au> - 2014-01-31 11:06 +1100

csiph-web