Best Python code snippet using tempest_python
tests.py
Source:tests.py
...9 c.increment_count("1", "foo", 3)10 c.increment_count("1", "bar", 2)11 c.increment_count("2", "foo", 2)12 c.increment_count("2", "bar", 1)13 self.assertEqual(c.get_counts()["foo"], 5)14 self.assertEqual(c.get_counts()["bar"], 3)15 def test_multi_level(self):16 import termdoc17 c = termdoc.HTDM()18 c.increment_count("1.1", "foo", 3)19 c.increment_count("1.2", "foo", 3)20 c.increment_count("1.1", "bar", 2)21 c.increment_count("1.2", "bar", 2)22 c.increment_count("2.1", "foo", 2)23 c.increment_count("2.2", "foo", 2)24 c.increment_count("2.1", "bar", 1)25 c.increment_count("2.2", "bar", 1)26 self.assertEqual(c.get_counts()["foo"], 10)27 self.assertEqual(c.get_counts()["bar"], 6)28 self.assertEqual(c.get_counts("2")["foo"], 4)29 def test_load1(self):30 import termdoc31 c = termdoc.HTDM()32 c.load("test_data/test1.tsv")33 self.assertEqual(c.get_counts()["foo"], 10)34 self.assertEqual(c.get_counts()["bar"], 5)35 self.assertEqual(c.get_counts()["baz"], 5)36 self.assertEqual(c.get_counts("1")["foo"], 9)37 self.assertEqual(c.get_counts("1")["bar"], 5)38 self.assertEqual(c.get_counts("1")["baz"], 0)39 self.assertEqual(c.get_counts("2")["foo"], 1)40 self.assertEqual(c.get_counts("2")["bar"], 0)41 self.assertEqual(c.get_counts("2")["baz"], 5)42 self.assertEqual(c.get_counts("1.1")["foo"], 7)43 self.assertEqual(c.get_counts("1.1")["bar"], 4)44 self.assertEqual(c.get_counts("1.1")["baz"], 0)45 self.assertEqual(c.get_counts("1.2")["foo"], 2)46 self.assertEqual(c.get_counts("1.2")["bar"], 0)47 self.assertEqual(c.get_counts("1.2")["baz"], 0)48 self.assertEqual(c.get_counts("1.3")["foo"], 0)49 self.assertEqual(c.get_counts("1.3")["bar"], 1)50 self.assertEqual(c.get_counts("1.3")["baz"], 0)51 self.assertEqual(c.get_counts("2.1")["foo"], 1)52 self.assertEqual(c.get_counts("2.1")["bar"], 0)53 self.assertEqual(c.get_counts("2.1")["baz"], 5)54 def test_load2(self):55 import termdoc56 c = termdoc.HTDM()57 c.load("test_data/test2.tsv")58 self.assertEqual(c.get_counts()["foo"], 10)59 self.assertEqual(c.get_counts()["bar"], 5)60 self.assertEqual(c.get_counts()["baz"], 5)61 self.assertEqual(c.get_counts("1")["foo"], 9)62 self.assertEqual(c.get_counts("1")["bar"], 5)63 self.assertEqual(c.get_counts("1")["baz"], 0)64 self.assertEqual(c.get_counts("2")["foo"], 1)65 self.assertEqual(c.get_counts("2")["bar"], 0)66 self.assertEqual(c.get_counts("2")["baz"], 5)67 self.assertEqual(c.get_counts("1.1")["foo"], 7)68 self.assertEqual(c.get_counts("1.1")["bar"], 4)69 self.assertEqual(c.get_counts("1.1")["baz"], 0)70 self.assertEqual(c.get_counts("1.2")["foo"], 2)71 self.assertEqual(c.get_counts("1.2")["bar"], 0)72 self.assertEqual(c.get_counts("1.2")["baz"], 0)73 self.assertEqual(c.get_counts("1.3")["foo"], 0)74 self.assertEqual(c.get_counts("1.3")["bar"], 1)75 self.assertEqual(c.get_counts("1.3")["baz"], 0)76 self.assertEqual(c.get_counts("2.1")["foo"], 1)77 self.assertEqual(c.get_counts("2.1")["bar"], 0)78 self.assertEqual(c.get_counts("2.1")["baz"], 5)79 def test_load3(self):80 import termdoc81 c = termdoc.HTDM()82 self.assertRaises(ValueError, c.load, "test_data/test3e.tsv")83 def test_load4(self):84 import termdoc85 c = termdoc.HTDM()86 self.assertRaises(ValueError, c.load, "test_data/test4e.tsv")87 def test_load5(self):88 import termdoc89 c = termdoc.HTDM()90 c.load("test_data/test2.tsv", prefix="xxx")91 self.assertEqual(c.get_counts()["foo"], 10)92 self.assertEqual(c.get_counts()["bar"], 5)93 self.assertEqual(c.get_counts()["baz"], 5)94 self.assertEqual(c.get_counts("xxx")["foo"], 10)95 self.assertEqual(c.get_counts("xxx")["bar"], 5)96 self.assertEqual(c.get_counts("xxx")["baz"], 5)97 self.assertEqual(c.get_counts("xxx.1")["foo"], 9)98 self.assertEqual(c.get_counts("xxx.1")["bar"], 5)99 self.assertEqual(c.get_counts("xxx.1")["baz"], 0)100 self.assertEqual(c.get_counts("xxx.2")["foo"], 1)101 self.assertEqual(c.get_counts("xxx.2")["bar"], 0)102 self.assertEqual(c.get_counts("xxx.2")["baz"], 5)103 def test_save(self):104 import termdoc105 _, filename = tempfile.mkstemp()106 c1 = termdoc.HTDM()107 c1.load("test_data/test2.tsv")108 c1.save(filename)109 c2 = termdoc.HTDM()110 c2.load(filename)111 os.remove(filename)112 self.assertEqual(c2.get_counts()["foo"], 10)113 self.assertEqual(c2.get_counts()["bar"], 5)114 self.assertEqual(c2.get_counts()["baz"], 5)115 self.assertEqual(c2.get_counts("1")["foo"], 9)116 self.assertEqual(c2.get_counts("1")["bar"], 5)117 self.assertEqual(c2.get_counts("1")["baz"], 0)118 self.assertEqual(c2.get_counts("2")["foo"], 1)119 self.assertEqual(c2.get_counts("2")["bar"], 0)120 self.assertEqual(c2.get_counts("2")["baz"], 5)121 self.assertEqual(c2.get_counts("1.1")["foo"], 7)122 self.assertEqual(c2.get_counts("1.1")["bar"], 4)123 self.assertEqual(c2.get_counts("1.1")["baz"], 0)124 self.assertEqual(c2.get_counts("1.2")["foo"], 2)125 self.assertEqual(c2.get_counts("1.2")["bar"], 0)126 self.assertEqual(c2.get_counts("1.2")["baz"], 0)127 self.assertEqual(c2.get_counts("1.3")["foo"], 0)128 self.assertEqual(c2.get_counts("1.3")["bar"], 1)129 self.assertEqual(c2.get_counts("1.3")["baz"], 0)130 self.assertEqual(c2.get_counts("2.1")["foo"], 1)131 self.assertEqual(c2.get_counts("2.1")["bar"], 0)132 self.assertEqual(c2.get_counts("2.1")["baz"], 5)133 def test_prune(self):134 import termdoc135 c = termdoc.HTDM()136 c.increment_count("1", "foo", 3)137 c.increment_count("1", "bar", 2)138 c.increment_count("2", "foo", 2)139 c.increment_count("2", "bar", 1)140 self.assertEqual(c.get_counts()["foo"], 5)141 self.assertEqual(c.get_counts()["bar"], 3)142 self.assertEqual(len(c.counters), 2)143 c.prune(1)144 self.assertEqual(c.get_counts()["foo"], 5)145 self.assertEqual(c.get_counts()["bar"], 3)146 self.assertEqual(len(c.counters), 1)147 def test_leaf_entries(self):148 import termdoc149 c = termdoc.HTDM()150 c.increment_count("1", "foo", 3)151 c.increment_count("1", "bar", 2)152 c.increment_count("2", "foo", 2)153 c.increment_count("2", "bar", 1)154 self.assertEqual(155 list(c.leaf_entries()),156 [157 ("1", "foo", 3),158 ("1", "bar", 2),159 ("2", "foo", 2),160 ("2", "bar", 1),161 ],162 )163 def test_leaf_entries_with_prefix(self):164 import termdoc165 c = termdoc.HTDM()166 c.increment_count("1.1", "foo", 3)167 c.increment_count("1.1", "bar", 2)168 c.increment_count("1.2", "foo", 2)169 c.increment_count("1.2", "bar", 1)170 c.increment_count("2.1", "foo", 5)171 c.increment_count("2.1", "bar", 1)172 c.increment_count("2.2", "foo", 3)173 c.increment_count("2.2", "bar", 7)174 self.assertEqual(175 list(c.leaf_entries("1")),176 [177 ("1", "foo", 3),178 ("1", "bar", 2),179 ("2", "foo", 2),180 ("2", "bar", 1),181 ],182 )183 def test_graft(self):184 import termdoc185 c1 = termdoc.HTDM()186 c1.increment_count("1", "foo", 3)187 c1.increment_count("1", "bar", 2)188 c1.increment_count("2", "foo", 2)189 c1.increment_count("2", "bar", 1)190 c2 = termdoc.HTDM()191 c2.increment_count("4.1", "foo", 5)192 c2.increment_count("5.3", "foo", 6)193 c2.graft("5", c1)194 self.assertEqual(195 list(c2.leaf_entries()),196 [197 ("4.1", "foo", 5),198 ("5.3", "foo", 6),199 ("5.1", "foo", 3),200 ("5.1", "bar", 2),201 ("5.2", "foo", 2),202 ("5.2", "bar", 1),203 ],204 )205 def test_copy1(self):206 import termdoc207 c1 = termdoc.HTDM()208 c1.increment_count("1.1", "foo", 3)209 c1.increment_count("1.1", "bar", 2)210 c1.increment_count("1.2", "foo", 2)211 c1.increment_count("1.2", "bar", 1)212 c1.increment_count("2.1", "foo", 5)213 c1.increment_count("2.1", "bar", 1)214 c1.increment_count("2.2", "foo", 3)215 c1.increment_count("2.2", "bar", 7)216 c2 = c1.copy()217 c2.increment_count("1.3", "foo", 2)218 self.assertEqual(c1.get_counts("1")["foo"], 5)219 self.assertEqual(c2.get_counts("1")["foo"], 7)220 def test_copy2(self):221 import termdoc222 c1 = termdoc.HTDM()223 c1.increment_count("1.1", "foo", 3)224 c1.increment_count("1.1", "bar", 2)225 c1.increment_count("1.2", "foo", 2)226 c1.increment_count("1.2", "bar", 1)227 c1.increment_count("2.1", "foo", 5)228 c1.increment_count("2.1", "bar", 1)229 c1.increment_count("2.2", "foo", 3)230 c1.increment_count("2.2", "bar", 7)231 c2 = c1.copy("1")232 self.assertEqual(c1.get_counts()["foo"], 13)233 self.assertEqual(c1.get_counts("1")["foo"], 5)234 self.assertEqual(c2.get_counts()["foo"], 5)235class TestDuplicates(unittest.TestCase):236 def test_allow(self):237 import termdoc238 c = termdoc.HTDM()239 c.increment_count("", "foo", 3)240 c.increment_count("", "foo", 2)241 self.assertEqual(c.get_counts()["foo"], 5)242 def test_explicit_allow(self):243 import termdoc244 c = termdoc.HTDM(duplicates=termdoc.Duplicates.ALLOW)245 c.increment_count("", "foo", 3)246 c.increment_count("", "foo", 2)247 self.assertEqual(c.get_counts()["foo"], 5)248 def test_ignore(self):249 import termdoc250 c = termdoc.HTDM(duplicates=termdoc.Duplicates.IGNORE)251 c.increment_count("", "foo", 3)252 c.increment_count("", "foo", 2)253 self.assertEqual(c.get_counts()["foo"], 3)254 def test_error(self):255 import termdoc256 c = termdoc.HTDM(duplicates=termdoc.Duplicates.ERROR)257 c.increment_count("", "foo", 3)258 self.assertRaises(ValueError, c.increment_count, "", "foo", 2)259 self.assertEqual(c.get_counts()["foo"], 3)260 def test_multi_level_error(self):261 import termdoc262 # this test makes sure the `if first` is working in `increment_count`263 c = termdoc.HTDM(duplicates=termdoc.Duplicates.ERROR)264 c.increment_count("1.1", "foo", 3)265 c.increment_count("1.2", "foo", 3)266 c.increment_count("1.1", "bar", 2)267 c.increment_count("1.2", "bar", 2)268 c.increment_count("2.1", "foo", 2)269 c.increment_count("2.2", "foo", 2)270 c.increment_count("2.1", "bar", 1)271 c.increment_count("2.2", "bar", 1)272 self.assertEqual(c.get_counts()["foo"], 10)273 self.assertEqual(c.get_counts()["bar"], 6)274 self.assertEqual(c.get_counts("2")["foo"], 4)275if __name__ == "__main__":...
16_2_1_Simonbw_question1.py
Source:16_2_1_Simonbw_question1.py
...5 # print_counts(counts)6 if counts_is_empty(counts):7 return ''8 for i, word in enumerate(WORDS):9 word_counts = get_counts(word)10 try:11 return str(i) + get_number(sub_counts(counts, word_counts))12 except:13 pass14 raise Exception()15def counts_is_empty(counts):16 for k, v in counts.items():17 if v > 0:18 return False19 return True20def get_counts(s):21 counts = defaultdict(lambda: 0)22 for c in s:23 counts[c] += 124 return counts25def counts_contains(a, b):26 for k, v in b.items():27 if a[k] < v:28 return False29 return True30def sub_counts(a, b):31 if not counts_contains(a, b):32 raise Exception()33 result = get_counts('')34 for k, v in a.items():35 result[k] = v - b[k]36 return result37def print_counts(counts):38 s = ''39 for k, v in counts.items():40 s += str(v) + k + ' '41 print(s)42T = int(input())43for case_number in range(T):44 case_string = "Case #" + str(case_number + 1) + ":"45 s = input()46 number = get_number(get_counts(s))47 print(case_string, number)48# print(counts_contains(get_counts('aabc'), get_counts('abc')))49# print_counts(sub_counts(get_counts('aabc'), get_counts('abc')))50# print(counts_contains(get_counts('aabc'), get_counts('abcc')))51# print_counts(sub_counts(get_counts('aabc'), get_counts('abcc')))52# print('number:', get_number(get_counts('ONE')))53# print('number:', get_number(get_counts('ONETWOTHREE')))...
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!!