

"""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 ,						_A       )      ->      Any:






				'''simple docstring'''







				_UpperCAmelCase      :							Optional[int]       =   foo
			def __call__(						self       )      ->      Any:






				'''simple docstring'''







				return self.foo
class       _UpperCAmelCase     (						datasets.Dataset):
			@fingerprint_transform(inplace=_A       )
			def 							__snake_case	(						self ,						_A ,						*_A ,						**_A       )      ->      Tuple:






				'''simple docstring'''







				return DatasetChild(self.data ,						fingerprint=_A       )
			@fingerprint_transform(inplace=_A       )
			def 							__snake_case	(						self ,						_A ,						*_A ,						**_A       )      ->      Optional[Any]:






				'''simple docstring'''







				return DatasetChild(self.data ,						fingerprint=_A       )
class       _UpperCAmelCase     :
			def __init__(						self ,						_A       )      ->      str:






				'''simple docstring'''







				_UpperCAmelCase      :							str       =   callable
			def __call__(						self ,						*_A ,						**_A       )      ->      Dict:






				'''simple docstring'''







				if self.callable is not None:
					return self.callable(*_A ,						**_A       )
			def __getstate__(						self       )      ->      List[str]:






				'''simple docstring'''







				raise pickle.PicklingError()
class       _UpperCAmelCase     (						lowercase_):
			@require_transformers
			@pytest.mark.integration
			def 							__snake_case	(						self       )      ->      Any:






				'''simple docstring'''







				from transformers import AutoTokenizer

				def encode(_A       ):
					return tokenizer(_A       )

				# TODO: add hash consistency tests across sessions
				_UpperCAmelCase      :							Tuple       =   AutoTokenizer.from_pretrained("""bert-base-uncased"""       )
				_UpperCAmelCase      :							Dict       =   mda(datasets.utils.py_utils.dumps(_A       )       ).hexdigest()
				_UpperCAmelCase      :							Any       =   mda(datasets.utils.py_utils.dumps(lambda _A       : tokenizer(_A       )       )       ).hexdigest()
				_UpperCAmelCase      :							Any       =   mda(datasets.utils.py_utils.dumps(_A       )       ).hexdigest()
				_UpperCAmelCase      :							Tuple       =   AutoTokenizer.from_pretrained("""bert-base-cased"""       )
				_UpperCAmelCase      :							Tuple       =   mda(datasets.utils.py_utils.dumps(_A       )       ).hexdigest()
				_UpperCAmelCase      :							List[str]       =   mda(datasets.utils.py_utils.dumps(lambda _A       : tokenizer(_A       )       )       ).hexdigest()
				_UpperCAmelCase      :							List[str]       =   mda(datasets.utils.py_utils.dumps(_A       )       ).hexdigest()
				_UpperCAmelCase      :							int       =   AutoTokenizer.from_pretrained("""bert-base-uncased"""       )
				_UpperCAmelCase      :							List[Any]       =   mda(datasets.utils.py_utils.dumps(_A       )       ).hexdigest()
				_UpperCAmelCase      :							Dict       =   mda(datasets.utils.py_utils.dumps(lambda _A       : tokenizer(_A       )       )       ).hexdigest()
				_UpperCAmelCase      :							Optional[int]       =   mda(datasets.utils.py_utils.dumps(_A       )       ).hexdigest()
				self.assertEqual(_A ,						_A       )
				self.assertNotEqual(_A ,						_A       )
				self.assertEqual(_A ,						_A       )
				self.assertNotEqual(_A ,						_A       )
				self.assertEqual(_A ,						_A       )
				self.assertNotEqual(_A ,						_A       )
			@require_transformers
			@pytest.mark.integration
			def 							__snake_case	(						self       )      ->      str:






				'''simple docstring'''







				from transformers import AutoTokenizer

				_UpperCAmelCase      :							List[Any]       =   AutoTokenizer.from_pretrained("""gpt2"""       )
				_UpperCAmelCase      :							Union[str, Any]       =   mda(datasets.utils.py_utils.dumps(_A       )       ).hexdigest()
				tokenizer("""Hello world !"""       )  # call once to change the tokenizer's cache
				_UpperCAmelCase      :							Optional[int]       =   mda(datasets.utils.py_utils.dumps(_A       )       ).hexdigest()
				self.assertEqual(_A ,						_A       )
			@require_regex
			def 							__snake_case	(						self       )      ->      Dict:






				'''simple docstring'''







				import regex

				_UpperCAmelCase      :							Tuple       =   regex.Regex("""foo"""       )
				_UpperCAmelCase      :							Any       =   mda(datasets.utils.py_utils.dumps(_A       )       ).hexdigest()
				_UpperCAmelCase      :							Optional[Any]       =   regex.Regex("""bar"""       )
				_UpperCAmelCase      :							Any       =   mda(datasets.utils.py_utils.dumps(_A       )       ).hexdigest()
				_UpperCAmelCase      :							Optional[int]       =   regex.Regex("""foo"""       )
				_UpperCAmelCase      :							List[Any]       =   mda(datasets.utils.py_utils.dumps(_A       )       ).hexdigest()
				self.assertEqual(_A ,						_A       )
				self.assertNotEqual(_A ,						_A       )
class       _UpperCAmelCase     (						lowercase_):
			def 							__snake_case	(						self       )      ->      Optional[Any]:






				'''simple docstring'''







				def func():
					return foo

				_UpperCAmelCase      :							Dict       =   [0]
				_UpperCAmelCase      :							List[Any]       =   mda(datasets.utils.py_utils.dumps(_A       )       ).hexdigest()
				_UpperCAmelCase      :							Union[str, Any]       =   [1]
				_UpperCAmelCase      :							Any       =   mda(datasets.utils.py_utils.dumps(_A       )       ).hexdigest()
				_UpperCAmelCase      :							Optional[Any]       =   [0]
				_UpperCAmelCase      :							List[str]       =   mda(datasets.utils.py_utils.dumps(_A       )       ).hexdigest()
				self.assertEqual(_A ,						_A       )
				self.assertNotEqual(_A ,						_A       )
			def 							__snake_case	(						self       )      ->      Tuple:






				'''simple docstring'''







				def func():
					pass

				_UpperCAmelCase      :							Optional[int]       =   mda(datasets.utils.py_utils.dumps(_A       )       ).hexdigest()

				def func():
					pass

				_UpperCAmelCase      :							str       =   mda(datasets.utils.py_utils.dumps(_A       )       ).hexdigest()
				self.assertEqual(_A ,						_A       )
			def 							__snake_case	(						self       )      ->      Optional[int]:






				'''simple docstring'''







				_UpperCAmelCase      :							List[str]       =   mda(datasets.utils.py_utils.dumps(Foo([0]       )       )       ).hexdigest()
				_UpperCAmelCase      :							Union[str, Any]       =   mda(datasets.utils.py_utils.dumps(Foo([1]       )       )       ).hexdigest()
				_UpperCAmelCase      :							List[str]       =   mda(datasets.utils.py_utils.dumps(Foo([0]       )       )       ).hexdigest()
				self.assertEqual(_A ,						_A       )
				self.assertNotEqual(_A ,						_A       )
			def 							__snake_case	(						self       )      ->      int:






				'''simple docstring'''







				_UpperCAmelCase      :							Any       =   mda(datasets.utils.py_utils.dumps(Foo([0]       ).__call__       )       ).hexdigest()
				_UpperCAmelCase      :							Tuple       =   mda(datasets.utils.py_utils.dumps(Foo([1]       ).__call__       )       ).hexdigest()
				_UpperCAmelCase      :							Union[str, Any]       =   mda(datasets.utils.py_utils.dumps(Foo([0]       ).__call__       )       ).hexdigest()
				self.assertEqual(_A ,						_A       )
				self.assertNotEqual(_A ,						_A       )
			def 							__snake_case	(						self       )      ->      Tuple:






				'''simple docstring'''







				_UpperCAmelCase      :							Union[str, Any]       =   (
				    """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(*_A       ):
					if hasattr(args[-3] ,						"""encode"""       ):
						_UpperCAmelCase      :							Any       =   list(_A       )
						_UpperCAmelCase      :							Any       =   args[-3].encode()  # co_lnotab
						_UpperCAmelCase      :							Union[str, Any]       =   args[-10].encode()  # co_code
					if hasattr(_A ,						"""co_posonlyargcount"""       ):
						if len(_A       ) == 16:
							return CodeType(*_A       )
						elif len(_A       ) == 15:
							return CodeType(args[0] ,						0 ,						*args[1:]       )
						return CodeType(args[0] ,						0 ,						0 ,						*args[1:]       )
					elif hasattr(_A ,						"""co_kwonlyargcount"""       ):
						if len(_A       ) == 16:
							return CodeType(args[0] ,						*args[2:]       )
						elif len(_A       ) == 15:
							return CodeType(*_A       )
						return CodeType(args[0] ,						0 ,						*args[1:]       )
					if len(_A       ) == 16:
						return CodeType(args[0] ,						*args[3:]       )
					elif len(_A       ) == 15:
						return CodeType(args[0] ,						*args[2:]       )
					return CodeType(*_A       )

				def create_ipython_func(_A ,						_A       ):
					def func():
						return returned_obj

					_UpperCAmelCase      :							Tuple       =   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"""       ):
						_UpperCAmelCase      :							Union[str, Any]       =   _create_code_pyaa(
						    *[getattr(_A ,						_A       ) if k != """co_filename""" else co_filename for k in code_args_pyaa]       )
					else:
						_UpperCAmelCase      :							Dict       =   code.replace(co_filename=_A       )
					return FunctionType(_A ,						func.__globals__ ,						func.__name__ ,						func.__defaults__ ,						func.__closure__       )

				_UpperCAmelCase  ,				_UpperCAmelCase      :							Tuple       =   """<ipython-input-2-e0383a102aae>""", [0]
				_UpperCAmelCase      :							int       =   mda(datasets.utils.py_utils.dumps(create_ipython_func(_A ,						_A       )       )       ).hexdigest()
				_UpperCAmelCase  ,				_UpperCAmelCase      :							Any       =   """<ipython-input-2-e0383a102aae>""", [1]
				_UpperCAmelCase      :							List[Any]       =   mda(datasets.utils.py_utils.dumps(create_ipython_func(_A ,						_A       )       )       ).hexdigest()
				_UpperCAmelCase  ,				_UpperCAmelCase      :							str       =   """<ipython-input-5-713f6613acf3>""", [0]
				_UpperCAmelCase      :							List[Any]       =   mda(datasets.utils.py_utils.dumps(create_ipython_func(_A ,						_A       )       )       ).hexdigest()
				self.assertEqual(_A ,						_A       )
				self.assertNotEqual(_A ,						_A       )

				_UpperCAmelCase  ,				_UpperCAmelCase      :							Tuple       =   os.path.join(gettempdir() ,						"""ipykernel_12345""" ,						"""321456789.py"""       ), [0]
				_UpperCAmelCase      :							Any       =   mda(datasets.utils.py_utils.dumps(create_ipython_func(_A ,						_A       )       )       ).hexdigest()
				_UpperCAmelCase  ,				_UpperCAmelCase      :							Any       =   os.path.join(gettempdir() ,						"""ipykernel_12345""" ,						"""321456789.py"""       ), [1]
				_UpperCAmelCase      :							Union[str, Any]       =   mda(datasets.utils.py_utils.dumps(create_ipython_func(_A ,						_A       )       )       ).hexdigest()
				_UpperCAmelCase  ,				_UpperCAmelCase      :							Optional[int]       =   os.path.join(gettempdir() ,						"""ipykernel_12345""" ,						"""654123987.py"""       ), [0]
				_UpperCAmelCase      :							Optional[Any]       =   mda(datasets.utils.py_utils.dumps(create_ipython_func(_A ,						_A       )       )       ).hexdigest()
				self.assertEqual(_A ,						_A       )
				self.assertNotEqual(_A ,						_A       )
			def 							__snake_case	(						self       )      ->      List[Any]:






				'''simple docstring'''







				_UpperCAmelCase  ,				_UpperCAmelCase      :							Optional[int]       =   [0], [1]

				def func():
					return foo, bar

				_UpperCAmelCase      :							Tuple       =   """__main__"""

				def globalvars_mocka_side_effect(_A ,						*_A ,						**_A       ):
					return {"foo": foo, "bar": bar}

				def globalvars_mocka_side_effect(_A ,						*_A ,						**_A       ):
					return {"bar": bar, "foo": foo}

				with patch("""dill.detect.globalvars""" ,						side_effect=_A       ) as globalvars_mocka:
					_UpperCAmelCase      :							Any       =   mda(datasets.utils.py_utils.dumps(_A       )       ).hexdigest()
					self.assertGreater(globalvars_mocka.call_count ,						0       )
				with patch("""dill.detect.globalvars""" ,						side_effect=_A       ) as globalvars_mocka:
					_UpperCAmelCase      :							Optional[int]       =   mda(datasets.utils.py_utils.dumps(_A       )       ).hexdigest()
					self.assertGreater(globalvars_mocka.call_count ,						0       )
				self.assertEqual(_A ,						_A       )
class       _UpperCAmelCase     (						lowercase_):
			def 							__snake_case	(						self       )      ->      Any:






				'''simple docstring'''







				_UpperCAmelCase      :							Optional[int]       =   Hasher.hash("""hello"""       )
				_UpperCAmelCase      :							List[Any]       =   Hasher.hash("""hello"""       )
				_UpperCAmelCase      :							Tuple       =   Hasher.hash("""there"""       )
				self.assertEqual(_A ,						_A       )
				self.assertNotEqual(_A ,						_A       )
			def 							__snake_case	(						self       )      ->      int:






				'''simple docstring'''







				_UpperCAmelCase      :							Any       =   Hasher.hash(Foo("""hello"""       )       )
				_UpperCAmelCase      :							Union[str, Any]       =   Hasher.hash(Foo("""hello"""       )       )
				_UpperCAmelCase      :							Optional[Any]       =   Hasher.hash(Foo("""there"""       )       )
				self.assertEqual(_A ,						_A       )
				self.assertNotEqual(_A ,						_A       )
			def 							__snake_case	(						self       )      ->      int:






				'''simple docstring'''







				_UpperCAmelCase      :							Optional[Any]       =   Hasher()
				for x in ["hello", Foo("""hello"""       )]:
					hasher.update(_A       )
				_UpperCAmelCase      :							List[Any]       =   hasher.hexdigest()
				_UpperCAmelCase      :							Optional[int]       =   Hasher()
				for x in ["hello", Foo("""hello"""       )]:
					hasher.update(_A       )
				_UpperCAmelCase      :							int       =   hasher.hexdigest()
				_UpperCAmelCase      :							Any       =   Hasher()
				for x in ["there", Foo("""there"""       )]:
					hasher.update(_A       )
				_UpperCAmelCase      :							int       =   hasher.hexdigest()
				self.assertEqual(_A ,						_A       )
				self.assertNotEqual(_A ,						_A       )
			def 							__snake_case	(						self       )      ->      str:






				'''simple docstring'''







				with self.assertRaises(pickle.PicklingError       ):
					Hasher.hash(UnpicklableCallable(Foo("""hello"""       )       )       )
			def 							__snake_case	(						self       )      ->      Dict:






				'''simple docstring'''







				_UpperCAmelCase      :							Union[str, Any]       =   """abc"""
				_UpperCAmelCase      :							Optional[int]       =   [string, string]  # two strings have the same ids
				_UpperCAmelCase      :							Dict       =   [string, string]
				_UpperCAmelCase      :							str       =   json.loads(f'''[\"{string}\", \"{string}\"]'''       )  # two strings have different ids
				self.assertIs(obja[0] ,						_A       )
				self.assertIs(obja[0] ,						obja[1]       )
				self.assertIs(obja[0] ,						_A       )
				self.assertIs(obja[0] ,						obja[1]       )
				self.assertIsNot(obja[0] ,						_A       )
				self.assertIsNot(obja[0] ,						obja[1]       )
				_UpperCAmelCase      :							List[str]       =   Hasher.hash(_A       )
				_UpperCAmelCase      :							Dict       =   Hasher.hash(_A       )
				_UpperCAmelCase      :							Optional[Any]       =   Hasher.hash(_A       )
				self.assertEqual(_A ,						_A       )
				self.assertEqual(_A ,						_A       )
			@require_tiktoken
			def 							__snake_case	(						self       )      ->      Union[str, Any]:






				'''simple docstring'''







				import tiktoken

				_UpperCAmelCase      :							str       =   tiktoken.get_encoding("""gpt2"""       )
				_UpperCAmelCase      :							Dict       =   mda(datasets.utils.py_utils.dumps(_A       )       ).hexdigest()
				_UpperCAmelCase      :							Optional[int]       =   tiktoken.get_encoding("""r50k_base"""       )
				_UpperCAmelCase      :							Optional[int]       =   mda(datasets.utils.py_utils.dumps(_A       )       ).hexdigest()
				_UpperCAmelCase      :							Dict       =   tiktoken.get_encoding("""gpt2"""       )
				_UpperCAmelCase      :							List[Any]       =   mda(datasets.utils.py_utils.dumps(_A       )       ).hexdigest()
				self.assertEqual(_A ,						_A       )
				self.assertNotEqual(_A ,						_A       )
			@require_torch
			def 							__snake_case	(						self       )      ->      Union[str, Any]:






				'''simple docstring'''







				import torch

				_UpperCAmelCase      :							Dict       =   torch.tensor([1.0]       )
				_UpperCAmelCase      :							str       =   mda(datasets.utils.py_utils.dumps(_A       )       ).hexdigest()
				_UpperCAmelCase      :							str       =   torch.tensor([2.0]       )
				_UpperCAmelCase      :							Union[str, Any]       =   mda(datasets.utils.py_utils.dumps(_A       )       ).hexdigest()
				_UpperCAmelCase      :							int       =   torch.tensor([1.0]       )
				_UpperCAmelCase      :							str       =   mda(datasets.utils.py_utils.dumps(_A       )       ).hexdigest()
				self.assertEqual(_A ,						_A       )
				self.assertNotEqual(_A ,						_A       )
			@require_spacy
			@require_spacy_model("""en_core_web_sm"""       )
			@require_spacy_model("""fr_core_news_sm"""       )
			@pytest.mark.integration
			def 							__snake_case	(						self       )      ->      Dict:






				'''simple docstring'''







				import spacy

				_UpperCAmelCase      :							Dict       =   spacy.load("""en_core_web_sm"""       )
				_UpperCAmelCase      :							Dict       =   mda(datasets.utils.py_utils.dumps(_A       )       ).hexdigest()
				_UpperCAmelCase      :							Union[str, Any]       =   spacy.load("""fr_core_news_sm"""       )
				_UpperCAmelCase      :							List[Any]       =   mda(datasets.utils.py_utils.dumps(_A       )       ).hexdigest()
				_UpperCAmelCase      :							Optional[Any]       =   spacy.load("""en_core_web_sm"""       )
				_UpperCAmelCase      :							Optional[int]       =   mda(datasets.utils.py_utils.dumps(_A       )       ).hexdigest()
				self.assertEqual(_A ,						_A       )
				self.assertNotEqual(_A ,						_A       )
@pytest.mark.integration
def  UpperCamelCase   (     _lowerCAmelCase       :  Any	)			->     Any:
	_UpperCAmelCase      :							Any       =   tmp_path / """dir1"""
	_UpperCAmelCase      :							Tuple       =   tmp_path / """dir2"""
	dira.mkdir()
	dira.mkdir()
	_UpperCAmelCase      :							Dict       =   """script.py"""
	_UpperCAmelCase      :							int       =   dedent(
	    """
    from datasets.fingerprint import Hasher
    def foo():
        pass
    print(Hasher.hash(foo))
    """	)
	_UpperCAmelCase      :							Any       =   dira / script_filename
	_UpperCAmelCase      :							Union[str, Any]       =   dira / script_filename
	with script_patha.open("""w"""	) as f:
		f.write(_lowerCAmelCase	)
	with script_patha.open("""w"""	) as f:
		f.write(_lowerCAmelCase	)
	_UpperCAmelCase      :							str       =   subprocess.check_output(["""python""", str(_lowerCAmelCase	)]	)
	_UpperCAmelCase      :							Optional[int]       =   subprocess.check_output(["""python""", str(_lowerCAmelCase	)]	)
	assert fingerprinta == fingerprinta
def  UpperCamelCase   (     )			->     List[Any]:
	_UpperCAmelCase      :							List[Any]       =   {"""a""": [0, 1, 2]}
	_UpperCAmelCase      :							Union[str, Any]       =   DatasetChild(InMemoryTable.from_pydict(_lowerCAmelCase	)	)
	_UpperCAmelCase      :							Optional[Any]       =   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  UpperCamelCase   (     )			->     List[str]:
	_UpperCAmelCase      :							int       =   {"""a""": [0, 1, 2]}
	class       _UpperCAmelCase     (						datasets.Dataset):
				@fingerprint_transform(inplace=_A       )
				def 							__snake_case	(						self ,						_A       )      ->      str:






					'''simple docstring'''







					return DummyDatasetChild(self.data ,						fingerprint=_A       )
	_UpperCAmelCase      :							Dict       =   DummyDatasetChild(InMemoryTable.from_pydict(_lowerCAmelCase	)	).func()
	class       _UpperCAmelCase     (						datasets.Dataset):
				@fingerprint_transform(inplace=_A ,						version="""1.0.0"""       )
				def 							__snake_case	(						self ,						_A       )      ->      str:






					'''simple docstring'''







					return DummyDatasetChild(self.data ,						fingerprint=_A       )
	_UpperCAmelCase      :							Union[str, Any]       =   DummyDatasetChild(InMemoryTable.from_pydict(_lowerCAmelCase	)	).func()
	class       _UpperCAmelCase     (						datasets.Dataset):
				@fingerprint_transform(inplace=_A ,						version="""2.0.0"""       )
				def 							__snake_case	(						self ,						_A       )      ->      Optional[int]:






					'''simple docstring'''







					return DummyDatasetChild(self.data ,						fingerprint=_A       )
	_UpperCAmelCase      :							Any       =   DummyDatasetChild(InMemoryTable.from_pydict(_lowerCAmelCase	)	).func()

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





def  UpperCamelCase   (     )			->     Any:
	_UpperCAmelCase      :							int       =   Hasher()
	hasher.update(lambda _lowerCAmelCase	: x	)








"""simple docstring"""






from math import factorial
def  UpperCamelCase   (     _lowerCAmelCase       :  Dict,					_lowerCAmelCase       :  List[str]	)			->     Dict:
	return factorial(_lowerCAmelCase	) / (factorial(_lowerCAmelCase	) * factorial(n - r	))
def  UpperCamelCase   (     )			->     Tuple:
	_UpperCAmelCase      :							Optional[int]       =   0

	for i in range(1,					101	):
		for j in range(1,					i + 1	):
			if combinations(_lowerCAmelCase,					_lowerCAmelCase	) > 1E6:
				total += 1
	return total


if __name__ == "__main__":
						print(solution())





