Best Python code snippet using selenium-respectful_python
app.py
Source:app.py
...18import metrics19app = typer.Typer()20@app.command()21def train(config_file: str):22 estimator_config = _load_config(config_file, "estimator")23 split = "train"24 X, y = _get_dataset(_load_config(config_file, "data"), splits=[split])[split]25 estimator = model.build_estimator(estimator_config)26 estimator.fit(X, y)27 output_dir = _load_config(config_file, "export")["output_dir"]28 _save_versioned_estimator(estimator, estimator_config, output_dir)29def _get_dataset(data_config, splits):30 filepath = data_config["filepath"]31 reader = partial(pd.read_csv, filepath_or_buffer=filepath)32 return data.get_dataset(reader=reader, splits=splits)33def _save_versioned_estimator(34 estimator: BaseEstimator, config: model.EstimatorConfig, output_dir: str35):36 version = datetime.now(timezone.utc).strftime("%Y-%m-%d %H-%M")37 model_dir = os.path.join(output_dir, version)38 os.makedirs(model_dir, exist_ok=True)39 try:40 joblib.dump(estimator, os.path.join(model_dir, "model.joblib"))41 _save_yaml(config, os.path.join(model_dir, "params.yml"))42 except Exception as e:43 typer.echo(f"Coudln't serialize model due to error {e}")44 shutil.rmtree(model_dir)45@app.command()46def find_hyperparams(47 config_file: str,48):49 search_config = _load_config(config_file, "search")50 param_grid = search_config["grid"]51 n_jobs = search_config["jobs"]52 metric = _load_config(config_file, "metrics")[0]53 estimator_config = _load_config(config_file, "estimator")54 estimator = model.build_estimator(estimator_config)55 scoring = metrics.get_scoring_function(metric["name"], **metric["params"])56 gs = GridSearchCV(57 estimator,58 _param_grid_to_sklearn_format(param_grid),59 n_jobs=n_jobs,60 scoring=scoring,61 verbose=3,62 )63 split = "train"64 X, y = _get_dataset(_load_config(config_file, "data"), splits=[split])[split]65 gs.fit(X, y)66 estimator_config = _param_grid_to_custom_format(gs.best_params_)67 estimator = gs.best_estimator_68 output_dir = _load_config(config_file, "export")["output_dir"]69 _save_versioned_estimator(estimator, estimator_config, output_dir)70def _param_grid_to_sklearn_format(param_grid):71 result = {72 f"{spec['name']}__{pname}": pvalues73 for spec in param_grid74 for pname, pvalues in spec["params"].items()75 }76 return result77def _param_grid_to_custom_format(param_grid):78 grid = {}79 for name, values in param_grid.items():80 estimator_name, param_name = name.split("__", maxsplit=1)81 if estimator_name not in grid:82 grid[estimator_name] = {}83 grid[estimator_name][param_name] = values84 result = grid85 result = [{"name": name, "hparams": params} for name, params in grid.items()]86 return result87@app.command()88def eval(89 config_file: str,90 model_version: str,91 splits: t.List[str] = ["test"],92):93 output_dir = _load_config(config_file, "export")["output_dir"]94 saved_model = os.path.join(output_dir, model_version, "model.joblib")95 estimator = joblib.load(saved_model)96 dataset = _get_dataset(_load_config(config_file, "data"), splits=splits)97 report = defaultdict(list)98 all_metrics = _load_config(config_file, "metrics")99 for name, (X, y) in dataset.items():100 y_pred = estimator.predict(X)101 for m in all_metrics:102 metric_name, params = m["name"], m["params"]103 fn = metrics.get_metric_function(metric_name, **params)104 value = float(fn(y, y_pred))105 report[metric_name].append({"split": name, "value": value})106 reports_dir = _load_config(config_file, "reports")["dir"]107 os.makedirs(reports_dir, exist_ok=True)108 _save_yaml(109 dict(report),110 os.path.join(reports_dir, f"{model_version}.yml"),111 )112def _load_config(filepath: str, key: str):113 content = _load_yaml(filepath)114 config = content[key]115 return config116@lru_cache(None)117def _load_yaml(filepath: str) -> t.Dict[str, t.Any]:118 with open(filepath, "r") as f:119 content = yaml.load(f)120 return content121def _save_yaml(content: t.Dict[str, t.Any], filepath: str):122 with open(filepath, "w") as f:123 yaml.dump(content, f)124if __name__ == "__main__":...
config.py
Source:config.py
...9 Config.__CONFIG_FILE = path + '\\' + filename10 @staticmethod11 def set_general_option(option, value):12 """ Set an option in the general section """13 Config._load_config()14 Config._set('general', option, value)15 @staticmethod16 def set_requests_option(option, value):17 """ Set an option in the torrent section """18 Config._load_config()19 Config._set('requests', option, value)20 # ADD MORE SECTIONS HERE21 @staticmethod22 def option_exists(section, option):23 """ Returns True if the option exists in the specified section """24 Config._load_config()25 return Config.__config_parser.has_option(section, option)26 @staticmethod27 def getboolean(section, option):28 """ Get a boolean from the specified section """29 Config._load_config()30 return Config.__config_parser.getboolean(section, option)31 @staticmethod32 def getint(section, option):33 """ Get an int from the specified section """34 Config._load_config()35 return Config.__config_parser.getint(section, option)36 @staticmethod37 def getfloat(section, option):38 """ Get a float from the specified section """39 Config._load_config()40 return Config.__config_parser.getfloat(section, option)41 @staticmethod42 def getstring(section, option):43 """ Get a string from the specified section """44 Config._load_config()45 return Config.__config_parser.get(section, option)46 # Internal functions47 @staticmethod48 def _load_config():49 """ Loads the config file from disk into the config_parses object """50 if not Config.__config_loaded:51 Config.__config_loaded = True52 # Load the config file into the config_parses object53 try:54 with Config._open_config_file(read=True) as config_file:55 Config.__config_parser.readfp(fp=config_file)56 except IOError:57 pass # File does not exist, do not load it but don't create it either (what use is an empty file?)58 @staticmethod59 def _open_config_file(read=False, write=False):60 """ Open the config file for reading and/or writing """61 mode = ''62 if read: mode += 'r'...
test_config.py
Source:test_config.py
...7def _config_path():8 return os.path.abspath("account_foundations/config/all.yaml")910@pytest.fixture11def _load_config():12 return Config.load_config(os.path.abspath("account_foundations/config/all.yaml"))1314def test_config_loader(_load_config):15 yaml_config = _load_config16 assert yaml_config is not None17 assert isinstance(yaml_config, dict)1819def test_config_general_attributes(_load_config):20 yaml_config = _load_config21 assert "general" in yaml_config22 assert isinstance(yaml_config["general"], dict)23 assert "region" in yaml_config["general"]2425def test_config_vpc_attributes(_load_config):
...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!