


'''simple docstring'''


import shutil
import tempfile
import unittest
from unittest.mock import patch

from transformers import (
    DefaultFlowCallback,
    IntervalStrategy,
    PrinterCallback,
    ProgressCallback,
    Trainer,
    TrainerCallback,
    TrainingArguments,
    is_torch_available,
)
from transformers.testing_utils import require_torch


if is_torch_available():
						from transformers.trainer import DEFAULT_CALLBACKS

						from .test_trainer import RegressionDataset, RegressionModelConfig, RegressionPreTrainedModel






class 		lowerCAmelCase__	(	UpperCAmelCase__		):




			def __init__(						self     :   Optional[Any]       )      ->Optional[int]:



					'''simple docstring'''







					_UpperCAmelCase						:				str	    =					[]




			def 							lowerCAmelCase__			(						self     :   str		,     lowerCamelCase__     :   Union[str, Any]		,     lowerCamelCase__     :   Dict		,     lowerCamelCase__     :   Dict		,     **lowerCamelCase__     :   Optional[int]       )      ->Tuple:



					'''simple docstring'''







					self.events.append("on_init_end"       )




			def 							lowerCAmelCase__			(						self     :   Optional[Any]		,     lowerCamelCase__     :   Optional[int]		,     lowerCamelCase__     :   str		,     lowerCamelCase__     :   Any		,     **lowerCamelCase__     :   int       )      ->Optional[Any]:



					'''simple docstring'''







					self.events.append("on_train_begin"       )




			def 							lowerCAmelCase__			(						self     :   Dict		,     lowerCamelCase__     :   Optional[int]		,     lowerCamelCase__     :   Tuple		,     lowerCamelCase__     :   int		,     **lowerCamelCase__     :   Dict       )      ->Any:



					'''simple docstring'''







					self.events.append("on_train_end"       )




			def 							lowerCAmelCase__			(						self     :   Optional[int]		,     lowerCamelCase__     :   str		,     lowerCamelCase__     :   List[Any]		,     lowerCamelCase__     :   Optional[Any]		,     **lowerCamelCase__     :   Dict       )      ->Dict:



					'''simple docstring'''







					self.events.append("on_epoch_begin"       )




			def 							lowerCAmelCase__			(						self     :   int		,     lowerCamelCase__     :   List[str]		,     lowerCamelCase__     :   Any		,     lowerCamelCase__     :   Union[str, Any]		,     **lowerCamelCase__     :   Union[str, Any]       )      ->Any:



					'''simple docstring'''







					self.events.append("on_epoch_end"       )




			def 							lowerCAmelCase__			(						self     :   str		,     lowerCamelCase__     :   Optional[int]		,     lowerCamelCase__     :   List[str]		,     lowerCamelCase__     :   Optional[Any]		,     **lowerCamelCase__     :   int       )      ->Union[str, Any]:



					'''simple docstring'''







					self.events.append("on_step_begin"       )




			def 							lowerCAmelCase__			(						self     :   Union[str, Any]		,     lowerCamelCase__     :   Union[str, Any]		,     lowerCamelCase__     :   Tuple		,     lowerCamelCase__     :   Any		,     **lowerCamelCase__     :   Tuple       )      ->List[str]:



					'''simple docstring'''







					self.events.append("on_step_end"       )




			def 							lowerCAmelCase__			(						self     :   Dict		,     lowerCamelCase__     :   Any		,     lowerCamelCase__     :   Optional[int]		,     lowerCamelCase__     :   List[Any]		,     **lowerCamelCase__     :   Union[str, Any]       )      ->Dict:



					'''simple docstring'''







					self.events.append("on_evaluate"       )




			def 							lowerCAmelCase__			(						self     :   Dict		,     lowerCamelCase__     :   List[str]		,     lowerCamelCase__     :   Any		,     lowerCamelCase__     :   Optional[int]		,     **lowerCamelCase__     :   Optional[Any]       )      ->List[Any]:



					'''simple docstring'''







					self.events.append("on_predict"       )




			def 							lowerCAmelCase__			(						self     :   Optional[int]		,     lowerCamelCase__     :   Any		,     lowerCamelCase__     :   Optional[Any]		,     lowerCamelCase__     :   List[str]		,     **lowerCamelCase__     :   Dict       )      ->Tuple:



					'''simple docstring'''







					self.events.append("on_save"       )




			def 							lowerCAmelCase__			(						self     :   Optional[int]		,     lowerCamelCase__     :   int		,     lowerCamelCase__     :   Optional[Any]		,     lowerCamelCase__     :   Union[str, Any]		,     **lowerCamelCase__     :   List[str]       )      ->Optional[int]:



					'''simple docstring'''







					self.events.append("on_log"       )






			def 							lowerCAmelCase__			(						self     :   Optional[Any]		,     lowerCamelCase__     :   List[str]		,     lowerCamelCase__     :   List[Any]		,     lowerCamelCase__     :   Any		,     **lowerCamelCase__     :   List[Any]       )      ->Optional[Any]:



					'''simple docstring'''







					self.events.append("on_prediction_step"       )






@require_torch
class 		lowerCAmelCase__	(	unittest.TestCase		):




			def 							lowerCAmelCase__			(						self     :   Any       )      ->List[str]:



					'''simple docstring'''







					_UpperCAmelCase						:				Optional[Any]	    =					tempfile.mkdtemp()




			def 							lowerCAmelCase__			(						self     :   List[str]       )      ->Dict:



					'''simple docstring'''







					shutil.rmtree(self.output_dir       )




			def 							lowerCAmelCase__			(						self     :   List[Any]		,     lowerCamelCase__     :   Optional[Any]=0		,     lowerCamelCase__     :   Tuple=0		,     lowerCamelCase__     :   str=64		,     lowerCamelCase__     :   Optional[int]=64		,     lowerCamelCase__     :   Optional[Any]=None		,     lowerCamelCase__     :   Tuple=False		,     **lowerCamelCase__     :   List[str]       )      ->Optional[Any]:



					'''simple docstring'''







					_UpperCAmelCase						:				str	    =					RegressionDataset(length=lowerCamelCase__       )
					_UpperCAmelCase						:				str	    =					RegressionDataset(length=lowerCamelCase__       )
					_UpperCAmelCase						:				Tuple	    =					RegressionModelConfig(a=lowerCamelCase__		,     b=lowerCamelCase__       )
					_UpperCAmelCase						:				Tuple	    =					RegressionPreTrainedModel(lowerCamelCase__       )

					_UpperCAmelCase						:				Optional[Any]	    =					TrainingArguments(self.output_dir		,     disable_tqdm=lowerCamelCase__		,     report_to=[]		,     **lowerCamelCase__       )
					return Trainer(
					    lowerCamelCase__		,     lowerCamelCase__		,     train_dataset=lowerCamelCase__		,     eval_dataset=lowerCamelCase__		,     callbacks=lowerCamelCase__		,     )




			def 							lowerCAmelCase__			(						self     :   Union[str, Any]		,     lowerCamelCase__     :   List[Any]		,     lowerCamelCase__     :   Any       )      ->Tuple:



					'''simple docstring'''







					self.assertEqual(len(lowerCamelCase__       )		,     len(lowerCamelCase__       )       )

					# Order doesn't matter
					_UpperCAmelCase						:				Tuple	    =					sorted(lowerCamelCase__		,     key=lambda lowerCamelCase__       : cb.__name__ if isinstance(lowerCamelCase__		,     lowerCamelCase__       ) else cb.__class__.__name__       )
					_UpperCAmelCase						:				Any	    =					sorted(lowerCamelCase__		,     key=lambda lowerCamelCase__       : cb.__name__ if isinstance(lowerCamelCase__		,     lowerCamelCase__       ) else cb.__class__.__name__       )

					for cba, cba in zip(lowerCamelCase__		,     lowerCamelCase__       ):
							if isinstance(lowerCamelCase__		,     lowerCamelCase__       ) and isinstance(lowerCamelCase__		,     lowerCamelCase__       ):
									self.assertEqual(lowerCamelCase__		,     lowerCamelCase__       )
							elif isinstance(lowerCamelCase__		,     lowerCamelCase__       ) and not isinstance(lowerCamelCase__		,     lowerCamelCase__       ):
									self.assertEqual(lowerCamelCase__		,     cba.__class__       )
							elif not isinstance(lowerCamelCase__		,     lowerCamelCase__       ) and isinstance(lowerCamelCase__		,     lowerCamelCase__       ):
									self.assertEqual(cba.__class__		,     lowerCamelCase__       )
							else:
									self.assertEqual(lowerCamelCase__		,     lowerCamelCase__       )




			def 							lowerCAmelCase__			(						self     :   int		,     lowerCamelCase__     :   int       )      ->Optional[int]:



					'''simple docstring'''







					_UpperCAmelCase						:				Tuple	    =					["on_init_end", "on_train_begin"]
					_UpperCAmelCase						:				Any	    =					0
					_UpperCAmelCase						:				int	    =					len(trainer.get_eval_dataloader()       )
					_UpperCAmelCase						:				int	    =					["on_prediction_step"] * len(trainer.get_eval_dataloader()       ) + ["on_log", "on_evaluate"]
					for _ in range(trainer.state.num_train_epochs       ):
							expected_events.append("on_epoch_begin"       )
							for _ in range(lowerCamelCase__       ):
									step += 1
									expected_events += ["on_step_begin", "on_step_end"]
									if step % trainer.args.logging_steps == 0:
											expected_events.append("on_log"       )
									if trainer.args.evaluation_strategy == IntervalStrategy.STEPS and step % trainer.args.eval_steps == 0:
											expected_events += evaluation_events.copy()
									if step % trainer.args.save_steps == 0:
											expected_events.append("on_save"       )
							expected_events.append("on_epoch_end"       )
							if trainer.args.evaluation_strategy == IntervalStrategy.EPOCH:
									expected_events += evaluation_events.copy()
					expected_events += ["on_log", "on_train_end"]
					return expected_events




			def 							lowerCAmelCase__			(						self     :   Dict       )      ->Optional[Any]:



					'''simple docstring'''







					_UpperCAmelCase						:				List[str]	    =					self.get_trainer()
					_UpperCAmelCase						:				Any	    =					DEFAULT_CALLBACKS.copy() + [ProgressCallback]
					self.check_callbacks_equality(trainer.callback_handler.callbacks		,     lowerCamelCase__       )

					# Callbacks passed at init are added to the default callbacks
					_UpperCAmelCase						:				List[Any]	    =					self.get_trainer(callbacks=[MyTestTrainerCallback]       )
					expected_callbacks.append(lowerCamelCase__       )
					self.check_callbacks_equality(trainer.callback_handler.callbacks		,     lowerCamelCase__       )

					# TrainingArguments.disable_tqdm controls if use ProgressCallback or PrinterCallback
					_UpperCAmelCase						:				Optional[Any]	    =					self.get_trainer(disable_tqdm=lowerCamelCase__       )
					_UpperCAmelCase						:				Tuple	    =					DEFAULT_CALLBACKS.copy() + [PrinterCallback]
					self.check_callbacks_equality(trainer.callback_handler.callbacks		,     lowerCamelCase__       )




			def 							lowerCAmelCase__			(						self     :   int       )      ->Tuple:



					'''simple docstring'''







					_UpperCAmelCase						:				Union[str, Any]	    =					DEFAULT_CALLBACKS.copy() + [ProgressCallback]
					_UpperCAmelCase						:				Dict	    =					self.get_trainer()

					# We can add, pop, or remove by class name
					trainer.remove_callback(lowerCamelCase__       )
					expected_callbacks.remove(lowerCamelCase__       )
					self.check_callbacks_equality(trainer.callback_handler.callbacks		,     lowerCamelCase__       )

					_UpperCAmelCase						:				Any	    =					self.get_trainer()
					_UpperCAmelCase						:				str	    =					trainer.pop_callback(lowerCamelCase__       )
					self.assertEqual(cb.__class__		,     lowerCamelCase__       )
					self.check_callbacks_equality(trainer.callback_handler.callbacks		,     lowerCamelCase__       )

					trainer.add_callback(lowerCamelCase__       )
					expected_callbacks.insert(0		,     lowerCamelCase__       )
					self.check_callbacks_equality(trainer.callback_handler.callbacks		,     lowerCamelCase__       )

					# We can also add, pop, or remove by instance
					_UpperCAmelCase						:				int	    =					self.get_trainer()
					_UpperCAmelCase						:				List[Any]	    =					trainer.callback_handler.callbacks[0]
					trainer.remove_callback(lowerCamelCase__       )
					expected_callbacks.remove(lowerCamelCase__       )
					self.check_callbacks_equality(trainer.callback_handler.callbacks		,     lowerCamelCase__       )

					_UpperCAmelCase						:				Tuple	    =					self.get_trainer()
					_UpperCAmelCase						:				List[Any]	    =					trainer.callback_handler.callbacks[0]
					_UpperCAmelCase						:				Dict	    =					trainer.pop_callback(lowerCamelCase__       )
					self.assertEqual(lowerCamelCase__		,     lowerCamelCase__       )
					self.check_callbacks_equality(trainer.callback_handler.callbacks		,     lowerCamelCase__       )

					trainer.add_callback(lowerCamelCase__       )
					expected_callbacks.insert(0		,     lowerCamelCase__       )
					self.check_callbacks_equality(trainer.callback_handler.callbacks		,     lowerCamelCase__       )






			def 							lowerCAmelCase__			(						self     :   Optional[Any]       )      ->Dict:



					'''simple docstring'''







					import warnings

					# XXX: for now ignore scatter_gather warnings in this test since it's not relevant to what's being tested
					warnings.simplefilter(action="ignore"		,     category=lowerCamelCase__       )

					_UpperCAmelCase						:				Union[str, Any]	    =					self.get_trainer(callbacks=[MyTestTrainerCallback]       )
					trainer.train()
					_UpperCAmelCase						:				Any	    =					trainer.callback_handler.callbacks[-2].events
					self.assertEqual(lowerCamelCase__		,     self.get_expected_events(lowerCamelCase__       )       )

					# Independent log/save/eval
					_UpperCAmelCase						:				Dict	    =					self.get_trainer(callbacks=[MyTestTrainerCallback]		,     logging_steps=5       )
					trainer.train()
					_UpperCAmelCase						:				Dict	    =					trainer.callback_handler.callbacks[-2].events
					self.assertEqual(lowerCamelCase__		,     self.get_expected_events(lowerCamelCase__       )       )

					_UpperCAmelCase						:				Optional[int]	    =					self.get_trainer(callbacks=[MyTestTrainerCallback]		,     save_steps=5       )
					trainer.train()
					_UpperCAmelCase						:				Any	    =					trainer.callback_handler.callbacks[-2].events
					self.assertEqual(lowerCamelCase__		,     self.get_expected_events(lowerCamelCase__       )       )

					_UpperCAmelCase						:				Union[str, Any]	    =					self.get_trainer(callbacks=[MyTestTrainerCallback]		,     eval_steps=5		,     evaluation_strategy="steps"       )
					trainer.train()
					_UpperCAmelCase						:				Tuple	    =					trainer.callback_handler.callbacks[-2].events
					self.assertEqual(lowerCamelCase__		,     self.get_expected_events(lowerCamelCase__       )       )

					_UpperCAmelCase						:				str	    =					self.get_trainer(callbacks=[MyTestTrainerCallback]		,     evaluation_strategy="epoch"       )
					trainer.train()
					_UpperCAmelCase						:				Any	    =					trainer.callback_handler.callbacks[-2].events
					self.assertEqual(lowerCamelCase__		,     self.get_expected_events(lowerCamelCase__       )       )

					# A bit of everything
					_UpperCAmelCase						:				Union[str, Any]	    =					self.get_trainer(
					    callbacks=[MyTestTrainerCallback]		,     logging_steps=3		,     save_steps=10		,     eval_steps=5		,     evaluation_strategy="steps"		,     )
					trainer.train()
					_UpperCAmelCase						:				Optional[Any]	    =					trainer.callback_handler.callbacks[-2].events
					self.assertEqual(lowerCamelCase__		,     self.get_expected_events(lowerCamelCase__       )       )

					# warning should be emitted for duplicated callbacks
					with patch("transformers.trainer_callback.logger.warning"       ) as warn_mock:
							_UpperCAmelCase						:				int	    =					self.get_trainer(
							    callbacks=[MyTestTrainerCallback, MyTestTrainerCallback]		,     )
							assert str(lowerCamelCase__       ) in warn_mock.call_args[0][0]







'''simple docstring'''


from __future__ import annotations

import json

import requests
from bsa import BeautifulSoup
from fake_useragent import UserAgent

lowerCamelCase__					=   {'UserAgent': UserAgent().random}






def        __lowerCAmelCase       (__lowerCAmelCase      ):
		_UpperCAmelCase						:				List[str]	    =					script.contents[0]
		_UpperCAmelCase						:				Any	    =					json.loads(data[data.find("{\"config\""      ) : -1]      )
		return info["entry_data"]["ProfilePage"][0]["graphql"]["user"]






class 		lowerCAmelCase__	:




			def __init__(						self     :   str		,     lowerCamelCase__     :   List[Any]       )      ->List[str]:



					'''simple docstring'''







					_UpperCAmelCase						:				List[Any]	    =					F"""https://www.instagram.com/{username}/"""
					_UpperCAmelCase						:				Dict	    =					self.get_json()




			def 							lowerCAmelCase__			(						self     :   Any       )      ->dict:



					'''simple docstring'''







					_UpperCAmelCase						:				Optional[int]	    =					requests.get(self.url		,     headers=lowerCamelCase__       ).text
					_UpperCAmelCase						:				Any	    =					BeautifulSoup(lowerCamelCase__		,     "html.parser"       ).find_all("script"       )
					try:
							return extract_user_profile(scripts[4]       )
					except (json.decoder.JSONDecodeError, KeyError):
							return extract_user_profile(scripts[3]       )




			def __repr__(						self     :   Optional[Any]       )      ->str:



					'''simple docstring'''







					return F"""{self.__class__.__name__}('{self.username}')"""




			def __str__(						self     :   List[Any]       )      ->str:



					'''simple docstring'''







					return F"""{self.fullname} ({self.username}) is {self.biography}"""




			@property
			def 							lowerCAmelCase__			(						self     :   Optional[int]       )      ->str:



					'''simple docstring'''







					return self.user_data["username"]




			@property
			def 							lowerCAmelCase__			(						self     :   Optional[int]       )      ->str:



					'''simple docstring'''







					return self.user_data["full_name"]




			@property
			def 							lowerCAmelCase__			(						self     :   Optional[int]       )      ->str:



					'''simple docstring'''







					return self.user_data["biography"]




			@property
			def 							lowerCAmelCase__			(						self     :   int       )      ->str:



					'''simple docstring'''







					return self.user_data["business_email"]




			@property
			def 							lowerCAmelCase__			(						self     :   str       )      ->str:



					'''simple docstring'''







					return self.user_data["external_url"]




			@property
			def 							lowerCAmelCase__			(						self     :   Tuple       )      ->int:



					'''simple docstring'''







					return self.user_data["edge_followed_by"]["count"]




			@property
			def 							lowerCAmelCase__			(						self     :   str       )      ->int:



					'''simple docstring'''







					return self.user_data["edge_follow"]["count"]




			@property
			def 							lowerCAmelCase__			(						self     :   Any       )      ->int:



					'''simple docstring'''







					return self.user_data["edge_owner_to_timeline_media"]["count"]




			@property
			def 							lowerCAmelCase__			(						self     :   List[Any]       )      ->str:



					'''simple docstring'''







					return self.user_data["profile_pic_url_hd"]




			@property
			def 							lowerCAmelCase__			(						self     :   Optional[Any]       )      ->bool:



					'''simple docstring'''







					return self.user_data["is_verified"]






			@property
			def 							lowerCAmelCase__			(						self     :   int       )      ->bool:



					'''simple docstring'''







					return self.user_data["is_private"]






def        __lowerCAmelCase       (__lowerCAmelCase = "github"      ):
		import os

		if os.environ.get("CI"      ):
				return  # test failing on GitHub Actions
		_UpperCAmelCase						:				Dict	    =					InstagramUser(__lowerCAmelCase      )
		assert instagram_user.user_data
		assert isinstance(instagram_user.user_data					,       __lowerCAmelCase      )
		assert instagram_user.username == username
		if username != "github":
				return
		assert instagram_user.fullname == "GitHub"
		assert instagram_user.biography == "Built for developers."
		assert instagram_user.number_of_posts > 150
		assert instagram_user.number_of_followers > 120_000
		assert instagram_user.number_of_followings > 15
		assert instagram_user.email == "support@github.com"
		assert instagram_user.website == "https://github.com/readme"
		assert instagram_user.profile_picture_url.startswith("https://instagram."      )
		assert instagram_user.is_verified is True
		assert instagram_user.is_private is False


if __name__ == "__main__":
						import doctest

						doctest.testmod()
						lowerCamelCase__					=   InstagramUser('github')
						print(instagram_user)
						print(F'''{instagram_user.number_of_posts = }''')
						print(F'''{instagram_user.number_of_followers = }''')
						print(F'''{instagram_user.number_of_followings = }''')
						print(F'''{instagram_user.email = }''')
						print(F'''{instagram_user.website = }''')
						print(F'''{instagram_user.profile_picture_url = }''')
						print(F'''{instagram_user.is_verified = }''')
						print(F'''{instagram_user.is_private = }''')



