Re: Creating time stamps

2019-07-23 Thread Peter Pearson
On Mon, 22 Jul 2019 16:25:32 -0500, Michael F. Stemper wrote:
> On 22/07/2019 15.58, Chris Angelico wrote:
>> On Tue, Jul 23, 2019 at 6:34 AM Michael F. Stemper
>>  wrote:
>>>
[snip]
>>>   from datetime import datetime
>>>   from time import strftime
>>>   timestamp = datetime.now().strftime( "%Y-%m-%d %H:%M" )
[snip]
>> 
>> What's the second import doing though? You never use strftime.
>
> Cleaned my contacts, cleaned my readers. Still see strftime() in the
> third line. Tried to run the code without the second line. It ran
> without complaint.
>
> Apparently, the strftime() in that last line is not the one that I
> explicitly imported, but a method of datetime.now(). Did I get that
> right?

Yes, you got that right.  The statement datetime.now().strftime("...")
says that datetime.now will return an object that has a method
named strftime, and calling that method is the next thing to be done.
Other things that happen to be lying around named strftime just have
no part in the conversation.

-- 
To email me, substitute nowhere->runbox, invalid->com.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Creating time stamps

2019-07-22 Thread Michael F. Stemper
On 22/07/2019 16.00, Stefan Ram wrote:
> "Michael F. Stemper"  writes:
>> The first seems a little clunky with its accessing of multiple
>> attributes, but the second has an additional import. Is there
>> any reason to prefer one over the other?
> 
> |>>> import datetime
> |>>> datetime.datetime.now().replace(microsecond=0).isoformat()
> |'2019-07-22T21:59:58'

Yeah, if I'd wanted that format, isoformat() would have been simpler.
Since I did not want seconds, or the letter "T", I had to go for a
home-brewed solution.

-- 
Michael F. Stemper
No animals were harmed in the composition of this message.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Creating time stamps

2019-07-22 Thread MRAB

On 2019-07-22 22:41, Grant Edwards wrote:

On 2019-07-22, Michael F. Stemper  wrote:


  from datetime import datetime
  from time import strftime
  timestamp = datetime.now().strftime( "%Y-%m-%d %H:%M" )

[...]

Apparently, the strftime() in that last line is not the one that I
explicitly imported, but a method of datetime.now(). Did I get that
right?


Exactly.


You can tell that it's a method because it's:

something.strftime(...)

If it was using the imported 'strftime' then it would be:

timestamp = strftime(...)
--
https://mail.python.org/mailman/listinfo/python-list


Re: Creating time stamps

2019-07-22 Thread Grant Edwards
On 2019-07-22, Michael F. Stemper  wrote:

>>>   from datetime import datetime
>>>   from time import strftime
>>>   timestamp = datetime.now().strftime( "%Y-%m-%d %H:%M" )
[...]
> Apparently, the strftime() in that last line is not the one that I
> explicitly imported, but a method of datetime.now(). Did I get that
> right?

Exactly.

-- 
Grant Edwards   grant.b.edwardsYow! TONY RANDALL!  Is YOUR
  at   life a PATIO of FUN??
  gmail.com

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Creating time stamps

2019-07-22 Thread Michael F. Stemper
On 22/07/2019 15.58, Chris Angelico wrote:
> On Tue, Jul 23, 2019 at 6:34 AM Michael F. Stemper
>  wrote:
>>
>> I have some code that generates a time-stamp as follows:
>>
>>   from datetime import datetime
>>   tt = datetime.now()
>>   timestamp = "%4d-%02d-%02d %02d:%02d" % \
>> (tt.year, tt.month, tt.day, tt.hour, tt.minute)
>>
>> I later realized that I could have written it as:
>>
>>   from datetime import datetime
>>   from time import strftime
>>   timestamp = datetime.now().strftime( "%Y-%m-%d %H:%M" )
>>
>> The first seems a little clunky with its accessing of multiple
>> attributes, but the second has an additional import. Is there
>> any reason to prefer one over the other?
> 
> What's the second import doing though? You never use strftime.

Cleaned my contacts, cleaned my readers. Still see strftime() in the
third line. Tried to run the code without the second line. It ran
without complaint.

Apparently, the strftime() in that last line is not the one that I
explicitly imported, but a method of datetime.now(). Did I get that
right?


>   I'd go
> with the second one, but with just a single import.

Sounds like a winner to me. Thanks.


-- 
Michael F. Stemper
87.3% of all statistics are made up by the person giving them.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Creating time stamps

2019-07-22 Thread Albert-Jan Roskam


On 22 Jul 2019 23:12, Skip Montanaro  wrote:

Assuming you're using Python 3, why not use an f-string?

>>> dt = datetime.datetime.now()
>>> dt.strftime("%Y-%m-%d %H:%M")
'2019-07-22 16:10'
>>> f"{dt:%Y-%m-%d %H:%M}"
'2019-07-22 16:10'

===》》 Or if you're running < Python 3.6 (no f strings): format(datetime.now(), 
"%Y-%m-%d %H:%M")
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Creating time stamps

2019-07-22 Thread Skip Montanaro
Assuming you're using Python 3, why not use an f-string?

>>> dt = datetime.datetime.now()
>>> dt.strftime("%Y-%m-%d %H:%M")
'2019-07-22 16:10'
>>> f"{dt:%Y-%m-%d %H:%M}"
'2019-07-22 16:10'

Skip
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Creating time stamps

2019-07-22 Thread Chris Angelico
On Tue, Jul 23, 2019 at 6:34 AM Michael F. Stemper
 wrote:
>
> I have some code that generates a time-stamp as follows:
>
>   from datetime import datetime
>   tt = datetime.now()
>   timestamp = "%4d-%02d-%02d %02d:%02d" % \
> (tt.year, tt.month, tt.day, tt.hour, tt.minute)
>
> I later realized that I could have written it as:
>
>   from datetime import datetime
>   from time import strftime
>   timestamp = datetime.now().strftime( "%Y-%m-%d %H:%M" )
>
> The first seems a little clunky with its accessing of multiple
> attributes, but the second has an additional import. Is there
> any reason to prefer one over the other?
>

What's the second import doing though? You never use strftime. I'd go
with the second one, but with just a single import.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Creating time stamps

2019-07-22 Thread Michael F. Stemper
I have some code that generates a time-stamp as follows:

  from datetime import datetime
  tt = datetime.now()
  timestamp = "%4d-%02d-%02d %02d:%02d" % \
(tt.year, tt.month, tt.day, tt.hour, tt.minute)

I later realized that I could have written it as:

  from datetime import datetime
  from time import strftime
  timestamp = datetime.now().strftime( "%Y-%m-%d %H:%M" )

The first seems a little clunky with its accessing of multiple
attributes, but the second has an additional import. Is there
any reason to prefer one over the other?

-- 
Michael F. Stemper
There's no "me" in "team". There's no "us" in "team", either.
-- 
https://mail.python.org/mailman/listinfo/python-list