Best Python code snippet using tavern
test_core.py
Source:test_core.py
...82 run_test("heif", fulltest, includes)83 assert pmock.called84class TestIncludeStages:85 @pytest.fixture86 def fake_stages(self):87 stages = [88 {89 "id": "my_external_stage",90 "name": "My external stage",91 "request": {"url": "http://www.bing.com", "method": "GET"},92 "response": {93 "status_code": 200,94 "json": {"key": "value"},95 "headers": {"content-type": "application/json"},96 },97 }98 ]99 return stages100 def check_mocks_called(self, pmock):...
CalculateData.py
Source:CalculateData.py
1#!/usr/bin/env python2# -*- coding: utf-8 -*-3from math import exp4import os5import re6import sys7# This code implements the non-linear regression method described in:8# Jean Jacquelin 2009,9# Régressions et équations intégrales,10# sec. REGRESSIONS NON LINEAIRES des genres : PUISSANCE, EXPONENTIELLE, LOGARITHME, WEIBULL,11# pp. 16-18.12# http://www.scribd.com/doc/14674814/Regressions-et-equations-integrales13# Given n data points (x[1], y[1]), ... (x[n], y[n])14# it computes coefficients a, b, and c that minimize error in the fit equation:15# y = a + b exp(c*x)16# TODO: This regression is not very robust, and currently fails with division by17# zero if the regression matrix is singular.18def exponential_non_linear_regression(x, y, n):19 S = [0] * (n+1)20 S[1] = 021 sum_xx = 022 sum_xS = 023 sum_SS = 024 sum_yx = 025 sum_yS = 026 for k in range(2, n+1):27 S[k] = S[k-1] + 0.5 * (y[k] + y[k-1]) * (x[k] - x[k-1])28 sum_xx += (x[k] - x[1]) * (x[k] - x[1])29 sum_xS += (x[k] - x[1]) * S[k]30 sum_SS += S[k] * S[k]31 sum_yx += (y[k] - y[1]) * (x[k] - x[1])32 sum_yS += (y[k] - y[1]) * S[k]33 # ( A1 ) = ( sum_xx sum_xS ) -1 ( sum_yx )34 # ( B1 ) ( sum_xS sum_SS ) ( sum_yS )35 det_inv = sum_xx*sum_SS - sum_xS*sum_xS36 if (det_inv == 0.0):37 return None38 det = 1 / det_inv39 A1 = det * ( sum_SS*sum_yx - sum_xS*sum_yS)40 B1 = det * (-sum_xS*sum_yx + sum_xx*sum_yS)41 if (B1 == 0.0):42 a1 = 0.043 else:44 a1 = - A1 / B145 c1 = B146 c2 = c147 sum_theta = 048 sum_thetatheta = 049 sum_y = 050 sum_ytheta = 051 theta = [0] * (n+1)52 for k in range(1, n+1):53 theta[k] = exp(c2 * x[k])54 sum_theta += theta[k]55 sum_thetatheta += theta[k] * theta[k]56 sum_y += y[k]57 sum_ytheta += y[k] * theta[k]58 # ( a2 ) = ( n sum_theta ) -1 ( sum_y )59 # ( b2 ) ( sum_theta sum_thetatheta ) ( sum_ytheta )60 inv_det = n*sum_thetatheta - sum_theta*sum_theta61 if (inv_det == 0.0):62 det = 0.063 else:64 det = 1 / inv_det65 a2 = det * ( sum_thetatheta*sum_y - sum_theta*sum_ytheta)66 b2 = det * (-sum_theta*sum_y + n*sum_ytheta)67 if (det != 0):68 return "%g + %g * exp(%g*x)" % (a2, b2, c2)69 else:70 return None71# Output a datafile to output_file in the following form:72# # Load Value73# 0 074# 40 15.775# 90 31.976# Returns a string representing the "curve of best fit"77def calculate_data(grinder_files, accumulate_callback, output_file, include_origin=True):78 Load = []79 Value = []80 stages = 081 for file in grinder_files:82 threads = 083 stage = int(os.path.basename(os.path.dirname(file))[6:]) # strip initial 'stage-'84 if (stages < stage + 1):85 stages = stage + 186 while len(Value) <= stage:87 Load.append(0)88 Value.append(0)89 with open(file, 'r') as f:90 for line in f:91 # Count the threads in this Grinder logfile by looking at all the unique log messages.92 # This is to determine the load level empirically.93 # TODO: There is probably a more efficient way to communicate this from experiment execution.94 m = re.match(r'....-..-.. ..:..:..,... INFO .*-0 thread-([0-9]*)', line)95 if m:96 thread = int(m.group(1))97 if (threads < thread + 1):98 threads = thread + 199 m = re.match(r'Test ', line)100 if m:101 # grinder-0.log file format:102 # $1 = "Test"103 # $2 = "0"...104 # $3 = Tests105 # $4 = Errors106 # $5 = Mean Test Time (ms)107 # $6 = Test Time Standard Deviation (ms)108 # $7 = TPS109 # $8 = Mean response length110 # $9 = Response bytes per second111 # $10 = Response errors112 # $11 = Mean time to resolve host113 # $12 = Mean time to establish connection114 # $13 = Mean time to first byte115 # $14 = Description116 # Tests Errors Mean Test Test Time TPS Mean Response Response Mean time to Mean time to Mean time to117 # Time (ms) Standard response bytes per errors resolve host establish first byte118 # Deviation length second connection119 # (ms)120 #121 #Test 0 1060 0 10224.49 7229.27 0.58 19.77 11.51 5 0.04 0.28 10221.48 "Status"122 #Test 1 2408 0 10490.14 7157.16 1.32 16665.09 22049.02 8 0.04 0.30 10485.22 "Projects"123 fields = re.split("[ \t]+", line)124 agent_data = {125 'tests': float(fields[2]),126 'mean_test_time': float(fields[4]),127 'tps': float(fields[6]),128 'desc': line.split('"')[1]129 }130 accumulate_callback(Value, stage, agent_data)131 Load[stage] += threads132 Load = [0] + Load133 Value = [0.0] + Value134 if include_origin:135 # Inject a "fake" (0,0) stage in the Load,Value lists.136 Load = [0] + Load137 Value = [0.0] + Value138 fake_stages=1139 else:140 fake_stages=0141 with open(output_file, 'w') as f:142 f.write("Load Value\n")143 for stage in range(1+fake_stages, stages+1+fake_stages):144 f.write("%d %g\n" % (Load[stage], Value[stage]))...
tribit.py
Source:tribit.py
1import math2import sys3def apply_rules(elementary_pyramid):4 rules = {"0000": "0000", "0001": "1000", "0010": "0001", "0011": "0010", "0100": "0000",5 "0101": "0010", "0110": "1011", "0111": "1011", "1000": "0100", "1001": "0101",6 "1010": "0111", "1011": "1111", "1100": "1101", "1101": "1110", "1110": "0111",7 "1111": "1111"}8 return rules[elementary_pyramid]9def reduce_pyramid(pyramid):10 reduced_pyramid = str()11 for elementary_pyramid in pyramid:12 reduced_pyramid += elementary_pyramid[0]13 return reduced_pyramid14def split_pyramid_on_stages(pyramid):15 """16The function takes a one-line view of the pyramid and returns a list, each element of which is a "stage" of the pyramid.17Stage - two real rows of the pyramid.18 """19 stages = []20 power_of_pyramid = math.log(len(pyramid), 4)21 amount_pyramids_in_base_stage = int(2**power_of_pyramid - 1)22 while len(pyramid) != 4:23 stages.append(pyramid[0:amount_pyramids_in_base_stage*4])24 pyramid = pyramid[amount_pyramids_in_base_stage*4:]25 amount_pyramids_in_base_stage -= 226 stages.append(pyramid)27 return stages28def split_stage_on_elementary_pyramids(stage):29 """30The function takes a string containing one stage of the pyramid,31and returns a simple list of elementary pyramids entering this stage.32 """33 white_pyramids = [] # list of normal elementary pyramids34 black_pyramids = [] # list of upside-down elementary pyramids35 elementary_pyramids = []36 # calculate the number elementary pyramids in stage37 amount_of_white_pyramids = len(stage) // 8 + 138 amount_of_black_pyramids = len(stage) // 839 amount_of_base_elements = amount_of_white_pyramids*3 + amount_of_black_pyramids # amount of elements in first row of stage40 for idx in range(0, amount_of_base_elements, 4):41 white_pyramids.append(stage[idx:idx+3]+stage[idx+amount_of_base_elements])42 for idx in range(0, amount_of_base_elements - 4, 4):43 black_pyramids.append(stage[idx+amount_of_base_elements+1:idx+amount_of_base_elements + 4] + stage[idx+3])44 for idx in range(amount_of_black_pyramids):45 elementary_pyramids.append(white_pyramids[idx])46 elementary_pyramids.append(black_pyramids[idx])47 elementary_pyramids.append(white_pyramids[-1])48 return elementary_pyramids49def assemble_pyramid_from_separated_elementary_pyramids(elementary_pyramids_separated):50 fake_stages = [] # list of lists containing joined elementary pyramids. Will be regrouped into true_stage51 for elementary_pyramids in elementary_pyramids_separated:52 fake_stages.append("".join(elementary_pyramids))53 pyramid = str()54 for fake_stage in fake_stages:55 true_stage1 = fake_stage[0:3] # string contained first row of real pyramid's stage fetched from fake_stage56 true_stage2 = str()57 for idx in range(7, len(fake_stage), 8):58 true_stage1 += fake_stage[idx:idx+4]59 for idx in range(3, len(fake_stage), 8):60 true_stage2 += fake_stage[idx:idx+4]61 pyramid += true_stage1 + true_stage262 print(pyramid)63 return pyramid64def main():65 if len(sys.argv) != 2:66 print('Usage: {0} <bit string>'.format(sys.argv[0]))67 sys.exit()68 actual_pyramid = sys.argv[1] # one-line view of the current pyramid69 if set(actual_pyramid) != set(('0', '1')) and set(actual_pyramid) != set('1') and set(actual_pyramid) != set('0'): 70 print('<bit string> must be only 1 and/or 0!')71 sys.exit()72 if int(math.log(len(actual_pyramid), 4)) != math.log(len(actual_pyramid), 4):73 print('Length of the <bit string> must be power of four!')74 sys.exit()75 76 is_reducible_1 = set(['0000', '1111']) # conditions under which one can reduce the pyramid77 is_reducible_2 = set(['1111'])78 is_reducible_3 = set(['0000'])79 print(actual_pyramid)80 while len(actual_pyramid) != 1:81 elementary_pyramids_separated = []82 stages = split_pyramid_on_stages(actual_pyramid)83 for stage in stages:84 elementary_pyramids_separated.append(list(map(apply_rules, split_stage_on_elementary_pyramids(stage))))85 elementary_pyramids = [] 86 for idx in elementary_pyramids_separated: # creation a simple list with elementary pyramids for more convenient comparation87 elementary_pyramids += idx88 pyramid_state = set(elementary_pyramids) # conversion list of elementary pyramids to set to compare it with conditions is_reducible_1/2/389 actual_pyramid = assemble_pyramid_from_separated_elementary_pyramids(elementary_pyramids_separated)90 if pyramid_state == is_reducible_1:91 actual_pyramid = reduce_pyramid(elementary_pyramids)92 print(actual_pyramid)93 elif (pyramid_state == is_reducible_2) or (pyramid_state == is_reducible_3):94 print(actual_pyramid[0])95 break96if __name__ == "__main__":...
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!!