Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add offset or translation function to Interval #52

Closed
exoriente opened this issue Jan 16, 2021 · 4 comments
Closed

Add offset or translation function to Interval #52

exoriente opened this issue Jan 16, 2021 · 4 comments
Labels
enhancement New feature or request

Comments

@exoriente
Copy link

Hi Alexandre,

I love this library. It comes close to almost everything I would expect from a standard library for handling intervals. There's one feature I'm missing that I would love to see added or add myself, and that's a built-in way to add a value to all bounds of an interval (whether atomic or union).

I realize this can be done with .apply() (in fact, it's even one of the examples in the readme), but in the project where I would love to use portion it would be such a common occurrence it becomes really tempting to build in support for it, instead of handling it with lambda functions or an external wrapper for .apply.

In the planning software I'm working on we use timedelta intervals, tasks to be planned relative to some event time. Adding the event time to the timedelta interval would yield an interval of datetimes that represents the planned task. It seems to me that these and other use cases could be common enough to warrant a convenient function, like.offset(value) or .translate(value), that would be a shortcut for .apply(lambda x: (x.left, x.lower + value, x.upper + value, x.right)).

What do you think? Would you be open for a pull-request for it?

Best regards, Maarten

@AlexandreDecan
Copy link
Owner

AlexandreDecan commented Jan 17, 2021

Hello Marteen,

First of all, thank you for those kind words about 'portion'. It's always nice to see this kind of feedback and to see that the library is useful to others ;-)

Concerning the .offset method, I understand its interest and the proposed use-case. However, portion aims to remain as generic as possible, and proposing such a method finally applies only to objects supporting the + operator, which is not necessarily the case for all objects. Even if I realize that the main use-cases of portion are typically for objects implementing + (e.g., integers, floats, dates, etc.), I'm reluctant to implement .offset directly in Interval because of this "not-that-generic" thing, and also because it is finally easy enough, as you said, to implement a wrapper over .apply to offer the same service.

Ideally, what would be really practical and useful would be to be able to "add" methods on the fly on portion objects, a bit like the "extension class" proposed in Kotlin. This would allow users of the library to easily add new "services" in the form of methods (as opposed to what can already be done via functions). However, binding new methods on a class is not very "pythonic" and there is no specific reason to support this "out-of-the-box" in portion (since that's something a user can easily do in his own code, by e.g., doing Interval.func = custom_func if he doesn't care about bound methods, or about the scope of this ;-))

@AlexandreDecan AlexandreDecan added the enhancement New feature or request label Jan 17, 2021
@exoriente
Copy link
Author

Clear. I completely understand your preference to keep the library as generic as possible. Thanks for taking the time to look at the suggestion. I'll probably will be using portion either way.

(In the mean time I already wrote a PR for .offset. But that was fun to do even if it won't be used!)

@AlexandreDecan
Copy link
Owner

I'm glad you won't give up with portion because of this :-D

On a more serious note, I am sorry I had to answer in the negative. It is always difficult to refuse a good contribution for "philosophical" reasons.

@exoriente
Copy link
Author

exoriente commented Jan 19, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants