How do you remember to from-import- 'everything' that is needed?
I have a 'utility module' which contains a bunch of classes which
examine/check/log aspects of the execution environment. One of which is
PythonEnvironment, another relates to the HostSystem (as examples). They
are most-frequently used to ensure that an application runs within an
appropriate environment, eg don't use Posix code on MS-Windows and don't
call for v3.7 features from Python3.5.
Today, I 'grabbed' the module, imported the PythonEnvironment, and
PyTested the application code's Python version - as much for my
amusement as anything else I asked for an impossible versionNR. Of
course, the test failed.
Upon closer inspection, I realised it didn't just fail; it failed badly!
Some silly, little, boy had imported the PythonEnvironment class but
failed to ALSO import PythonVersionError. So, the reported error was not
the expected exception!
Yes, the two classes appear in the module one-above-the-other, and yes,
in the module's own pytest import-ing both classes is 'right there' in
front of my nose. So, not my finest hour!
Is there some 'easy way' to make sure that one doesn't just import the
desired class, but also remembers to import 'everything else' that might
be necessary. In this case, it'd be rather nice to:
from environment_module import Python*
NB this is a syntax error, and won't import both the PythonEnvironment
and PythonVersionError classes.
NBB I could import the module, fish-around in its __dict__, and do
something like update-ing locals() with every key containing "Python" -
but that is surely a truly, ugly, hack.
The 'lazy' answer is (purists, look away now!):
from environment_module import *
but that import-s "everything, including the kitchen sink"!
After you've recovered from the shock-horror of thinking about such
blatant "namespace pollution", if the module only contains a bunch of
classes, is it really such an evil act?
(and if the module's import-as-abbreviations are moved 'inside' the
relevant class, those 'disappear' from the global namespace reducing the
risk of name-clashes down to the names of the custom-classes and the PSL
imports, eg os and platform)
What do you do to (respecting purism) ensure 'everything' (necessary) is
imported (and nothing more), preferably without relying upon (faulty, in
my case) human-memory or reading through volumes of code/documentation?
--
Regards,
=dn
--
https://mail.python.org/mailman/listinfo/python-list