Lazy & Self-destructive Objects (xonsh.lazyasd
)¶
Lazy and self destructive containers for speeding up module import.
-
class
xonsh.lazyasd.
BackgroundModuleLoader
(name, package, replacements, *args, **kwargs)[source]¶ Thread to load modules in the background.
-
run
()[source]¶ Method representing the thread’s activity.
You may override this method in a subclass. The standard run() method invokes the callable object passed to the object’s constructor as the target argument, if any, with sequential and keyword arguments taken from the args and kwargs arguments, respectively.
-
-
class
xonsh.lazyasd.
BackgroundModuleProxy
(modname)[source]¶ Proxy object for modules loaded in the background that block attribute access until the module is loaded..
-
class
xonsh.lazyasd.
LazyBool
(load, ctx, name)[source]¶ Boolean like object that lazily computes it boolean value when it is first asked. Once loaded, this result will replace itself in the provided context (typically the globals of the call site) with the given name.
For example, you can prevent the complex boolean until it is actually used:
ALIVE = LazyDict(lambda: not DEAD, globals(), 'ALIVE')
Parameters: load : function with no arguments
A loader function that performs the actual boolean evaluation.
ctx : Mapping
Context to replace the LazyBool instance in with the the fully loaded mapping.
name : str
Name in the context to give the loaded mapping. This should be the name on the LHS of the assignment.
-
class
xonsh.lazyasd.
LazyDict
(loaders, ctx, name)[source]¶ Dictionary like object that lazily loads its values from an initial dict of key-loader function pairs. Each key is loaded when its value is first accessed. Once fully loaded, this object will replace itself in the provided context (typically the globals of the call site) with the given name.
For example, you can prevent the compilation of a bunch of regular expressions until they are actually used:
RES = LazyDict({ 'dot': lambda: re.compile('.'), 'all': lambda: re.compile('.*'), 'two': lambda: re.compile('..'), }, globals(), 'RES')
Parameters: loaders : Mapping of keys to functions with no arguments
A mapping of loader function that performs the actual value construction upon access.
ctx : Mapping
Context to replace the LazyDict instance in with the the fully loaded mapping.
name : str
Name in the context to give the loaded mapping. This should be the name on the LHS of the assignment.
-
class
xonsh.lazyasd.
LazyObject
(load, ctx, name)[source]¶ Lazily loads an object via the load function the first time an attribute is accessed. Once loaded it will replace itself in the provided context (typically the globals of the call site) with the given name.
For example, you can prevent the compilation of a regular expression until it is actually used:
DOT = LazyObject((lambda: re.compile('.')), globals(), 'DOT')
Parameters: load : function with no arguments
A loader function that performs the actual object construction.
ctx : Mapping
Context to replace the LazyObject instance in with the object returned by load().
name : str
Name in the context to give the loaded object. This should be the name on the LHS of the assignment.
-
xonsh.lazyasd.
load_module_in_background
(name, package=None, debug='DEBUG', env=None, replacements=None)[source]¶ Entry point for loading modules in background thread.
Parameters: name : str
Module name to load in background thread.
package : str or None, optional
Package name, has the same meaning as in importlib.import_module().
debug : str, optional
Debugging symbol name to look up in the environment.
env : Mapping or None, optional
Environment this will default to __xonsh__.env, if available, and os.environ otherwise.
replacements : Mapping or None, optional
Dictionary mapping fully qualified module names (eg foo.bar.baz) that import the lazily loaded module, with the variable name in that module. For example, suppose that foo.bar imports module a as b, this dict is then {‘foo.bar’: ‘b’}.
Returns: module : ModuleType
This is either the original module that is found in sys.modules or a proxy module that will block until delay attribute access until the module is fully loaded.