Best Python code snippet using unittest-xml-reporting_python
HW6.py
Source:HW6.py
...122 def delete_counter(self,counter):123 counter._posinL.data().delete (counter._position)124 counter._position=None125 counter._posinL = None126 def increment_counter(self,counter):127 #if counter._position._plist != self._L:128 # raise ValueError('counter does not belong to this Counter') 129 if counter._posinL._plist != self._L:130 raise ValueError ('counter does not belong to this Counter')131 before = None132 newpos = None133 counter._position.data()._count+=1134 if counter._posinL == self._L.first ():135 newp = PList()136 self._L.add_first (newp)137 before = self._L.before (counter._posinL)138 newpos = before.data().add_last (counter._position.data())139 if len (counter._position._plist) == 1:140 counter._position._plist.delete (counter._position)141 self._L.delete (counter._posinL)142 else:143 self.delete_counter (counter)144 counter._position = newpos145 counter._posinL = before146 else:147 before = self._L.before (counter._posinL)148 if before.data().first().data()._count == counter.count():149 newpos = before.data().add_last (counter._position.data())150 if len (counter._position._plist) == 1:151 counter._position._plist.delete (counter._position)152 self._L.delete (counter._posinL)153 else:154 self.delete_counter (counter)155 counter._position = newpos156 counter._posinL = before157 else:158 newp = PList()159 newpos = newp.add_first (counter._position.data())160 self._L.add_before (counter._posinL, newp)161 162 if len (counter._position._plist) == 1:163 counter._position._plist.delete (counter._position)164 self._L.delete (counter._posinL)165 else:166 self.delete_counter (counter)167 counter._posinL = self._L.before (counter._posinL)168 counter._position = newpos169# before_position=self._L.before(counter._position)170# while (before_position and 171# before_position.data()._count172# < counter.count()):173# new_position=self._L.add_before(before_position,counter._position.data())174# self._L.delete(counter._position)175# counter._position=new_position176# before_position=self._L.before(counter._position)177 def __iter__(self):178 position=self._L.first()179 while position:180 for stuff in position.data():181 #Counters.Counter(position)182 yield stuff183 position=self._L.after(position)184 185# position = self._L.first ()186# while position:187# for stuff in position.data():188# yield stuff._name189# yield stuff._count190# position = self._L.after (position)191class Counters2:192 class _Item:193 def __init__(self,name):194 self._name=name195 self._count=0196 class Counter:197 def __init__(self,position):198 self._position=position199 def name(self):200 return self._position.data()._name201 def count(self):202 return self._position.data()._count 203 def __init__(self):204 self._L=PList()205 def new_counter(self,name):206 self._L.add_last(Counters2._Item(name))207 return Counters2.Counter(self._L.last())208 def delete_counter(self,counter):209 self._L.delete(counter._position)210 counter._position=None211 def increment_counter(self,counter):212 counter._position.data()._count+=1213 before_position=self._L.before(counter._position)214 while (before_position and 215 before_position.data()._count216 < counter.count()):217 new_position=self._L.add_before(before_position,counter._position.data())218 self._L.delete(counter._position)219 counter._position=new_position220 before_position=self._L.before(counter._position)221 def __iter__(self):222 position=self._L.first()223 while position:224 yield Counters2.Counter(position)225 position=self._L.after(position)226#227#C=Counters() 228#D=Counters()229#cc=C.new_counter("A counter in C")230#D.increment_counter(cc)231C=Counters() 232a=C.new_counter("a")233b=C.new_counter("b")234c=C.new_counter("c")235d=C.new_counter("d")236e=C.new_counter("e")237f=C.new_counter("f")238g=C.new_counter("g")239h=C.new_counter("h")240i=C.new_counter("i")241j=C.new_counter("j")242C.increment_counter (d)243C.increment_counter (e)244C.increment_counter (f)245C.increment_counter (g)246C.increment_counter (h)247C.increment_counter (i)248C.increment_counter (j)249C.increment_counter (g)250C.increment_counter (h)251C.increment_counter (i)252C.increment_counter (j)253C.increment_counter (g)254C.increment_counter (h)255C.increment_counter (i)256C.increment_counter (j)257C.increment_counter (g)258C.increment_counter (h)259C.increment_counter (i)260C.increment_counter (j)261C.increment_counter (g)262C.increment_counter (h)263C.increment_counter (i)264#D = Counters ()265#D.increment_counter (a)266for stuff in C:267 print (stuff._name, stuff._count)268 269 270D=Counters2() 271a2=D.new_counter("a")272b2=D.new_counter("b")273c2=D.new_counter("c")274d2=D.new_counter("d")275e2=D.new_counter("e")276f2=D.new_counter("f")277g2=D.new_counter("g")278h2=D.new_counter("h")279i2=D.new_counter("i")280j2=D.new_counter("j")281D.increment_counter (d2)282D.increment_counter (e2)283D.increment_counter (f2)284D.increment_counter (g2)285D.increment_counter (h2)286D.increment_counter (i2)287D.increment_counter (j2)288D.increment_counter (g2)289D.increment_counter (h2)290D.increment_counter (i2)291D.increment_counter (j2)292D.increment_counter (g2)293D.increment_counter (h2)294D.increment_counter (i2)295D.increment_counter (j2)296D.increment_counter (g2)297D.increment_counter (h2)298D.increment_counter (i2)299D.increment_counter (j2)300D.increment_counter (g2)301D.increment_counter (h2)302D.increment_counter (i2)303for stuff in D:304 print (stuff._position.data())305"""306c = Counters ()307cc = C.new_counter ("A counter in C")308dd = C.new_counter ("B counter in C") 309#C.increment_counter (cc)310ee = C.new_counter ("C counter in C")311for stuff in C:312 print (stuff._name, stuff._count)313C.increment_counter (cc)314C.increment_counter (dd)315C.increment_counter (dd)316C.increment_counter (ee)317ff = C.new_counter ("D counter in C")318D.increment_counter(cc)319for stuff in C:320 if stuff:321 print (stuff._name, stuff._count)...
memoize.py
Source:memoize.py
...3class MemoizeTest(TestCase):4 def test_no_args(self):5 """Test @memoize on functions without any arguments."""6 counter = []7 def increment_counter():8 counter.append(None)9 return len(counter)10 self.assertEqual(len(counter), 0)11 self.assertEqual(increment_counter(), 1)12 self.assertEqual(len(counter), 1)13 self.assertEqual(increment_counter(), 2)14 self.assertEqual(len(counter), 2)15 increment_counter = memoize(increment_counter)16 self.assertEqual(increment_counter(), 3)17 self.assertEqual(len(counter), 3)18 self.assertEqual(increment_counter(), 3)19 self.assertEqual(len(counter), 3)20 def test_args(self):21 """Test @memoize on functions with arguments."""22 counter = []23 def sum_squared(x, y):24 counter.append(None)25 return x ** 2 + y ** 226 self.assertEqual(len(counter), 0)27 self.assertEqual(sum_squared(3, 4), 25)28 self.assertEqual(len(counter), 1)29 self.assertEqual(sum_squared(4, 5), 41)30 self.assertEqual(len(counter), 2)31 sum_squared = memoize(sum_squared)32 self.assertEqual(sum_squared(3, 4), 25)33 self.assertEqual(len(counter), 3)34 self.assertEqual(sum_squared(3, 4), 25)35 self.assertEqual(sum_squared(3, 4), 25)36 self.assertEqual(sum_squared(3, 4), 25)37 self.assertEqual(len(counter), 3)38 self.assertEqual(sum_squared(4, 5), 41)39 self.assertEqual(len(counter), 4)40 self.assertEqual(sum_squared(4, 5), 41)41 self.assertEqual(sum_squared(4, 5), 41)42 self.assertEqual(sum_squared(4, 5), 41)43 self.assertEqual(len(counter), 4)44 def test_clear(self):45 """Test the `clear()` method added to wrapped functions."""46 counter = []47 def increment_counter():48 counter.append(None)49 return len(counter)50 increment_counter = memoize(increment_counter)51 self.assertEqual(len(counter), 0)52 self.assertEqual(increment_counter(), 1)53 self.assertEqual(len(counter), 1)54 self.assertEqual(increment_counter(), 1)55 self.assertEqual(increment_counter(), 1)56 self.assertEqual(increment_counter(), 1)57 self.assertEqual(len(counter), 1)58 increment_counter.clear()59 self.assertEqual(increment_counter(), 2)60 self.assertEqual(len(counter), 2)61 self.assertEqual(increment_counter(), 2)62 self.assertEqual(increment_counter(), 2)63 self.assertEqual(increment_counter(), 2)...
counter.py
Source:counter.py
1count = 02def increment_counter():3 """Should skip 13."""4 global count5 if count != 12:6 count += 17 else:8 count += 29 return count10class Counter():11 """A counter"""12 def __init__(self):13 self.count = 014 def increment_counter(self):15 if self.count == 12:16 self.count += 217 else:18 self.count +=119 def get_count(self):20 return self.count 21def increment_counter_pure(count):22 """Should Skip 13."""23 return count + 1 if count != 12 else count + 224if __name__ == "__main__":25 # An Object26 # have to get it into a state before testing the key behavior27 counter = Counter()28 # increment 13 times29 for _ in range(13):30 counter.increment_counter()31 assert counter.get_count() == 1432 # Not a pure function33 # could have written this as a loop34 # but still have to get it into a state before testing the key behavior35 assert increment_counter() == 136 assert increment_counter() == 237 assert increment_counter() == 338 assert increment_counter() == 439 assert increment_counter() == 540 assert increment_counter() == 641 assert increment_counter() == 742 assert increment_counter() == 843 assert increment_counter() == 944 assert increment_counter() == 1045 assert increment_counter() == 1146 assert increment_counter() == 1247 assert increment_counter() == 14...
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!!