I mean, Python is awesome in every1 single aspect (I'm a huge fan). For instance, have you ever tried to override the "import" instruction in Java with your own implementation? Although maybe not always recommended, it's a piece of cake in Python. Delegates... piece of cake in Python (callables) because literally everything is kind of an object. Damn, even types are. How awesome's that?
We have a configuration in which it is desired to have some module dependencies 'optional'. That is, it is not considered necessary to have them installed and the program would also work as intended without them. Example is matplotlib. If you don't need visualization you can use the program without matplotlib and it would work in pretty much exactly the same way (aside from visualisation).
So for this purpose I overrode import with an implementation that checked whether some modules were marked as optional (from a configuration file) and in case that they were not installed (usually import would fail with an ImportError) would return a MagicMock object instead of the actual module. That way any use of the module would just kind of "work" (while not doing anything at all) and leave the rest of the program's functionality unimpeded. For modules not marked as optional or optional modules that are installed the overridden import works as the original import.
It's a pretty unique scenario and maybe a pretty nasty solution, but I'm still happy that it's been possible at all. I don't know of any other language that is capable of doing something like this.
Would have been an option in the beginning of the project, maybe.
In the current state of the project though it would require some major refactoring for each individual optional dependency. Overriding the import has turned out to be the better solution.
Well, maybe one missing detail is that it doesn't really override the original import but just wraps it. The original import instruction is used internally to import all other non-optional modules and optional modules that are installed.
What about the places where the modules are actually used? We would have had to catch those calls as well in the case that a module is not available. That were a huge amount of additional try-except blocks and another huge amount of actual error handling (avoiding all calls to that module). We tried that. It's ugly.
Specifically for matplotlib that hasn't made much sense to us. It's totally okay that the program runs with or without matplotlib since matplotlib does not alter anything. The MagicMock objects returned by the wrapped import do no harm. They just pretend to do what matplotlib was supposed to do - without actually doing it. A dry run, if you will. The user will be informed that matplotlib is optional and not installed so that they don't wonder why visualization does not work.
To help you understand: we were after a solution where most of the already existing code could remain untouched. Only specific modules are marked optional. That is, if a module is missing that is not explicitly marked optional, Python will still raise an ImportError. Otherwise it will only show a warning, but the program will continue to run. The thing is, the existing code base is quite large and quite old. We still have instructions/syntax in there that date back to Python 2.5. Messing with that code aside from upgrading it to at least Python 2.7 is a risky challenge.
Furthermore, it's political. There's currently no official project that involves working on the code. So everything that has to be done, must take as little time as possible.
Wrappingimport was the quickest and cleanest solution we could come up with within the available time. It may not be beautiful but it works and we're okay with it. It's also documented so that future developers will understand what's happening.
I mean I do exactly that. Here's an excerpt from some code of mine.
try:
import matplotlib.pyplot as plt
except ImportError:
plt = None
print( "Warning: could not import from matplotlib. Echogram.show() will not work.")
Now later on in the class that would call imshow ...
def show(self):
'''
Plot an image of the echogram.
'''
if plt is None:
logging.warning("matplotlib was not imported, cannot plot")
return
plt.imshow(self.data, aspect='auto')
So your code (apparently) ...
literally changes the semantic meaning of a python builtin
alters behavior the user may not be expecting
probably makes the code seem odd to a new comer
I would prefer my solution - which isn't full of thousands of try / excepts - any day.
-2
u/[deleted] Jan 05 '18 edited Jan 05 '18
... but why?
I mean, Python is awesome in every1 single aspect (I'm a huge fan). For instance, have you ever tried to override the "import" instruction in Java with your own implementation? Although maybe not always recommended, it's a piece of cake in Python. Delegates... piece of cake in Python (
callables) because literally everything is kind of an object. Damn, even types are. How awesome's that?/e: (1 - relevant for my requirements)