Rewrite to_java and to_python as a set of converter objects#33
Rewrite to_java and to_python as a set of converter objects#33
Conversation
68d574a to
195d381
Compare
It'd be cool if we could just use org.scijava.priority.Priority, but we can't do that without pulling in all of SJC. We could get some good use out of SciJava3 having a SciJava Priority module :)
195d381 to
107140a
Compare
If you are going to add a significant number of these Converters, you are going to want to use the Converter class. For that reason, we get rid of the API that takes all of the Converter components...
Note that we do not go in the other direction. This is because Python does not have an array type, preferring Lists (and we have a List -> List converter already)
hinerm
left a comment
There was a problem hiding this comment.
This looks well-designed and easy to use.
I still think it would be nice to explore using something like SortedList for the converter data structures, to allow short-circuiting the search but it's not essential and wouldn't affect the API if done later.
I also have two thoughts related to typing...
Convertercould be rewritten to useTypeVar+Genericso thatmypywill catch the creation ofConverters with incompatiblepredicate+callablepairs- I can envision frustration when a user adds a
Converterthat doesn't take effect e.g. because there is a higher-priority converter that operates on the same type or a supertype. It might be nice, when adding aConverter, to see if there are other converters that operate on the same (super)type. I looked at inspect a bit but think this would only be reliable if there was an instance of the generic type was taken as a param. Anyway this is solving a problem that hasn't been raised and probably never will be, but wanted to think about it in case there's a simpler solution.
|
Thanks for looking it over @hinerm!
I can't say I'm against this, but we would be adding another dependency. If that is of no consequence, then I can do it.
I don't think that is allowed. See this SO post. We could make
We could add a function |
OK this is my python lack-of-knowledge problem but what's the difference between the from typing import TypeVar, Generic, Any
T = TypeVar('T')
class Converter(Generic[T]):
def __init__(self, predicate: Callable[[T], bool], converter: Callable[[T], Any], priority: float = Priority.NORMAL) -> None:
self.predicate = predicate
self.converter = converter
self.priority: = priority |
|
@hinerm at this point, nothing 😆 In earlier stages of my design I was not planning to promote the |
|
Edit: N/m, I see that in fact, there is a Python-side priority class. So, good! 👍 |
Okay, @hinerm, now that I tried this, I am less in favor of it 😅 To start, we actually don't want With this in mind, we'd have: from typing import TypeVar, Generic, Any
T = TypeVar('T')
class Converter(Generic[T]):
def __init__(self, predicate: Callable[[Any], bool], converter: Callable[[T], Any], priority: float = Priority.NORMAL) -> None:
self.predicate = predicate
self.converter = converter
self.priority: = priorityIs this still worth the generic? Maybe, I think it ties a This might be an argument to extract the |
Certainly, Python does not frown on declaring global variables outside of functions. Of course, you don't need the But are you suggesting you would do some |
No, I was just suggesting we could move the variable declaration outside of the function; of course the Although, honestly, I'm not sure that would fix the mypy errors anyways... |
oh of course.. that makes total sense. It's not worth generifying if there aren't actually params to tie together! |
This PR seeks to do away with the confusing case logic of the
to_javaandto_pythonfunctions. Towards this effort, we introduceConverter:We then consolidate a number of these
Converters into two maps: one holds allConverters going from Java to Python, and the other holds allConverters going from Python to Java.This reduces
to_javato:where
to_pythonis similarly reduced to a call to_convertwithpy_convertersThe
Converters lists are exposed as global variables; this is intended to allow users of ScyJava (namely PyImageJ, and likely napari-imagej) to addConverters. By using high priority, this should allow those projects to also overwrite theConverters of parent projects.As of this writing, all commits build with passing tests.