






from importlib import import_module

from .logging import get_logger


A        =			get_logger(__name__)


class 							__a							:





  '''simple docstring'''



  def __init__(						self ,    UpperCamelCase__ ,    UpperCamelCase__=None       ):
      SCREAMING_SNAKE_CASE_   :	Any													=   attrs or []
      if module is not None:
          for key in module.__dict__:
              if key in attrs or not key.startswith('__'       ):
                  setattr(self ,    UpperCamelCase__ ,    getattr(UpperCamelCase__ ,    UpperCamelCase__       )       )
      SCREAMING_SNAKE_CASE_   :	Any													=   module._original_module if isinstance(UpperCamelCase__ ,    _PatchedModuleObj       ) else module






class 							__a							:





  '''simple docstring'''



  UpperCAmelCase__      :				List[Any]				    =						[]


  def __init__(						self ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__=None       ):
      SCREAMING_SNAKE_CASE_   :	Optional[int]													=   obj
      SCREAMING_SNAKE_CASE_   :	Optional[int]													=   target
      SCREAMING_SNAKE_CASE_   :	Optional[Any]													=   new
      SCREAMING_SNAKE_CASE_   :	int													=   target.split('.'       )[0]
      SCREAMING_SNAKE_CASE_   :	Union[str, Any]													=   {}
      SCREAMING_SNAKE_CASE_   :	Any													=   attrs or []


  def __enter__(						self       ):
      SCREAMING_SNAKE_CASE_   :	List[str]													=   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(UpperCamelCase__       )       ):
          try:
              SCREAMING_SNAKE_CASE_   :	Optional[int]													=   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__():
              SCREAMING_SNAKE_CASE_   :	str													=   getattr(self.obj ,    UpperCamelCase__       )
              # 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(UpperCamelCase__ ,    _PatchedModuleObj       ) and obj_attr._original_module is submodule)
              ):
                  SCREAMING_SNAKE_CASE_   :	Optional[int]													=   obj_attr
                  # patch at top level
                  setattr(self.obj ,    UpperCamelCase__ ,    _PatchedModuleObj(UpperCamelCase__ ,    attrs=self.attrs       )       )
                  SCREAMING_SNAKE_CASE_   :	List[Any]													=   getattr(self.obj ,    UpperCamelCase__       )
                  # construct lower levels patches
                  for key in submodules[i + 1 :]:
                      setattr(UpperCamelCase__ ,    UpperCamelCase__ ,    _PatchedModuleObj(getattr(UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__       ) ,    attrs=self.attrs       )       )
                      SCREAMING_SNAKE_CASE_   :	Union[str, Any]													=   getattr(UpperCamelCase__ ,    UpperCamelCase__       )
                  # finally set the target attribute
                  setattr(UpperCamelCase__ ,    UpperCamelCase__ ,    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:
              SCREAMING_SNAKE_CASE_   :	Optional[int]													=   getattr(import_module('.'.join(UpperCamelCase__       )       ) ,    UpperCamelCase__       )
          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 ,    UpperCamelCase__       ) is attr_value:
                  SCREAMING_SNAKE_CASE_   :	Optional[int]													=   getattr(self.obj ,    UpperCamelCase__       )
                  setattr(self.obj ,    UpperCamelCase__ ,    self.new       )
      elif target_attr in globals()["__builtins__"]:  # if it'a s builtin like "open"
          SCREAMING_SNAKE_CASE_   :	Dict													=   globals()['__builtins__'][target_attr]
          setattr(self.obj ,    UpperCamelCase__ ,    self.new       )
      else:
          raise RuntimeError(F'''Tried to patch attribute {target_attr} instead of a submodule.'''       )


  def __exit__(						self ,    *UpperCamelCase__       ):
      for attr in list(self.original       ):
          setattr(self.obj ,    UpperCamelCase__ ,    self.original.pop(UpperCamelCase__       )       )


  def   __snake_case			(						self       ):
      self.__enter__()
      self._active_patches.append(self       )






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

      return self.__exit__()







from timeit import timeit



def 					_lowerCamelCase(			lowerCAmelCase__				:		int      ):





				'''simple docstring'''







				if number < 0:
								raise ValueError('the value of input must not be negative'      )
				SCREAMING_SNAKE_CASE_   :	Tuple													=   0
				while number:
								number &= number - 1
								result += 1
				return result



def 					_lowerCamelCase(			lowerCAmelCase__				:		int      ):





				'''simple docstring'''







				if number < 0:
								raise ValueError('the value of input must not be negative'      )
				SCREAMING_SNAKE_CASE_   :	Optional[Any]													=   0
				while number:
								if number % 2 == 1:
												result += 1
								number >>= 1
				return result



def 					_lowerCamelCase(			):





				'''simple docstring'''







				def do_benchmark(lowerCAmelCase__				:		int      ) -> None:
								SCREAMING_SNAKE_CASE_   :	int													=   'import __main__ as z'
								print(F'''Benchmark when {number = }:'''      )
								print(F'''{get_set_bits_count_using_modulo_operator(lowerCAmelCase__      ) = }'''      )
								SCREAMING_SNAKE_CASE_   :	int													=   timeit('z.get_set_bits_count_using_modulo_operator(25)'    ,    setup=lowerCAmelCase__      )
								print(F'''timeit() runs in {timing} seconds'''      )
								print(F'''{get_set_bits_count_using_brian_kernighans_algorithm(lowerCAmelCase__      ) = }'''      )
								SCREAMING_SNAKE_CASE_   :	Optional[Any]													=   timeit(
								    'z.get_set_bits_count_using_brian_kernighans_algorithm(25)'    ,    setup=lowerCAmelCase__    ,    )
								print(F'''timeit() runs in {timing} seconds'''      )

				for number in (25, 37, 58, 0):
								do_benchmark(lowerCAmelCase__      )
								print()


if __name__ == "__main__":
				import doctest

				doctest.testmod()
				benchmark()