Best Python code snippet using autotest_python
translate.py
Source:translate.py
...25def translate_text(text):26 if len(text) > 255:27 return f"Too many characters. Try sending less than 255 characters"28 if not text.strip():29 return parse_global(TYPE_HELP, EXAMPLES, MISSING_TEXT)30 if text == "help":31 return parse_global(32 ["Usage", "Examples"], HELP_TEXT, "Translate guide"33 )34 if text == "languages":35 return "https://cloud.google.com/translate/docs/languages"36 kwargs = text.split(" ")[0].split("=")37 if len(kwargs) == 2 and kwargs[0] == "target":38 if not kwargs[1]:39 return parse_global(TYPE_HELP, EXAMPLES, MISSING_TARGET)40 target = kwargs[1]41 text = " ".join(text.split(" ")[1:])42 if not text.strip():43 return parse_global(TYPE_HELP, EXAMPLES, MISSING_TEXT)44 else:45 target = "en"46 translate_client = translate.Client()47 if isinstance(text, six.binary_type):48 text = text.decode("utf-8")49 # Text can also be a sequence of strings, in which case this method50 # will return a sequence of results for each text.51 try:52 result = translate_client.translate(53 text, target_language=target, format_="text"54 )55 except (GoogleAPICallError, BadRequest) as e:56 logger.error(e)57 return "Something went wrong. For usage and examples type '/translate help'."58 return parse_global(59 title="ð¬ Translate",60 stats={61 "Source text": result["input"],62 "Source language": result["detectedSourceLanguage"],63 "Translation": result["translatedText"],64 },65 items={},...
03-extract_list.py
Source:03-extract_list.py
2import csv3import re4from scrapy.http import HtmlResponse5# Extract links from bullet point lists.6def parse_global(html):7 response = HtmlResponse("", body=html)8 return response.css("li a[href*='wikidata.org/wiki/Q']::attr(href)").getall()9# Mapping from Wikidata IDs to the corresponding enwiki page titles.10with open("wikidata_to_enwiki.tsv", "rt") as f:11 wikidata_to_enwiki = {12 wikidata: enwiki for wikidata, enwiki in csv.reader(f, delimiter="\t")13 }14# List of sections from the following page:15# https://meta.wikimedia.org/wiki/List_of_articles_every_Wikipedia_should_have/Expanded16PAGES = "People History Geography Arts Philosophy_and_religion Anthropology,_psychology_and_everyday_life Society_and_social_sciences Biology_and_health_sciences Physical_sciences Technology Mathematics".split()17WIKIDATA_LINK = re.compile("https://www\.wikidata\.org/wiki/(Q[0-9]+)$")18# Extract of English Wikipedia page titles corresponding to pages which are considered19# fundamental. Write them to a TSV file, including their broad category too.20count = 021skipped = 122with open("global_list.tsv", "wt") as fout:23 for page in PAGES:24 category = page.replace("_", " ")25 with open(f"html/Global:{page}.html", "rb") as fin:26 for link in parse_global(fin.read()):27 match = re.match(WIKIDATA_LINK, link)28 if match:29 entity_id = match.group(1)30 if entity_id not in wikidata_to_enwiki:31 skipped += 132 else:33 fout.write(f"{category}\t{wikidata_to_enwiki[entity_id]}\n")...
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!!