Best Python code snippet using tox_python
sem.py
Source: sem.py
1#!/usr/bin/env python32#3# sem.py4#5# An implementation of the concrete semantics, including an6# interpreter7#8# Author: Sreepathi Pai9#10# Written for CSC2/455 Spring 202011#12# To the extent possible under law, Sreepathi Pai has waived all13# copyright and related or neighboring rights to sem.py. This work14# is published from: United States.15from typing import Dict, List16from tinyast import *17import random18import logging19logger = logging.getLogger(__name__)20# map of variables (here str, instead of Var) -> values21#TODO: we could use var if we defined hash to be on the name of Var?22Memory = Dict[str, int]23def f_binop(op: BinaryOps, left: Scalar, right: Scalar) -> Scalar:24 if op == '+':25 return left + right26 elif op == '-':27 return left - right28 elif op == '*':29 return left * right30 elif op == '/':31 return left // right32 else:33 raise NotImplementedError(f"Unknown operator: {op}")34def f_cmpop(op: ComparisonOps, left: Scalar, right: Scalar) -> bool:35 if op == '<':36 return left < right37 elif op == '>':38 return left > right39 elif op == '<=':40 return left <= right41 elif op == '>=':42 return left >= right43 elif op == '!=':44 return left != right45 else:46 raise NotImplementedError(f"Unknown comparison operator: {op}")47def evaluate_Expr(E: Expr, m: Memory) -> Scalar:48 if isinstance(E, Scalar):49 return E50 elif isinstance(E, Var):51 return m[E.name]52 elif isinstance(E, BinOp):53 return f_binop(E.op,54 evaluate_Expr(E.left, m),55 evaluate_Expr(E.right, m))56def evaluate_BoolExpr(B: BoolExpr, m: Memory) -> bool:57 return f_cmpop(B.op, m[B.left.name], B.right)58def filter_memory(B: BoolExpr, M: List[Memory], res = True) -> List[Memory]:59 out = [m for m in M if evaluate_BoolExpr(B, m) == res]60 return list(out) #TODO: why materialize this generator?61def union_memories(M0: List[Memory], M1: List[Memory]) -> List[Memory]:62 # this is, of course, ridiculous63 # convert everything to sets64 M0_set = set([frozenset(m.items()) for m in M0])65 M1_set = set([frozenset(m.items()) for m in M1])66 M_set = M0_set.union(M1_set)67 # convert back to lists of dicts68 return list([dict(m) for m in M_set])69# M is a set of memory states, it belongs to Powerset(Memory)70# We're using List, because set would choke on Dict and we don't have a frozendict type...71def evaluate_Cmd(C: Cmd, M: List[Memory]) -> List[Memory]:72 def update_memories(var, value_lambda):73 out = []74 for m in M:75 # not sure using dicts is gaining us anything when we're copying dicts around...76 m_out = dict(m)77 m_out[var] = value_lambda(m)78 out.append(m_out)79 return out80 if isinstance(C, Skip):81 return M82 elif isinstance(C, Program):83 return evaluate_Cmd(C.program, M)84 elif isinstance(C, Assign):85 return update_memories(C.left.name, lambda m: evaluate_Expr(C.right, m))86 elif isinstance(C, Input):87 n = random.randint(0, 100) # could be anything, actually88 return update_memories(C.var.name, lambda _: n)89 elif isinstance(C, Seq):90 return evaluate_Cmd(C.cmd1, evaluate_Cmd(C.cmd0, M))91 elif isinstance(C, IfThenElse):92 then_memory = evaluate_Cmd(C.then_, filter_memory(C.cond, M))93 else_memory = evaluate_Cmd(C.else_, filter_memory(C.cond, M, res = False))94 return union_memories(then_memory, else_memory)95 elif isinstance(C, While):96 # L0 but we apply filter at the end97 out = [m for m in M] # copy all input states98 # the next loop computes L1, L2, L3, ....99 # identify those memories where condition is true100 pre_iter_memories = filter_memory(C.cond, out)101 accum: List[Memory] = []102 while len(pre_iter_memories):103 logger.debug(f"pre_iter_memories: {pre_iter_memories}")104 after_iter_memories = evaluate_Cmd(C.body, pre_iter_memories)105 logger.debug(f"after_iter_memories: {after_iter_memories}")106 accum = union_memories(accum, after_iter_memories)107 logger.debug(f"accum: {accum}")108 # only keep memories where the condition is true for the next iteration109 pre_iter_memories = filter_memory(C.cond, after_iter_memories)110 # This computes L0 U (L1 U L2...) and retains only those memory states where the loop has111 # terminated.112 #113 # we have exited the loop, so only keep those memories where condition is false114 out = filter_memory(C.cond, union_memories(out, accum), res=False)115 return out116 else:117 raise NotImplementedError(f"Don't know how to interpret {type(C).__name__}({C})")118def test_evaluate_Expr():119 x = Var('x')120 y = Var('y')121 m = {'x': 5, 'y': 6}122 x1 = BinOp('+', x, y)123 ex1 = evaluate_Expr(x1, m)124 assert ex1 == 11, ex1125def test_evaluate_BoolExpr():126 x = Var('x')127 y = Var('y')128 m = {'x': 5, 'y': 6}129 b1 = BoolExpr('<', x, 6)130 eb1 = evaluate_BoolExpr(b1, m)131 assert eb1 == True, eb1132def test_evaluate_Cmd():133 #TODO: actually put in asserts for testing. Right now, rely on visual inspection...134 x = Var('x')135 y = Var('y')136 m1 = {'x': 5, 'y': 6}137 m2 = {'x': 8, 'y': 7}138 M_in = [m1, m2]139 s = Program(Skip())140 M_out = evaluate_Cmd(s, M_in)141 print(M_out)142 pasgn = Program(Assign(x, 9))143 M_out = evaluate_Cmd(pasgn, M_in)144 print(M_out)145 pinput = Program(Input(y))146 M_out = evaluate_Cmd(pinput, M_in)147 print(M_out)148 pseq = Program(sequence([Assign(x, 10), Assign(y, 11)]))149 M_out = evaluate_Cmd(pseq, M_in)150 print(M_out)151 pite = Program(IfThenElse(BoolExpr('>', x, 7),152 Assign(y, BinOp('-', x, 7)),153 Assign(y, BinOp('-', 7, x))154 )155 )156 M_out = evaluate_Cmd(pite, M_in)157 print(M_out)158 ploop = Program(While(BoolExpr('<', x, 7),159 Seq(Assign(y, BinOp('-', y, 1)),160 Assign(x, BinOp('+', x, 1)))161 ))162 M_out = evaluate_Cmd(ploop, M_in)163 print(M_in, M_out)164def test_While():165 x = Var('x')166 y = Var('y')167 m1 = {x.name: 4, y.name: 0}168 m2 = {x.name: 8, y.name: 0}169 m3 = {x.name: 5, y.name: 0}170 M_in = [m1, m2, m3]171 print(M_in)172 p = Program(While(BoolExpr('<', x, 7),173 Seq(Assign(y, BinOp('+', y, 1)),174 Assign(x, BinOp('+', x, 1)))))175 print(p)176 M_out = evaluate_Cmd(p, M_in)177 print(M_out)178if __name__ == "__main__":179 logging.basicConfig(level = logging.DEBUG)180 test_evaluate_Expr()181 test_evaluate_BoolExpr()182 test_evaluate_Cmd()...
test_Evaluate.py
Source: test_Evaluate.py
1# import2## batteries3import os4import sys5import pytest6import logging7## 3rd party8import numpy as np9## package10from DeepMAsED.Commands import Evaluate as Evaluate_CMD11# test/data dir12test_dir = os.path.join(os.path.dirname(__file__))13data_dir = os.path.join(test_dir, 'data')14# tests15def test_help():16 args = ['-h']17 with pytest.raises(SystemExit) as pytest_wrapped_e:18 Evaluate_CMD.parse_args(args)19 assert pytest_wrapped_e.type == SystemExit20 assert pytest_wrapped_e.value.code == 021def test_evaluate(tmpdir, caplog):22 caplog.set_level(logging.INFO) 23 save_path = tmpdir.mkdir('save_dir')24 args = ['--save-path', str(save_path),25 os.path.join(data_dir, 'n10_r2/feature_files.tsv')]26 args = Evaluate_CMD.parse_args(args) 27 Evaluate_CMD.main(args)28def test_evaluate_not_syn(tmpdir, caplog):29 caplog.set_level(logging.INFO) 30 save_path = tmpdir.mkdir('save_dir')31 args = ['--save-path', str(save_path),32 '--is-synthetic', '0', 33 os.path.join(data_dir, 'n10_r2/feature_files.tsv')]34 args = Evaluate_CMD.parse_args(args) ...
evaluation.py
Source: evaluation.py
1import argparse2import os3import json4# TEST_DIR=../datasets/references/references5parser = argparse.ArgumentParser(description='Process some integers.')6parser.add_argument('--encoder', metavar='N', type=str,7 help='an integer for the accumulator', required=True)8args = parser.parse_args()9ENCODER = args.encoder10TEST_DIR = "../datasets/test"11ORIGINA_SENT_PATH = f"{TEST_DIR}/src-references.txt"12evaluate_cmd = f"easse evaluate -t custom --orig_sents_path {ORIGINA_SENT_PATH} "13evaluate_cmd += f" --refs_sents_paths {TEST_DIR}/references/reference_1.txt,{TEST_DIR}/references/reference_2.txt," \14 f"{TEST_DIR}/references/reference_3.txt,{TEST_DIR}/references/reference_4.txt "15evaluate_cmd += f"-m bleu,sari -q < ../{ENCODER}/prediction/prediction.txt"16result = os.popen(evaluate_cmd).read()17print(result)18result = os.popen(evaluate_cmd).read()19print(os.system(evaluate_cmd))...
Check out the latest blogs from LambdaTest on this topic:
Hey Testers! We know it’s been tough out there at this time when the pandemic is far from gone and remote working has become the new normal. Regardless of all the hurdles, we are continually working to bring more features on-board for a seamless cross-browser testing experience.
Manual cross browser testing is neither efficient nor scalable as it will take ages to test on all permutations & combinations of browsers, operating systems, and their versions. Like every developer, I have also gone through that ‘I can do it all phase’. But if you are stuck validating your code changes over hundreds of browsers and OS combinations then your release window is going to look even shorter than it already is. This is why automated browser testing can be pivotal for modern-day release cycles as it speeds up the entire process of cross browser compatibility.
With new-age project development methodologies like Agile and DevOps slowly replacing the old-age waterfall model, the demand for testing is increasing in the industry. Testers are now working together with the developers and automation testing is vastly replacing manual testing in many ways. If you are new to the domain of automation testing, the organization that just hired you, will expect you to be fast, think out of the box, and able to detect bugs or deliver solutions which no one thought of. But with just basic knowledge of testing, how can you be that successful test automation engineer who is different from their predecessors? What are the skills to become a successful automation tester in 2019? Let’s find out.
Before we discuss Scala testing, let us understand the fundamentals of Scala and how this programming language is a preferred choice for your development requirements.The popularity and usage of Scala are rapidly rising, evident by the ever-increasing open positions for Scala developers.
I routinely come across test strategy documents when working with customers. They are lengthy—100 pages or more—and packed with monotonous text that is routinely reused from one project to another. Yawn once more— the test halt and resume circumstances, the defect management procedure, entrance and exit criteria, unnecessary generic risks, and in fact, one often-used model replicates the requirements of textbook testing, from stress to systems integration.
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!!