Skip to content
Back to top
View this page
Edit this page

# xonsh.lib.lazyasd

Lazy and self destructive containers for speeding up module import.

class xonsh.lib.lazyasd.LazyBool(load, ctx, name)

  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.lib.lazyasd.LazyDict(loaders, ctx, name)

  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.

  clear() → None. Remove all items from D.
  get(k[, d]) → D[k] if k in D, else d. d defaults to None.
  items() → a set-like object providing a view on D's items
  keys() → a set-like object providing a view on D's keys
  pop(k[, d]) → v, remove specified key and return the corresponding

    value. If key is not found, d is returned if given, otherwise KeyError is raised.

  popitem() → (k, v), remove and return some (key, value) pair

    as a 2-tuple; but raise KeyError if D is empty.

  setdefault(k[, d]) → D.get(k,d), also set D[k]=d if k not in D
  update([E, ]**F) → None. Update D from mapping/iterable E and F.
    If E present and has a .keys() method, does:

      for k in E.keys(): D[k] = E[k]

    If E present and lacks .keys() method, does:

      for (k, v) in E: D[k] = v

    In either case, this is followed by:

      for k, v in F.items(): D[k] = v

  values() → an object providing a view on D's values

class xonsh.lib.lazyasd.LazyObject(load, ctx, name)

  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.lib.lazyasd.lazybool(f)

  Decorator for constructing lazy booleans from a function.

xonsh.lib.lazyasd.lazydict(f)

  Decorator for constructing lazy dicts from a function.

xonsh.lib.lazyasd.lazyobject(f: Callable[[...], RT]) → RT

  Decorator for constructing lazy objects from a function.