Package nltk :: Module lazyimport
[hide private]
[frames] | no frames]

Source Code for Module nltk.lazyimport

  1  # This module is from mx/DateTime/LazyModule.py and is 
  2  # distributed under the terms of the eGenix.com Public License Agreement 
  3  # http://www.egenix.com/products/eGenix.com-Public-License-1.1.0.pdf 
  4   
  5  """ Helper to enable simple lazy module import.  
  6   
  7      'Lazy' means the actual import is deferred until an attribute is 
  8      requested from the module's namespace. This has the advantage of 
  9      allowing all imports to be done at the top of a script (in a 
 10      prominent and visible place) without having a great impact 
 11      on startup time. 
 12   
 13      Copyright (c) 1999-2005, Marc-Andre Lemburg; mailto:mal@lemburg.com 
 14      See the documentation for further information on copyrights, 
 15      or contact the author. All Rights Reserved. 
 16  """ 
 17   
 18  ### Constants 
 19   
 20  _debug = 0 
 21   
 22  ### 
 23   
24 -class LazyModule:
25 26 """ Lazy module class. 27 28 Lazy modules are imported into the given namespaces whenever a 29 non-special attribute (there are some attributes like __doc__ 30 that class instances handle without calling __getattr__) is 31 requested. The module is then registered under the given name 32 in locals usually replacing the import wrapper instance. The 33 import itself is done using globals as global namespace. 34 35 Example of creating a lazy load module: 36 37 ISO = LazyModule('ISO',locals(),globals()) 38 39 Later, requesting an attribute from ISO will load the module 40 automatically into the locals() namespace, overriding the 41 LazyModule instance: 42 43 t = ISO.Week(1998,1,1) 44 45 """ 46 # Flag which inidicates whether the LazyModule is initialized or not 47 __lazymodule_init = 0 48 49 # Name of the module to load 50 __lazymodule_name = '' 51 52 # Flag which indicates whether the module was loaded or not 53 __lazymodule_loaded = 0 54 55 # Locals dictionary where to register the module 56 __lazymodule_locals = None 57 58 # Globals dictionary to use for the module import 59 __lazymodule_globals = None 60
61 - def __init__(self, name, locals, globals=None):
62 63 """ Create a LazyModule instance wrapping module name. 64 65 The module will later on be registered in locals under the 66 given module name. 67 68 globals is optional and defaults to locals. 69 70 """ 71 self.__lazymodule_locals = locals 72 if globals is None: 73 globals = locals 74 self.__lazymodule_globals = globals 75 mainname = globals.get('__name__', '') 76 if mainname: 77 self.__name__ = mainname + '.' + name 78 self.__lazymodule_name = name 79 else: 80 self.__name__ = self.__lazymodule_name = name 81 self.__lazymodule_init = 1
82
83 - def __lazymodule_import(self):
84 85 """ Import the module now. 86 """ 87 # Load and register module 88 name = self.__lazymodule_name 89 if self.__lazymodule_loaded: 90 return self.__lazymodule_locals[name] 91 if _debug: 92 print 'LazyModule: Loading module %r' % name 93 self.__lazymodule_locals[name] \ 94 = module \ 95 = __import__(name, 96 self.__lazymodule_locals, 97 self.__lazymodule_globals, 98 '*') 99 100 # Fill namespace with all symbols from original module to 101 # provide faster access. 102 self.__dict__.update(module.__dict__) 103 104 # Set import flag 105 self.__dict__['__lazymodule_loaded'] = 1 106 107 if _debug: 108 print 'LazyModule: Module %r loaded' % name 109 return module
110
111 - def __getattr__(self, name):
112 113 """ Import the module on demand and get the attribute. 114 """ 115 if self.__lazymodule_loaded: 116 raise AttributeError, name 117 if _debug: 118 print 'LazyModule: ' \ 119 'Module load triggered by attribute %r read access' % name 120 module = self.__lazymodule_import() 121 return getattr(module, name)
122
123 - def __setattr__(self, name, value):
124 125 """ Import the module on demand and set the attribute. 126 """ 127 if not self.__lazymodule_init: 128 self.__dict__[name] = value 129 return 130 if self.__lazymodule_loaded: 131 self.__lazymodule_locals[self.__lazymodule_name] = value 132 self.__dict__[name] = value 133 return 134 if _debug: 135 print 'LazyModule: ' \ 136 'Module load triggered by attribute %r write access' % name 137 module = self.__lazymodule_import() 138 setattr(module, name, value)
139
140 - def __repr__(self):
141 return "<LazyModule '%s'>" % self.__name__
142