






import os
import tempfile
from functools import partial
from unittest import TestCase
from unittest.mock import patch

import numpy as np
import pytest

from datasets.arrow_dataset import Dataset
from datasets.search import ElasticSearchIndex, FaissIndex, MissingIndex

from .utils import require_elasticsearch, require_faiss


__UpperCAmelCase							:   List[Any]							=  pytest.mark.integration


@require_faiss
class 					_snake_case						(			__lowerCAmelCase ):






	def    lowerCAmelCase_							(		self       )						->    Dict:
					snake_case__      :Dict         =   Dataset.from_dict({"filename": ["my_name-train" + "_" + str(_UpperCamelCase       ) for x in np.arange(30       ).tolist()]}       )
					return dset






	def    lowerCAmelCase_							(		self       )						->    Union[str, Any]:
					import faiss

					snake_case__      :Dataset         =   self._create_dummy_dataset()
					snake_case__      :Optional[Any]         =   dset.map(
					    lambda UpperCamelCase					,UpperCamelCase       : {"vecs": i * np.ones(5					,dtype=np.floataa       )}					,with_indices=_UpperCamelCase					,keep_in_memory=_UpperCamelCase       )
					snake_case__      :Dict         =   dset.add_faiss_index("vecs"					,batch_size=100					,metric_type=faiss.METRIC_INNER_PRODUCT       )
					snake_case__      :Optional[int]         =   dset.get_nearest_examples("vecs"					,np.ones(5					,dtype=np.floataa       )       )
					self.assertEqual(examples["filename"][0]					,"my_name-train_29"       )
					dset.drop_index("vecs"       )






	def    lowerCAmelCase_							(		self       )						->    int:
					import faiss

					snake_case__      :Dataset         =   self._create_dummy_dataset()
					dset.add_faiss_index_from_external_arrays(
					    external_arrays=np.ones((30, 5)       ) * np.arange(30       ).reshape(-1					,1       )					,index_name="vecs"					,batch_size=100					,metric_type=faiss.METRIC_INNER_PRODUCT					,)
					snake_case__      :Optional[int]         =   dset.get_nearest_examples("vecs"					,np.ones(5					,dtype=np.floataa       )       )
					self.assertEqual(examples["filename"][0]					,"my_name-train_29"       )






	def    lowerCAmelCase_							(		self       )						->    Optional[int]:
					import faiss

					snake_case__      :Dataset         =   self._create_dummy_dataset()
					dset.add_faiss_index_from_external_arrays(
					    external_arrays=np.ones((30, 5)       ) * np.arange(30       ).reshape(-1					,1       )					,index_name="vecs"					,metric_type=faiss.METRIC_INNER_PRODUCT					,)

					# Setting delete=False and unlinking manually is not pretty... but it is required on Windows to
					# ensure somewhat stable behaviour. If we don't, we get PermissionErrors. This is an age-old issue.
					# see https://bugs.python.org/issue14243 and
					# https://stackoverflow.com/questions/23212435/permission-denied-to-write-to-my-temporary-file/23212515
					with tempfile.NamedTemporaryFile(delete=_UpperCamelCase       ) as tmp_file:
									dset.save_faiss_index("vecs"					,tmp_file.name       )
									dset.load_faiss_index("vecs2"					,tmp_file.name       )
					os.unlink(tmp_file.name       )

					snake_case__      :str         =   dset.get_nearest_examples("vecs2"					,np.ones(5					,dtype=np.floataa       )       )
					self.assertEqual(examples["filename"][0]					,"my_name-train_29"       )






	def    lowerCAmelCase_							(		self       )						->    Any:
					snake_case__      :Dataset         =   self._create_dummy_dataset()
					dset.add_faiss_index_from_external_arrays(
					    external_arrays=np.ones((30, 5)       ) * np.arange(30       ).reshape(-1					,1       )					,index_name="vecs"       )
					dset.drop_index("vecs"       )
					self.assertRaises(_UpperCamelCase					,partial(dset.get_nearest_examples					,"vecs2"					,np.ones(5					,dtype=np.floataa       )       )       )






	def    lowerCAmelCase_							(		self       )						->    Dict:
					from elasticsearch import Elasticsearch

					snake_case__      :Dataset         =   self._create_dummy_dataset()
					with patch("elasticsearch.Elasticsearch.search"       ) as mocked_search, patch(
					    "elasticsearch.client.IndicesClient.create"       ) as mocked_index_create, patch("elasticsearch.helpers.streaming_bulk"       ) as mocked_bulk:
									snake_case__      :Union[str, Any]         =   {"""acknowledged""": True}
									mocked_bulk.return_value([(True, None)] * 30       )
									snake_case__      :Tuple         =   {"""hits""": {"""hits""": [{"""_score""": 1, """_id""": 29}]}}
									snake_case__      :Any         =   Elasticsearch()

									dset.add_elasticsearch_index("filename"					,es_client=_UpperCamelCase       )
									snake_case__      :List[Any]         =   dset.get_nearest_examples("filename"					,"my_name-train_29"       )
									self.assertEqual(examples["filename"][0]					,"my_name-train_29"       )




@require_faiss
class 					_snake_case						(			__lowerCAmelCase ):






	def    lowerCAmelCase_							(		self       )						->    str:
					import faiss

					snake_case__      :Optional[Any]         =   FaissIndex(metric_type=faiss.METRIC_INNER_PRODUCT       )

					# add vectors
					index.add_vectors(np.eye(5					,dtype=np.floataa       )       )
					self.assertIsNotNone(index.faiss_index       )
					self.assertEqual(index.faiss_index.ntotal					,5       )
					index.add_vectors(np.zeros((5, 5)					,dtype=np.floataa       )       )
					self.assertEqual(index.faiss_index.ntotal					,10       )

					# single query
					snake_case__      :List[Any]         =   np.zeros(5					,dtype=np.floataa       )
					snake_case__      :int         =   1
					snake_case__      :Tuple         =   index.search(_UpperCamelCase       )
					self.assertRaises(_UpperCamelCase					,index.search					,query.reshape(-1					,1       )       )
					self.assertGreater(scores[0]					,0       )
					self.assertEqual(indices[0]					,1       )

					# batched queries
					snake_case__      :List[str]         =   np.eye(5					,dtype=np.floataa       )[::-1]
					snake_case__      :Tuple         =   index.search_batch(_UpperCamelCase       )
					self.assertRaises(_UpperCamelCase					,index.search_batch					,queries[0]       )
					snake_case__      :int         =   [scores[0] for scores in total_scores]
					snake_case__      :str         =   [indices[0] for indices in total_indices]
					self.assertGreater(np.min(_UpperCamelCase       )					,0       )
					self.assertListEqual([4, 3, 2, 1, 0]					,_UpperCamelCase       )






	def    lowerCAmelCase_							(		self       )						->    str:
					import faiss

					snake_case__      :List[str]         =   FaissIndex(string_factory="Flat"       )
					index.add_vectors(np.eye(5					,dtype=np.floataa       )       )
					self.assertIsInstance(index.faiss_index					,faiss.IndexFlat       )
					snake_case__      :Union[str, Any]         =   FaissIndex(string_factory="LSH"       )
					index.add_vectors(np.eye(5					,dtype=np.floataa       )       )
					self.assertIsInstance(index.faiss_index					,faiss.IndexLSH       )
					with self.assertRaises(_UpperCamelCase       ):
									snake_case__      :List[str]         =   FaissIndex(string_factory="Flat"					,custom_index=faiss.IndexFlat(5       )       )






	def    lowerCAmelCase_							(		self       )						->    Optional[Any]:
					import faiss

					snake_case__      :str         =   faiss.IndexFlat(5       )
					snake_case__      :List[str]         =   FaissIndex(custom_index=_UpperCamelCase       )
					index.add_vectors(np.eye(5					,dtype=np.floataa       )       )
					self.assertIsInstance(index.faiss_index					,faiss.IndexFlat       )






	def    lowerCAmelCase_							(		self       )						->    int:
					import faiss

					snake_case__      :Dict         =   FaissIndex(metric_type=faiss.METRIC_INNER_PRODUCT       )
					index.add_vectors(np.eye(5					,dtype=np.floataa       )       )

					# Setting delete=False and unlinking manually is not pretty... but it is required on Windows to
					# ensure somewhat stable behaviour. If we don't, we get PermissionErrors. This is an age-old issue.
					# see https://bugs.python.org/issue14243 and
					# https://stackoverflow.com/questions/23212435/permission-denied-to-write-to-my-temporary-file/23212515
					with tempfile.NamedTemporaryFile(delete=_UpperCamelCase       ) as tmp_file:
									index.save(tmp_file.name       )
									snake_case__      :Optional[int]         =   FaissIndex.load(tmp_file.name       )
					os.unlink(tmp_file.name       )

					snake_case__      :Optional[Any]         =   np.zeros(5					,dtype=np.floataa       )
					snake_case__      :Tuple         =   1
					snake_case__      :Optional[int]         =   index.search(_UpperCamelCase       )
					self.assertGreater(scores[0]					,0       )
					self.assertEqual(indices[0]					,1       )





@require_faiss
def 					lowercase_ (    __snake_case     :							Any      )     -> Any:





				'''simple docstring'''


				import faiss

				snake_case__      :Any         =   FaissIndex(metric_type=faiss.METRIC_INNER_PRODUCT      )
				index.add_vectors(np.eye(5	,	dtype=np.floataa      )      )

				snake_case__      :int         =   """index.faiss"""
				snake_case__      :List[Any]         =   F'mock://{index_name}'
				index.save(__A	,	storage_options=mockfs.storage_options      )
				snake_case__      :str         =   FaissIndex.load(__A	,	storage_options=mockfs.storage_options      )

				snake_case__      :List[str]         =   np.zeros(5	,	dtype=np.floataa      )
				snake_case__      :Any         =   1
				snake_case__      :Union[str, Any]         =   index.search(__A      )
				assert scores[0] > 0
				assert indices[0] == 1


@require_elasticsearch
class 					_snake_case						(			__lowerCAmelCase ):






	def    lowerCAmelCase_							(		self       )						->    List[str]:
					from elasticsearch import Elasticsearch

					with patch("elasticsearch.Elasticsearch.search"       ) as mocked_search, patch(
					    "elasticsearch.client.IndicesClient.create"       ) as mocked_index_create, patch("elasticsearch.helpers.streaming_bulk"       ) as mocked_bulk:
									snake_case__      :Optional[int]         =   Elasticsearch()
									snake_case__      :List[Any]         =   {"""acknowledged""": True}
									snake_case__      :Union[str, Any]         =   ElasticSearchIndex(es_client=_UpperCamelCase       )
									mocked_bulk.return_value([(True, None)] * 3       )
									index.add_documents(["foo", "bar", "foobar"]       )

									# single query
									snake_case__      :int         =   """foo"""
									snake_case__      :List[str]         =   {"""hits""": {"""hits""": [{"""_score""": 1, """_id""": 0}]}}
									snake_case__      :Optional[int]         =   index.search(_UpperCamelCase       )
									self.assertEqual(scores[0]					,1       )
									self.assertEqual(indices[0]					,0       )

									# single query with timeout
									snake_case__      :Dict         =   """foo"""
									snake_case__      :str         =   {"""hits""": {"""hits""": [{"""_score""": 1, """_id""": 0}]}}
									snake_case__      :List[Any]         =   index.search(_UpperCamelCase					,request_timeout=30       )
									self.assertEqual(scores[0]					,1       )
									self.assertEqual(indices[0]					,0       )

									# batched queries
									snake_case__      :List[str]         =   ["""foo""", """bar""", """foobar"""]
									snake_case__      :Union[str, Any]         =   {"""hits""": {"""hits""": [{"""_score""": 1, """_id""": 1}]}}
									snake_case__      :Optional[Any]         =   index.search_batch(_UpperCamelCase       )
									snake_case__      :str         =   [scores[0] for scores in total_scores]
									snake_case__      :Tuple         =   [indices[0] for indices in total_indices]
									self.assertGreater(np.min(_UpperCamelCase       )					,0       )
									self.assertListEqual([1, 1, 1]					,_UpperCamelCase       )

									# batched queries with timeout
									snake_case__      :Union[str, Any]         =   ["""foo""", """bar""", """foobar"""]
									snake_case__      :str         =   {"""hits""": {"""hits""": [{"""_score""": 1, """_id""": 1}]}}
									snake_case__      :Any         =   index.search_batch(_UpperCamelCase					,request_timeout=30       )
									snake_case__      :List[Any]         =   [scores[0] for scores in total_scores]
									snake_case__      :Any         =   [indices[0] for indices in total_indices]
									self.assertGreater(np.min(_UpperCamelCase       )					,0       )
									self.assertListEqual([1, 1, 1]					,_UpperCamelCase       )







def 					lowercase_ (    __snake_case     :							Tuple	,	__snake_case     :							Optional[int]      )     -> List[Any]:





				'''simple docstring'''


				snake_case__      :Dict         =   ""
				for i in table:
								res += inp[i - 1]
				return res





def 					lowercase_ (    __snake_case     :							List[str]      )     -> int:





				'''simple docstring'''


				return data[1:] + data[0]





def 					lowercase_ (    __snake_case     :							int	,	__snake_case     :							Union[str, Any]      )     -> Union[str, Any]:





				'''simple docstring'''


				snake_case__      :Union[str, Any]         =   ""
				for i in range(len(__snake_case      )      ):
								if a[i] == b[i]:
												res += "0"
								else:
												res += "1"
				return res





def 					lowercase_ (    __snake_case     :							Optional[int]	,	__snake_case     :							Dict      )     -> Union[str, Any]:





				'''simple docstring'''


				snake_case__      :int         =   int("0b" + data[0] + data[-1]	,	2      )
				snake_case__      :Union[str, Any]         =   int("0b" + data[1:3]	,	2      )
				return bin(s[row][col]      )[2:]





def 					lowercase_ (    __snake_case     :							Dict	,	__snake_case     :							Optional[Any]	,	__snake_case     :							Dict	,	__snake_case     :							List[Any]	,	__snake_case     :							Optional[int]      )     -> List[str]:





				'''simple docstring'''


				snake_case__      :Tuple         =   message[:4]
				snake_case__      :int         =   message[4:]
				snake_case__      :int         =   apply_table(__snake_case	,	__snake_case      )
				snake_case__      :Union[str, Any]         =   xor(__snake_case	,	__snake_case      )
				snake_case__      :Tuple         =   apply_sbox(__snake_case	,	temp[:4]      )  # noqa: E741
				snake_case__      :List[str]         =   apply_sbox(__snake_case	,	temp[4:]      )
				snake_case__      :int         =   "0" * (2 - len(__snake_case      )) + l  # noqa: E741
				snake_case__      :int         =   "0" * (2 - len(__snake_case      )) + r
				snake_case__      :Optional[Any]         =   apply_table(l + r	,	__snake_case      )
				snake_case__      :Tuple         =   xor(__snake_case	,	__snake_case      )
				return temp + right


if __name__ == "__main__":
	__UpperCAmelCase							:   Dict							=  input("Enter 10 bit key: ")
	__UpperCAmelCase							:   Tuple							=  input("Enter 8 bit message: ")

	__UpperCAmelCase							:   Any							=  [6, 3, 7, 4, 8, 5, 1_0, 9]
	__UpperCAmelCase							:   List[str]							=  [3, 5, 2, 7, 4, 1_0, 1, 9, 8, 6]
	__UpperCAmelCase							:   Tuple							=  [2, 4, 3, 1]
	__UpperCAmelCase							:   List[Any]							=  [2, 6, 3, 1, 4, 8, 5, 7]
	__UpperCAmelCase							:   Optional[Any]							=  [4, 1, 3, 5, 7, 2, 8, 6]
	__UpperCAmelCase							:   Optional[int]							=  [4, 1, 2, 3, 2, 3, 4, 1]
	__UpperCAmelCase							:   List[Any]							=  [[1, 0, 3, 2], [3, 2, 1, 0], [0, 2, 1, 3], [3, 1, 3, 2]]
	__UpperCAmelCase							:   Union[str, Any]							=  [[0, 1, 2, 3], [2, 0, 1, 3], [3, 0, 1, 0], [2, 1, 0, 3]]

	# key generation
	__UpperCAmelCase							:   int							=  apply_table(key, paa_table)
	__UpperCAmelCase							:   Dict							=  temp[:5]
	__UpperCAmelCase							:   Optional[int]							=  temp[5:]
	__UpperCAmelCase							:   Optional[int]							=  left_shift(left)
	__UpperCAmelCase							:   Union[str, Any]							=  left_shift(right)
	__UpperCAmelCase							:   int							=  apply_table(left + right, pa_table)
	__UpperCAmelCase							:   Tuple							=  left_shift(left)
	__UpperCAmelCase							:   Union[str, Any]							=  left_shift(right)
	__UpperCAmelCase							:   Dict							=  left_shift(left)
	__UpperCAmelCase							:   Optional[Any]							=  left_shift(right)
	__UpperCAmelCase							:   Optional[int]							=  apply_table(left + right, pa_table)

	# encryption
	__UpperCAmelCase							:   Tuple							=  apply_table(message, IP)
	__UpperCAmelCase							:   Tuple							=  function(expansion, sa, sa, keya, temp)
	__UpperCAmelCase							:   List[Any]							=  temp[4:] + temp[:4]
	__UpperCAmelCase							:   int							=  function(expansion, sa, sa, keya, temp)
	__UpperCAmelCase							:   Union[str, Any]							=  apply_table(temp, IP_inv)
	print("Cipher text is:", CT)

	# decryption
	__UpperCAmelCase							:   List[Any]							=  apply_table(CT, IP)
	__UpperCAmelCase							:   List[Any]							=  function(expansion, sa, sa, keya, temp)
	__UpperCAmelCase							:   int							=  temp[4:] + temp[:4]
	__UpperCAmelCase							:   Union[str, Any]							=  function(expansion, sa, sa, keya, temp)
	__UpperCAmelCase							:   Union[str, Any]							=  apply_table(temp, IP_inv)
	print("Plain text after decypting is:", PT)