Best Python code snippet using autotest_python
test_userdict.py
Source:test_userdict.py
...120 # (E) subclass defines __missing__ method raising RuntimeError121 # (F) subclass sets __missing__ instance variable (no effect)122 # (G) subclass doesn't define __missing__ at a all123 class D(collections.UserDict):124 def __missing__(self, key):125 return 42126 d = D({1: 2, 3: 4})127 self.assertEqual(d[1], 2)128 self.assertEqual(d[3], 4)129 self.assertNotIn(2, d)130 self.assertNotIn(2, d.keys())131 self.assertEqual(d[2], 42)132 class E(collections.UserDict):133 def __missing__(self, key):134 raise RuntimeError(key)135 e = E()136 try:137 e[42]138 except RuntimeError as err:139 self.assertEqual(err.args, (42,))140 else:141 self.fail("e[42] didn't raise RuntimeError")142 class F(collections.UserDict):143 def __init__(self):144 # An instance variable __missing__ should have no effect145 self.__missing__ = lambda key: None146 collections.UserDict.__init__(self)147 f = F()...
demo_3_7.py
Source:demo_3_7.py
...15å 为é£ä¹ä¼å¯¼è´ __contains__ 被éå½è°ç¨ã16为äºé¿å
è¿ä¸æ
åµï¼è¿ééåäºæ´æ¾å¼çæ¹æ³ï¼ç´æ¥å¨è¿ä¸ª self.keys() éæ¥è¯¢17'''18class StrKeyDict0(dict):19 def __missing__(self, key):20 # å¦ææ¾ä¸å°çé®æ¬èº«å°±æ¯å符串ï¼é£å°±æåº KeyError å¼å¸¸21 if isinstance(key, str):22 raise KeyError(key)23 # å¦ææ¾ä¸å°çé®ä¸æ¯å符串ï¼é£ä¹æå®è½¬æ¢æå符串åè¿è¡æ¥æ¾24 return self[str(key)]25 def get(self, key, default=None):26 try:27 # get æ¹æ³ææ¥æ¾å·¥ä½ç¨ self[key] çå½¢å¼å§æç» __getitem__ï¼ 28 # è¿æ ·å¨å®£å¸æ¥æ¾å¤±è´¥ä¹åï¼è¿è½éè¿ __missing__ åç»æ个é®ä¸ä¸ªæºä¼29 return self[key]30 except KeyError:31 # å¦ææåº KeyErrorï¼é£ä¹è¯´æ __missing__ ä¹å¤±è´¥äºï¼äºæ¯è¿å default32 return default33 ...
3_4_2.py
Source:3_4_2.py
...8 æä¾ __missing__ æ¹æ³å¯¹ get æè
__contains__(in è¿ç®ç¬¦ä¼ç¨å°è¿ä¸ªæ¹æ³)è¿9 äºæ¹æ³ç使ç¨æ²¡æå½±åã10'''11class StrKeyDict0(dict):12 def __missing__(self, key):13 '''14 å¦ææ¾ä¸å°çé®ä¸æ¯å符串ï¼å转æ¢æä»çå符串形å¼åæ¥æ¾ï¼15 å¦ææ¯å符串åæåºKeyError.16 为ä»ä¹è¦å¤ækeyæ¯ä¸æ¯å符串ï¼17 å¦æ没æè¿ä¸ªæµè¯,åªè¦ str(k) è¿åçæ¯ä¸ä¸ªåå¨çé®,é£ä¹ __missing__ æ¹æ³æ¯æ²¡18 é®é¢ç,ä¸ç®¡æ¯å符串é®è¿æ¯éå符串é®,å®é½è½æ£å¸¸è¿è¡ãä½æ¯å¦æ str(k) ä¸æ¯ä¸ä¸ª19 åå¨çé®,代ç å°±ä¼é·å
¥æ ééå½ãè¿æ¯å 为 __missing__ çæåä¸è¡ä¸ç20 self[str(key)] ä¼è°ç¨ __getitem__,èè¿ä¸ª str(key) åä¸åå¨,äºæ¯21 __missing__ åä¼è¢«è°ç¨ã22 '''23 if isinstance(key,str):24 raise KeyError(key)25 return self[str(key)]26 def get(self,key,default=None):...
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!!