Re: [Python-ideas] datetime.timedelta literals

2018-06-26 Thread Robert Vanden Eynde
I found it fun to be able to write minutes(50) alongside with 50 * minutes so I did that : from datetime import date, time, datetime, timedelta class CallableTimedelta(timedelta): def __call__(self, x): return self * x seconds, milliseconds, microseconds, days, hours, minutes,

Re: [Python-ideas] datetime.timedelta literals

2018-06-07 Thread Pål Grønås Drange
For closure, I've added a package, timeliterals (env) [pgdr@hostname ~]$ pip install timeliterals (env) [pgdr@hostname ~]$ python >>> from timeliterals import * >>> 3*hours datetime.timedelta(0, 10800) >>> 3*minutes datetime.timedelta(0, 180) >>> 3*seconds datetime.timedelta(0, 3) The source

Re: [Python-ideas] datetime.timedelta literals

2018-06-05 Thread Jacco van Dorp
i'd also be pretty simple to implement Just list: minute = timedelta(minutes=1) hour = timedelta(hours=1) etc... and you could import and use them like that. Or if you really want to write 5*m, the just from datetime import minute as m ___

Re: [Python-ideas] datetime.timedelta literals

2018-06-05 Thread Robert Vanden Eynde
second, minute, hour (singular) timedelta objects in the module are a good idea, one could do 5 * minute to get a timedelta or one could do value / minute to get a float. a = datetime.now() b = datetime(2018, 2, 3) + 5 * minute print((a - b).total_seconds()) print((a - b) / minute) Le mar. 5

Re: [Python-ideas] datetime.timedelta literals

2018-06-05 Thread Jacco van Dorp
2018-06-05 10:08 GMT+02:00 Pål Grønås Drange : >> You can't import literals. They're syntax, not just bound names. > > I'm way out of my comfort zone now, but the parser could for > `123.45_f` > give > `__literal_f__(123.45)` > and then that function should be imported. > > I'm sure this idea has

Re: [Python-ideas] datetime.timedelta literals

2018-06-05 Thread Pål Grønås Drange
> You can't import literals. They're syntax, not just bound names. I'm way out of my comfort zone now, but the parser could for `123.45_f` give `__literal_f__(123.45)` and then that function should be imported. I'm sure this idea has many shortcomings that I don't see, but that was the reason

Re: [Python-ideas] datetime.timedelta literals

2018-06-04 Thread Chris Barker via Python-ideas
On Mon, Jun 4, 2018 at 3:21 PM, Paul Moore wrote: > > So maybe you could propose adding: > > > > seconds > > minutes > > hours > > days > > > > to the datetime module, and then we could write: > > > > 60*seconds == 1*minutes > > > > Without any changes to the language at all. > > This strikes me

Re: [Python-ideas] datetime.timedelta literals

2018-06-04 Thread Rob Cliffe via Python-ideas
On 04/06/2018 19:50, Kyle Lahnakoski wrote: Maybe the Python parser can be made to add an implied multiplication between a-number-followed-directly-by-a-variable-name. If so, then I could write: (2.5HOUR - 14MINUTE + 9300MILLISECOND).total_seconds() This strikes me as quite a nifty

Re: [Python-ideas] datetime.timedelta literals

2018-06-04 Thread Paul Moore
On 4 June 2018 at 22:50, Chris Barker via Python-ideas wrote: > So maybe you could propose adding: > > seconds > minutes > hours > days > > to the datetime module, and then we could write: > > 60*seconds == 1*minutes > > Without any changes to the language at all. This strikes me as more of a

Re: [Python-ideas] datetime.timedelta literals

2018-06-04 Thread Chris Barker via Python-ideas
On Mon, Jun 4, 2018 at 1:59 PM, Pål Grønås Drange wrote: > For the general user-defined literals, here are some example use cases: > I kind of like the idea of user-defined literals, but: > Yes, we could write all these as easily as function calls, > > deg(90) > celsius(20) > center('my

Re: [Python-ideas] datetime.timedelta literals

2018-06-04 Thread Pål Grønås Drange
For the general user-defined literals, here are some example use cases: Here is how to expose literals: __literals__ = ['__literal_h__', '__literal_min__', '__literal_s__'] And here is how to import them: from mymodule import _* # imports all literals from mymodule # could also be *_ or ** or

Re: [Python-ideas] datetime.timedelta literals

2018-06-04 Thread Chris Barker via Python-ideas
On Mon, Jun 4, 2018 at 12:58 AM, Paul Moore wrote: > > That's good to hear, but if you don't mind asking, is your lack of > > support because you use timedelta "programatically" instead of > > hard-coded time units, or is there a different (or more) reason(s)? > > > > (I'm ready to yield, now

Re: [Python-ideas] datetime.timedelta literals

2018-06-04 Thread Kyle Lahnakoski
Pål Grønås Drange, I do like the idea of literals typed with scientific units, but I often get short variable names mixed up, so I am not sure if I could use them without a cheat sheet. Formatting datetime is a good example of how confusing a collection of short names can get: Is month %m or %M? 

Re: [Python-ideas] datetime.timedelta literals

2018-06-04 Thread Clint Hepner
> On 2018 Jun 4 , at 9:08 a, Giampaolo Rodola' wrote: > > > > > > IMO datetimes are not common enough to deserve their own literals. It would > make the language more complex and harder to learn for a relatively little > benefit. This would probably make more sense as a third party lib:

Re: [Python-ideas] datetime.timedelta literals

2018-06-04 Thread Giampaolo Rodola'
On Sat, Jun 2, 2018 at 2:21 PM, Pål Grønås Drange wrote: > Elevator pitch: > > (2.5h - 14min + 9300ms).total_seconds() > # 8169.3 > > from datetime import datetime as dt > start = dt.now() > end = dt.now() > (end-start) < 5s > # True > > > chrono::duration: > > In C++ 14 the

Re: [Python-ideas] datetime.timedelta literals

2018-06-04 Thread Paul Moore
On 4 June 2018 at 11:59, Pål Grønås Drange wrote: > One thing that could solve both this proposal and the aforementioned > SI-proposition by Ken Kundert, could be supporting user-defined > literals. Suppose that __litXXX___ would make XXX a literal one could > use as a suffix for numbers and

Re: [Python-ideas] datetime.timedelta literals

2018-06-04 Thread Pål Grønås Drange
One thing that could solve both this proposal and the aforementioned SI-proposition by Ken Kundert, could be supporting user-defined literals. Suppose that __litXXX___ would make XXX a literal one could use as a suffix for numbers and strings (and lists, dicts, sets?). A user-defined literal

Re: [Python-ideas] datetime.timedelta literals

2018-06-04 Thread Paul Moore
On 4 June 2018 at 09:44, Pål Grønås Drange wrote: >> Speaking for myself, not for Paul, I guess my big objection would be >> that there would be too many collisions with other interpretations. >> Eg, does "5m" mean "5 minutes", "5 meters", or "the number 0.005"? Or >> perhaps "5 minutes of arc"?

Re: [Python-ideas] datetime.timedelta literals

2018-06-04 Thread Pål Grønås Drange
> Speaking for myself, not for Paul, I guess my big objection would be > that there would be too many collisions with other interpretations. > Eg, does "5m" mean "5 minutes", "5 meters", or "the number 0.005"? Or > perhaps "5 minutes of arc"? Just to clarify, the proposition was actually `5min`,

Re: [Python-ideas] datetime.timedelta literals

2018-06-04 Thread Stephen J. Turnbull
Pål Grønås Drange writes: > That's good to hear, but if you don't mind asking, is your lack of > support because you use timedelta "programatically" instead of > hard-coded time units, or is there a different (or more) reason(s)? Speaking for myself, not for Paul, I guess my big objection

Re: [Python-ideas] datetime.timedelta literals

2018-06-04 Thread Paul Moore
On 4 June 2018 at 07:44, Pål Grønås Drange wrote: >> I'm not entirely sure what point you're trying to make here, but you >> quoted that section somewhat out of context. >> >> [...] >> >> That seems directly relevant here. I'm not aware of a "timedelta >> users" community, nor is there a

Re: [Python-ideas] datetime.timedelta literals

2018-06-04 Thread Pål Grønås Drange
> I'm not entirely sure what point you're trying to make here, but you > quoted that section somewhat out of context. > > [...] > > That seems directly relevant here. I'm not aware of a "timedelta > users" community, nor is there a particular package (or set of > packages) other than the stdlib

Re: [Python-ideas] datetime.timedelta literals

2018-06-03 Thread Paul Moore
On 3 June 2018 at 22:03, Pål Grønås Drange wrote: > I want to highlight one comment I found enlightening, and that is one from > Paul Moore: > > [Python-ideas] SI scale factors in Python > Paul Moore p.f.moore at gmail.com > Thu Aug 25 16:03:32 EDT 2016 > >> Python has a track record of being

Re: [Python-ideas] datetime.timedelta literals

2018-06-03 Thread Pål Grønås Drange
> They are more general than your proposal but they still cover pitfalls that > may affect yours. It would be better if you could expand your proposal to the > concerns raised on those threads. After reading that thread (this summary should be taken with a grain of salt), the first half is

Re: [Python-ideas] datetime.timedelta literals

2018-06-03 Thread Sebastian Kreft
Please read the discussions on *https://mail.python.org/pipermail//python-ideas/2016-August/041890.html *, https://mail.python.org/pipermail//python-ideas/2016-August/041939.html,

Re: [Python-ideas] datetime.timedelta literals

2018-06-03 Thread Pål Grønås Drange
> What about > > 2.5*h - 14*min + 9300*ms * 2 That doesn't seem feasible to implement, however, that is essentially how the Pint [1] module works: import pint u = pint.UnitRegistry() (2.5*u.hour - 14*u.min + 9300*u.ms) * 2 # ((2.5*u.hour - 14*u.min + 9300*u.ms) * 2).to('sec') # > However

Re: [Python-ideas] datetime.timedelta literals

2018-06-02 Thread Robert Vanden Eynde
What about 2.5*h - 14*min + 9300*ms * 2 where: h = timedelta(hours=1) min = timedelta (minutes=1) ms = timedelta (milliseconds=1) By the way "min" isn't a keyword, it's a standard function so it can be used as a variable name. However why be limited to time units ? One would want in certain

[Python-ideas] datetime.timedelta literals

2018-06-02 Thread Pål Grønås Drange
Elevator pitch: (2.5h - 14min + 9300ms).total_seconds() # 8169.3 from datetime import datetime as dt start = dt.now() end = dt.now() (end-start) < 5s # True chrono::duration: In C++ 14 the std::chrono::duration was introduced which corresponds somewhat to datetime.timedelta. C++ 14