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

Using an external comparator? #74

Closed
egemenimre opened this issue Jan 21, 2023 · 3 comments
Closed

Using an external comparator? #74

egemenimre opened this issue Jan 21, 2023 · 3 comments
Labels
question Issue is a question

Comments

@egemenimre
Copy link
Contributor

Hallo Alexandre,

I am currently working with an external library that provides an AbsoluteDate class. To compare these objects, it provides a comparator, but not the python native _gt_, _lt_ etc. Now I don't want to subclass it as it is rather core class and actually it is written in java - I am using it with a python wrapper. The java native project is Orekit and the Python wrapper is here. And with only a comparator I can't seem to use portion

I can do sorting etc. using key=functools.cmp_to_key(AbsoluteDate.compareTo), so the question is: Can I define an external key like this to portion, so that it knows how to compare things and works properly.

Otherwise I will be forced to subclass AbsoluteDate, add the usual comparison operators, and hope nothing breaks while jumping between java and python.

Thanks!

@AlexandreDecan AlexandreDecan added the question Issue is a question label Jan 21, 2023
@AlexandreDecan
Copy link
Owner

Hi,

Unfortunately, there's no support for a cmp function for objects that do no support usual comparison operators. While the code of portion could possibly be changed to deal with such a function, doing so will make the code much less readable, so that's not an option I envision ;-)

Apart from subclassing AbsoluteDate, I don't see any other possibility :-/ I see there's an absolutedate_to_datetime function. Would it be possible to use this instead of AbsoluteDate instances?

@egemenimre
Copy link
Contributor Author

egemenimre commented Jan 21, 2023 via email

@AlexandreDecan
Copy link
Owner

Unfortunately, that's not the case. portion supports "any object that is comparable" implicitly meaning "any object that supports <, >, etc." not cmp :-/ Sorry :-)

Supporting cmp might be useful in some circumstances, but I would say that it's probably better to open a PR on a project whose objects do not support the usual comparison operators, rather than to open a PR on portion to support these cases... :-/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Issue is a question
Projects
None yet
Development

No branches or pull requests

2 participants