



import os
import pickle
import unittest
from collections import UserDict, namedtuple

import torch

from accelerate.test_utils.testing import require_cuda, require_torch_min_version
from accelerate.test_utils.training import RegressionModel
from accelerate.utils import (
    convert_outputs_to_fpaa,
    extract_model_from_parallel,
    find_device,
    listify,
    patch_environment,
    recursively_apply,
    send_to_device,
)


A__     				=	namedtuple("""ExampleNamedTuple""", """a b c""")





class  __lowerCAmelCase			(					unittest.TestCase	):




    def 							snake_case       (							self		):



         """simple docstring"""





         _lowerCAmelCase      =   torch.randn(5						,    2		)
         _lowerCAmelCase      =   torch.device("""cuda"""		) if torch.cuda.is_available() else torch.device("""cpu"""		)

         _lowerCAmelCase      =   send_to_device(_snake_case						,    _snake_case		)
         self.assertTrue(torch.equal(resulta.cpu()						,    _snake_case		)		)

         _lowerCAmelCase      =   send_to_device((tensor, [tensor, tensor], 1)						,    _snake_case		)
         self.assertIsInstance(_snake_case						,    _snake_case		)
         self.assertTrue(torch.equal(resulta[0].cpu()						,    _snake_case		)		)
         self.assertIsInstance(resulta[1]						,    _snake_case		)
         self.assertTrue(torch.equal(resulta[1][0].cpu()						,    _snake_case		)		)
         self.assertTrue(torch.equal(resulta[1][1].cpu()						,    _snake_case		)		)
         self.assertEqual(resulta[2]						,    1		)

         _lowerCAmelCase      =   send_to_device({"""a""": tensor, """b""": [tensor, tensor], """c""": 1}						,    _snake_case		)
         self.assertIsInstance(_snake_case						,    _snake_case		)
         self.assertTrue(torch.equal(resulta["""a"""].cpu()						,    _snake_case		)		)
         self.assertIsInstance(resulta["""b"""]						,    _snake_case		)
         self.assertTrue(torch.equal(resulta["""b"""][0].cpu()						,    _snake_case		)		)
         self.assertTrue(torch.equal(resulta["""b"""][1].cpu()						,    _snake_case		)		)
         self.assertEqual(resulta["""c"""]						,    1		)

         _lowerCAmelCase      =   send_to_device(ExampleNamedTuple(a=_snake_case						,    b=[tensor, tensor]						,    c=1		)						,    _snake_case		)
         self.assertIsInstance(_snake_case						,    _snake_case		)
         self.assertTrue(torch.equal(resulta.a.cpu()						,    _snake_case		)		)
         self.assertIsInstance(resulta.b						,    _snake_case		)
         self.assertTrue(torch.equal(resulta.b[0].cpu()						,    _snake_case		)		)
         self.assertTrue(torch.equal(resulta.b[1].cpu()						,    _snake_case		)		)
         self.assertEqual(resulta.c						,    1		)

         _lowerCAmelCase      =   send_to_device(UserDict({"""a""": tensor, """b""": [tensor, tensor], """c""": 1}		)						,    _snake_case		)
         self.assertIsInstance(_snake_case						,    _snake_case		)
         self.assertTrue(torch.equal(resulta["""a"""].cpu()						,    _snake_case		)		)
         self.assertIsInstance(resulta["""b"""]						,    _snake_case		)
         self.assertTrue(torch.equal(resulta["""b"""][0].cpu()						,    _snake_case		)		)
         self.assertTrue(torch.equal(resulta["""b"""][1].cpu()						,    _snake_case		)		)
         self.assertEqual(resulta["""c"""]						,    1		)




    def 							snake_case       (							self		):



         """simple docstring"""





         with self.assertRaises(_snake_case		) as cm:
              _lowerCAmelCase      =   recursively_apply(torch.tensor						,    (torch.tensor(1		), 1)						,    error_on_other_type=_snake_case		)
         self.assertEqual(
             str(cm.exception		)						,    """Unsupported types (<class 'int'>) passed to `tensor`. Only nested list/tuple/dicts of objects that are valid for `is_torch_tensor` should be passed."""						,    )




    def 							snake_case       (							self		):



         """simple docstring"""





         _lowerCAmelCase      =   torch.tensor([1, 2, 3, 4, 5]		)
         self.assertEqual(listify(_snake_case		)						,    [1, 2, 3, 4, 5]		)

         _lowerCAmelCase      =   torch.tensor([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]		)
         self.assertEqual(listify(_snake_case		)						,    [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]		)

         _lowerCAmelCase      =   torch.tensor([[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]], [[11, 12, 13, 14, 15], [16, 17, 18, 19, 20]]]		)
         self.assertEqual(
             listify(_snake_case		)						,    [[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]], [[11, 12, 13, 14, 15], [16, 17, 18, 19, 20]]]		)




    def 							snake_case       (							self		):



         """simple docstring"""





         with patch_environment(aa=1						,    BB=2		):
              self.assertEqual(os.environ.get("""AA"""		)						,    """1"""		)
              self.assertEqual(os.environ.get("""BB"""		)						,    """2"""		)

         self.assertNotIn("""AA"""						,    os.environ		)
         self.assertNotIn("""BB"""						,    os.environ		)




    def 							snake_case       (							self		):



         """simple docstring"""





         _lowerCAmelCase      =   RegressionModel()
         _lowerCAmelCase      =   model.forward
         _lowerCAmelCase      =   convert_outputs_to_fpaa(model.forward		)
         _lowerCAmelCase      =   extract_model_from_parallel(_snake_case						,    keep_fpaa_wrapper=_snake_case		)
         _lowerCAmelCase      =   pickle.dumps(_snake_case		)




    @require_cuda
    def 							snake_case       (							self		):



         """simple docstring"""





         _lowerCAmelCase      =   RegressionModel()
         _lowerCAmelCase      =   model.forward
         _lowerCAmelCase      =   torch.cuda.amp.autocast(dtype=torch.floataa		)(model.forward		)
         _lowerCAmelCase      =   convert_outputs_to_fpaa(model.forward		)
         _lowerCAmelCase      =   extract_model_from_parallel(_snake_case						,    keep_fpaa_wrapper=_snake_case		)
         _lowerCAmelCase      =   pickle.dumps(_snake_case		)




    @require_cuda
    @require_torch_min_version(version="""2.0"""		)
    def 							snake_case       (							self		):



         """simple docstring"""





         _lowerCAmelCase      =   RegressionModel()
         _lowerCAmelCase      =   model.forward
         _lowerCAmelCase      =   torch.cuda.amp.autocast(dtype=torch.floataa		)(model.forward		)
         _lowerCAmelCase      =   convert_outputs_to_fpaa(model.forward		)
         _lowerCAmelCase      =   torch.compile(model.forward						,    backend="""inductor"""		)
         _lowerCAmelCase      =   torch.randn(4						,    10		).cuda()
         _lowerCAmelCase      =   model(_snake_case		)




    def 							snake_case       (							self		):



         """simple docstring"""





         _lowerCAmelCase      =   RegressionModel()
         # could also do a test with DistributedDataParallel, but difficult to run on CPU or single GPU
         _lowerCAmelCase      =   torch.nn.parallel.DataParallel(_snake_case		)
         _lowerCAmelCase      =   extract_model_from_parallel(_snake_case		)

         self.assertEqual(_snake_case						,    _snake_case		)




    @require_torch_min_version(version="""2.0"""		)
    def 							snake_case       (							self		):



         """simple docstring"""





         _lowerCAmelCase      =   RegressionModel()
         _lowerCAmelCase      =   torch.compile(_snake_case		)

         # could also do a test with DistributedDataParallel, but difficult to run on CPU or single GPU
         _lowerCAmelCase      =   torch.nn.parallel.DataParallel(_snake_case		)
         _lowerCAmelCase      =   torch.compile(_snake_case		)
         _lowerCAmelCase      =   extract_model_from_parallel(_snake_case		)

         self.assertEqual(compiled_model._orig_mod						,    compiled_model_unwrapped._orig_mod		)




    def 							snake_case       (							self		):



         """simple docstring"""





         self.assertEqual(find_device([1, """a""", torch.tensor([1, 2, 3]		)]		)						,    torch.device("""cpu"""		)		)
         self.assertEqual(find_device({"""a""": 1, """b""": torch.tensor([1, 2, 3]		)}		)						,    torch.device("""cpu"""		)		)
         self.assertIsNone(find_device([1, """a"""]		)		)








from urllib.error import HTTPError

from bsa import BeautifulSoup
from requests import exceptions, get

A__     				=	"""https://www.wellrx.com/prescriptions/{0}/{1}/?freshSearch=true"""


def 		_UpperCAmelCase    (				snake_case     ,						snake_case      ):





					"""simple docstring"""

					try:
										# Has user provided both inputs?
										if not drug_name or not zip_code:
															return None

										_lowerCAmelCase      =   BASE_URL.format(snake_case     ,						snake_case      )
										_lowerCAmelCase      =   get(snake_case      )

										# Is the response ok?
										response.raise_for_status()

										# Scrape the data using bs4
										_lowerCAmelCase      =   BeautifulSoup(response.text     ,						"""html.parser"""      )

										# This list will store the name and price.
										_lowerCAmelCase      =   []

										# Fetch all the grids that contains the items.
										_lowerCAmelCase      =   soup.find_all("""div"""     ,						{"""class""": """grid-x pharmCard"""}      )
										if grid_list and len(snake_case      ) > 0:
															for grid in grid_list:
																				# Get the pharmacy price.
																				_lowerCAmelCase      =   grid.find("""p"""     ,						{"""class""": """list-title"""}      ).text

																				# Get price of the drug.
																				_lowerCAmelCase      =   grid.find("""span"""     ,						{"""p""", """price price-large"""}      ).text

																				pharmacy_price_list.append(
																				    {
																				        """pharmacy_name""": pharmacy_name,
																				        """price""": price,
																				    }      )

										return pharmacy_price_list

					except (HTTPError, exceptions.RequestException, ValueError):
										return None


if __name__ == "__main__":
	# Enter a drug name and a zip code
	A__     				=	input("""Enter drug name: """).strip()
	A__     				=	input("""Enter zip code: """).strip()

	A__     				=	fetch_pharmacy_and_price_list(
	    drug_name, zip_code
	)

	if pharmacy_price_list:
		print(f"\nSearch results for {drug_name} at location {zip_code}:")
		for pharmacy_price in pharmacy_price_list:
			A__     				=	pharmacy_price["""pharmacy_name"""]
			A__     				=	pharmacy_price["""price"""]

			print(f"Pharmacy: {name} Price: {price}")
	else:
		print(f"No results found for {drug_name}")



