






import logging
import os
import threading
import time


try:
							import warnings
except ImportError:
							A__			:    str         =				None

try:
							import msvcrt
except ImportError:
							A__			:    Any         =				None

try:
							import fcntl
except ImportError:
							A__			:    Dict         =				None


# Backward compatibility
# ------------------------------------------------
try:
							TimeoutError
except NameError:
							A__			:    Any         =				OSError


# Data
# ------------------------------------------------
A__			:    int         =				[
    'Timeout',
    'BaseFileLock',
    'WindowsFileLock',
    'UnixFileLock',
    'SoftFileLock',
    'FileLock',
]

A__			:    int         =				'3.0.12'


A__			:    List[str]         =				None






def 						a    (		):






				'''simple docstring'''
				global _logger
				lowercase__     				=    _logger or logging.getLogger(__name__       )
				return _logger
class    _UpperCAmelCase      ( A__			):

				"""simple docstring"""




				def __init__(  self		:					List[str],						lowerCamelCase		:					Tuple						):

								'''simple docstring'''
								lowercase__     				=    lock_file
								return None




				def __str__(  self		:					Dict						):

								'''simple docstring'''
								lowercase__     				=    F"""The file lock '{self.lock_file}' could not be acquired."""
								return temp



class    _UpperCAmelCase      :

				"""simple docstring"""




				def __init__(  self		:					Optional[int],						lowerCamelCase		:					int						):

								'''simple docstring'''
								lowercase__     				=    lock
								return None




				def __enter__(  self		:					Tuple						):

								'''simple docstring'''
								return self.lock




				def __exit__(  self		:					Optional[int],						lowerCamelCase		:					Optional[int],						lowerCamelCase		:					Optional[Any],						lowerCamelCase		:					Tuple						):

								'''simple docstring'''
								self.lock.release()
								return None



class    _UpperCAmelCase      :

				"""simple docstring"""




				def __init__(  self		:					Tuple,						lowerCamelCase		:					Dict,						lowerCamelCase		:					str=-1,						lowerCamelCase		:					str=None						):

								'''simple docstring'''
								lowercase__     				=    max_filename_length if max_filename_length is not None else 255
								# Hash the filename if it's too long
								lowercase__     				=    self.hash_filename_if_too_long(lowerCamelCase,						lowerCamelCase						)
								# The path to the lock file.
								lowercase__     				=    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.
								lowercase__     				=    None

								# The default timeout value.
								lowercase__     				=    timeout

								# We use this lock primarily for the lock counter.
								lowercase__     				=    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.
								lowercase__     				=    0
								return None




				@property
				def        lowercase__     (  self		:					Any						):

								'''simple docstring'''
								return self._lock_file




				@property
				def        lowercase__     (  self		:					Union[str, Any]						):

								'''simple docstring'''
								return self._timeout




				@timeout.setter
				def        lowercase__     (  self		:					Optional[int],						lowerCamelCase		:					List[Any]						):

								'''simple docstring'''
								lowercase__     				=    float(lowerCamelCase						)
								return None




				def        lowercase__     (  self		:					Tuple						):

								'''simple docstring'''
								raise NotImplementedError()




				def        lowercase__     (  self		:					Tuple						):

								'''simple docstring'''
								raise NotImplementedError()




				@property
				def        lowercase__     (  self		:					int						):

								'''simple docstring'''
								return self._lock_file_fd is not None




				def        lowercase__     (  self		:					List[Any],						lowerCamelCase		:					Union[str, Any]=None,						lowerCamelCase		:					List[Any]=0.05						):

								'''simple docstring'''
								# Use the default timeout, if no timeout is provided.
								if timeout is None:
												lowercase__     				=    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

								lowercase__     				=    id(self						)
								lowercase__     				=    self._lock_file
								lowercase__     				=    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(lowerCamelCase						)
								except:  # noqa
												# Something did go wrong, so decrement the counter.
												with self._thread_lock:
																lowercase__     				=    max(0,						self._lock_counter - 1						)

												raise
								return _Acquire_ReturnProxy(lock=self						)




				def        lowercase__     (  self		:					Tuple,						lowerCamelCase		:					Any=False						):

								'''simple docstring'''
								with self._thread_lock:
												if self.is_locked:
																self._lock_counter -= 1

																if self._lock_counter == 0 or force:
																				lowercase__     				=    id(self						)
																				lowercase__     				=    self._lock_file

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

								return None




				def __enter__(  self		:					Dict						):

								'''simple docstring'''
								self.acquire()
								return self




				def __exit__(  self		:					Union[str, Any],						lowerCamelCase		:					Tuple,						lowerCamelCase		:					List[Any],						lowerCamelCase		:					str						):

								'''simple docstring'''
								self.release()
								return None




				def __del__(  self		:					Tuple						):

								'''simple docstring'''
								self.release(force=lowerCamelCase						)
								return None




				def        lowercase__     (  self		:					Tuple,						lowerCamelCase		:					str,						lowerCamelCase		:					int						):

								'''simple docstring'''
								lowercase__     				=    os.path.basename(lowerCamelCase						)
								if len(lowerCamelCase						) > max_length and max_length > 0:
												lowercase__     				=    os.path.dirname(lowerCamelCase						)
												lowercase__     				=    str(hash(lowerCamelCase						)						)
												lowercase__     				=    filename[: max_length - len(lowerCamelCase						) - 8] + '''...''' + hashed_filename + '''.lock'''
												return os.path.join(lowerCamelCase,						lowerCamelCase						)
								else:
												return path



class    _UpperCAmelCase      ( A__			):

				"""simple docstring"""




				def __init__(  self		:					Tuple,						lowerCamelCase		:					Optional[int],						lowerCamelCase		:					Optional[Any]=-1,						lowerCamelCase		:					Any=None						):

								'''simple docstring'''
								from .file_utils import relative_to_absolute_path

								super().__init__(lowerCamelCase,						timeout=lowerCamelCase,						max_filename_length=lowerCamelCase						)
								lowercase__     				=    '''\\\\?\\''' + relative_to_absolute_path(self.lock_file						)




				def        lowercase__     (  self		:					Tuple						):

								'''simple docstring'''
								lowercase__     				=    os.O_RDWR | os.O_CREAT | os.O_TRUNC

								try:
												lowercase__     				=    os.open(self._lock_file,						lowerCamelCase						)
								except OSError:
												pass
								else:
												try:
																msvcrt.locking(lowerCamelCase,						msvcrt.LK_NBLCK,						1						)
												except OSError:
																os.close(lowerCamelCase						)
												else:
																lowercase__     				=    fd
								return None




				def        lowercase__     (  self		:					Optional[Any]						):

								'''simple docstring'''
								lowercase__     				=    self._lock_file_fd
								lowercase__     				=    None
								msvcrt.locking(lowerCamelCase,						msvcrt.LK_UNLCK,						1						)
								os.close(lowerCamelCase						)

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



class    _UpperCAmelCase      ( A__			):

				"""simple docstring"""




				def __init__(  self		:					Union[str, Any],						lowerCamelCase		:					Optional[int],						lowerCamelCase		:					str=-1,						lowerCamelCase		:					Optional[Any]=None						):

								'''simple docstring'''
								lowercase__     				=    os.statvfs(os.path.dirname(lowerCamelCase						)						).f_namemax
								super().__init__(lowerCamelCase,						timeout=lowerCamelCase,						max_filename_length=lowerCamelCase						)




				def        lowercase__     (  self		:					Optional[Any]						):

								'''simple docstring'''
								lowercase__     				=    os.O_RDWR | os.O_CREAT | os.O_TRUNC
								lowercase__     				=    os.open(self._lock_file,						lowerCamelCase						)

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




				def        lowercase__     (  self		:					int						):

								'''simple docstring'''
								# Do not remove the lockfile:
								#
								#   https://github.com/benediktschmitt/py-filelock/issues/31
								#   https://stackoverflow.com/questions/17708885/flock-removing-locked-file-without-race-condition
								lowercase__     				=    self._lock_file_fd
								lowercase__     				=    None
								fcntl.flock(lowerCamelCase,						fcntl.LOCK_UN						)
								os.close(lowerCamelCase						)
								return None



class    _UpperCAmelCase      ( A__			):

				"""simple docstring"""




				def        lowercase__     (  self		:					Any						):

								'''simple docstring'''
								lowercase__     				=    os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_TRUNC
								try:
												lowercase__     				=    os.open(self._lock_file,						lowerCamelCase						)
								except OSError:
												pass
								else:
												lowercase__     				=    fd
								return None




				def        lowercase__     (  self		:					List[Any]						):

								'''simple docstring'''
								os.close(self._lock_file_fd						)
								lowercase__     				=    None

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



A__			:    str         =				None

if msvcrt:
							A__			:    Union[str, Any]         =				WindowsFileLock
elif fcntl:
							A__			:    List[Any]         =				UnixFileLock
else:
							A__			:    Dict         =				SoftFileLock

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












import json
import os
import tempfile

import transformers

import datasets
from utils import generate_example_dataset, get_duration


A__			:    Dict         =				50_00_00

A__     ,     A__			:    str         =				os.path.split(__file__)
A__			:    Optional[Any]         =				os.path.join(RESULTS_BASEPATH, 'results', RESULTS_FILENAME.replace('.py', '.json'))






@get_duration
def 						a    (		lowerCamelCase_	,     **lowerCamelCase_       ):






				'''simple docstring'''
				lowercase__     				=    dataset.map(**lowerCamelCase_       )






@get_duration
def 						a    (		lowerCamelCase_	,     **lowerCamelCase_       ):






				'''simple docstring'''
				lowercase__     				=    dataset.filter(**lowerCamelCase_       )






def 						a    (		):






				'''simple docstring'''
				lowercase__     				=    {'''num examples''': SPEED_TEST_N_EXAMPLES}
				with tempfile.TemporaryDirectory() as tmp_dir:
								lowercase__     				=    datasets.Features({'''text''': datasets.Value('''string'''       ), '''numbers''': datasets.Value('''float32'''       )}       )
								lowercase__     				=    generate_example_dataset(
								    os.path.join(lowerCamelCase_	,     '''dataset.arrow'''       )	,     lowerCamelCase_	,     num_examples=lowerCamelCase_       )

								lowercase__     				=    transformers.AutoTokenizer.from_pretrained('''bert-base-cased'''	,     use_fast=lowerCamelCase_       )

								def tokenize(lowerCamelCase_       ):
												return tokenizer(examples['''text''']       )

								lowercase__     				=    map(lowerCamelCase_       )

								lowercase__     				=    map(lowerCamelCase_	,     batched=lowerCamelCase_       )

								lowercase__     				=    map(lowerCamelCase_	,     function=lambda lowerCamelCase_       : None	,     batched=lowerCamelCase_       )

								with dataset.formatted_as(type='''numpy'''       ):
												lowercase__     				=    map(lowerCamelCase_	,     function=lambda lowerCamelCase_       : None	,     batched=lowerCamelCase_       )

								with dataset.formatted_as(type='''pandas'''       ):
												lowercase__     				=    map(lowerCamelCase_	,     function=lambda lowerCamelCase_       : None	,     batched=lowerCamelCase_       )

								with dataset.formatted_as(type='''torch'''	,     columns='''numbers'''       ):
												lowercase__     				=    map(lowerCamelCase_	,     function=lambda lowerCamelCase_       : None	,     batched=lowerCamelCase_       )

								with dataset.formatted_as(type='''tensorflow'''	,     columns='''numbers'''       ):
												lowercase__     				=    map(lowerCamelCase_	,     function=lambda lowerCamelCase_       : None	,     batched=lowerCamelCase_       )

								lowercase__     				=    map(lowerCamelCase_	,     function=lowerCamelCase_	,     batched=lowerCamelCase_       )

								lowercase__     				=    filter(lowerCamelCase_       )

								# Activate later when tokenizer support batched inputs
								# with dataset.formatted_as(type='numpy'):
								#     times[func.__name__ + " fast-tokenizer batched numpy"] = func(dataset, function=tokenize, batched=True)

				with open(lowerCamelCase_	,     '''wb'''       ) as f:
								f.write(json.dumps(lowerCamelCase_       ).encode('''utf-8'''       )       )


if __name__ == "__main__":  # useful to run the profiler
							benchmark_map_filter()




