Best Python code snippet using SeleniumBase
gen_decomposition_mapping.py
Source:gen_decomposition_mapping.py
1#!/usr/bin/env python2""" Copyright 2020 Zhao HG3Permission is hereby granted, free of charge, to any person obtaining a copy4of this software and associated documentation files (the "Software"), to deal5in the Software without restriction, including without limitation the rights6to use, copy, modify, merge, publish, distribute, sublicense, and/or sell7copies of the Software, and to permit persons to whom the Software is8furnished to do so, subject to the following conditions:9The above copyright notice and this permission notice shall be included in all10copies or substantial portions of the Software.11THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR12IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,13FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE14AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER15LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,16OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE17SOFTWARE.18"""19with open('UnicodeData.txt', 'r') as reader:20 codes, tags, decompositions = [], [], []21 tag_map = {'NO_MAPPING': '', 'CANONICAL': 'canonical'}22 for line in reader:23 parts = line.strip().split(';')24 code, decomposition = parts[0], parts[5]25 if decomposition:26 decomposition = decomposition.split(' ')27 codes.append(code)28 if decomposition[0][0] == '<':29 tag = decomposition[0][1:-1]30 tag_map[tag.upper()] = tag31 tags.append(tag.upper())32 decompositions.append(decomposition[1:])33 else:34 tags.append('CANONICAL')35 decompositions.append(decomposition)36 unique_tags = ['NO_MAPPING'] + list(sorted(list(set(tags))))37 offsets, last_offset = [], 038 for decomposition in decompositions:39 offsets.append(last_offset)40 last_offset += len(decomposition)41 offsets.append(last_offset)42with open('include/unicode_char.h', 'a') as writer:43 writer.write('/** The tags of decomposition mappingy. */\n')44 writer.write('enum class DecompositionMappingTag {')45 for i, tag in enumerate(unique_tags):46 if i == 0:47 writer.write('\n ')48 elif i % 10 == 0:49 writer.write(',\n ')50 else:51 writer.write(', ')52 writer.write(tag)53 writer.write('\n};\n\n')54 writer.write('/** Outputs the decomposition mapping tag with its name. */\n')55 writer.write('std::ostream& operator<<(std::ostream&, DecompositionMappingTag);\n\n')56with open('include/unicode_data.h', 'a') as writer:57 writer.write('/** The total number of indices used to store the decomposition mappings. */\n')58 writer.write('const int32_t DECOMPOSITION_MAPPING_NUM = {};\n'.format(len(codes)))59 writer.write('/** The indices of the first character that have a different type. */\n')60 writer.write('extern const int32_t DECOMPOSITION_MAPPING_INDEX[];\n')61 writer.write('/** The decomposition mapping tags data. */\n')62 writer.write('extern const DecompositionMappingTag DECOMPOSITION_MAPPING_TAG[];\n')63 writer.write('/** The start indices of decomposition mappings. */\n')64 writer.write('extern const int32_t DECOMPOSITION_MAPPING_OFFSET[];\n')65 writer.write('/** The decomposition mapping characters data. */\n')66 writer.write('extern const UChar DECOMPOSITION_MAPPING_CHARS[];\n\n')67with open('src/decomposition_mapping.cpp', 'w') as writer:68 with open('copyright.txt', 'r') as reader:69 writer.write(reader.read())70 writer.write('#include "unicode_data.h"\n\n')71 writer.write('namespace unicode {\n\n')72 for tag in unique_tags:73 writer.write('const DecompositionMappingTag {} = DecompositionMappingTag::{};\n'.format(tag, tag))74 writer.write("\n")75 writer.write('std::ostream& operator<<(std::ostream& os, DecompositionMappingTag tag) {\n')76 writer.write(' switch (tag) {\n')77 for tag, mapped in tag_map.items():78 writer.write(' case {}: os << "{}"; break;\n'.format(tag, mapped))79 writer.write(' }\n')80 writer.write(' return os;\n')81 writer.write('}\n\n')82 writer.write('\nconst int32_t DECOMPOSITION_MAPPING_INDEX[] = {')83 for i, code in enumerate(codes):84 if i == 0:85 writer.write('\n ')86 elif i % 8 == 0:87 writer.write(',\n ')88 else:89 writer.write(', ')90 writer.write('0x' + code)91 writer.write('\n};\n')92 writer.write('\nconst DecompositionMappingTag DECOMPOSITION_MAPPING_TAG[] = {')93 for i, tag in enumerate(tags):94 if i == 0:95 writer.write('\n ')96 elif i % 5 == 0:97 writer.write(',\n ')98 else:99 writer.write(', ')100 writer.write(tag)101 writer.write('\n};\n')102 writer.write('\nconst int32_t DECOMPOSITION_MAPPING_OFFSET[] = {')103 for i, offset in enumerate(offsets):104 if i == 0:105 writer.write('\n ')106 elif i % 10 == 0:107 writer.write(',\n ')108 else:109 writer.write(', ')110 writer.write(str(offset))111 writer.write('\n};\n')112 writer.write('\nconst int32_t DECOMPOSITION_MAPPING_CHARS[] = {')113 i = 0114 for decomposition in decompositions:115 for code in decomposition:116 if i == 0:117 writer.write('\n ')118 elif i % 8 == 0:119 writer.write(',\n ')120 else:121 writer.write(', ')122 writer.write('0x' + code)123 i += 1124 writer.write('\n};\n\n')125 writer.write('} // namespace unicode\n')126with open('tests/test_decomposition_mapping_gen.cpp', 'w') as writer:127 codes = ['0000'] + codes128 tags = ['NO_MAPPING'] + tags129 decompositions = [['0000']] + decompositions130 with open('copyright.txt', 'r') as reader:131 writer.write(reader.read())132 writer.write('#include "test.h"\n')133 writer.write('#include "unicode_char.h"\n\n')134 writer.write('namespace test {\n\n')135 writer.write('class DecompositionMappingGenTest : public UnitTest {};\n\n')136 writer.write('__TEST_U(DecompositionMappingGenTest, test_cats) {\n')137 writer.write(' unicode::UChar buffer[16];\n')138 appeared = set()139 for code, tag, decomposition in zip(codes, tags, decompositions):140 if tag not in appeared:141 appeared.add(tag)142 writer.write(' __ASSERT_EQ(unicode::DecompositionMappingTag::{}, '143 'unicode::getDecompositionMappingTag({})); {{\n'.format(144 tag, '0x' + code))145 writer.write(' auto decomposition = unicode::getDecompositionMapping({});\n'.format(146 '0x' + code))147 writer.write(' __ASSERT_EQ({}u, decomposition.size());\n'.format(len(decomposition)))148 for i in range(len(decomposition)):149 writer.write(' __ASSERT_EQ({}, decomposition[{}]);\n'.format('0x' + decomposition[i], i))150 writer.write(' unicode::getDecompositionMapping({}, buffer);\n'.format('0x' + code))151 for i in range(len(decomposition)):152 writer.write(' __ASSERT_EQ({}, buffer[{}]);\n'.format('0x' + decomposition[i], i))153 writer.write(' __ASSERT_EQ(0, buffer[{}]);\n'.format(len(decomposition)))154 writer.write(' }\n')155 writer.write('}\n\n')...
gen_general_category.py
Source:gen_general_category.py
1#!/usr/bin/env python2""" Copyright 2020 Zhao HG3Permission is hereby granted, free of charge, to any person obtaining a copy4of this software and associated documentation files (the "Software"), to deal5in the Software without restriction, including without limitation the rights6to use, copy, modify, merge, publish, distribute, sublicense, and/or sell7copies of the Software, and to permit persons to whom the Software is8furnished to do so, subject to the following conditions:9The above copyright notice and this permission notice shall be included in all10copies or substantial portions of the Software.11THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR12IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,13FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE14AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER15LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,16OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE17SOFTWARE.18"""19with open('UnicodeData.txt', 'r') as reader:20 categories = [line.strip().split(';')[2] for line in reader]21 base_categories = [cat[0] for cat in categories]22with open('include/unicode_char.h', 'a') as writer:23 writer.write('/** The types of general category. */\n')24 writer.write('enum class GeneralCategory {')25 for i, category in enumerate(sorted(list(set(categories)))):26 if i == 0:27 writer.write('\n ')28 elif i % 10 == 0:29 writer.write(',\n ')30 else:31 writer.write(', ')32 writer.write(category)33 writer.write('\n};\n\n')34 writer.write('/** The types of base general category. */\n')35 writer.write('enum class BaseGeneralCategory {')36 for i, category in enumerate(sorted(list(set(base_categories)))):37 if i == 0:38 writer.write('\n ')39 elif i % 10 == 0:40 writer.write(',\n ')41 else:42 writer.write(', ')43 writer.write(category)44 writer.write('\n};\n\n')45 writer.write('/** Outputs the general category with its name. */\n')46 writer.write('std::ostream& operator<<(std::ostream&, GeneralCategory);\n')47 writer.write('/** Outputs the base general category with its name. */\n')48 writer.write('std::ostream& operator<<(std::ostream&, BaseGeneralCategory);\n\n')49with open('include/unicode_data.h', 'a') as writer:50 writer.write('/** The general category data. */\n')51 writer.write('extern const GeneralCategory GENERAL_CATEGORY[];\n')52 writer.write('/** The base general category data. */\n')53 writer.write('extern const BaseGeneralCategory BASE_GENERAL_CATEGORY[];\n\n')54with open('src/general_category.cpp', 'w') as writer:55 with open('copyright.txt', 'r') as reader:56 writer.write(reader.read())57 writer.write('#include "unicode_data.h"\n\n')58 writer.write('namespace unicode {\n\n')59 writer.write('std::ostream& operator<<(std::ostream& os, GeneralCategory c) {\n')60 writer.write(' switch (c) {\n')61 for category in sorted(list(set(categories))):62 writer.write(' case GeneralCategory::{}: os << "{}"; break;\n'.format(category, category))63 writer.write(' }\n')64 writer.write(' return os;\n')65 writer.write('}\n\n')66 writer.write('std::ostream& operator<<(std::ostream& os, BaseGeneralCategory c) {\n')67 writer.write(' switch (c) {\n')68 for category in sorted(list(set(base_categories))):69 writer.write(' case BaseGeneralCategory::{}: os << "{}"; break;\n'.format(category, category))70 writer.write(' }\n')71 writer.write(' return os;\n')72 writer.write('}\n\n')73 writer.write('\nconst GeneralCategory GENERAL_CATEGORY[] = {')74 for i, category in enumerate(categories):75 if i == 0:76 writer.write('\n ')77 elif i % 5 == 0:78 writer.write(',\n ')79 else:80 writer.write(', ')81 writer.write('GeneralCategory::%s' % category)82 writer.write('\n};\n\n')83 writer.write('\nconst BaseGeneralCategory BASE_GENERAL_CATEGORY[] = {')84 for i, category in enumerate(base_categories):85 if i == 0:86 writer.write('\n ')87 elif i % 4 == 0:88 writer.write(',\n ')89 else:90 writer.write(', ')91 writer.write('BaseGeneralCategory::%s' % category)92 writer.write('\n};\n\n')93 writer.write('} // namespace unicode\n')94with open('tests/test_general_category_gen.cpp', 'w') as writer:95 with open('copyright.txt', 'r') as reader:96 writer.write(reader.read())97 writer.write('#include "test.h"\n')98 writer.write('#include "unicode_char.h"\n\n')99 writer.write('namespace test {\n\n')100 writer.write('class GeneralCategoryGenTest : public UnitTest {};\n\n')101 writer.write('__TEST_U(GeneralCategoryGenTest, test_cats) {\n')102 appeared = set()103 with open('UnicodeData.txt', 'r') as reader:104 for line in reader:105 parts = line.strip().split(';')106 category = parts[2]107 if category not in appeared:108 appeared.add(category)109 writer.write(' __ASSERT_EQ(unicode::GeneralCategory::{}, unicode::getGeneralCategory({}));\n'.format(110 category, '0x' + parts[0]111 ))112 writer.write('}\n\n')113 writer.write('__TEST_U(GeneralCategoryGenTest, test_base_cats) {\n')114 appeared = set()115 with open('UnicodeData.txt', 'r') as reader:116 for line in reader:117 parts = line.strip().split(';')118 category = parts[2][0]119 if category not in appeared:120 appeared.add(category)121 writer.write(' __ASSERT_EQ(unicode::BaseGeneralCategory::{}, '122 'unicode::getBaseGeneralCategory({}));\n'.format(category, '0x' + parts[0]))123 writer.write('}\n\n')...
gen_canonical_combining_class.py
Source:gen_canonical_combining_class.py
1#!/usr/bin/env python2""" Copyright 2020 Zhao HG3Permission is hereby granted, free of charge, to any person obtaining a copy4of this software and associated documentation files (the "Software"), to deal5in the Software without restriction, including without limitation the rights6to use, copy, modify, merge, publish, distribute, sublicense, and/or sell7copies of the Software, and to permit persons to whom the Software is8furnished to do so, subject to the following conditions:9The above copyright notice and this permission notice shall be included in all10copies or substantial portions of the Software.11THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR12IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,13FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE14AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER15LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,16OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE17SOFTWARE.18"""19with open('UnicodeData.txt', 'r') as reader:20 last, indices, canonicals, classes = '', [], [], {}21 for line in reader:22 parts = line.strip().split(';')23 if parts[3] != last:24 last = parts[3]25 indices.append(parts[0])26 canonicals.append(parts[3])27 classes[parts[3]] = parts[0]28with open('include/unicode_data.h', 'a') as writer:29 writer.write('/** The total number of indices used to store the canonical combing class. */\n')30 writer.write('const int32_t CANONICAL_COMBINING_NUM = {};\n'.format(len(indices)))31 writer.write('/** The indices of the first character that have a different type. */\n')32 writer.write('extern const int32_t CANONICAL_COMBINING_INDEX[];\n')33 writer.write('/** The canonical combining class data. */\n')34 writer.write('extern const int32_t CANONICAL_COMBINING_CLASS[];\n\n')35with open('src/canonical_combining_class.cpp', 'w') as writer:36 with open('copyright.txt', 'r') as reader:37 writer.write(reader.read())38 writer.write('#include "unicode_data.h"\n\n')39 writer.write('namespace unicode {\n\n')40 writer.write('\nconst int32_t CANONICAL_COMBINING_INDEX[] = {')41 for i, index in enumerate(indices):42 if i == 0:43 writer.write('\n ')44 elif i % 8 == 0:45 writer.write(',\n ')46 else:47 writer.write(', ')48 writer.write('0x' + index)49 writer.write('\n};\n')50 writer.write('\nconst int32_t CANONICAL_COMBINING_CLASS[] = {')51 for i, canonical in enumerate(canonicals):52 if i == 0:53 writer.write('\n ')54 elif i % 8 == 0:55 writer.write(',\n ')56 else:57 writer.write(', ')58 writer.write(canonical)59 writer.write('\n};\n\n')60 writer.write('} // namespace unicode\n')61with open('tests/test_canonical_combining_class_gen.cpp', 'w') as writer:62 with open('copyright.txt', 'r') as reader:63 writer.write(reader.read())64 writer.write('#include "test.h"\n')65 writer.write('#include "unicode_char.h"\n\n')66 writer.write('namespace test {\n\n')67 writer.write('class CanonicalCombiningClassGenTest : public UnitTest {};\n\n')68 writer.write('__TEST_U(CanonicalCombiningClassGenTest, test_classes) {\n')69 for canonical, code in classes.items():70 writer.write(' __ASSERT_EQ({}, unicode::getCanonicalCombiningClass({}));\n'.format(71 canonical, '0x' + code72 ))73 writer.write('}\n\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!!