





"""simple docstring"""
import json
import os
import pickle
import subprocess
from hashlib import mda
from pathlib import Path
from tempfile import gettempdir
from textwrap import dedent
from types import CodeType, FunctionType
from unittest import TestCase
from unittest.mock import patch

import pytest
from multiprocess import Pool
from packaging import version

import datasets
from datasets.fingerprint import Hasher, fingerprint_transform
from datasets.table import InMemoryTable

from .utils import (
    require_regex,
    require_spacy,
    require_spacy_model,
    require_tiktoken,
    require_torch,
    require_transformers,
)






class    _UpperCAmelCase  :


					def __init__(   self     :Union[str, Any]	,			__UpperCamelCase     :Any						):
												A						=    foo


					def __call__(   self     :int						):
												return self.foo







class    _UpperCAmelCase  (      datasets.Dataset					):


					@fingerprint_transform(inplace=__UpperCamelCase						)
					def       lowerCamelCase			(   self     :List[Any]	,			__UpperCamelCase     :Tuple	,			*__UpperCamelCase     :Any	,			**__UpperCamelCase     :List[str]						):
												return DatasetChild(self.data	,			fingerprint=__UpperCamelCase						)


					@fingerprint_transform(inplace=__UpperCamelCase						)
					def       lowerCamelCase			(   self     :Any	,			__UpperCamelCase     :str	,			*__UpperCamelCase     :Optional[int]	,			**__UpperCamelCase     :Optional[Any]						):
												return DatasetChild(self.data	,			fingerprint=__UpperCamelCase						)







class    _UpperCAmelCase  :


					def __init__(   self     :List[Any]	,			__UpperCamelCase     :Optional[int]						):
												A						=    callable


					def __call__(   self     :Optional[int]	,			*__UpperCamelCase     :Optional[Any]	,			**__UpperCamelCase     :List[Any]						):
												if self.callable is not None:
																			return self.callable(*__UpperCamelCase	,			**__UpperCamelCase						)


					def __getstate__(   self     :Tuple						):
												raise pickle.PicklingError()







class    _UpperCAmelCase  (      lowercase_					):


					@require_transformers
					@pytest.mark.integration
					def       lowerCamelCase			(   self     :Any						):
												from transformers import AutoTokenizer

												def encode(__UpperCamelCase     :List[Any]						):
																			return tokenizer(__UpperCamelCase						)

												# TODO: add hash consistency tests across sessions
												A						=    AutoTokenizer.from_pretrained("bert-base-uncased"						)
												A						=    mda(datasets.utils.py_utils.dumps(__UpperCamelCase						)						).hexdigest()
												A						=    mda(datasets.utils.py_utils.dumps(lambda __UpperCamelCase						: tokenizer(__UpperCamelCase						)						)						).hexdigest()
												A						=    mda(datasets.utils.py_utils.dumps(__UpperCamelCase						)						).hexdigest()
												A						=    AutoTokenizer.from_pretrained("bert-base-cased"						)
												A						=    mda(datasets.utils.py_utils.dumps(__UpperCamelCase						)						).hexdigest()
												A						=    mda(datasets.utils.py_utils.dumps(lambda __UpperCamelCase						: tokenizer(__UpperCamelCase						)						)						).hexdigest()
												A						=    mda(datasets.utils.py_utils.dumps(__UpperCamelCase						)						).hexdigest()
												A						=    AutoTokenizer.from_pretrained("bert-base-uncased"						)
												A						=    mda(datasets.utils.py_utils.dumps(__UpperCamelCase						)						).hexdigest()
												A						=    mda(datasets.utils.py_utils.dumps(lambda __UpperCamelCase						: tokenizer(__UpperCamelCase						)						)						).hexdigest()
												A						=    mda(datasets.utils.py_utils.dumps(__UpperCamelCase						)						).hexdigest()
												self.assertEqual(__UpperCamelCase	,			__UpperCamelCase						)
												self.assertNotEqual(__UpperCamelCase	,			__UpperCamelCase						)
												self.assertEqual(__UpperCamelCase	,			__UpperCamelCase						)
												self.assertNotEqual(__UpperCamelCase	,			__UpperCamelCase						)
												self.assertEqual(__UpperCamelCase	,			__UpperCamelCase						)
												self.assertNotEqual(__UpperCamelCase	,			__UpperCamelCase						)


					@require_transformers
					@pytest.mark.integration
					def       lowerCamelCase			(   self     :Dict						):
												from transformers import AutoTokenizer

												A						=    AutoTokenizer.from_pretrained("gpt2"						)
												A						=    mda(datasets.utils.py_utils.dumps(__UpperCamelCase						)						).hexdigest()
												tokenizer("Hello world !"						)  # call once to change the tokenizer's cache
												A						=    mda(datasets.utils.py_utils.dumps(__UpperCamelCase						)						).hexdigest()
												self.assertEqual(__UpperCamelCase	,			__UpperCamelCase						)


					@require_regex
					def       lowerCamelCase			(   self     :Union[str, Any]						):
												import regex

												A						=    regex.Regex("foo"						)
												A						=    mda(datasets.utils.py_utils.dumps(__UpperCamelCase						)						).hexdigest()
												A						=    regex.Regex("bar"						)
												A						=    mda(datasets.utils.py_utils.dumps(__UpperCamelCase						)						).hexdigest()
												A						=    regex.Regex("foo"						)
												A						=    mda(datasets.utils.py_utils.dumps(__UpperCamelCase						)						).hexdigest()
												self.assertEqual(__UpperCamelCase	,			__UpperCamelCase						)
												self.assertNotEqual(__UpperCamelCase	,			__UpperCamelCase						)







class    _UpperCAmelCase  (      lowercase_					):


					def       lowerCamelCase			(   self     :Any						):
												def func():
																			return foo

												A						=    [0]
												A						=    mda(datasets.utils.py_utils.dumps(__UpperCamelCase						)						).hexdigest()
												A						=    [1]
												A						=    mda(datasets.utils.py_utils.dumps(__UpperCamelCase						)						).hexdigest()
												A						=    [0]
												A						=    mda(datasets.utils.py_utils.dumps(__UpperCamelCase						)						).hexdigest()
												self.assertEqual(__UpperCamelCase	,			__UpperCamelCase						)
												self.assertNotEqual(__UpperCamelCase	,			__UpperCamelCase						)


					def       lowerCamelCase			(   self     :str						):
												def func():
																			pass

												A						=    mda(datasets.utils.py_utils.dumps(__UpperCamelCase						)						).hexdigest()

												def func():
																			pass

												A						=    mda(datasets.utils.py_utils.dumps(__UpperCamelCase						)						).hexdigest()
												self.assertEqual(__UpperCamelCase	,			__UpperCamelCase						)


					def       lowerCamelCase			(   self     :Any						):
												A						=    mda(datasets.utils.py_utils.dumps(Foo([0]						)						)						).hexdigest()
												A						=    mda(datasets.utils.py_utils.dumps(Foo([1]						)						)						).hexdigest()
												A						=    mda(datasets.utils.py_utils.dumps(Foo([0]						)						)						).hexdigest()
												self.assertEqual(__UpperCamelCase	,			__UpperCamelCase						)
												self.assertNotEqual(__UpperCamelCase	,			__UpperCamelCase						)


					def       lowerCamelCase			(   self     :Dict						):
												A						=    mda(datasets.utils.py_utils.dumps(Foo([0]						).__call__						)						).hexdigest()
												A						=    mda(datasets.utils.py_utils.dumps(Foo([1]						).__call__						)						).hexdigest()
												A						=    mda(datasets.utils.py_utils.dumps(Foo([0]						).__call__						)						).hexdigest()
												self.assertEqual(__UpperCamelCase	,			__UpperCamelCase						)
												self.assertNotEqual(__UpperCamelCase	,			__UpperCamelCase						)


					def       lowerCamelCase			(   self     :Optional[Any]						):
												A						=    (
												    "co_argcount",
												    "co_kwonlyargcount",
												    "co_nlocals",
												    "co_stacksize",
												    "co_flags",
												    "co_code",
												    "co_consts",
												    "co_names",
												    "co_varnames",
												    "co_filename",
												    "co_name",
												    "co_firstlineno",
												    "co_lnotab",
												    "co_freevars",
												    "co_cellvars",
												)

												def _create_code_pyaa(*__UpperCamelCase     :Any						):
																			if hasattr(args[-3]	,			"encode"						):
																										A						=    list(__UpperCamelCase						)
																										A						=    args[-3].encode()  # co_lnotab
																										A						=    args[-10].encode()  # co_code
																			if hasattr(__UpperCamelCase	,			"co_posonlyargcount"						):
																										if len(__UpperCamelCase						) == 16:
																																	return CodeType(*__UpperCamelCase						)
																										elif len(__UpperCamelCase						) == 15:
																																	return CodeType(args[0]	,			0	,			*args[1:]						)
																										return CodeType(args[0]	,			0	,			0	,			*args[1:]						)
																			elif hasattr(__UpperCamelCase	,			"co_kwonlyargcount"						):
																										if len(__UpperCamelCase						) == 16:
																																	return CodeType(args[0]	,			*args[2:]						)
																										elif len(__UpperCamelCase						) == 15:
																																	return CodeType(*__UpperCamelCase						)
																										return CodeType(args[0]	,			0	,			*args[1:]						)
																			if len(__UpperCamelCase						) == 16:
																										return CodeType(args[0]	,			*args[3:]						)
																			elif len(__UpperCamelCase						) == 15:
																										return CodeType(args[0]	,			*args[2:]						)
																			return CodeType(*__UpperCamelCase						)

												def create_ipython_func(__UpperCamelCase     :List[str]	,			__UpperCamelCase     :Any						):
																			def func():
																										return returned_obj

																			A						=    func.__code__
																			# Use _create_code from dill in order to make it work for different python versions
																			if datasets.config.PY_VERSION < version.parse("3.8"						):
																										A						=    _create_code_pyaa(
																										    *[getattr(__UpperCamelCase	,			__UpperCamelCase						) if k != "co_filename" else co_filename for k in code_args_pyaa]						)
																			else:
																										A						=    code.replace(co_filename=__UpperCamelCase						)
																			return FunctionType(__UpperCamelCase	,			func.__globals__	,			func.__name__	,			func.__defaults__	,			func.__closure__						)

												A,      A						=    "<ipython-input-2-e0383a102aae>", [0]
												A						=    mda(datasets.utils.py_utils.dumps(create_ipython_func(__UpperCamelCase	,			__UpperCamelCase						)						)						).hexdigest()
												A,      A						=    "<ipython-input-2-e0383a102aae>", [1]
												A						=    mda(datasets.utils.py_utils.dumps(create_ipython_func(__UpperCamelCase	,			__UpperCamelCase						)						)						).hexdigest()
												A,      A						=    "<ipython-input-5-713f6613acf3>", [0]
												A						=    mda(datasets.utils.py_utils.dumps(create_ipython_func(__UpperCamelCase	,			__UpperCamelCase						)						)						).hexdigest()
												self.assertEqual(__UpperCamelCase	,			__UpperCamelCase						)
												self.assertNotEqual(__UpperCamelCase	,			__UpperCamelCase						)

												A,      A						=    os.path.join(gettempdir()	,			"ipykernel_12345"	,			"321456789.py"						), [0]
												A						=    mda(datasets.utils.py_utils.dumps(create_ipython_func(__UpperCamelCase	,			__UpperCamelCase						)						)						).hexdigest()
												A,      A						=    os.path.join(gettempdir()	,			"ipykernel_12345"	,			"321456789.py"						), [1]
												A						=    mda(datasets.utils.py_utils.dumps(create_ipython_func(__UpperCamelCase	,			__UpperCamelCase						)						)						).hexdigest()
												A,      A						=    os.path.join(gettempdir()	,			"ipykernel_12345"	,			"654123987.py"						), [0]
												A						=    mda(datasets.utils.py_utils.dumps(create_ipython_func(__UpperCamelCase	,			__UpperCamelCase						)						)						).hexdigest()
												self.assertEqual(__UpperCamelCase	,			__UpperCamelCase						)
												self.assertNotEqual(__UpperCamelCase	,			__UpperCamelCase						)


					def       lowerCamelCase			(   self     :Union[str, Any]						):
												A,      A						=    [0], [1]

												def func():
																			return foo, bar

												A						=    "__main__"

												def globalvars_mocka_side_effect(__UpperCamelCase     :Optional[Any]	,			*__UpperCamelCase     :str	,			**__UpperCamelCase     :List[str]						):
																			return {"foo": foo, "bar": bar}

												def globalvars_mocka_side_effect(__UpperCamelCase     :Optional[Any]	,			*__UpperCamelCase     :str	,			**__UpperCamelCase     :Any						):
																			return {"bar": bar, "foo": foo}

												with patch("dill.detect.globalvars"	,			side_effect=__UpperCamelCase						) as globalvars_mocka:
																			A						=    mda(datasets.utils.py_utils.dumps(__UpperCamelCase						)						).hexdigest()
																			self.assertGreater(globalvars_mocka.call_count	,			0						)
												with patch("dill.detect.globalvars"	,			side_effect=__UpperCamelCase						) as globalvars_mocka:
																			A						=    mda(datasets.utils.py_utils.dumps(__UpperCamelCase						)						).hexdigest()
																			self.assertGreater(globalvars_mocka.call_count	,			0						)
												self.assertEqual(__UpperCamelCase	,			__UpperCamelCase						)







class    _UpperCAmelCase  (      lowercase_					):


					def       lowerCamelCase			(   self     :Optional[int]						):
												A						=    Hasher.hash("hello"						)
												A						=    Hasher.hash("hello"						)
												A						=    Hasher.hash("there"						)
												self.assertEqual(__UpperCamelCase	,			__UpperCamelCase						)
												self.assertNotEqual(__UpperCamelCase	,			__UpperCamelCase						)


					def       lowerCamelCase			(   self     :List[Any]						):
												A						=    Hasher.hash(Foo("hello"						)						)
												A						=    Hasher.hash(Foo("hello"						)						)
												A						=    Hasher.hash(Foo("there"						)						)
												self.assertEqual(__UpperCamelCase	,			__UpperCamelCase						)
												self.assertNotEqual(__UpperCamelCase	,			__UpperCamelCase						)


					def       lowerCamelCase			(   self     :int						):
												A						=    Hasher()
												for x in ["hello", Foo("hello"						)]:
																			hasher.update(__UpperCamelCase						)
												A						=    hasher.hexdigest()
												A						=    Hasher()
												for x in ["hello", Foo("hello"						)]:
																			hasher.update(__UpperCamelCase						)
												A						=    hasher.hexdigest()
												A						=    Hasher()
												for x in ["there", Foo("there"						)]:
																			hasher.update(__UpperCamelCase						)
												A						=    hasher.hexdigest()
												self.assertEqual(__UpperCamelCase	,			__UpperCamelCase						)
												self.assertNotEqual(__UpperCamelCase	,			__UpperCamelCase						)


					def       lowerCamelCase			(   self     :Optional[int]						):
												with self.assertRaises(pickle.PicklingError						):
																			Hasher.hash(UnpicklableCallable(Foo("hello"						)						)						)


					def       lowerCamelCase			(   self     :str						):
												A						=    "abc"
												A						=    [string, string]  # two strings have the same ids
												A						=    [string, string]
												A						=    json.loads(f"[\"{string}\", \"{string}\"]"						)  # two strings have different ids
												self.assertIs(obja[0]	,			__UpperCamelCase						)
												self.assertIs(obja[0]	,			obja[1]						)
												self.assertIs(obja[0]	,			__UpperCamelCase						)
												self.assertIs(obja[0]	,			obja[1]						)
												self.assertIsNot(obja[0]	,			__UpperCamelCase						)
												self.assertIsNot(obja[0]	,			obja[1]						)
												A						=    Hasher.hash(__UpperCamelCase						)
												A						=    Hasher.hash(__UpperCamelCase						)
												A						=    Hasher.hash(__UpperCamelCase						)
												self.assertEqual(__UpperCamelCase	,			__UpperCamelCase						)
												self.assertEqual(__UpperCamelCase	,			__UpperCamelCase						)


					@require_tiktoken
					def       lowerCamelCase			(   self     :str						):
												import tiktoken

												A						=    tiktoken.get_encoding("gpt2"						)
												A						=    mda(datasets.utils.py_utils.dumps(__UpperCamelCase						)						).hexdigest()
												A						=    tiktoken.get_encoding("r50k_base"						)
												A						=    mda(datasets.utils.py_utils.dumps(__UpperCamelCase						)						).hexdigest()
												A						=    tiktoken.get_encoding("gpt2"						)
												A						=    mda(datasets.utils.py_utils.dumps(__UpperCamelCase						)						).hexdigest()
												self.assertEqual(__UpperCamelCase	,			__UpperCamelCase						)
												self.assertNotEqual(__UpperCamelCase	,			__UpperCamelCase						)


					@require_torch
					def       lowerCamelCase			(   self     :List[Any]						):
												import torch

												A						=    torch.tensor([1.0]						)
												A						=    mda(datasets.utils.py_utils.dumps(__UpperCamelCase						)						).hexdigest()
												A						=    torch.tensor([2.0]						)
												A						=    mda(datasets.utils.py_utils.dumps(__UpperCamelCase						)						).hexdigest()
												A						=    torch.tensor([1.0]						)
												A						=    mda(datasets.utils.py_utils.dumps(__UpperCamelCase						)						).hexdigest()
												self.assertEqual(__UpperCamelCase	,			__UpperCamelCase						)
												self.assertNotEqual(__UpperCamelCase	,			__UpperCamelCase						)


					@require_spacy
					@require_spacy_model("en_core_web_sm"						)
					@require_spacy_model("fr_core_news_sm"						)
					@pytest.mark.integration
					def       lowerCamelCase			(   self     :int						):
												import spacy

												A						=    spacy.load("en_core_web_sm"						)
												A						=    mda(datasets.utils.py_utils.dumps(__UpperCamelCase						)						).hexdigest()
												A						=    spacy.load("fr_core_news_sm"						)
												A						=    mda(datasets.utils.py_utils.dumps(__UpperCamelCase						)						).hexdigest()
												A						=    spacy.load("en_core_web_sm"						)
												A						=    mda(datasets.utils.py_utils.dumps(__UpperCamelCase						)						).hexdigest()
												self.assertEqual(__UpperCamelCase	,			__UpperCamelCase						)
												self.assertNotEqual(__UpperCamelCase	,			__UpperCamelCase						)







@pytest.mark.integration
def    A__							(     UpperCamelCase			):
							A						=    tmp_path / "dir1"
							A						=    tmp_path / "dir2"
							dira.mkdir()
							dira.mkdir()
							A						=    "script.py"
							A						=    dedent(
							    "\n    from datasets.fingerprint import Hasher\n    def foo():\n        pass\n    print(Hasher.hash(foo))\n    "			)
							A						=    dira / script_filename
							A						=    dira / script_filename
							with script_patha.open("w"			) as f:
														f.write(UpperCamelCase			)
							with script_patha.open("w"			) as f:
														f.write(UpperCamelCase			)
							A						=    subprocess.check_output(["python", str(UpperCamelCase			)]			)
							A						=    subprocess.check_output(["python", str(UpperCamelCase			)]			)
							assert fingerprinta == fingerprinta






def    A__							(     ):
							A						=    {"a": [0, 1, 2]}
							A						=    DatasetChild(InMemoryTable.from_pydict(UpperCamelCase			)			)
							A						=    dataset.funca()._fingerprint
							assert expected_fingerprint == dataset.funca()._fingerprint
							assert expected_fingerprint != dataset.funca()._fingerprint

							with Pool(2			) as p:
														assert expected_fingerprint == p.apply_async(dataset.funca			).get()._fingerprint
														assert expected_fingerprint != p.apply_async(dataset.funca			).get()._fingerprint






def    A__							(     ):
							A						=    {"a": [0, 1, 2]}






							class    _UpperCAmelCase  (      datasets.Dataset					):


												@fingerprint_transform(inplace=__UpperCamelCase						)
												def       lowerCamelCase			(   self     :Tuple	,			__UpperCamelCase     :List[Any]						):
																			return DummyDatasetChild(self.data	,			fingerprint=__UpperCamelCase						)







							A						=    DummyDatasetChild(InMemoryTable.from_pydict(UpperCamelCase			)			).func()






							class    _UpperCAmelCase  (      datasets.Dataset					):


												@fingerprint_transform(inplace=__UpperCamelCase	,			version="1.0.0"						)
												def       lowerCamelCase			(   self     :int	,			__UpperCamelCase     :int						):
																			return DummyDatasetChild(self.data	,			fingerprint=__UpperCamelCase						)







							A						=    DummyDatasetChild(InMemoryTable.from_pydict(UpperCamelCase			)			).func()






							class    _UpperCAmelCase  (      datasets.Dataset					):


												@fingerprint_transform(inplace=__UpperCamelCase	,			version="2.0.0"						)
												def       lowerCamelCase			(   self     :Dict	,			__UpperCamelCase     :Any						):
																			return DummyDatasetChild(self.data	,			fingerprint=__UpperCamelCase						)







							A						=    DummyDatasetChild(InMemoryTable.from_pydict(UpperCamelCase			)			).func()

							assert len({fingeprint_no_version, fingeprint_a, fingeprint_a}			) == 3






def    A__							(     ):
							# AttributeError: module 'dill._dill' has no attribute 'stack'
							A						=    Hasher()
							hasher.update(lambda UpperCamelCase			: x			)









"""simple docstring"""
from functools import lru_cache






@lru_cache
def    A__							(     UpperCamelCase			):
							if not isinstance(UpperCamelCase   ,    UpperCamelCase			):
														raise TypeError("k must be an integer."			)
							if k < 0:
														raise ValueError("k integer must be greater or equal to zero."			)
							if k == 0:
														return 0
							elif k == 1:
														return 1
							else:
														return fibonacci(k - 1			) + fibonacci(k - 2			)






def    A__							(     UpperCamelCase   ,    UpperCamelCase			):
							A						=    len(UpperCamelCase			)
							# Find m such that F_m >= n where F_i is the i_th fibonacci number.
							A						=    0
							while True:
														if fibonacci(UpperCamelCase			) >= len_list:
																					A						=    i
																					break
														i += 1
							A						=    0
							while fibb_k > 0:
														A						=    min(
														    offset + fibonacci(fibb_k - 1			)   ,    len_list - 1			)  # Prevent out of range
														A						=    arr[index_k]
														if item_k_1 == val:
																					return index_k
														elif val < item_k_1:
																					fibb_k -= 1
														elif val > item_k_1:
																					offset += fibonacci(fibb_k - 1			)
																					fibb_k -= 2
							else:
														return -1


if __name__ == "__main__":
		import doctest

		doctest.testmod()


