Best Python code snippet using slash
parameter_types.py
Source:parameter_types.py
...14"""15import re16import unicodedata17import six18def _is_printable(char):19 """determine if a unicode code point is printable.20 This checks if the character is either "other" (mostly control21 codes), or a non-horizontal space. All characters that don't match22 those criteria are considered printable; that is: letters;23 combining marks; numbers; punctuation; symbols; (horizontal) space24 separators.25 """26 category = unicodedata.category(char)27 return (not category.startswith("C") and28 (not category.startswith("Z") or category == "Zs"))29def _get_all_chars():30 for i in range(0xFFFF):31 yield six.unichr(i)32# build a regex that matches all printable characters. This allows33# spaces in the middle of the name. Also note that the regexp below34# deliberately allows the empty string. This is so only the constraint35# which enforces a minimum length for the name is triggered when an36# empty string is tested. Otherwise it is not deterministic which37# constraint fails and this causes issues for some unittests when38# PYTHONHASHSEED is set randomly.39def _build_regex_range(ws=True, invert=False, exclude=None):40 """Build a range regex for a set of characters in utf8.41 This builds a valid range regex for characters in utf8 by42 iterating the entire space and building up a set of x-y ranges for43 all the characters we find which are valid.44 :param ws: should we include whitespace in this range.45 :param exclude: any characters we want to exclude46 :param invert: invert the logic47 The inversion is useful when we want to generate a set of ranges48 which is everything that's not a certain class. For instance,49 produce all all the non printable characters as a set of ranges.50 """51 if exclude is None:52 exclude = []53 regex = ""54 # are we currently in a range55 in_range = False56 # last character we found, for closing ranges57 last = None58 # last character we added to the regex, this lets us know that we59 # already have B in the range, which means we don't need to close60 # it out with B-B. While the later seems to work, it's kind of bad form.61 last_added = None62 def valid_char(char):63 if char in exclude:64 result = False65 elif ws:66 result = _is_printable(char)67 else:68 # Zs is the unicode class for space characters, of which69 # there are about 10 in this range.70 result = (_is_printable(char) and71 unicodedata.category(char) != "Zs")72 if invert is True:73 return not result74 return result75 # iterate through the entire character range. in_76 for c in _get_all_chars():77 if valid_char(c):78 if not in_range:79 regex += re.escape(c)80 last_added = c81 in_range = True82 else:83 if in_range and last != last_added:84 regex += "-" + re.escape(last)...
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!!