Best Python code snippet using prospector_python
test.py
Source: test.py
...17 self.assertListEqual(rpn('4-8'), [Decimal('4'), Decimal('8'), '-'])18 self.assertListEqual(rpn('5*3'), [Decimal('5'), Decimal('3'), '*'])19 self.assertListEqual(rpn('6/4'), [Decimal('6'), Decimal('4'), '/'])20 self.assertListEqual(rpn('2^3'), [Decimal('2'), Decimal('3'), '^'])21 def test_precedence(self):22 self.assertListEqual(rpn('2+3*4'), [Decimal('2'), Decimal('3'), Decimal('4'), '*', '+'])23 self.assertListEqual(rpn('5*6+7'), [Decimal('5'), Decimal('6'), '*', Decimal('7'), '+'])24 def test_brackets(self):25 self.assertListEqual(rpn('(2+3)-5'),[Decimal('2'), Decimal('3'), '+', Decimal('5'), '-'])26 self.assertListEqual(rpn('4*(6-7)'), [Decimal('4'), Decimal('6'), Decimal('7'), '-', '*'])27 self.assertListEqual(rpn('(2.78+3)*(7-8)'), [Decimal('2.78'), Decimal('3'), '+',28 Decimal('7'), Decimal('8'), '-',29 '*'])30 def test_constants(self):31 self.assertListEqual(rpn('e'), [Decimal(math.e)])32 self.assertListEqual(rpn('e+pi'), [Decimal(math.e), Decimal(math.pi), '+'])33 self.assertListEqual(rpn('e*(3 + 7 - e)'), [Decimal(math.e), Decimal('3'),34 Decimal('7'), Decimal(math.e),35 '-', '+', '*'])36 def test_functions(self):37 self.assertListEqual(rpn('sin(3)'), [Decimal('3'), 'sin'])38 self.assertListEqual(rpn('arcsin(0.5)'), [Decimal('0.5'), 'arcsin'])39 self.assertListEqual(rpn('cos( 8 ) + tan(7)'), [Decimal('8'), 'cos', Decimal('7'), 'tan', '+'])40 self.assertListEqual(rpn('cos(arcsin(1/e))'), [Decimal('1'), Decimal(math.e),41 '/', 'arcsin', 'cos'])42 def test_precedence(self):43 self.assertListEqual(rpn('3 * 4 + 6'), [Decimal('3'), Decimal('4'), '*', Decimal('6'), '+'])44 self.assertListEqual(rpn('1-2*5'), [Decimal('1'), Decimal('2'), Decimal('5'), '*', '-'])45 self.assertListEqual(rpn('1/2*4'), [Decimal('1'), Decimal('2'), '/', Decimal('4'), '*'])46 def test_unary_minus(self):47 self.assertListEqual(rpn('-6'), [Decimal('6'), '--'])48 self.assertListEqual(rpn('-4+3'), [Decimal('4'), '--', Decimal('3'), '+'])49 self.assertListEqual(rpn('2^-2'), [Decimal('2'), Decimal('2'), '--', '^'])50 self.assertListEqual(rpn('-5^2'), [Decimal('5'), Decimal('2'), '^', '--'])51class Test_computing_rpn(TestCase):52 def test_simple_algebra(self):53 self.assertEqual(compute_rpn([Decimal('2'), Decimal('3'), '+']), Decimal('5'))54 self.assertEqual(compute_rpn([Decimal('4'), Decimal('8'), '-']), Decimal('-4'))55 self.assertEqual(compute_rpn([Decimal('5'), Decimal('3'), '*']), Decimal('15'))56 self.assertEqual(compute_rpn([Decimal('6'), Decimal('4'), '/']), Decimal('1.5'))57 self.assertEqual(compute_rpn([Decimal('2'), Decimal('3'), '^']), Decimal('8'))58 def test_brackets(self):59 self.assertEqual(compute_rpn([Decimal('2'), Decimal('3'), '+', Decimal('5'), '-']), Decimal('0'))60 self.assertEqual(compute_rpn([Decimal('4'), Decimal('6'), Decimal('7'), '-', '*']), Decimal('-4'))61 self.assertEqual(compute_rpn([Decimal('2.78'), Decimal('3'), '+',62 Decimal('7'), Decimal('8'), '-', '*']), Decimal('-5.78'))63 def test_constants(self):64 self.assertEqual(compute_rpn([Decimal(math.e)]), Decimal(math.e))65 self.assertEqual(compute_rpn([Decimal(math.e), Decimal(math.pi), '+']), Decimal(math.e)+Decimal(math.pi))66 self.assertEqual(compute_rpn([Decimal(math.e), Decimal('3'), Decimal('7'), Decimal(math.e),67 '-', '+', '*']),68 Decimal(math.e)*(Decimal('3') + Decimal('7')-Decimal(math.e)))69 def test_functions(self):70 self.assertEqual(compute_rpn([Decimal('3'), 'sin']), Decimal(math.sin(3)))71 self.assertEqual(compute_rpn([Decimal('0.5'), 'arcsin']), Decimal(math.asin(0.5)))72 self.assertEqual(compute_rpn([Decimal('8'), 'cos', Decimal('7'), 'tan', '+']),73 Decimal(math.cos(8)+math.tan(7)))74 self.assertEqual(compute_rpn([Decimal('1'), Decimal(math.e), '/', 'arcsin', 'cos']),75 Decimal(math.cos(math.asin(1/math.e))))76 def test_precedence(self):77 self.assertEqual(compute_rpn([Decimal('2'), Decimal('3'), Decimal('4'), '*', '+']), Decimal('14'))78 self.assertEqual(compute_rpn([Decimal('5'), Decimal('6'), '*', Decimal('7'), '+']), Decimal('37'))79 def test_unary_minus(self):80 self.assertEqual(compute_rpn([Decimal('6'), '--']), Decimal('-6'))81 self.assertEqual(compute_rpn([Decimal('4'), '--', Decimal('3'), '+']), Decimal('-1'))82 self.assertEqual(compute_rpn([Decimal('2'), Decimal('2'), '--', '^']), Decimal('0.25'))83 self.assertEqual(compute_rpn([Decimal('5'), Decimal('2'), '^', '--']), Decimal('-25'))84class TestGraphingCalc(TestCase):85 def test_implementable_function(self):86 pass87if __name__ == '__main__':...
train_overfit.py
Source: train_overfit.py
1import time2import numpy as np3import torch4from torch import optim5from utils import *6from Envs.JobShopMultiGymEnv import *7from Models.actorcritic import *89embeddim = 12810actorLR = 1e-411criticLR = 1e-41213device = torch.device("cuda" if torch.cuda.is_available() else "cpu")1415jobs = 1516ops = 1517macs = ops18maxTime = 10019i = 020size_agnostic = True21resume_run = True22seed = 023BS = 1282425start = time.time()26########################################################################2728print('ta{}'.format(i + 1))29test_precedence, test_timepre_ = read_instances('./data/ta/ta{}'.format(i + 1))30test_timepre = test_timepre_ / maxTime3132# Create batch33test_precedence = np.repeat(test_precedence, BS, axis=0)34test_timepre = np.repeat(test_timepre, BS, axis=0)3536test_venv = JobShopMultiGymEnv(BS, jobs, ops, macs)37test_venv.setGame(test_precedence, test_timepre)38testSamples = [i for i in range(BS)]3940filedir = './Results/%dx%d/' % (jobs, macs)41if size_agnostic:42 filedir = './Results/size_agnostic/'4344model_name = 'A2C_Seed%d_BS%d.tar' % (seed, BS)45if size_agnostic:46 model_name = '6_6_100.tar'4748# Instanitate actor-critic49actor = Actor(embeddim, jobs, ops, macs, device).to(device)50critic = Critic(embeddim, jobs, ops, macs, device).to(device)5152# Environment training5354actor_opt = optim.Adam(actor.parameters(), lr=actorLR)55critic_opt = optim.Adam(critic.parameters(), lr=criticLR)5657if resume_run:58 checkpoint = torch.load(filedir + model_name, map_location=device)59 actor.load_state_dict(checkpoint['actor_state_dict'])60 critic.load_state_dict(checkpoint['critic_state_dict'])61 actor_opt.load_state_dict(checkpoint['actor_opt_state_dict'])62 critic_opt.load_state_dict(checkpoint['critic_opt_state_dict'])6364for epi in range(10000):6566 if epi%10==1 or epi==0:67 epi_st = time.time()6869 test_venv.reset(testSamples)7071 train_rollout = Rollout(test_venv, actor, critic, device)72 ite, total_reward, States, Log_Prob, Prob, Action, Value, tr_reward, tr_entropy = train_rollout.play(BS,testSamples)73 Log_Prob = torch.stack(Log_Prob)7475 tr_reward.reverse()7677 #Compute Advantage: Qvalue - Value78 Qvalue = np.zeros((ite, BS))79 q = 0.080 for i in range(ite):81 q += np.array(tr_reward[i]) # q.shape=[BS]82 Qvalue[ite - i - 1] = q8384 Advantage = torch.tensor(Qvalue, device=device) - torch.stack(Value) # advantage.shape=[ite,BS]8586 # Zero grad87 actor.zero_grad()88 critic.zero_grad()8990 # Compute loss fucntion91 # actor_loss = torch.mean(torch.sum(Log_Prob, dim=1)*torch.tensor(total_reward).to(device))92 actor_loss = torch.mean(-Log_Prob * Advantage.detach())93 critic_loss = 0.5 * torch.mean(Advantage ** 2)9495 # Update weights Actor and critic96 actor_loss.backward()97 critic_loss.backward()98 actor_opt.step()99 critic_opt.step()100101 if epi % 10 == 0:102103 epi_et = time.time()104 105 print('epi: %d, al: %.2e, cl: %.2e, trSpan: %.4f, trEntropy: %.2f, time: %.2f' \106 % (epi, actor_loss.data.item(), critic_loss.data.item(), np.mean(total_reward), tr_entropy, epi_et-epi_st))
...
test_instances.py
Source: test_instances.py
1from Envs.JobShopMultiGymEnv import *2from utils import *3from Models.actorcritic import *4from torch import optim5import time6embeddim = 1287device = torch.device("cuda" if torch.cuda.is_available() else "cpu")8# Change9instances = 'ta' # Select: ta or dmu10size_agnostic = True11testsize = 112jobs = 1513macs = 1514ops = macs 15maxTime = 100 # maxTime: ta -> 100, for dmu -> 20016seed = 017BS = 12818beam_size = 219model_jobs = 620model_ops = 621start = time.time()22########################################################################23for i in range(0,10):24 25 print('{}{}'.format(instances,i+1))26 if instances=='ta':27 test_precedence,test_timepre_ = read_instances('./data/ta/ta{}'.format(i+1))28 else:29 test_precedence,test_timepre_ = read_instances('./data/dmu/dmu{}.txt'.format(i+1))30 test_timepre = test_timepre_ / maxTime31 test_venv = JobShopMultiGymEnv(testsize,jobs,ops,macs)32 test_venv.setGame(test_precedence,test_timepre)33 testSamples = [i for i in range(testsize)]34 test_venv.reset(testSamples)35 # Instanitate actor-critic36 actor = Actor(embeddim,jobs,ops,macs,device).to(device)37 critic = Critic(embeddim,jobs,ops,macs,device).to(device)38 # Environment training39 actor_opt = optim.Adam(actor.parameters())40 critic_opt = optim.Adam(critic.parameters())41 filedir = './Results/%dx%d/'%(jobs,macs)42 if size_agnostic:43 filedir = './Results/size_agnostic/%dx%d_%d/'%(model_jobs,model_ops,maxTime)44 model_name = 'A2C_Seed%d_BS%d.tar'%(seed,BS)45 checkpoint = torch.load(filedir+model_name, map_location=torch.device('cpu'))46 actor.load_state_dict(checkpoint['actor_state_dict'])47 critic.load_state_dict(checkpoint['critic_state_dict'])48 actor_opt.load_state_dict(checkpoint['actor_opt_state_dict'])49 critic_opt.load_state_dict(checkpoint['critic_opt_state_dict'])50 test_rollout = Rollout(test_venv,actor,critic,device)51 te_ite,te_total_reward,te_States,te_Log_Prob,te_Prob,te_Action,te_Value,te_reward, te_entropy = test_rollout.play(testsize,testSamples,False,beam_size)52 print(te_total_reward)53end = time.time()...
Check out the latest blogs from LambdaTest on this topic:
“Test frequently and early.” If you’ve been following my testing agenda, you’re probably sick of hearing me repeat that. However, it is making sense that if your tests detect an issue soon after it occurs, it will be easier to resolve. This is one of the guiding concepts that makes continuous integration such an effective method. I’ve encountered several teams who have a lot of automated tests but don’t use them as part of a continuous integration approach. There are frequently various reasons why the team believes these tests cannot be used with continuous integration. Perhaps the tests take too long to run, or they are not dependable enough to provide correct results on their own, necessitating human interpretation.
As a developer, checking the cross browser compatibility of your CSS properties is of utmost importance when building your website. I have often found myself excited to use a CSS feature only to discover that it’s still not supported on all browsers. Even if it is supported, the feature might be experimental and not work consistently across all browsers. Ask any front-end developer about using a CSS feature whose support is still in the experimental phase in most prominent web browsers. ????
Have you ever visited a website that only has plain text and images? Most probably, no. It’s because such websites do not exist now. But there was a time when websites only had plain text and images with almost no styling. For the longest time, websites did not focus on user experience. For instance, this is how eBay’s homepage looked in 1999.
Recently, I was going through some of the design patterns in Java by reading the book Head First Design Patterns by Eric Freeman, Elisabeth Robson, Bert Bates, and Kathy Sierra.
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!!