
'''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       ,				lowercase_):
     snake_case_       :			Optional[Any]   	=  foo





 def __call__(			self):
     return self.foo






class       UpperCAmelCase_ (		datasets.Dataset    ):





 @fingerprint_transform(inplace=_UpperCAmelCase)
 def 				snake_case__						(			self       ,				lowercase_       ,				*lowercase_       ,				**lowercase_):
     return DatasetChild(self.data       ,				fingerprint=_UpperCAmelCase)





 @fingerprint_transform(inplace=_UpperCAmelCase)
 def 				snake_case__						(			self       ,				lowercase_       ,				*lowercase_       ,				**lowercase_):
     return DatasetChild(self.data       ,				fingerprint=_UpperCAmelCase)






class       UpperCAmelCase_ :





 def __init__(			self       ,				lowercase_):
     snake_case_       :			List[Any]   	=  callable





 def __call__(			self       ,				*lowercase_       ,				**lowercase_):
     if self.callable is not None:
         return self.callable(*_UpperCAmelCase       ,				**_UpperCAmelCase)





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






class       UpperCAmelCase_ (		lowerCamelCase__    ):





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

     def encode(lowercase_):
         return tokenizer(_UpperCAmelCase)

     # TODO: add hash consistency tests across sessions
     snake_case_       :			int   	=  AutoTokenizer.from_pretrained("bert-base-uncased")
     snake_case_       :			Any   	=  mda(datasets.utils.py_utils.dumps(_UpperCAmelCase)).hexdigest()
     snake_case_       :			int   	=  mda(datasets.utils.py_utils.dumps(lambda lowercase_: tokenizer(_UpperCAmelCase))).hexdigest()
     snake_case_       :			str   	=  mda(datasets.utils.py_utils.dumps(_UpperCAmelCase)).hexdigest()
     snake_case_       :			int   	=  AutoTokenizer.from_pretrained("bert-base-cased")
     snake_case_       :			str   	=  mda(datasets.utils.py_utils.dumps(_UpperCAmelCase)).hexdigest()
     snake_case_       :			Tuple   	=  mda(datasets.utils.py_utils.dumps(lambda lowercase_: tokenizer(_UpperCAmelCase))).hexdigest()
     snake_case_       :			Tuple   	=  mda(datasets.utils.py_utils.dumps(_UpperCAmelCase)).hexdigest()
     snake_case_       :			str   	=  AutoTokenizer.from_pretrained("bert-base-uncased")
     snake_case_       :			Optional[Any]   	=  mda(datasets.utils.py_utils.dumps(_UpperCAmelCase)).hexdigest()
     snake_case_       :			Optional[int]   	=  mda(datasets.utils.py_utils.dumps(lambda lowercase_: tokenizer(_UpperCAmelCase))).hexdigest()
     snake_case_       :			Tuple   	=  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 				snake_case__						(			self):
     from transformers import AutoTokenizer

     snake_case_       :			List[str]   	=  AutoTokenizer.from_pretrained("gpt2")
     snake_case_       :			Tuple   	=  mda(datasets.utils.py_utils.dumps(_UpperCAmelCase)).hexdigest()
     tokenizer("Hello world !")  # call once to change the tokenizer's cache
     snake_case_       :			Optional[int]   	=  mda(datasets.utils.py_utils.dumps(_UpperCAmelCase)).hexdigest()
     self.assertEqual(_UpperCAmelCase       ,				_UpperCAmelCase)





 @require_regex
 def 				snake_case__						(			self):
     import regex

     snake_case_       :			Any   	=  regex.Regex("foo")
     snake_case_       :			Optional[Any]   	=  mda(datasets.utils.py_utils.dumps(_UpperCAmelCase)).hexdigest()
     snake_case_       :			Tuple   	=  regex.Regex("bar")
     snake_case_       :			List[str]   	=  mda(datasets.utils.py_utils.dumps(_UpperCAmelCase)).hexdigest()
     snake_case_       :			Optional[Any]   	=  regex.Regex("foo")
     snake_case_       :			List[Any]   	=  mda(datasets.utils.py_utils.dumps(_UpperCAmelCase)).hexdigest()
     self.assertEqual(_UpperCAmelCase       ,				_UpperCAmelCase)
     self.assertNotEqual(_UpperCAmelCase       ,				_UpperCAmelCase)






class       UpperCAmelCase_ (		lowerCamelCase__    ):





 def 				snake_case__						(			self):
     def func():
         return foo

     snake_case_       :			Optional[Any]   	=  [0]
     snake_case_       :			int   	=  mda(datasets.utils.py_utils.dumps(_UpperCAmelCase)).hexdigest()
     snake_case_       :			List[Any]   	=  [1]
     snake_case_       :			Dict   	=  mda(datasets.utils.py_utils.dumps(_UpperCAmelCase)).hexdigest()
     snake_case_       :			Any   	=  [0]
     snake_case_       :			int   	=  mda(datasets.utils.py_utils.dumps(_UpperCAmelCase)).hexdigest()
     self.assertEqual(_UpperCAmelCase       ,				_UpperCAmelCase)
     self.assertNotEqual(_UpperCAmelCase       ,				_UpperCAmelCase)





 def 				snake_case__						(			self):
     def func():
         pass

     snake_case_       :			int   	=  mda(datasets.utils.py_utils.dumps(_UpperCAmelCase)).hexdigest()

     def func():
         pass

     snake_case_       :			Dict   	=  mda(datasets.utils.py_utils.dumps(_UpperCAmelCase)).hexdigest()
     self.assertEqual(_UpperCAmelCase       ,				_UpperCAmelCase)





 def 				snake_case__						(			self):
     snake_case_       :			str   	=  mda(datasets.utils.py_utils.dumps(Foo([0]))).hexdigest()
     snake_case_       :			Tuple   	=  mda(datasets.utils.py_utils.dumps(Foo([1]))).hexdigest()
     snake_case_       :			Union[str, Any]   	=  mda(datasets.utils.py_utils.dumps(Foo([0]))).hexdigest()
     self.assertEqual(_UpperCAmelCase       ,				_UpperCAmelCase)
     self.assertNotEqual(_UpperCAmelCase       ,				_UpperCAmelCase)





 def 				snake_case__						(			self):
     snake_case_       :			Any   	=  mda(datasets.utils.py_utils.dumps(Foo([0]).__call__)).hexdigest()
     snake_case_       :			Tuple   	=  mda(datasets.utils.py_utils.dumps(Foo([1]).__call__)).hexdigest()
     snake_case_       :			int   	=  mda(datasets.utils.py_utils.dumps(Foo([0]).__call__)).hexdigest()
     self.assertEqual(_UpperCAmelCase       ,				_UpperCAmelCase)
     self.assertNotEqual(_UpperCAmelCase       ,				_UpperCAmelCase)





 def 				snake_case__						(			self):
     snake_case_       :			Tuple   	=  (
         "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(*lowercase_):
         if hasattr(args[-3]       ,				"encode"):
             snake_case_       :			Optional[int]   	=  list(_UpperCAmelCase)
             snake_case_       :			int   	=  args[-3].encode()  # co_lnotab
             snake_case_       :			Dict   	=  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(lowercase_       ,				lowercase_):
         def func():
             return returned_obj

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

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

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





 def 				snake_case__						(			self):
     snake_case_	,       snake_case_       :			List[str]   	=  [0], [1]

     def func():
         return foo, bar

     snake_case_       :			List[Any]   	=  "__main__"

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

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

     with patch("dill.detect.globalvars"       ,				side_effect=_UpperCAmelCase) as globalvars_mocka:
         snake_case_       :			Optional[Any]   	=  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:
         snake_case_       :			Union[str, Any]   	=  mda(datasets.utils.py_utils.dumps(_UpperCAmelCase)).hexdigest()
         self.assertGreater(globalvars_mocka.call_count       ,				0)
     self.assertEqual(_UpperCAmelCase       ,				_UpperCAmelCase)






class       UpperCAmelCase_ (		lowerCamelCase__    ):





 def 				snake_case__						(			self):
     snake_case_       :			str   	=  Hasher.hash("hello")
     snake_case_       :			Optional[int]   	=  Hasher.hash("hello")
     snake_case_       :			str   	=  Hasher.hash("there")
     self.assertEqual(_UpperCAmelCase       ,				_UpperCAmelCase)
     self.assertNotEqual(_UpperCAmelCase       ,				_UpperCAmelCase)





 def 				snake_case__						(			self):
     snake_case_       :			List[Any]   	=  Hasher.hash(Foo("hello"))
     snake_case_       :			int   	=  Hasher.hash(Foo("hello"))
     snake_case_       :			List[Any]   	=  Hasher.hash(Foo("there"))
     self.assertEqual(_UpperCAmelCase       ,				_UpperCAmelCase)
     self.assertNotEqual(_UpperCAmelCase       ,				_UpperCAmelCase)





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





 def 				snake_case__						(			self):
     with self.assertRaises(pickle.PicklingError):
         Hasher.hash(UnpicklableCallable(Foo("hello")))





 def 				snake_case__						(			self):
     snake_case_       :			Optional[int]   	=  "abc"
     snake_case_       :			Dict   	=  [string, string]  # two strings have the same ids
     snake_case_       :			Any   	=  [string, string]
     snake_case_       :			Any   	=  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])
     snake_case_       :			Optional[Any]   	=  Hasher.hash(_UpperCAmelCase)
     snake_case_       :			Tuple   	=  Hasher.hash(_UpperCAmelCase)
     snake_case_       :			int   	=  Hasher.hash(_UpperCAmelCase)
     self.assertEqual(_UpperCAmelCase       ,				_UpperCAmelCase)
     self.assertEqual(_UpperCAmelCase       ,				_UpperCAmelCase)





 @require_tiktoken
 def 				snake_case__						(			self):
     import tiktoken

     snake_case_       :			int   	=  tiktoken.get_encoding("gpt2")
     snake_case_       :			Optional[Any]   	=  mda(datasets.utils.py_utils.dumps(_UpperCAmelCase)).hexdigest()
     snake_case_       :			Optional[Any]   	=  tiktoken.get_encoding("r50k_base")
     snake_case_       :			Dict   	=  mda(datasets.utils.py_utils.dumps(_UpperCAmelCase)).hexdigest()
     snake_case_       :			List[str]   	=  tiktoken.get_encoding("gpt2")
     snake_case_       :			str   	=  mda(datasets.utils.py_utils.dumps(_UpperCAmelCase)).hexdigest()
     self.assertEqual(_UpperCAmelCase       ,				_UpperCAmelCase)
     self.assertNotEqual(_UpperCAmelCase       ,				_UpperCAmelCase)





 @require_torch
 def 				snake_case__						(			self):
     import torch

     snake_case_       :			int   	=  torch.tensor([1.0])
     snake_case_       :			Optional[int]   	=  mda(datasets.utils.py_utils.dumps(_UpperCAmelCase)).hexdigest()
     snake_case_       :			Tuple   	=  torch.tensor([2.0])
     snake_case_       :			str   	=  mda(datasets.utils.py_utils.dumps(_UpperCAmelCase)).hexdigest()
     snake_case_       :			Tuple   	=  torch.tensor([1.0])
     snake_case_       :			int   	=  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 				snake_case__						(			self):
     import spacy

     snake_case_       :			List[Any]   	=  spacy.load("en_core_web_sm")
     snake_case_       :			Tuple   	=  mda(datasets.utils.py_utils.dumps(_UpperCAmelCase)).hexdigest()
     snake_case_       :			Optional[Any]   	=  spacy.load("fr_core_news_sm")
     snake_case_       :			Any   	=  mda(datasets.utils.py_utils.dumps(_UpperCAmelCase)).hexdigest()
     snake_case_       :			Any   	=  spacy.load("en_core_web_sm")
     snake_case_       :			List[str]   	=  mda(datasets.utils.py_utils.dumps(_UpperCAmelCase)).hexdigest()
     self.assertEqual(_UpperCAmelCase       ,				_UpperCAmelCase)
     self.assertNotEqual(_UpperCAmelCase       ,				_UpperCAmelCase)


@pytest.mark.integration
def   UpperCamelCase_   (		__SCREAMING_SNAKE_CASE				):
    """simple docstring"""






    snake_case_       :			List[str]   	=  tmp_path / "dir1"
    snake_case_       :			Tuple   	=  tmp_path / "dir2"
    dira.mkdir()
    dira.mkdir()
    snake_case_       :			Optional[Any]   	=  "script.py"
    snake_case_       :			Dict   	=  dedent(
        "\n    from datasets.fingerprint import Hasher\n    def foo():\n        pass\n    print(Hasher.hash(foo))\n    "				)
    snake_case_       :			Dict   	=  dira / script_filename
    snake_case_       :			Any   	=  dira / script_filename
    with script_patha.open("w"				) as f:
        f.write(__SCREAMING_SNAKE_CASE				)
    with script_patha.open("w"				) as f:
        f.write(__SCREAMING_SNAKE_CASE				)
    snake_case_       :			List[str]   	=  subprocess.check_output(["python", str(__SCREAMING_SNAKE_CASE				)]				)
    snake_case_       :			List[Any]   	=  subprocess.check_output(["python", str(__SCREAMING_SNAKE_CASE				)]				)
    assert fingerprinta == fingerprinta

def   UpperCamelCase_   (		):
    """simple docstring"""






    snake_case_       :			Tuple   	=  {"a": [0, 1, 2]}
    snake_case_       :			List[Any]   	=  DatasetChild(InMemoryTable.from_pydict(__SCREAMING_SNAKE_CASE				)				)
    snake_case_       :			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_   (		):
    """simple docstring"""






    snake_case_       :			Optional[Any]   	=  {"a": [0, 1, 2]}






    class       UpperCAmelCase_ (		datasets.Dataset    ):





     @fingerprint_transform(inplace=_UpperCAmelCase)
     def 				snake_case__						(			self       ,				lowercase_):
         return DummyDatasetChild(self.data       ,				fingerprint=_UpperCAmelCase)


    snake_case_       :			Any   	=  DummyDatasetChild(InMemoryTable.from_pydict(__SCREAMING_SNAKE_CASE				)				).func()






    class       UpperCAmelCase_ (		datasets.Dataset    ):





     @fingerprint_transform(inplace=_UpperCAmelCase       ,				version="1.0.0")
     def 				snake_case__						(			self       ,				lowercase_):
         return DummyDatasetChild(self.data       ,				fingerprint=_UpperCAmelCase)


    snake_case_       :			str   	=  DummyDatasetChild(InMemoryTable.from_pydict(__SCREAMING_SNAKE_CASE				)				).func()






    class       UpperCAmelCase_ (		datasets.Dataset    ):





     @fingerprint_transform(inplace=_UpperCAmelCase       ,				version="2.0.0")
     def 				snake_case__						(			self       ,				lowercase_):
         return DummyDatasetChild(self.data       ,				fingerprint=_UpperCAmelCase)


    snake_case_       :			str   	=  DummyDatasetChild(InMemoryTable.from_pydict(__SCREAMING_SNAKE_CASE				)				).func()

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


def   UpperCamelCase_   (		):
    """simple docstring"""






    snake_case_       :			Dict   	=  Hasher()
    hasher.update(lambda __SCREAMING_SNAKE_CASE				: x				)








def        __lowerCAmelCase    (SCREAMING_SNAKE_CASE					)->  int:




      """simple docstring"""

      if number != int(SCREAMING_SNAKE_CASE					):
            raise ValueError('''factorial() only accepts integral values'''					)
      if number < 0:
            raise ValueError('''factorial() not defined for negative values'''					)
      snake_case_       =      1
      for i in range(1					,					number + 1					):
            value *= i
      return value






def        __lowerCAmelCase    (SCREAMING_SNAKE_CASE					)->  int:




      """simple docstring"""

      if not isinstance(SCREAMING_SNAKE_CASE					,					SCREAMING_SNAKE_CASE					):
            raise ValueError('''factorial() only accepts integral values'''					)
      if n < 0:
            raise ValueError('''factorial() not defined for negative values'''					)
      return 1 if n == 0 or n == 1 else n * factorial(n - 1					)


if __name__ == "__main__":
   import doctest

   doctest.testmod()

   UpperCAmelCase			=    int(input("""Enter a positive integer: """).strip() or 0)
   print(f'''factorial{n} is {factorial(n)}''')