



from importlib import import_module

from .logging import get_logger


lowercase__			   =  get_logger(__name__)




class SCREAMING_SNAKE_CASE__					:



						def __init__(self						,     _lowercase						,     _lowercase=None							):





													'''simple docstring'''

													__a		:		Union[str, Any]									=       attrs or []
													if module is not None:
																				for key in module.__dict__:
																											if key in attrs or not key.startswith("""__"""							):
																																		setattr(self						,     _lowercase						,     getattr(_lowercase						,     _lowercase							)							)
													__a		:		Optional[Any]									=       module._original_module if isinstance(_lowercase						,     _PatchedModuleObj							) else module







class SCREAMING_SNAKE_CASE__					:
						_lowerCAmelCase							=					[]



						def __init__(self						,     _lowercase						,     _lowercase						,     _lowercase						,     _lowercase=None							):





													'''simple docstring'''

													__a		:		List[str]									=       obj
													__a		:		List[Any]									=       target
													__a		:		Any									=       new
													__a		:		List[str]									=       target.split("""."""							)[0]
													__a		:		Any									=       {}
													__a		:		str									=       attrs or []



						def __enter__(self							):





													'''simple docstring'''

													*__a							,     __a		:		int									=       self.target.split("""."""							)

													# Patch modules:
													# it's used to patch attributes of submodules like "os.path.join";
													# in this case we need to patch "os" and "os.path"

													for i in range(len(_lowercase							)							):
																				try:
																											__a		:		Optional[Any]									=       import_module(""".""".join(submodules[: i + 1]							)							)
																				except ModuleNotFoundError:
																											continue
																				# We iterate over all the globals in self.obj in case we find "os" or "os.path"
																				for attr in self.obj.__dir__():
																											__a		:		List[Any]									=       getattr(self.obj						,     _lowercase							)
																											# We don't check for the name of the global, but rather if its value *is* "os" or "os.path".
																											# This allows to patch renamed modules like "from os import path as ospath".
																											if obj_attr is submodule or (
																											    (isinstance(_lowercase						,     _PatchedModuleObj							) and obj_attr._original_module is submodule)
																											):
																																		__a		:		Tuple									=       obj_attr
																																		# patch at top level
																																		setattr(self.obj						,     _lowercase						,     _PatchedModuleObj(_lowercase						,     attrs=self.attrs							)							)
																																		__a		:		List[Any]									=       getattr(self.obj						,     _lowercase							)
																																		# construct lower levels patches
																																		for key in submodules[i + 1 :]:
																																									setattr(_lowercase						,     _lowercase						,     _PatchedModuleObj(getattr(_lowercase						,     _lowercase						,     _lowercase							)						,     attrs=self.attrs							)							)
																																									__a		:		Optional[int]									=       getattr(_lowercase						,     _lowercase							)
																																		# finally set the target attribute
																																		setattr(_lowercase						,     _lowercase						,     self.new							)

        # Patch attribute itself:
        # it's used for builtins like "open",
        # and also to patch "os.path.join" we may also need to patch "join"
        # itself if it was imported as "from os.path import join".

													if submodules:  # if it's an attribute of a submodule like "os.path.join"
																				try:
																											__a		:		Dict									=       getattr(import_module(""".""".join(_lowercase							)							)						,     _lowercase							)
																				except (AttributeError, ModuleNotFoundError):
																											return
																				# We iterate over all the globals in self.obj in case we find "os.path.join"
																				for attr in self.obj.__dir__():
																											# We don't check for the name of the global, but rather if its value *is* "os.path.join".
																											# This allows to patch renamed attributes like "from os.path import join as pjoin".
																											if getattr(self.obj						,     _lowercase							) is attr_value:
																																		__a		:		Optional[int]									=       getattr(self.obj						,     _lowercase							)
																																		setattr(self.obj						,     _lowercase						,     self.new							)
													elif target_attr in globals()["__builtins__"]:  # if it'a s builtin like "open"
																				__a		:		Any									=       globals()["""__builtins__"""][target_attr]
																				setattr(self.obj						,     _lowercase						,     self.new							)
													else:
																				raise RuntimeError(F'''Tried to patch attribute {target_attr} instead of a submodule.'''							)



						def __exit__(self						,     *_lowercase							):





													'''simple docstring'''

													for attr in list(self.original							):
																				setattr(self.obj						,     _lowercase						,     self.original.pop(_lowercase							)							)



						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													self.__enter__()
													self._active_patches.append(self							)





						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													try:
																				self._active_patches.remove(self							)
													except ValueError:
																				# If the patch hasn't been started this will fail
																				return None

													return self.__exit__()






"""simple docstring"""

def       __magic_name__    (   _lowerCamelCase				: list[int]					):
							if not nums:  # Makes sure that the list is not empty
														raise ValueError("""List is empty"""					)

							__a		:		Any									=       sum(_lowerCamelCase					) / len(_lowerCamelCase					)  # Calculate the average
							return sum(abs(x - average					) for x in nums					) / len(_lowerCamelCase					)


if __name__ == "__main__":
	import doctest

	doctest.testmod()

