this module is from mxdatetimelazymodule py and is distributed under the terms of the egenix com public license agreement https www egenix comproductsegenix compubliclicense1 1 0 pdf helper to enable simple lazy module import lazy means the actual import is deferred until an attribute is requested from the module s namespace this has the advantage of allowing all imports to be done at the top of a script in a prominent and visible place without having a great impact on startup time c 19992005 marcandre lemburg mailto mallemburg com see the documentation for further information on s or contact the constants lazy module class lazy modules are imported into the given namespaces whenever a nonspecial attribute there are some attributes like doc that class instances handle without calling getattr is requested the module is then registered under the given name in locals usually replacing the import wrapper instance the import itself is done using globals as global namespace example of creating a lazy load module iso lazymodule iso locals globals later requesting an attribute from iso will load the module automatically into the locals namespace overriding the lazymodule instance t iso week1998 1 1 flag which indicates whether the lazymodule is initialized or not name of the module to load flag which indicates whether the module was loaded or not locals dictionary where to register the module globals dictionary to use for the module import create a lazymodule instance wrapping module name the module will later on be registered in locals under the given module name globals is optional and defaults to locals import the module now load and register module localname self lazymodulename e g toolbox fullname self name e g nltk toolbox if self lazymoduleloaded return self lazymodulelocalslocalname if debug printlazymodule loading module r fullname self lazymodulelocalslocalname module import fullname self lazymodulelocals self lazymoduleglobals fill namespace with all symbols from original module to provide faster access self dict updatemodule dict set import flag self dictlazymoduleloaded 1 if debug printlazymodule module r loaded fullname return module def getattrself name import the module on demand and set the attribute if not self lazymoduleinit self dictname value return if self lazymoduleloaded self lazymodulelocalsself lazymodulename value self dictname value return if debug print lazymodule module load triggered by attribute r write access name module self lazymoduleimport setattrmodule name value def reprself return lazymodule s self name this module is from mx datetime lazymodule py and is distributed under the terms of the egenix com public license agreement https www egenix com products egenix com public license 1 1 0 pdf helper to enable simple lazy module import lazy means the actual import is deferred until an attribute is requested from the module s namespace this has the advantage of allowing all imports to be done at the top of a script in a prominent and visible place without having a great impact on startup time c 1999 2005 marc andre lemburg mailto mal lemburg com see the documentation for further information on s or contact the constants lazy module class lazy modules are imported into the given namespaces whenever a non special attribute there are some attributes like __doc__ that class instances handle without calling __getattr__ is requested the module is then registered under the given name in locals usually replacing the import wrapper instance the import itself is done using globals as global namespace example of creating a lazy load module iso lazymodule iso locals globals later requesting an attribute from iso will load the module automatically into the locals namespace overriding the lazymodule instance t iso week 1998 1 1 flag which indicates whether the lazymodule is initialized or not name of the module to load flag which indicates whether the module was loaded or not locals dictionary where to register the module globals dictionary to use for the module import create a lazymodule instance wrapping module name the module will later on be registered in locals under the given module name globals is optional and defaults to locals import the module now load and register module e g toolbox e g nltk toolbox fill namespace with all symbols from original module to provide faster access set import flag import the module on demand and get the attribute import the module on demand and set the attribute
_debug = 0




class LazyModule:

    

    
    __lazymodule_init = 0

    
    __lazymodule_name = ""

    
    __lazymodule_loaded = 0

    
    __lazymodule_locals = None

    
    __lazymodule_globals = None

    def __init__(self, name, locals, globals=None):

        
        self.__lazymodule_locals = locals
        if globals is None:
            globals = locals
        self.__lazymodule_globals = globals
        mainname = globals.get("__name__", "")
        if mainname:
            self.__name__ = mainname + "." + name
            self.__lazymodule_name = name
        else:
            self.__name__ = self.__lazymodule_name = name
        self.__lazymodule_init = 1

    def __lazymodule_import(self):

        
        
        local_name = self.__lazymodule_name  
        full_name = self.__name__  
        if self.__lazymodule_loaded:
            return self.__lazymodule_locals[local_name]
        if _debug:
            print("LazyModule: Loading module %r" % full_name)
        self.__lazymodule_locals[local_name] = module = __import__(
            full_name, self.__lazymodule_locals, self.__lazymodule_globals, "*"
        )

        
        
        self.__dict__.update(module.__dict__)

        
        self.__dict__["__lazymodule_loaded"] = 1

        if _debug:
            print("LazyModule: Module %r loaded" % full_name)
        return module

    def __getattr__(self, name):

        
        if self.__lazymodule_loaded:
            raise AttributeError(name)
        if _debug:
            print(
                "LazyModule: "
                "Module load triggered by attribute %r read access" % name
            )
        module = self.__lazymodule_import()
        return getattr(module, name)

    def __setattr__(self, name, value):

        
        if not self.__lazymodule_init:
            self.__dict__[name] = value
            return
        if self.__lazymodule_loaded:
            self.__lazymodule_locals[self.__lazymodule_name] = value
            self.__dict__[name] = value
            return
        if _debug:
            print(
                "LazyModule: "
                "Module load triggered by attribute %r write access" % name
            )
        module = self.__lazymodule_import()
        setattr(module, name, value)

    def __repr__(self):
        return "<LazyModule '%s'>" % self.__name__