Best Python code snippet using lemoncheesecake
test_feature_engineering.py
Source:test_feature_engineering.py
1import pytest2import pandas as pd34from src.features.feature_engineering import (standardize_features,5 discretize_features,6 encode_features)789@pytest.fixture(scope='function')10def dataframes():11 """Return a tuple of pandas DataFrame objects. The first element of the12 tuple is considered as a subset of a dataset intended for training and the13 second one - for testing.14 """15 in_train = pd.DataFrame({'col1': [1, 4, 3, 7, 2],16 'col2': ['a', 'a', 'b', 'c', 'c']})17 in_test = pd.DataFrame({'col1': [4, 5, 3, 4, 10],18 'col2': ['c', 'b', 'b', 'a', 'b']})1920 return in_train, in_test212223def test_standardization(dataframes):24 in_train = dataframes[0]25 in_test = dataframes[1]2627 in_train_mean = in_train['col1'].mean()28 in_train_std = in_train['col1'].std(ddof=0)2930 out_train, out_test = standardize_features(in_train,31 in_test,32 cols=['col1'])3334 expected_col1_train = (in_train['col1'] - in_train_mean) / in_train_std35 expected_col1_test = (in_test['col1'] - in_train_mean) / in_train_std3637 expected_train = pd.DataFrame({'col1': expected_col1_train,38 'col2': in_train['col2']})39 expected_test = pd.DataFrame({'col1': expected_col1_test,40 'col2': in_test['col2']})4142 assert out_train.equals(expected_train) and out_test.equals(expected_test)434445def test_if_mean_zero(dataframes):46 in_train = dataframes[0]47 in_test = dataframes[1]4849 out_train, out_test = standardize_features(in_train,50 in_test,51 cols=['col1'])5253 assert pytest.approx(out_train['col1'].mean()) == 0545556def test_if_std_one(dataframes):57 in_train = dataframes[0]58 in_test = dataframes[1]5960 out_train, out_test = standardize_features(in_train,61 in_test,62 cols=['col1'])6364 assert pytest.approx(out_train['col1'].std(ddof=0)) == 1656667@pytest.mark.parametrize('function', [68 standardize_features,69 discretize_features,70 encode_features71])72def test_default_no_col(dataframes, function):73 in_train = dataframes[0]74 in_test = dataframes[1]7576 with pytest.raises(ValueError):77 function(in_train, in_test)787980@pytest.mark.parametrize('function', [81 standardize_features,82 discretize_features83])84def test_text_col(dataframes, function):85 in_train = dataframes[0]86 in_test = dataframes[1]8788 with pytest.raises(ValueError):89 function(in_train, in_test, cols=['col2'])909192def test_one_bin(dataframes):93 in_train = dataframes[0]94 in_test = dataframes[1]9596 with pytest.raises(ValueError):97 discretize_features(in_train, in_test, ['col1'], cat_number=1)9899100def test_discretization(dataframes):101 in_train = dataframes[0]102 in_test = dataframes[1]103104 out_train, out_test = discretize_features(in_train,105 in_test,106 cols=['col1'],107 cat_number=3)108109 expected_train = pd.DataFrame({'col1': [0, 1, 1, 2, 0],110 'col2': in_train['col2']})111 expected_test = pd.DataFrame({'col1': [1, 2, 1, 1, 2],112 'col2': in_test['col2']})113114 assert out_train.equals(expected_train) and out_test.equals(expected_test)115116117def test_encoding(dataframes):118 in_train = dataframes[0]119 in_test = dataframes[1]120121 out_train, out_test = encode_features(in_train,122 in_test,123 cols=['col1', 'col2'])124125 expected_train = pd.DataFrame({'col1_2': [0, 0, 0, 0, 1],126 'col1_3': [0, 0, 1, 0, 0],127 'col1_4': [0, 1, 0, 0, 0],128 'col1_7': [0, 0, 0, 1, 0],129 'col2_b': [0, 0, 1, 0, 0],130 'col2_c': [0, 0, 0, 1, 1]})131132 expected_test = pd.DataFrame({'col1_2': [0, 0, 0, 0, 0],133 'col1_3': [0, 0, 1, 0, 0],134 'col1_4': [1, 0, 0, 1, 0],135 'col1_7': [0, 0, 0, 0, 0],136 'col2_b': [0, 1, 1, 0, 1],137 'col2_c': [1, 0, 0, 0, 0]})138139 assert out_train.equals(expected_train) and out_test.equals(expected_test)140141142def test_cols_number(dataframes):143 in_train = dataframes[0]144 in_test = dataframes[1]145146 out_train, out_test = encode_features(in_train, in_test, ['col1'])147 out_cols = len(out_train.columns), len(out_test.columns)148
...
test_range_in.py
Source:test_range_in.py
...20def test_in_storage_list(get_contract_with_gas_estimation):21 code = """22allowed: int128[10]23@external24def in_test(x: int128) -> bool:25 self.allowed = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]26 if x in self.allowed:27 return True28 return False29 """30 c = get_contract_with_gas_estimation(code)31 assert c.in_test(1) is True32 assert c.in_test(9) is True33 assert c.in_test(11) is False34 assert c.in_test(-1) is False35 assert c.in_test(32000) is False36def test_in_calldata_list(get_contract_with_gas_estimation):37 code = """38@external39def in_test(x: int128, y: int128[10]) -> bool:40 if x in y:41 return True42 return False43 """44 c = get_contract_with_gas_estimation(code)45 assert c.in_test(1, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) is True46 assert c.in_test(9, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) is True47 assert c.in_test(11, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) is False48 assert c.in_test(-1, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) is False49 assert c.in_test(32000, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) is False50def test_cmp_in_list(get_contract_with_gas_estimation):51 code = """52@external53def in_test(x: int128) -> bool:54 if x in [9, 7, 6, 5]:55 return True56 return False57 """58 c = get_contract_with_gas_estimation(code)59 assert c.in_test(1) is False60 assert c.in_test(-7) is False61 assert c.in_test(-9) is False62 assert c.in_test(5) is True63 assert c.in_test(7) is True64def test_cmp_not_in_list(get_contract_with_gas_estimation):65 code = """66@external67def in_test(x: int128) -> bool:68 if x not in [9, 7, 6, 5]:69 return True70 return False71 """72 c = get_contract_with_gas_estimation(code)73 assert c.in_test(1) is True74 assert c.in_test(-7) is True75 assert c.in_test(-9) is True76 assert c.in_test(5) is False77 assert c.in_test(7) is False78def test_mixed_in_list(assert_compile_failed, get_contract_with_gas_estimation):79 code = """80@external81def testin() -> bool:82 s: int128[4] = [1, 2, 3, 4]83 if "test" in s:84 return True85 return False86 """87 assert_compile_failed(lambda: get_contract_with_gas_estimation(code), TypeMismatch)88def test_ownership(w3, assert_tx_failed, get_contract_with_gas_estimation):89 code = """90owners: address[2]91@external...
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!!