r/Python • • Jan 05 '18

Really??

Post image
2.0k Upvotes

317 comments sorted by

View all comments

Show parent comments

2

u/rhytnen Jan 06 '18

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.

1

u/[deleted] Jan 06 '18

That's pretty much what we had before. But it turned out really hard to maintain because other developers of certain modules (plugins) did not take the fact of optionality of some modules into account. They just imported without checking and since the plugin system that we have just bulk loads everything at once, a broken plugin could bring the whole program to its knees. It's not my job to fix plugin after plugin like this. So instead of trying to teach every developer to obey our optionality rules, this method gives them the freedom to code like all the modules were installed, and the checking takes place somewhere else.

To be honest, the whole project has become awfully hard to maintain because so many developers with different kinds of experience had their fingers in it in the past. Mathematicians with some basic programming skills as well as computer scientists with all the theory but no practice. I'm the first one who throws in a good amount of theoretical and practical experience and I think I know quite well what I'm doing.

Yes, it tinkers with Python builtins and it may confuse future developers at first. But I also think I've documented/commented it sufficiently so that any future developer should be able to quickly grasp what's going on.

It may be removed again at some point in the future when the overall project has a better structure. One of the things I intend to do is to build a better plugin system which is capable of dealing with plugin meta data and load plugins on demand instead of just bulk importing everything at once. It's a lot of work and this import wrapping is just some dirty little helper for the time being.

1

u/rhytnen Jan 06 '18

I think the normal this use the technique i showed but insde of init the all and to get the behavior you want.

You have to admit changing import is a sign something went wrong.

1

u/[deleted] Jan 06 '18

Um, could you fix your first sentence? I'm not sure if I understand what you're trying to say. No offense.

Yes, I admit that. But there's not enough (paid) time to fix everything and there are more important things to fix/change/refactor. It may be a temporary solution or, like a usual makeshift, will last in there forever. I can't tell. It works and that's all that counts for the moment. Maybe in the future in the course of refactoring the project it will be fixed as well. Let me quote one of the greates musicians of our time, Mike Oldfield: only time will tell.

1

u/rhytnen Jan 07 '18

Yes sorry - was on my phone.

What I was trying to say is that, assuming I understand correctly, the normal way to do something like that would be to use the techniques listed above inside of your __ init __.py file.

Then when they import your module that has some set of undefined plugins, plugin detection and mocking happens there.

I don't know your architecture specifically but my instinct here is that I would never let this out of design because if this were an app, I'm not sure how you inform the user behavior is faked and if is a library, I may be expecting behavior that never happens.

For example, if it was some kind of computational library and it's optional that I have a linear algebra library installed, but I still use that library, my results will be wrong b/c you're going to pretend the function ran. If is a UI, I will apparently think the plugin is installed and be confused b/c it does nothing.

It SEEMS from my perspective that it's more appropriate to let your junior devs code fail b/c it is written wrong then write some code that hides how the infrastructure actually works from them.