I have a Python mathematics module that provides some relatively simple mathematical functions and that provides some very complicated ones that rely on another (very large) module that can take a while to import.
For some little scripts, I want only to use the little functions of my mathematics module and for some big scripts, I want to use the complicated functions it provides (which are dependent on the large module).
I could add the import of this large module to the complicated functions in my mathematics module so that the large module is imported only when its needed. That would work fine for the little scripts that don't need that functionality. However, then the big scripts would end up importing the large module multiple times (and remember that it takes a little while to import) so that isn't so good.
Is there some sensible way to structure my mathematics module to be able to deal with both the little scripts and the big scripts in the way I want or is there a way to pass to the module the need for no additional complexity (like: import supermathematicsmodule(version = "lite")).
Maybe you are overthinking this? I would just split the functions in two modules, say bigmath and littlemath. You could put something like from littlemath import * in bigmath for convenience so all functions are available from there.
Another option would be to have the complicated functions in a private module and a function to import and add them to the public module namespace, so to have all functions available you would do:
import mymath
mymath.import_complicated_functions()
But this would require some trickery (= potential source of problems) and I don't think it's actually an improvement.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With