Skip to content

Commit

Permalink
STYLE: Reduce aliased object naming
Browse files Browse the repository at this point in the history
Make the intended behavior more clear by minimizing the
aliasing of module object that is being manipulated.
  • Loading branch information
hjmjohnson committed Oct 27, 2020
1 parent 30882f5 commit 17051ed
Showing 1 changed file with 23 additions and 26 deletions.
49 changes: 23 additions & 26 deletions Wrapping/Generators/Python/itk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ def _get_lazy_attributes(local_lazy_attributes, l_module, l_data):
import os
import sys

this_module = sys.modules[__name__]
if itkConfig.LazyLoading:
# If we are loading lazily (on-demand), make a dict mapping the available
# classes/functions/etc. (read from the configuration modules) to the
Expand All @@ -64,64 +63,62 @@ def _get_lazy_attributes(local_lazy_attributes, l_module, l_data):
for module, data in itkBase.itk_base_global_module_data.items():
_get_lazy_attributes(lazy_attributes, module, data)

if isinstance(this_module, itkLazy.LazyITKModule):
if isinstance(sys.modules[__name__], itkLazy.LazyITKModule):
# Handle reload case where we've already done this once.
# If we made a new module every time, multiple reload()s would fail
# because the identity of sys.modules['itk'] would always be changing.
this_module.__init__(__name__, lazy_attributes)
sys.modules[__name__].__init__(__name__, lazy_attributes)
del lazy_attributes
else:
this_module = itkLazy.LazyITKModule(__name__, lazy_attributes)
# Set the __path__ attribute, which is required for this module to be used as a
# package
setattr(this_module, "__path__", __path__)
setattr(this_module, "__spec__", __spec__) # pytype: disable=name-error
# Create a new LazyITKModule
lzy_module = itkLazy.LazyITKModule(__name__, lazy_attributes)
# Set the __path__ attribute, which is required for this_module
# to be used as a package
setattr(lzy_module, "__path__", __path__)
setattr(lzy_module, "__spec__", __spec__) # pytype: disable=name-error
# Now override the default sys.modules[__name__] (__name__ == 'itk' )
sys.modules[__name__] = lzy_module
else:
# We're not lazy-loading. Just load the modules in the order specified in
# the known_modules list for consistency.
for module in itkBase.itk_base_global_module_data.keys():
itkBase.itk_load_swig_module(module, this_module.__dict__)
itkBase.itk_load_swig_module(module, sys.modules[__name__].__dict__)

# Populate itk.ITKModuleName
for module, data in itkBase.itk_base_global_module_data.items():
attributes = {}
_get_lazy_attributes(attributes, module, data)
itk_module = itkLazy.LazyITKModule(module, attributes)
setattr(sys.modules[__name__], module, itk_module)

# Regardless of how it was loaded, fill up the itk module with the ITK types
# and extras.
import itkTypes

for k, v in itkTypes.__dict__.items():
if k != "itkCType" and not k.startswith("_"):
setattr(this_module, k, v)
setattr(sys.modules[__name__], k, v)
del itkTypes
for k, v in itkInitHelpers.__dict__.items():
if not k.startswith("_"):
setattr(this_module, k, v)
setattr(sys.modules[__name__], k, v)

import itkExtras

for k, v in itkExtras.__dict__.items():
if not k.startswith("_"):
setattr(this_module, k, v)
setattr(sys.modules[__name__], k, v)
del itkExtras

# --
# Needed to propagate symbol to itk.image from itkTemplate.image
import itkTemplate

setattr(this_module, "image", itkTemplate.image)
setattr(this_module, "output", itkTemplate.output)
setattr(sys.modules[__name__], "image", itkTemplate.image)
setattr(sys.modules[__name__], "output", itkTemplate.output)
del itkTemplate
# --

# Populate itk.ITKModuleName
for module, data in itkBase.itk_base_global_module_data.items():
attributes = {}
_get_lazy_attributes(attributes, module, data)
itk_module = itkLazy.LazyITKModule(module, attributes)
setattr(this_module, module, itk_module)

if itkConfig.LazyLoading:
# this has to be the last step, else python gets confused about itkTypes
# and itkExtras above. I'm not sure why...
sys.modules[__name__] = this_module


# Now do the initialization
_initialize_module()

0 comments on commit 17051ed

Please sign in to comment.