



"""simple docstring"""






import logging
import os
import threading
import time


try:
	import warnings
except ImportError:
	__snake_case							:     int   =					None

try:
	import msvcrt
except ImportError:
	__snake_case							:     Tuple   =					None

try:
	import fcntl
except ImportError:
	__snake_case							:     Optional[Any]   =					None


# Backward compatibility
# ------------------------------------------------
try:
	TimeoutError
except NameError:
	__snake_case							:     str   =					OSError


# Data
# ------------------------------------------------
__snake_case							:     Dict   =					[
    'Timeout',
    'BaseFileLock',
    'WindowsFileLock',
    'UnixFileLock',
    'SoftFileLock',
    'FileLock',
]

__snake_case							:     Optional[Any]   =					'3.0.12'


__snake_case							:     Optional[int]   =					None


def     _lowercase       (						)		->     Any:
			global _logger
			__lowerCAmelCase   :						Optional[int]				= _logger or logging.getLogger(__name__			)
			return _logger



class     A__     (						__SCREAMING_SNAKE_CASE							):




	'''simple docstring'''





	def __init__(  self:	List[str] ,    _SCREAMING_SNAKE_CASE:	List[Any])       -> List[str]:

				"""simple docstring"""

				__lowerCAmelCase   :						int				= lock_file
				return None







	def __str__(  self:	Optional[Any])       -> Any:

				"""simple docstring"""

				__lowerCAmelCase   :						int				= F"""The file lock '{self.lock_file}' could not be acquired."""
				return temp






class     A__     :




	'''simple docstring'''





	def __init__(  self:	Dict ,    _SCREAMING_SNAKE_CASE:	Any)       -> Optional[Any]:

				"""simple docstring"""

				__lowerCAmelCase   :						Any				= lock
				return None

	def __enter__(  self:	Dict)       -> List[str]:

				"""simple docstring"""

				return self.lock







	def __exit__(  self:	Any ,    _SCREAMING_SNAKE_CASE:	Union[str, Any] ,    _SCREAMING_SNAKE_CASE:	Any ,    _SCREAMING_SNAKE_CASE:	int)       -> Union[str, Any]:

				"""simple docstring"""

				self.lock.release()
				return None






class     A__     :




	'''simple docstring'''





	def __init__(  self:	Tuple ,    _SCREAMING_SNAKE_CASE:	Dict ,    _SCREAMING_SNAKE_CASE:	Optional[int]=-1 ,    _SCREAMING_SNAKE_CASE:	List[str]=None)       -> Tuple:

				"""simple docstring"""

				__lowerCAmelCase   :						Union[str, Any]				= max_filename_length if max_filename_length is not None else 255
				# Hash the filename if it's too long
				__lowerCAmelCase   :						Any				= self.hash_filename_if_too_long(_SCREAMING_SNAKE_CASE ,    _SCREAMING_SNAKE_CASE)
				# The path to the lock file.
				__lowerCAmelCase   :						Any				= lock_file

				# The file descriptor for the *_lock_file* as it is returned by the
				# os.open() function.
				# This file lock is only NOT None, if the object currently holds the
				# lock.
				__lowerCAmelCase   :						int				= None

				# The default timeout value.
				__lowerCAmelCase   :						Tuple				= timeout

				# We use this lock primarily for the lock counter.
				__lowerCAmelCase   :						Dict				= threading.Lock()

				# The lock counter is used for implementing the nested locking
				# mechanism. Whenever the lock is acquired, the counter is increased and
				# the lock is only released, when this value is 0 again.
				__lowerCAmelCase   :						Union[str, Any]				= 0
				return None

	@property
	def 							_SCREAMING_SNAKE_CASE     (  self:	str)       -> List[str]:

				"""simple docstring"""

				return self._lock_file

	@property
	def 							_SCREAMING_SNAKE_CASE     (  self:	List[str])       -> List[str]:

				"""simple docstring"""

				return self._timeout

	@timeout.setter
	def 							_SCREAMING_SNAKE_CASE     (  self:	int ,    _SCREAMING_SNAKE_CASE:	List[Any])       -> int:

				"""simple docstring"""

				__lowerCAmelCase   :						Union[str, Any]				= float(_SCREAMING_SNAKE_CASE)
				return None

	def 							_SCREAMING_SNAKE_CASE     (  self:	List[Any])       -> Optional[Any]:

				"""simple docstring"""

				raise NotImplementedError()

	def 							_SCREAMING_SNAKE_CASE     (  self:	int)       -> Union[str, Any]:

				"""simple docstring"""

				raise NotImplementedError()

	@property
	def 							_SCREAMING_SNAKE_CASE     (  self:	Any)       -> Union[str, Any]:

				"""simple docstring"""

				return self._lock_file_fd is not None

	def 							_SCREAMING_SNAKE_CASE     (  self:	Tuple ,    _SCREAMING_SNAKE_CASE:	Union[str, Any]=None ,    _SCREAMING_SNAKE_CASE:	List[str]=0.05)       -> str:

				"""simple docstring"""

				if timeout is None:
							__lowerCAmelCase   :						int				= self.timeout

				# Increment the number right at the beginning.
				# We can still undo it, if something fails.
				with self._thread_lock:
							self._lock_counter += 1

				__lowerCAmelCase   :						str				= id(self)
				__lowerCAmelCase   :						str				= self._lock_file
				__lowerCAmelCase   :						Dict				= time.time()
				try:
							while True:
										with self._thread_lock:
													if not self.is_locked:
																logger().debug(F"""Attempting to acquire lock {lock_id} on {lock_filename}""")
																self._acquire()

										if self.is_locked:
													logger().debug(F"""Lock {lock_id} acquired on {lock_filename}""")
													break
										elif timeout >= 0 and time.time() - start_time > timeout:
													logger().debug(F"""Timeout on acquiring lock {lock_id} on {lock_filename}""")
													raise Timeout(self._lock_file)
										else:
													logger().debug(
													    F"""Lock {lock_id} not acquired on {lock_filename}, waiting {poll_intervall} seconds ...""")
													time.sleep(_SCREAMING_SNAKE_CASE)
				except:  # noqa
							# Something did go wrong, so decrement the counter.
							with self._thread_lock:
										__lowerCAmelCase   :						List[Any]				= max(0 ,    self._lock_counter - 1)

							raise
				return _Acquire_ReturnProxy(lock=self)

	def 							_SCREAMING_SNAKE_CASE     (  self:	int ,    _SCREAMING_SNAKE_CASE:	Optional[int]=False)       -> List[Any]:

				"""simple docstring"""

				with self._thread_lock:
							if self.is_locked:
										self._lock_counter -= 1

										if self._lock_counter == 0 or force:
													__lowerCAmelCase   :						Optional[int]				= id(self)
													__lowerCAmelCase   :						List[Any]				= self._lock_file

													logger().debug(F"""Attempting to release lock {lock_id} on {lock_filename}""")
													self._release()
													__lowerCAmelCase   :						str				= 0
													logger().debug(F"""Lock {lock_id} released on {lock_filename}""")

				return None

	def __enter__(  self:	Tuple)       -> Optional[Any]:

				"""simple docstring"""

				self.acquire()
				return self

	def __exit__(  self:	List[str] ,    _SCREAMING_SNAKE_CASE:	Tuple ,    _SCREAMING_SNAKE_CASE:	int ,    _SCREAMING_SNAKE_CASE:	List[Any])       -> str:

				"""simple docstring"""

				self.release()
				return None

	def __del__(  self:	Optional[int])       -> Optional[Any]:

				"""simple docstring"""

				self.release(force=_SCREAMING_SNAKE_CASE)
				return None







	def 							_SCREAMING_SNAKE_CASE     (  self:	Dict ,    _SCREAMING_SNAKE_CASE:	str ,    _SCREAMING_SNAKE_CASE:	int)       -> str:

				"""simple docstring"""

				__lowerCAmelCase   :						List[str]				= os.path.basename(_SCREAMING_SNAKE_CASE)
				if len(_SCREAMING_SNAKE_CASE) > max_length and max_length > 0:
							__lowerCAmelCase   :						Any				= os.path.dirname(_SCREAMING_SNAKE_CASE)
							__lowerCAmelCase   :						Any				= str(hash(_SCREAMING_SNAKE_CASE))
							__lowerCAmelCase   :						List[Any]				= filename[: max_length - len(_SCREAMING_SNAKE_CASE) - 8] + "..." + hashed_filename + ".lock"
							return os.path.join(_SCREAMING_SNAKE_CASE ,    _SCREAMING_SNAKE_CASE)
				else:
							return path






class     A__     (						__SCREAMING_SNAKE_CASE							):




	'''simple docstring'''





	def __init__(  self:	Dict ,    _SCREAMING_SNAKE_CASE:	Optional[int] ,    _SCREAMING_SNAKE_CASE:	Dict=-1 ,    _SCREAMING_SNAKE_CASE:	List[Any]=None)       -> Dict:

				"""simple docstring"""

				from .file_utils import relative_to_absolute_path

				super().__init__(_SCREAMING_SNAKE_CASE ,    timeout=_SCREAMING_SNAKE_CASE ,    max_filename_length=_SCREAMING_SNAKE_CASE)
				__lowerCAmelCase   :						Tuple				= "\\\\?\\" + relative_to_absolute_path(self.lock_file)

	def 							_SCREAMING_SNAKE_CASE     (  self:	Union[str, Any])       -> Optional[int]:

				"""simple docstring"""

				__lowerCAmelCase   :						Union[str, Any]				= os.O_RDWR | os.O_CREAT | os.O_TRUNC

				try:
							__lowerCAmelCase   :						List[str]				= os.open(self._lock_file ,    _SCREAMING_SNAKE_CASE)
				except OSError:
							pass
				else:
							try:
										msvcrt.locking(_SCREAMING_SNAKE_CASE ,    msvcrt.LK_NBLCK ,    1)
							except OSError:
										os.close(_SCREAMING_SNAKE_CASE)
							else:
										__lowerCAmelCase   :						Optional[Any]				= fd
				return None







	def 							_SCREAMING_SNAKE_CASE     (  self:	List[Any])       -> List[str]:

				"""simple docstring"""

				__lowerCAmelCase   :						Optional[int]				= self._lock_file_fd
				__lowerCAmelCase   :						str				= None
				msvcrt.locking(_SCREAMING_SNAKE_CASE ,    msvcrt.LK_UNLCK ,    1)
				os.close(_SCREAMING_SNAKE_CASE)

				try:
							os.remove(self._lock_file)
				# Probably another instance of the application
				# that acquired the file lock.
				except OSError:
							pass
				return None






class     A__     (						__SCREAMING_SNAKE_CASE							):




	'''simple docstring'''





	def __init__(  self:	List[Any] ,    _SCREAMING_SNAKE_CASE:	List[Any] ,    _SCREAMING_SNAKE_CASE:	Any=-1 ,    _SCREAMING_SNAKE_CASE:	Any=None)       -> List[Any]:

				"""simple docstring"""

				__lowerCAmelCase   :						List[Any]				= os.statvfs(os.path.dirname(_SCREAMING_SNAKE_CASE)).f_namemax
				super().__init__(_SCREAMING_SNAKE_CASE ,    timeout=_SCREAMING_SNAKE_CASE ,    max_filename_length=_SCREAMING_SNAKE_CASE)

	def 							_SCREAMING_SNAKE_CASE     (  self:	Any)       -> List[Any]:

				"""simple docstring"""

				__lowerCAmelCase   :						Tuple				= os.O_RDWR | os.O_CREAT | os.O_TRUNC
				__lowerCAmelCase   :						Dict				= os.open(self._lock_file ,    _SCREAMING_SNAKE_CASE)

				try:
							fcntl.flock(_SCREAMING_SNAKE_CASE ,    fcntl.LOCK_EX | fcntl.LOCK_NB)
				except OSError:
							os.close(_SCREAMING_SNAKE_CASE)
				else:
							__lowerCAmelCase   :						str				= fd
				return None







	def 							_SCREAMING_SNAKE_CASE     (  self:	int)       -> Union[str, Any]:

				"""simple docstring"""

				__lowerCAmelCase   :						Any				= self._lock_file_fd
				__lowerCAmelCase   :						Dict				= None
				fcntl.flock(_SCREAMING_SNAKE_CASE ,    fcntl.LOCK_UN)
				os.close(_SCREAMING_SNAKE_CASE)
				return None






class     A__     (						__SCREAMING_SNAKE_CASE							):




	'''simple docstring'''





	def 							_SCREAMING_SNAKE_CASE     (  self:	Dict)       -> Optional[Any]:

				"""simple docstring"""

				__lowerCAmelCase   :						Any				= os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_TRUNC
				try:
							__lowerCAmelCase   :						Optional[Any]				= os.open(self._lock_file ,    _SCREAMING_SNAKE_CASE)
				except OSError:
							pass
				else:
							__lowerCAmelCase   :						List[Any]				= fd
				return None







	def 							_SCREAMING_SNAKE_CASE     (  self:	Tuple)       -> Union[str, Any]:

				"""simple docstring"""

				os.close(self._lock_file_fd)
				__lowerCAmelCase   :						Tuple				= None

				try:
							os.remove(self._lock_file)
				# The file is already deleted and that's what we want.
				except OSError:
							pass
				return None






__snake_case							:     Optional[Any]   =					None

if msvcrt:
	__snake_case							:     Tuple   =					WindowsFileLock
elif fcntl:
	__snake_case							:     List[str]   =					UnixFileLock
else:
	__snake_case							:     Optional[Any]   =					SoftFileLock

	if warnings is not None:
		warnings.warn('only soft file lock is available')




"""simple docstring"""






import math


def     _lowercase       (						__snake_case      ,__snake_case			)		->     float:
			return math.pow(__snake_case      ,2			) - a


def     _lowercase       (						__snake_case			)		->     float:
			return 2 * x


def     _lowercase       (						__snake_case			)		->     float:
			__lowerCAmelCase   :						List[str]				= 2.0

			while start <= a:
						__lowerCAmelCase   :						Union[str, Any]				= math.pow(__snake_case      ,2			)

			return start


def     _lowercase       (						__snake_case      ,__snake_case = 9_999      ,__snake_case = 0.00000000000001			)		->     float:

			if a < 0:
						raise ValueError("math domain error"			)

			__lowerCAmelCase   :						List[Any]				= get_initial_point(__snake_case			)

			for _ in range(__snake_case			):
						__lowerCAmelCase   :						Union[str, Any]				= value
						__lowerCAmelCase   :						List[str]				= value - fx(__snake_case      ,__snake_case			) / fx_derivative(__snake_case			)
						if abs(prev_value - value			) < tolerance:
									return value

			return value


if __name__ == "__main__":
	from doctest import testmod

	testmod()