# Path: web/core/context.py
# class Context(MutableMapping):
# 	"""An attribute access dictionary, of a kind.
# 	
# 	This utility class is used to cooperatively construct the ApplicationContext (and subsequent RequestContext)
# 	from the contributions of multiple extensions. The concept of "promotion to a class" is needed in order to enable
# 	the use of descriptor protocol attributes; without promotion the protocol would not be utilized.
# 	"""
# 	
# 	# M-Morty! We're, *belch*, gonna have to go in deep, Morty!  Elbow deep!
# 	def _promote(self, name, instantiate=True):
# 		"""Create a new subclass of Context which incorporates instance attributes and new descriptors.
# 		
# 		This promotes an instance and its instance attributes up to being a class with class attributes, then
# 		returns an instance of that class.
# 		"""
# 		
# 		metaclass = type(self.__class__)
# 		contents = self.__dict__.copy()
# 		cls = metaclass(str(name), (self.__class__, ), contents)
# 		
# 		if instantiate:
# 			return cls()
# 		
# 		return cls
# 	
# 	def __init__(self, **kw):
# 		"""Construct a new Context instance.
# 		
# 		All keyword arguments are applied to the instance as attributes through direct assignment to `__dict__`.
# 		"""
# 		self.__dict__.update(kw)
# 		super(Context, self).__init__()
# 	
# 	def __len__(self):
# 		"""Get a list of the public data attributes."""
# 		return len([i for i in (set(dir(self)) - self._STANDARD_ATTRS) if i[0] != '_'])
# 	
# 	def __iter__(self):
# 		"""Iterate all valid (public) attributes/keys."""
# 		return (i for i in (set(dir(self)) - self._STANDARD_ATTRS) if i[0] != '_')
# 	
# 	def __getitem__(self, name):
# 		"""Retrieve an attribute through dictionary access."""
# 		try:
# 			return getattr(self, name)
# 		except AttributeError:
# 			pass
# 		
# 		# We do this here to avoid Python 3's nested exception support.
# 		raise KeyError(name)
# 	
# 	def __setitem__(self, name, value):
# 		"""Assign an attribute through dictionary access."""
# 		setattr(self, name, value)
# 	
# 	def __delitem__(self, name):
# 		"""Delete an attribute through dictionary access."""
# 		try:
# 			return delattr(self, name)
# 		except AttributeError:
# 			pass
# 		
# 		# We do this here to avoid Python 3's nested exception support.
# 		raise KeyError(name)
# 
# Path: web/core/util.py
# class lazy(object):
# 	"""Lazily record the result of evaluating a function and cache the result.
# 	
# 	This is a non-data descriptor which tells Python to allow the instance __dict__ to override. Intended to be used
# 	by extensions to add zero-overhead (if un-accessed) values to the context.
# 	"""
# 	
# 	def __init__(self, func, name=None, doc=None):
# 		self.__name__ = name or func.__name__
# 		self.__module__ = func.__module__
# 		self.__doc__ = func.__doc__
# 		self.lock = RLock()
# 		self.func = func
# 	
# 	def __get__(self, instance, type=None):
# 		if instance is None:  # Allow direct access to the non-data descriptor via the class.
# 			return self
# 		
# 		with self.lock:  # Try to avoid situations with parallel thread access hammering the function.
# 			value = instance.__dict__.get(self.__name__, sentinel)
# 			
# 			if value is sentinel:
# 				value = instance.__dict__[self.__name__] = self.func(instance)
# 		
# 		return value


from web.core.context import Context
from web.core.util import lazy
# encoding: utf-8


def mock_lazy_value(context):
	context.ran = True
	context.count += 1
	return 42


def test_lazy_context_value():

# Path: web/core/context.py
# class Context(MutableMapping):
# 	"""An attribute access dictionary, of a kind.
# 	
# 	This utility class is used to cooperatively construct the ApplicationContext (and subsequent RequestContext)
# 	from the contributions of multiple extensions. The concept of "promotion to a class" is needed in order to enable
# 	the use of descriptor protocol attributes; without promotion the protocol would not be utilized.
# 	"""
# 	
# 	# M-Morty! We're, *belch*, gonna have to go in deep, Morty!  Elbow deep!
# 	def _promote(self, name, instantiate=True):
# 		"""Create a new subclass of Context which incorporates instance attributes and new descriptors.
# 		
# 		This promotes an instance and its instance attributes up to being a class with class attributes, then
# 		returns an instance of that class.
# 		"""
# 		
# 		metaclass = type(self.__class__)
# 		contents = self.__dict__.copy()
# 		cls = metaclass(str(name), (self.__class__, ), contents)
# 		
# 		if instantiate:
# 			return cls()
# 		
# 		return cls
# 	
# 	def __init__(self, **kw):
# 		"""Construct a new Context instance.
# 		
# 		All keyword arguments are applied to the instance as attributes through direct assignment to `__dict__`.
# 		"""
# 		self.__dict__.update(kw)
# 		super(Context, self).__init__()
# 	
# 	def __len__(self):
# 		"""Get a list of the public data attributes."""
# 		return len([i for i in (set(dir(self)) - self._STANDARD_ATTRS) if i[0] != '_'])
# 	
# 	def __iter__(self):
# 		"""Iterate all valid (public) attributes/keys."""
# 		return (i for i in (set(dir(self)) - self._STANDARD_ATTRS) if i[0] != '_')
# 	
# 	def __getitem__(self, name):
# 		"""Retrieve an attribute through dictionary access."""
# 		try:
# 			return getattr(self, name)
# 		except AttributeError:
# 			pass
# 		
# 		# We do this here to avoid Python 3's nested exception support.
# 		raise KeyError(name)
# 	
# 	def __setitem__(self, name, value):
# 		"""Assign an attribute through dictionary access."""
# 		setattr(self, name, value)
# 	
# 	def __delitem__(self, name):
# 		"""Delete an attribute through dictionary access."""
# 		try:
# 			return delattr(self, name)
# 		except AttributeError:
# 			pass
# 		
# 		# We do this here to avoid Python 3's nested exception support.
# 		raise KeyError(name)
# 
# Path: web/core/util.py
# class lazy(object):
# 	"""Lazily record the result of evaluating a function and cache the result.
# 	
# 	This is a non-data descriptor which tells Python to allow the instance __dict__ to override. Intended to be used
# 	by extensions to add zero-overhead (if un-accessed) values to the context.
# 	"""
# 	
# 	def __init__(self, func, name=None, doc=None):
# 		self.__name__ = name or func.__name__
# 		self.__module__ = func.__module__
# 		self.__doc__ = func.__doc__
# 		self.lock = RLock()
# 		self.func = func
# 	
# 	def __get__(self, instance, type=None):
# 		if instance is None:  # Allow direct access to the non-data descriptor via the class.
# 			return self
# 		
# 		with self.lock:  # Try to avoid situations with parallel thread access hammering the function.
# 			value = instance.__dict__.get(self.__name__, sentinel)
# 			
# 			if value is sentinel:
# 				value = instance.__dict__[self.__name__] = self.func(instance)
# 		
# 		return value


# Path: test/test_core/test_util.py
from web.core.context import Context
from web.core.util import lazy

# encoding: utf-8


def mock_lazy_value(context):
	context.ran = True
	context.count += 1
	return 42


def test_lazy_context_value():

	Ctx = Context(sample=lazy(mock_lazy_value, 'sample'))._promote('MockContext', False)