How to use __missing__ method in autotest

Best Python code snippet using autotest_python

test_userdict.py

Source:test_userdict.py Github

copy

Full Screen

...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()...

Full Screen

Full Screen

demo_3_7.py

Source:demo_3_7.py Github

copy

Full Screen

...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 ...

Full Screen

Full Screen

3_4_2.py

Source:3_4_2.py Github

copy

Full Screen

...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):...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run autotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful