Best Python code snippet using grail_python
app.py
Source:app.py
...24 query=APP.local_stack.query25 self.finder = FIND_TICKET(query)26 time.sleep(1)27 while not APP.local_stack.mission:28 log_input(APP.local_stack.user["user_name"],"è·åä»»å¡ï¼å¼å§æ¥è¯¢")29 #æ¥è¯¢åæ£æ¥æ¶é´30 APP.myclock.check_clock(APP.local_stack.user["user_name"])31 missions = self.finder()32 if missions:33 for mission in missions:34 APP.local_stack.mission.append(mission)35 log_input(APP.local_stack.user["user_name"],"å®æä¸æ¬¡æ¥è¯¢ï¼åç°ç¬¦åè¦æ±ç车票")36 else:37 log_input(APP.local_stack.user["user_name"],"å®æä¸æ¬¡æ¥è¯¢ï¼æªåç°ç¬¦åè¦æ±ç车票")38 time.sleep(random.randrange(5, 10))39 def execute_mission(self):40 #å½æ¥è¯¢å¨è·å符åä»»å¡éæ±ç票æ®ä¿¡æ¯æ¶ï¼è¿è¡ä¹°ç¥¨æä½41 mission=APP.local_stack.mission.pop()42 try:43 APP.myclock.check_clock(APP.local_stack.user["user_name"])44 res =self.buyer.buy_ticket(mission)45 except:46 self.buyer.driver.close()47 raise Exception("买票失败ï¼æªç¥é误ï¼è¯·èç³»ä½è
")48 return res49 @classmethod50 def start_mission(cls,user,query):51 app=cls(user,query)52 APP.local_stack.user=user53 APP.local_stack.query=query54 log_input(APP.local_stack.user["user_name"], "åå§åæµè§å¨")55 try:56 app.buyer=BUY_TICKET(user,query)57 except:58 raise Exception("æµè§å¨åå§å失败")59 log_input(APP.local_stack.user["user_name"], "æµè§å¨åå§åæåï¼ä¹°æå°±ä½")60 APP.local_stack.mission = []61 while True:62 try:63 app.get_mission()64 except:65 log_input(APP.local_stack.user["user_name"], "æ¥è¯¢å¤±è´¥")66 raise Exception("æ¥è¯¢å¤±è´¥")67 log_input(APP.local_stack.user["user_name"],"å¼å§ä¹°ç¥¨")68 res = app.execute_mission()69 if res == "succeeded":70 log_input(APP.local_stack.user["user_name"],"买票æåï¼è¯·å¨30åéå
è¿å
¥12306appä»æ¬¾")71 log_input(APP.local_stack.user["user_name"], "ä»»å¡ç»æï¼éåºçº¿ç¨")72 break73 else:74 app.buyer.driver.close()75 log_input(APP.local_stack.user["user_name"],"买票失败ï¼å次å°è¯ä¹°ç¥¨")76 log_input(APP.local_stack.user["user_name"], "éå¯æµè§å¨")77 try:78 app.buyer = BUY_TICKET(user, query)79 except:80 raise Exception("æµè§å¨åå§å失败")81 log_input(APP.local_stack.user["user_name"], "æµè§å¨åå§åæåï¼ä¹°æå°±ä½")82 APP.local_stack.mission = []83 @classmethod84 def start(cls,users,querys):85 APP.checker.check_before_start(users,querys)86 APP.users=users87 APP.querys=querys88 if len(users)==1:89 user = users[0]90 query=querys.get(user['user_name'])91 log_input(user["user_name"], "*" * 120)92 log_input(user["user_name"], "ç¨åºå¯å¨")93 log_input(user["user_name"], "è½½å
¥ç¨æ·")94 cls.start_mission(user,query)95 return None96 else:97 thread_list=[]98 for user in APP.users:99 log_input(user["user_name"], "*"*120)100 log_input(user["user_name"], "ç¨åºå¯å¨")101 log_input(user["user_name"], "è½½å
¥ç¨æ·ï¼å¼å¯çº¿ç¨")102 query=querys.get(user["user_name"])103 t=threading.Thread(target=cls.start_mission,args=(user,query))104 t.start()105 thread_list.append(t)106 time.sleep(1)107 for t in thread_list:108 t.join()...
loss_functions.py
Source:loss_functions.py
1import torch2import math3def log_poisson_loss(targets, log_input, compute_full_loss=False):4 """Computes log Poisson loss given `log_input`.5 FOLLOWS TF IMPLEMENTATION6 Gives the log-likelihood loss between the prediction and the target under the7 assumption that the target has a Poisson distribution.8 Caveat: By default, this is not the exact loss, but the loss minus a9 constant term [log(z!)]. That has no effect for optimization, but10 does not play well with relative loss comparisons. To compute an11 approximation of the log factorial term, specify12 compute_full_loss=True to enable Stirling's Approximation.13 For brevity, let `c = log(x) = log_input`, `z = targets`. The log Poisson14 loss is15 -log(exp(-x) * (x^z) / z!)16 = -log(exp(-x) * (x^z)) + log(z!)17 ~ -log(exp(-x)) - log(x^z) [+ z * log(z) - z + 0.5 * log(2 * pi * z)]18 [ Note the second term is the Stirling's Approximation for log(z!).19 It is invariant to x and does not affect optimization, though20 important for correct relative loss comparisons. It is only21 computed when compute_full_loss == True. ]22 = x - z * log(x) [+ z * log(z) - z + 0.5 * log(2 * pi * z)]23 = exp(c) - z * c [+ z * log(z) - z + 0.5 * log(2 * pi * z)]24 Args:25 targets: A `Tensor` of the same type and shape as `log_input`.26 log_input: A `Tensor` of type `float32` or `float64`.27 compute_full_loss: whether to compute the full loss. If false, a constant28 term is dropped in favor of more efficient optimization.29 name: A name for the operation (optional).30 Returns:31 A `Tensor` of the same shape as `log_input` with the componentwise32 logistic losses.33 Raises:34 ValueError: If `log_input` and `targets` do not have the same shape.35 """36 try:37 assert (targets.size() == log_input.size())38 except ValueError:39 raise ValueError(40 "log_input and targets must have the same shape (%s vs %s)" %41 (log_input.size(), targets.size()))42 result = torch.exp(log_input) - log_input * targets43 if compute_full_loss:44 point_five = 0.545 two_pi = 2 * math.pi46 stirling_approx = (targets * torch.log(targets)) - targets + \47 (point_five * torch.log(two_pi * targets))48 zeros = torch.zeros_like(targets, dtype=targets.dtype)49 ones = torch.ones_like(targets, dtype=targets.dtype)50 cond = (targets >= zeros) & (targets <= ones)51 result += torch.where(cond, zeros, stirling_approx)...
module.py
Source:module.py
1import cv2 as cv2import random3import tensorflow as tf4import numpy as np5from cv2.ximgproc import guidedFilter678def decomposition(input):9 # 1. Logarithm10 log_input = np.log(input + 1e-6)1112 # 2. Scale to [0,1]13 log_input = (log_input - log_input.min() + 1e-6) / (log_input.max() - log_input.min() + 1e-6)1415 # 3. Guided filter16 base = guidedFilter(log_input.astype('single'), log_input.astype('single'), radius=3, eps=0.1)1718 # 4. Decomposition19 detail = log_input - base2021 return base, detail222324def decomposition_NotLog(input):25 # 1. Logarithm26 # log_input = np.log(input + 1e-6)2728 # 2. Scale to [0,1]29 # log_input = (log_input - log_input.min() + 1e-6) / (log_input.max() - log_input.min() + 1e-6)3031 # 3. Guided filter32 base = guidedFilter(input.astype('single'), input.astype('single'), radius=3, eps=0.1)3334 # 4. Decomposition35 detail = input - base3637 return base, detail3839def randomCropHL(img, img2, width, height):40 if img.shape[0] < height or img.shape[1] < width:41 img = cv.resize(img, dsize=(width, height))42 img2 = cv.resize(img2, dsize=(width, height))43 else:44 x = random.randint(np.round((img.shape[1] - width)/2.1), np.round((img.shape[1] - width)*1.1/2.1))45 y = random.randint(np.round((img.shape[0] - height)/2.1), np.round((img.shape[0] - height)*1.1/2.1))46 img = img[y:y + height, x:x + width]47 img2 = img2[y:y + height, x:x + width]48 return img, img2495051def randomCrop(img, width, height):52 if img.shape[0] < height or img.shape[1] < width:53 img = cv.resize(img, dsize=(width, height))54 else:55 x = random.randint(0, img.shape[1] - width)56 y = random.randint(0, img.shape[0] - height)57 img = img[y:y + height, x:x + width]58 return img596061def ref_padding(tensor, pad_size=1):62 output = tf.pad(tensor, [[0, 0], [pad_size, pad_size], [pad_size, pad_size], [0, 0]], "REFLECT")
...
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!!