Best Python code snippet using green
test_exquisitecorpse.py
Source:test_exquisitecorpse.py
1import os2import shutil3import tempfile4import pytest5from flask_migrate import init, migrate, upgrade6from exquisitecorpse import create_app, db, migrate7@pytest.fixture8def client():9 app = create_app()10 database_path = tempfile.mkstemp()11 app.config['DATABASE'] = "sql:///" + database_path[1]12 app.config['TESTING'] = True13 client = app.test_client()14 with app.app_context():15 upgrade()16 yield client17 os.close(database_path[0])18 os.unlink(database_path[1])19def login(client, username, password):20 return client.post('/auth/login', data=dict(21 username=username,22 password=password23 ), follow_redirects=True)24def register(client, username, password, repassword):25 return client.post('/auth/register', data=dict(26 username=username,27 password=password,28 re_entered_password=repassword29 ), follow_redirects=True)30def logout(client):31 return client.get('/auth/logout', follow_redirects=True)32def test_homepage(client):33 """Can we get to the homepage?"""34 rv = client.get('/')35 assert b'All corpses are completed! Please release a new corpse into the universe.' in rv.data36def test_register_login_logout(client):37 rv = register(client, 'test', 'testpass', 'testpass')38 assert b'You are now registered!' in rv.data39 rv = logout(client)40 assert b'Successfully logged out' in rv.data41 rv = login(client, 'test', 'testpass')42 assert b'Successfully logged in' in rv.data43 rv = logout(client)44 assert b'Successfully logged out' in rv.data45 46def test_bad_register(client):47 rv = register(client, None, None, None)48 assert b'Username is required' in rv.data49 assert b'Password is required' in rv.data50 logout(client)51 rv = register(client, None, 'testpass', 'testpass')52 assert b'Username is required' in rv.data53 logout(client)54 rv = register(client, 'test', None, 'testpass')55 assert b'Password is required' in rv.data56 assert b'Passwords do not match' in rv.data57 logout(client)58 rv = register(client, 'test', 'testpass', None)59 assert b'Passwords do not match' in rv.data60 logout(client)61 rv = register(client, None, None, 'testpass')62 assert b'Username is required' in rv.data63 assert b'Password is required' in rv.data64 assert b'Passwords do not match' in rv.data65 logout(client)66 rv = register(client, None, 'testpass', None)67 assert b'Username is required' in rv.data68 assert b'Passwords do not match' in rv.data69 logout(client)70 rv = register(client, 'test', None, None)71 assert b'Password is required' in rv.data72 logout(client)73 rv = register(client, 'test', 'testpass', 'nottestpass')74 assert b'Passwords do not match' in rv.data75 logout(client)76def test_bad_login(client):77 rv = register(client, 'test', 'testpass', 'testpass')78 rv = login(client, 'nottest', 'testpass')79 assert b'Incorrect username' in rv.data80 rv = login(client, 'test', 'nottestpass')81 assert b'Incorrect password' in rv.data82def test_bad_logout(client):83 pass84def test_double_register(client):85 rv = register(client, 'test', 'testpass', 'testpass')86 logout(client)87 rv = register(client, 'test', 'testpass', 'testpass')...
classifier.py
Source:classifier.py
1import numpy as np2import sys3def load_data(fileName, datasize):4 """ this function takes filepath as an argument and loads data inside a list"""5 f= open(fileName)6 data= []7 for i in range(datasize):8 line= f.readline()9 line= np.fromstring(line, dtype= 'int', sep=',')10 data.append(line)11 data= np.array(data)12 f.close()13 return data14def calculate_priors(class_occ):15 ones= class_occ.sum()16 prob_1= ones/ class_occ.shape[0]17 prob_0= 1- prob_118 return prob_0, prob_119def calculate_conditionals(data_normal, data_abnormal, p_n, p_ab):20 testpass_normal= data_normal.sum(axis=0)/data_normal.shape[0]21 testfail_normal= 1- testpass_normal22 testpass_normal= testpass_normal*p_n23 testfail_normal= testfail_normal*p_n 24 testpass_abnormal= data_abnormal.sum(axis=0)/data_abnormal.shape[0]25 testfail_abnormal= 1- testpass_abnormal26 testpass_abnormal= testpass_abnormal*p_ab27 testfail_abnormal= testfail_abnormal*p_ab 28 return testpass_normal,testfail_normal, testpass_abnormal, testfail_abnormal29def test(test_data, testpass_normal,testfail_normal, testpass_abnormal, testfail_abnormal):30 labels= []31 for i in range(test_data.shape[0]):32 a=np.prod((test_data[i]*testpass_normal)+((1-test_data[i])*testfail_normal)) 33 b=np.prod((test_data[i]*testpass_abnormal)+((1-test_data[i])*testfail_abnormal))34 if a<b:35 labels.append(0)36 else:37 labels.append(1)38 return labels39def accuracy(y_pred, y_true):40 correct = np.sum(y_pred==y_true)41 accuracy= correct/len(y_pred)42 return accuracy43def main():44 #load train and test files45 train_file= sys.argv[1]46 test_file = sys.argv[2]47 train_data= load_data(train_file, 80)48 test_data = load_data(test_file, 187)49 #caculate priors ie probabilty of normal and abnormal50 p_ab, p_n= calculate_priors(train_data[:,0])51 print("\nStarting to train on 80 data points .....")52 #calculate conditional probabililities for each test53 testpass_normal,testfail_normal, testpass_abnormal, testfail_abnormal= calculate_conditionals(train_data[:40,1:],train_data[40:,1:],p_n , p_ab)54 print("Training Complete")55 print("-------------------------------------------")56 print("Testing on 187 data points")57 labels= test(test_data[:,1:], testpass_normal,testfail_normal, testpass_abnormal, testfail_abnormal)58 acc= accuracy(labels, test_data[:,0])59 print("Total Accuracy= " + str(acc*100) + " %") ...
test_events.py
Source:test_events.py
1'''2Events3'''4from init import test, import_pymt_no_window5def unittest_dispatcher():6 import_pymt_no_window()7 from pymt import EventDispatcher8 class MyEventDispatcher(EventDispatcher):9 def on_test(self, *largs):10 pass11 global testpass, testargs12 testpass = False13 testargs = None14 def callbacktest(*largs):15 global testpass, testargs16 testpass = True17 testargs = largs18 def resettest():19 global testpass, testargs20 testpass = False21 testargs = None22 a = MyEventDispatcher()23 # test unknown event24 resettest()25 try:26 a.connect('on_test', callbacktest)27 except:28 testpass = True29 test('no register' and testpass)30 # register event + test31 resettest()32 a.register_event_type('on_test')33 try:34 a.connect('on_test', callbacktest)35 testpass = True36 except:37 pass38 test('register' and testpass)39 # test dispatch40 resettest()41 a.dispatch_event('on_test')42 test('dispatch' and testpass)43 test(testargs == ())44 resettest()45 a.dispatch_event('on_test', 123)46 test('disp+arg' and testpass)47 test(testargs == (123,))48 resettest()49 a.dispatch_event('on_test', 123, 'blhe')50 test('disp+2args' and testpass)51 test(testargs == (123, 'blhe'))52 # remove handler53 resettest()54 a.remove_handler('on_test', callbacktest)55 a.dispatch_event('on_test')...
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!!