Best Python code snippet using tox_python
parse_slack.py
Source:parse_slack.py
...38 """39 Search for matches to self._search pattern in the text string.40 """41 return self._search_pattern.finditer(text_string)42 def _replace_match(self,field):43 """44 Try to replace the field with match in replace_dict. Replace both45 match and whether replacement was successful.46 """47 try:48 new_field = self._replace_dict[field]49 return new_field, True50 except KeyError:51 return field, False52 def _process_match(self,field):53 """54 Process a match.55 """56 field, status = self._replace_match(field)57 return field58class SymmetricalStringHandler(StringHandler):59 """60 Identical to string handler except only returns every other match of the61 regex match to avoid screwing up patterns like 'code1' and 'code2'.62 """63 def _search(self,text_string):64 # Only return every other match if search pattern is symmetrical65 # (like `blah` or *blah*)66 return list(self._search_pattern.finditer(text_string))[::2]67class UrlHandler(StringHandler):68 """69 Process urls.70 """71 def __init__(self,pattern="\<http.*?\>|\<ftp.*?\>",replace_dict={}):72 super().__init__(pattern,replace_dict)73 def _process_match(self,field):74 field, status = self._replace_match(field)75 url_pattern = re.compile("\?|\|")76 url = url_pattern.split(field)[0]77 if url[-4:].lower() in [".png",".jpg","jpeg",".gif",".bmp"]:78 field = "<a href=\"{}\"><img src=\"{}\" class=\"img-fluid url-image\"></a>".format(url,url)79 else:80 field = "<a href=\"{}\">{}</a>".format(url,url)81 return field82class AtUserHandler(StringHandler):83 """84 Process @user fields.85 """86 def __init__(self,pattern="\<\@.*?\>",replace_dict={}):87 super().__init__(pattern,replace_dict)88 def _process_match(self,field):89 field, status = self._replace_match(field)90 return "<span class=\"font-italic\">@{} </span>".format(field)91class HashChannelHandler(StringHandler):92 """93 Process #channel fields.94 """95 def __init__(self,pattern="\<\#.*?\>",replace_dict={}):96 super().__init__(pattern,replace_dict)97 def _process_match(self,field):98 field, status = self._replace_match(field)99 try:100 new_field = field.split("|")[1]101 except IndexError:102 new_field = field103 return "<span class=\"font-italic\">#{} </span>".format(new_field)104class CodeHandler(SymmetricalStringHandler):105 """106 Process `code` fields.107 """108 def __init__(self,pattern="\`.*?\`",replace_dict={}):109 super().__init__(pattern,replace_dict)110 def _process_match(self,field):111 field, status = self._replace_match(field)112 return "<code>{}</code>".format(field)113class CodeBlockHandler(SymmetricalStringHandler):114 """115 Process ```code``` fields.116 """117 def __init__(self,pattern="```.*?```",replace_dict={}):118 super().__init__(pattern,replace_dict)119 def _process_match(self,field):120 field, status = self._replace_match(field)121 return "<div class=\"card m-1 bg-light p-2\"><pre><code>{}</code></pre></div>".format(field[2:-2])122def _process_polly_poll(msg):123 name = "Polly Poll"124 try:125 icon = msg["icons"]["image_48"]126 except KeyError:127 icon = None128 poll_creator = msg["text"].split()[0]129 question = msg["blocks"][0]["text"]["text"][1:-1]130 i = 2131 responses = []132 while True:133 try:134 this_response = msg["blocks"][i]["fields"]...
subst.py
Source:subst.py
...102 the "{posargs:andy}" is replaced with "andy" (since no posargs were103 passed).104 """105 def replace(m):106 return _replace_match(m, env)107 for _ in range(DEPTH):108 s = re.sub(r"{[^{}]*}", replace, s)109 return s110def _replace_match(m, env):111 """Given a match object, having matched something inside curly braces,112 replace the contents if matches one of the supported tox-substitutions."""113 # ditch the curly braces114 s = m.group()[1:-1].strip()115 try:116 # get the env attributes e.g. envpython or toxinidir.117 # Note: if you ask for a env methodname this will raise118 # later on... so don't do that.119 return getattr(env, s)120 except AttributeError:121 pass122 for r in [_replace_envvar, _replace_config, _replace_posargs]:123 try:124 return r(s, env)...
arguments.py
Source:arguments.py
...10 if isinstance(arg, list):11 return [arg_substitute(a, data) for a in arg]12 elif not isinstance(arg, string_types):13 return None14 def _replace_match(m):15 r = data.get(m.group().strip("}{"))16 return str(r) if r is not None else m.group() # put back the original if we don't have a match17 return _find_arg_re.sub(_replace_match, arg)18def variable_substitute(arg, data, flatten=False):19 if isinstance(arg, list) and flatten:20 # TODO: switch to chain? maybe just remove flatten altogether?21 return reduce(operator.add, (variable_substitute(a, data, flatten) for a in arg))22 elif isinstance(arg, list):23 return [variable_substitute(a, data, flatten) for a in arg]24 elif not isinstance(arg, string_types):25 return None26 matches = _find_arg_re.findall(arg)27 subs = [data.get(m) for m in matches] if matches else None28 if matches and subs:...
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!!