Best Python code snippet using pytest-django_python
test_day_16.py
Source:test_day_16.py
1import unittest2import day_163test_input_1 = 'D2FE28'4test_input_2 = '38006F45291200'5test_input_3 = 'EE00D40C823060'6test_input_4 = '8A004A801A8002F478'7class TestDay16(unittest.TestCase):8 def test_parse_input(self):9 self.assertEqual(10 day_16.parse_input(test_input_1),11 '110100101111111000101000'12 )13 self.assertEqual(14 day_16.parse_input(test_input_2),15 '00111000000000000110111101000101001010010001001000000000'16 )17 self.assertEqual(18 day_16.parse_input(test_input_3),19 '11101110000000001101010000001100100000100011000001100000'20 )21 def test_read_packet(self):22 test_packet_str_1 = day_16.parse_input(test_input_1)23 test_packet_1 = day_16.get_bits(test_packet_str_1)24 self.assertEqual(25 day_16.read_packet(test_packet_1),26 [{'v': 6, 't': 4, 'value': 2021}]27 )28 test_packet_str_2 = day_16.parse_input(test_input_2)29 test_packet_2 = day_16.get_bits(test_packet_str_2)30 self.assertEqual(31 day_16.read_packet(test_packet_2),32 [{33 'v': 1,34 't': 6,35 'sub': [36 {'v': 6, 't': 4, 'value': 10},37 {'v': 2, 't': 4, 'value': 20}38 ]39 }]40 )41 test_packet_str_3 = day_16.parse_input(test_input_3)42 test_packet_3 = day_16.get_bits(test_packet_str_3)43 self.assertEqual(44 day_16.read_packet(test_packet_3),45 [{46 'v': 7,47 't': 3,48 'sub': [49 {'v': 2, 't': 4, 'value': 1},50 {'v': 4, 't': 4, 'value': 2},51 {'v': 1, 't': 4, 'value': 3}52 ]53 }]54 )55 def test_sum_versions(self):56 test_packet_str_4 = day_16.parse_input(57 '8A004A801A8002F478')58 test_packet_4 = day_16.get_bits(test_packet_str_4)59 test_trans_4 = day_16.read_packet(test_packet_4)60 self.assertEqual(61 day_16.sum_v(test_trans_4),62 1663 )64 test_packet_str_5 = day_16.parse_input(65 '620080001611562C8802118E34')66 test_packet_5 = day_16.get_bits(test_packet_str_5)67 test_trans_5 = day_16.read_packet(test_packet_5)68 self.assertEqual(69 day_16.sum_v(test_trans_5),70 1271 )72 test_packet_str_6 = day_16.parse_input(73 'C0015000016115A2E0802F182340')74 test_packet_6 = day_16.get_bits(test_packet_str_6)75 test_trans_6 = day_16.read_packet(test_packet_6)76 self.assertEqual(77 day_16.sum_v(test_trans_6),78 2379 )80 test_packet_str_7 = day_16.parse_input(81 'A0016C880162017C3686B18A3D4780')82 test_packet_7 = day_16.get_bits(test_packet_str_7)83 test_trans_7 = day_16.read_packet(test_packet_7)84 self.assertEqual(85 day_16.sum_v(test_trans_7),86 3187 )88 def test_get_val(self):89 test_packet_str = day_16.parse_input(90 'C200B40A82')91 test_packet = day_16.get_bits(test_packet_str)92 test_trans = day_16.read_packet(test_packet)93 self.assertEqual(day_16.get_value(test_trans), [3])94 test_packet_str = day_16.parse_input(95 '04005AC33890')96 test_packet = day_16.get_bits(test_packet_str)97 test_trans = day_16.read_packet(test_packet)98 self.assertEqual(day_16.get_value(test_trans), [54])99 test_packet_str = day_16.parse_input(100 '880086C3E88112')101 test_packet = day_16.get_bits(test_packet_str)102 test_trans = day_16.read_packet(test_packet)103 self.assertEqual(day_16.get_value(test_trans), [7])104 test_packet_str = day_16.parse_input(105 'CE00C43D881120')106 test_packet = day_16.get_bits(test_packet_str)107 test_trans = day_16.read_packet(test_packet)108 self.assertEqual(day_16.get_value(test_trans), [9])109 test_packet_str = day_16.parse_input(110 'D8005AC2A8F0')111 test_packet = day_16.get_bits(test_packet_str)112 test_trans = day_16.read_packet(test_packet)113 self.assertEqual(day_16.get_value(test_trans), [1])114 test_packet_str = day_16.parse_input(115 'F600BC2D8F')116 test_packet = day_16.get_bits(test_packet_str)117 test_trans = day_16.read_packet(test_packet)118 self.assertEqual(day_16.get_value(test_trans), [0])119 test_packet_str = day_16.parse_input(120 '9C005AC2F8F0')121 test_packet = day_16.get_bits(test_packet_str)122 test_trans = day_16.read_packet(test_packet)123 self.assertEqual(day_16.get_value(test_trans), [0])124 test_packet_str = day_16.parse_input(125 '9C0141080250320F1802104A08')126 test_packet = day_16.get_bits(test_packet_str)127 test_trans = day_16.read_packet(test_packet)128 self.assertEqual(day_16.get_value(test_trans), [1])129if __name__ == '__main__':...
bank.py
Source:bank.py
1from domain.get_and_set import set_sum, set_day, set_type, get_trans, get_type, get_undo_list, set_undo_list,\2 set_trans_list3from domain.transaction import create_dict4def generate_trans():5 return [{'12', '123', 'intrare'}, {'13', '345', 'iesire'}, {'14', '567', 'intrare'}, {'15', '789', 'iesire'}, {'16', '897', 'int'}]6def setup_bank():7 mylist = []8 return mylist9def modify_day_in_transaction(mylist, new_day):10 set_day(mylist, new_day)11def modify_sum_in_transaction(mylist, new_sum):12 set_sum(mylist, new_sum)13def modify_type_in_transaction(mylist, new_type):14 set_type(mylist, new_type)15def remove_from_dict_by_day(mylist, my_day):16 for i in range(len(mylist)):17 if mylist[i]['zi'] != my_day:18 del mylist[i]19 break20 return mylist21def remove_from_dict_by_dayinterval(mylist, my_day1, my_day2):22 for i in range(len(mylist)):23 if my_day1 < mylist[i]['zi'] < my_day2:24 del mylist[i]25 break26 elif my_day1 == mylist[i]['zi'] or my_day2 == mylist[i]['zi']:27 del mylist[i]28 break29 return mylist30def remove_from_dict_by_type(mylist, mytype):31 for i in range(len(mylist)):32 if mylist[i]['zi'] != mytype:33 del mylist[i]34 break35 return mylist36def search_dict_greater_than(mylist, mysum):37 res = list(filter(lambda i: i['suma'] > mysum, mylist))38 return res39def search_dict_greater_than_and_before(mylist, mysum, myday):40 res = list(filter(lambda i: (i['suma'] > mysum and i['zi'] < myday), mylist))41 return res42def search_dict_by_type(mylist, mytype):43 res = list(filter(lambda i: i['tipul'] == mytype, mylist))44 return res45def sum_type(mylist, mytype):46 s = 047 for i in range(len(mylist)):48 if mylist[i]['tipul'] == mytype:49 s = s + mylist[i]['suma']50 return s51def sold_date(mylist, myday):52 s = 053 s1 = 054 for i in range(len(mylist)):55 if mylist[i]['zi'] <= myday and mylist[i]['tipul'] == "intrare":56 s = s + mylist[i]['suma']57 if mylist[i]['zi'] <= myday and mylist[i]['tipul'] == "iesire":58 s1 = s1 + mylist[i]['suma']59 sf = s - s160 return sf61def sortingkey(e):62 return e['suma']63def sort_trans_by_sum(mylist, mytype):64 res = list(filter(lambda i: i['tipul'] == mytype, mylist))65 res.sort(key=sortingkey)66 return res67def elim_trans_by_type(mylist, mytype):68 res = list(filter(lambda i: i['tipul'] != mytype, mylist))69 return res70def elim_trans_by_type_and_sum(mylist, mytype, mysum):71 res = list(filter(lambda i: (i['tipul'] == mytype and i['suma'] < mysum), mylist))72 return res73def undo(mylist):74 undo_list = get_undo_list(mylist)75 if len(undo_list) == 0:76 raise ValueError("Nu se mai poate face undo.")77 else:78 previous_list = undo_list[-1]79 set_trans_list(mylist, previous_list)80 set_undo_list(mylist, undo_list[:-1])81# tests82def test_remove_by_type():83 test_list1 = generate_trans()84 initial_length = len(test_list1)85 test_list1 = elim_trans_by_type(test_list1, 'iesire')86 assert (len(test_list1) == initial_length - 2)87 test_list2 = []88 test_list2 = elim_trans_by_type(test_list2, 'iesire')89 assert (len(test_list2) == 0)90def test_elim_trans_by_type():91 test_trans = setup_bank()92 create_dict(test_trans, 69, 420, 'intrare')93 create_dict(test_trans, 7, 12, 'iesire')94 assert (len(get_trans(test_trans)) == 2)95 elim_trans_by_type(setup_bank, 'iesire')96 assert (len(get_trans(test_trans)) == 1)97def test_filter_by_name():98 test_list = []99 filtered_list1 = search_dict_by_type(test_list, 'ies')100 assert (len(filtered_list1) == 0)101 test_list2 = generate_trans()102 filtered_list2 = search_dict_by_type(test_list2, 'iesire')103 assert (len(filtered_list2) == 2)104 filtered_list3 = search_dict_by_type(test_list2, 'int')105 assert (len(filtered_list3) == 1)106 assert (get_type(filtered_list3[0]) == 'int')107 filtered_list4 = search_dict_by_type(test_list2, 'schimbare')108 assert (len(filtered_list4) == 0)109 filtered_list5 = search_dict_by_type(test_list2, '')110 assert (len(filtered_list5) == 0)111def test_elim_trans_by_type_and_sum():112 test_trans = setup_bank()113 create_dict(test_trans, 69, 420, 'intrare')114 create_dict(test_trans, 7, 12, 'iesire')115 assert (len(get_trans(test_trans)) == 2)116 elim_trans_by_type_and_sum(setup_bank, 'iesire', 100)117 assert (len(get_trans(test_trans)) == 1)118def test_sort_trans_by_sum():119 test_list = []120 filtered_list1 = sort_trans_by_sum(test_list, 'iesire')...
utils.py
Source:utils.py
1import numpy as np2import pandas as pd3import matplotlib.pyplot as plt4from sklearn.decomposition import PCA5from sklearn.neighbors import NearestNeighbors6import math7from kneebow.rotor import Rotor8""" Transform """9def transfrom_all_data(transformer, train, test, feature_list):10 """11 Apply transformer to train and test features12 13 Usage:14 logTrans = FunctionTransformer(np.log1p)15 train_trans, test_trans = transfrom_all_data(transformer, train, test, feature_list)16 """17 train_trans = transformer.fit_transform(train[feature_list])18 test_trans = transformer.transform(test[feature_list])19 20 if type(train_trans) != np.ndarray:21 train_trans = np.array(train_trans)22 test_trans = np.array(test_trans)23 24 return train_trans, test_trans25def make_features(transformer, train, test, feature_list, name, normalize=False, scaler=None):26 """27 Add newly generated transformed features to train and test dataframe28 29 Usage:30 scaler = StandardScaler()31 logTrans = FunctionTransformer(np.log1p)32 train_X, val_X = make_features(qTrans, train_X, val_X, feature_list=range(10), name="qTrans", normalize=False, scaler=scaler)33 """34 train, test = train.copy(), test.copy()35 train_trans, test_trans = transfrom_all_data(transformer, train, test, feature_list)36 37 if normalize and scaler is not None:38 train_trans = scaler.fit_transform(train_trans).astype(np.float32)39 test_trans = scaler.transform(test_trans).astype(np.float32)40 41 for i in range(train_trans.shape[1]):42 train['{0}_{1}'.format(name, i)] = train_trans[:, i]43 test['{0}_{1}'.format(name, i)] = test_trans[:, i]44 45 return train, test46""" Optimal Parameters """47def calculate_kn_distance(X, neigh=2):48 neigh = NearestNeighbors(n_neighbors=neigh)49 nbrs = neigh.fit(X)50 distances, indices = nbrs.kneighbors(X)51 return distances[:,1:].reshape(-1)52def get_eps(X, neigh=2):53 eps_dist = np.sort(calculate_kn_distance(X, neigh=neigh))54 plt.hist(eps_dist, bins=60)55 plt.ylabel('n');56 plt.xlabel('Epsilon distance');57 plt.show()58 59 rotor = Rotor()60 curve_xy = np.concatenate([np.arange(eps_dist.shape[0]).reshape(-1, 1), eps_dist.reshape(-1, 1)],1)61 rotor.fit_rotate(curve_xy)62 rotor.plot_elbow()63 e_idx = rotor.get_elbow_index()64 65 return curve_xy[e_idx]66def calc_optimal_components(X, var_thr=0.95):67 """68 Find optimal numbers of PCA components69 70 Usage:71 calc_optimal_components(X)72 """73 74 pca = PCA(min(X.shape))75 pca.fit(X)76 var_ratio_cum = pca.explained_variance_ratio_.cumsum()77 n_comp = np.where(var_ratio_cum>=var_thr)[0][0]78 plt.plot(range(min(X.shape)), var_ratio_cum, "o-")79 plt.vlines([n_comp], ymin=var_ratio_cum[0], ymax=var_ratio_cum[n_comp], linestyles="--", color="red", alpha=0.5)80 plt.plot([n_comp], var_ratio_cum[n_comp], marker="x", markersize=12)81 82 print("n_components: ", n_comp)...
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!!