How to use t_string method in avocado

Best Python code snippet using avocado_python

functions.py

Source:functions.py Github

copy

Full Screen

1import inspect2import os3import subprocess4from ft.types import (5 T_STRING,6 T_ARRAY,7 T_BOOL,8 T_PATH,9 T_INT,10 T_VOID,11 TypeConversionError,12 dynamic_cast,13)14from ft.internal import TypedValue, add_dynamic_type15from ft.error import panic16function_list = {}17def register(*names):18 if len(names) == 0 or not isinstance(names[0], str):19 panic("Called @register without arguments")20 def wrap(fn):21 global function_list22 for n in names:23 function_list[n] = fn24 return wrap25def typed(type_in, type_out):26 def wrap(fn):27 def fn_typecheck(*args):28 inp = args[-1]29 if type_in is not None:30 try:31 inp = type_in.create_from(inp)32 except TypeConversionError as e:33 panic(34 "Incompatible input type: expected '{}', got '{}'".format(35 e.type_to, e.type_from36 )37 )38 if len(args) > 1:39 result = fn(*args[0:-1], inp=inp.value)40 else:41 result = fn(inp=inp.value)42 if type_out is None:43 return TypedValue(result, inp.fttype)44 elif type_out == "returned":45 return result46 return TypedValue(result, type_out)47 fn_typecheck.type_in = type_in48 fn_typecheck.type_out = type_out49 fn_typecheck.inner_argspec = inspect.getargspec(fn)50 return fn_typecheck51 return wrap52@register("max")53@typed(T_INT, T_INT)54def max(value, inp):55 v = dynamic_cast(T_INT, value).value56 return inp if inp > v else v57@register("min")58@typed(T_INT, T_INT)59def min(value, inp):60 v = dynamic_cast(T_INT, value).value61 return inp if inp < v else v62@register("strip")63@typed(T_STRING, T_STRING)64def strip(inp):65 return inp.strip()66@register("append")67@typed(T_STRING, T_STRING)68def append(suffix, inp):69 return inp + dynamic_cast(T_STRING, suffix).value70@register("prepend")71@typed(T_STRING, T_STRING)72def prepend(prefix, inp):73 return dynamic_cast(T_STRING, prefix).value + inp74@register("take")75@typed(T_STRING, T_STRING)76def take(count, inp):77 count = dynamic_cast(T_INT, count).value78 return inp[0 : int(count)]79@register("drop")80@typed(T_STRING, T_STRING)81def drop(count, inp):82 count = dynamic_cast(T_INT, count).value83 return inp[int(count) :]84@register("capitalize")85@typed(T_STRING, T_STRING)86def capitalize(inp):87 return inp.capitalize()88@register("to_lower")89@typed(T_STRING, T_STRING)90def to_lower(inp):91 return inp.lower()92@register("to_upper")93@typed(T_STRING, T_STRING)94def to_upper(inp):95 return inp.upper()96@register("substr")97@typed(T_STRING, T_STRING)98def substr(start, end, inp):99 start = dynamic_cast(T_INT, start).value100 end = dynamic_cast(T_INT, end).value101 return inp[start:end]102@register("replace")103@typed(T_STRING, T_STRING)104def replace(old, new, inp):105 old = dynamic_cast(T_STRING, old)106 new = dynamic_cast(T_STRING, new)107 return inp.replace(old.value, new.value)108@register("starts_with", "startswith")109@typed(T_STRING, T_BOOL)110def starts_with(pattern, inp):111 pattern = dynamic_cast(T_STRING, pattern).value112 return inp.startswith(pattern)113@register("ends_with", "endswith")114@typed(T_STRING, T_BOOL)115def ends_with(pattern, inp):116 pattern = dynamic_cast(T_STRING, pattern).value117 return inp.endswith(pattern)118@register("split")119@typed(T_STRING, T_ARRAY)120def split(separator, inp):121 separator = dynamic_cast(T_STRING, separator)122 return map(add_dynamic_type, inp.split(separator.value))123@register("join")124@typed(T_ARRAY, T_STRING)125def join(separator, inp):126 separator = dynamic_cast(T_STRING, separator).value127 vals = map(lambda x: T_STRING.create_from(x).value, inp)128 return separator.join(vals)129@register("index", "at")130@typed(T_ARRAY, T_STRING)131def index(idx, inp):132 idx = dynamic_cast(T_INT, idx).value133 try:134 return T_STRING.create_from(inp[idx]).value135 except IndexError:136 panic("array index out of range")137@register("length", "len")138@typed(T_STRING, T_INT)139def length(inp):140 return len(inp)141@register("basename")142@typed(T_PATH, T_PATH)143def basename(inp):144 return os.path.basename(inp)145@register("abspath")146@typed(T_PATH, T_PATH)147def abspath(inp):148 return os.path.abspath(inp)149@register("filesize")150@typed(T_PATH, T_INT)151def filesize(inp):152 return os.path.getsize(inp)153@register("file_ext")154@typed(T_PATH, T_STRING)155def file_ext(inp):156 return os.path.splitext(inp)[1]157@register("dirname")158@typed(T_PATH, T_PATH)159def dirname(inp):160 return os.path.dirname(inp)161@register("replace_ext")162@typed(T_PATH, T_PATH)163def replace_ext(new_ext, inp):164 new_ext = dynamic_cast(T_STRING, new_ext).value165 (base, ext) = os.path.splitext(inp)166 if ext != "":167 return base + "." + new_ext168 return inp169@register("strip_ext")170@typed(T_PATH, T_STRING)171def strip_ext(inp):172 return os.path.splitext(inp)[0]173@register("has_ext")174@typed(T_PATH, T_BOOL)175def has_ext(ext, inp):176 ext = dynamic_cast(T_STRING, ext).value177 (_, file_ext) = os.path.splitext(inp)178 file_ext = file_ext[1:] # strip leading dot179 return file_ext == ext180@register("split_ext")181@typed(T_PATH, T_ARRAY)182def split_ext(inp):183 parts = os.path.splitext(inp)184 return [TypedValue(parts[0], T_STRING), TypedValue(parts[1][1:], T_STRING)]185@register("id", "identity")186@typed(None, None)187def id(inp):188 return inp189@register("const")190@typed(None, "returned")191def const(value, inp):192 return value193@register("add")194@typed(T_INT, T_INT)195def add(num, inp):196 num = dynamic_cast(T_INT, num).value197 return inp + num198@register("sub")199@typed(T_INT, T_INT)200def sub(num, inp):201 num = dynamic_cast(T_INT, num).value202 return inp - num203@register("pow")204@typed(T_INT, T_INT)205def power(num, inp):206 num = dynamic_cast(T_INT, num).value207 return pow(inp, num)208@register("mul")209@typed(T_INT, T_INT)210def mul(num, inp):211 num = dynamic_cast(T_INT, num).value212 return inp * num213@register("even")214@typed(T_INT, T_BOOL)215def even(inp):216 return inp % 2 == 0217@register("odd")218@typed(T_INT, T_BOOL)219def odd(inp):220 return inp % 2 == 1221@register("duplicate")222@typed(T_STRING, T_ARRAY)223def duplicate(inp):224 return [TypedValue(inp, T_STRING), TypedValue(inp, T_STRING)]225@register("run")226@typed(T_ARRAY, T_VOID)227def run(command, inp):228 command = dynamic_cast(T_STRING, command).value229 args = map(T_STRING.create_from, inp)230 args = list(map(lambda v: v.value, args))231 print("Running '{}' with arguments {}".format(command, args))232 subprocess.call([command] + args)233@register("exists")234@typed(T_PATH, T_BOOL)235def exists(inp):236 return os.path.exists(inp)237@register("is_dir")238@typed(T_PATH, T_BOOL)239def is_dir(inp):240 return os.path.isdir(inp)241@register("is_file")242@typed(T_PATH, T_BOOL)243def is_file(inp):244 return os.path.isfile(inp)245@register("is_link")246@typed(T_PATH, T_BOOL)247def is_link(inp):248 return os.path.islink(inp)249@register("is_executable")250@typed(T_PATH, T_BOOL)251def is_executable(inp):252 return os.path.isfile(inp) and os.access(inp, os.X_OK)253@register("contains")254@typed(T_STRING, T_BOOL)255def contains(substring, inp):256 substring = dynamic_cast(T_STRING, substring).value257 return substring in inp258@register("nonempty", "non_empty")259@typed(None, T_BOOL)260def nonempty(inp):261 if type(inp) == list:262 return bool(inp)263 elif type(inp) == str:264 return inp.strip() != ""265 return True266@register("equal", "equals", "eq")267@typed(None, T_BOOL)268def equal(other, inp):269 return other.value == inp270@register("not_equal", "not_equals", "ne")271@typed(None, T_BOOL)272def not_equals(other, inp):273 return other.value != inp274@register("greater", "greater_than", "gt")275@typed(T_INT, T_BOOL)276def greater_than(i, inp):277 i = dynamic_cast(T_INT, i).value278 return inp > i279@register("greater_equal", "greater_equals", "ge")280@typed(T_INT, T_BOOL)281def greater_equals(i, inp):282 i = dynamic_cast(T_INT, i).value283 return inp >= i284@register("less", "less_than", "lt")285@typed(T_INT, T_BOOL)286def less_than(i, inp):287 i = dynamic_cast(T_INT, i).value288 return inp < i289@register("less_equal", "less_equals", "le")290@typed(T_INT, T_BOOL)291def less_equals(i, inp):292 i = dynamic_cast(T_INT, i).value293 return inp <= i294@register("format")295@typed(None, T_STRING)296def format(format_str, inp):297 try:298 return format_str.value.format(inp)299 except ValueError:300 panic(301 "Incorrect format string '{}' for input '{}'.".format(format_str.value, inp)302 )303@register("reverse")304@typed(None, None)305def reverse(inp):306 # slice arrays and string from start to finish in reversed order307 if type(inp) == str or type(inp) == list:308 return inp[::-1]309 # treat integers as strings310 elif type(inp) == int:311 return str(inp)[::-1]312 # booleans can not be reversed313 elif type(inp) == bool:314 panic("Cannot reverse bool value")315 # we got something unexpected316 else:...

Full Screen

Full Screen

parser.py

Source:parser.py Github

copy

Full Screen

1import sys2import re3from . import term4from . import utils as u5EXIT_NO_INPUT = 16EXIT_NO_VARS = 27EXIT_NO_SYMB = 38EXIT_PARSE = 49def parse(input):10 variables = get_variables(input)11 symbols = get_symbols(input)12 (t1, t2) = get_terms(input, variables, symbols)13 return (variables, symbols, t1, t2)14def get_variables(input):15 search = re.search(r"variables:(.*?)(?=\n)", input);16 if search:17 return list(map(lambda x : x.strip(), search.group(1).split(',')))18 else:19 u.err("no variables found; please declare them", EXIT_NO_VARS);20def get_symbols(input):21 search = re.search(r"symbols:(.*?)(?=\n)", input);22 if search:23 return list(map(lambda x : x.strip(), search.group(1).split(',')))24 else:25 u.err("no symbols found; please declare them", EXIT_NO_SYMB);26def get_terms(input, vars, symbols):27 search = re.search(r"problem:(.+)=\?(.+)", input, re.DOTALL)28 if search:29 t1 = search.group(1).strip()30 t2 = search.group(2).strip()31 t1 = "".join(t1.split())32 t2 = "".join(t2.split())33 (term1, rest1) = parse_term(t1, vars, symbols)34 if (rest1 != ''):35 parse_err("cannot parse " + t1 + ", stopped here " + rest1)36 (term2, rest2) = parse_term(t2, vars, symbols)37 if (rest2 != ''):38 parse_err("cannot parse " + t2 + ", stopped here " + rest2)39 return(term1, term2)40 return (None, None)41def parse_err(message):42 u.err(message, exit_code = EXIT_PARSE);43def parse_constant_or_var(t_string, vars, symbols):44 if (t_string in vars):45 return term.Term(t_string, [], True)46 elif (t_string in symbols):47 return term.Term(t_string, [])48 else:49 parse_err("expecting constant or variable but found " + t_string)50def find_pos_of_first_delim(t_string):51 separators = [',', '(', ')']52 indices = (list(map(lambda sep : t_string.find(sep), separators)))53 indices.append(len(t_string))54 return min(filter(lambda x : x >= 0, indices))55def parse_term_list(t_string, vars, symbols):56 t_string = t_string.lstrip()57 if (not t_string):58 return ([], '')59 if (t_string[0] == ','):60 t_string = t_string[1:]61 if (t_string[0] == ')'):62 return ([], t_string[1:])63 (trm, rest) = parse_term(t_string, vars, symbols)64 (subterms, rem) = parse_term_list(rest, vars, symbols)65 return ([trm] + subterms, rem)66def parse_term(t_string, vars, symbols):67 if (not t_string):68 parse_err("expected term but found empty string")69 first = find_pos_of_first_delim(t_string)70 tag = t_string[:first].strip()71 rest = t_string[first:].strip()72 if (not tag):73 parse_err("expected term but found " + t_string)74 if (not rest):75 return (parse_constant_or_var(tag, vars, symbols), '')76 if (rest[0] == '('):77 (subterms, rem) = parse_term_list(rest[1:], vars, symbols)78 if (tag in symbols):79 return (term.Term(tag, subterms), rem)80 else:81 parse_err("bad symbol " + tag + ", expecting one of " + str(symbols))82 elif (rest[0] == ','):83 return (parse_constant_or_var(tag, vars, symbols), rest[1:])84 elif (rest[0] == ')'):85 return (parse_constant_or_var(tag, vars, symbols), rest)86 else:...

Full Screen

Full Screen

B_Many_110.py

Source:B_Many_110.py Github

copy

Full Screen

1N_length = int(input())2T_string = input()3if len(T_string) == 1:4 if T_string == '1':5 print(2 * 10**10)6 else:7 print(10**10)8elif len(T_string) == 2:9 if T_string == '11':10 print(10**10)11 elif T_string == '10':12 print(10**10)13 elif T_string == '01':14 print(10**10 - 1)15 else:16 print(0)17elif len(T_string) == 3:18 if T_string == '110':19 print(10**10)20 elif T_string == '101':21 print(10**10 - 1)22 elif T_string == '011':23 print(10**10 - 1)24 else:25 print(0)26else:27 flag_zero = False28 for i in range(0, N_length - 2):29 sample = T_string[i: i + 3]30 if sample != '110' and sample != '101' and sample != '011':31 flag_zero = True32 break33 if flag_zero:34 print(0)35 else:36 zero_counter = T_string.count('0')37 if T_string[0:2] == '01' and T_string[-1] == '0':38 print(10**10 - zero_counter + 1)39 elif T_string[0:2] == '11' and T_string[-1] == '0':40 print(10**10 - zero_counter + 1)41 else:...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run avocado automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful