I use Python a lot at work, and if you're doing anything vaguely data oriented, you want to use NumPy. I gave a talk about how much I love NumPy. It's one of the things that I automatically include in every requriements.txt
because it's so goddamn useful.
Lanny supports a product which uses NumPy, which is why he was surprised to find this block:
#################### Import Section of the code #############################
try:
import numpy as np
except Exception as e:
print(e,"\nPlease Install the package")
#################### Import Section ends here ################################
Now, Python is perfectly happy to let you put code inline in modules. This allows for useful idioms- like, for example, I often do import blocks like this in the entry point script of a product:
if mode == "debugging":
from debugging_main import main
elif mode == "server":
from server_main import main
elif mode == "client":
from client_main import main
main()
It's a powerful tool that lets me deploy the same core product in different modes depending on how I'm using it. It's also useful when you don't know exactly which version of a dependency should be used- some Python packages have binary versions, but those are platform specific, so you may have some pure Python wrapper that implements a matching API.
So, for example, you might do:
try:
import some_binary_dependency as my_lib
except:
import some_python_wrapper as my_lib
This practice lets us fail gracefully: when the exceptional situation happens, we fail into a state where the program can continue, perhaps with some limitations. Which is not what happens in the example Lanny found. Here, the program warns you that you should have NumPy installed- well, no, it tells you to "Please Install the package", and isn't terribly specific about which one- and then crashes out when it tries to use the package. This is one of the odder cases of "just swallow the exception and keep going". As Lanny points out, "NumPy is used in nearly every line of the following code, so it's not like this is getting partial functionality."
Frankly, though, that's not even TRWTF to me. By convention, all Python imports go at the top of the script. At a glance, it lets you see what is imported. You don't need those comments to mark out the section. Those comments are just visual noise, they're not making the code any clearer.
And also, if you're the sort that cares about such things, they're not the same length. It's like a painting hung not quite straight on the wall. Everything just feels lopsided and wrong.