Best Python code snippet using localstack_python
test_subclassinit.py
Source:test_subclassinit.py
...75 self.assertEqual(Left.calls, [])76 self.assertEqual(Right.calls, [])77 def test_set_name(self):78 class Descriptor:79 def __set_name__(self, owner, name):80 self.owner = owner81 self.name = name82 class A:83 d = Descriptor()84 self.assertEqual(A.d.name, "d")85 self.assertIs(A.d.owner, A)86 def test_set_name_metaclass(self):87 class Meta(type):88 def __new__(cls, name, bases, ns):89 ret = super().__new__(cls, name, bases, ns)90 self.assertEqual(ret.d.name, "d")91 self.assertIs(ret.d.owner, ret)92 return 093 class Descriptor:94 def __set_name__(self, owner, name):95 self.owner = owner96 self.name = name97 class A(metaclass=Meta):98 d = Descriptor()99 self.assertEqual(A, 0)100 def test_set_name_error(self):101 class Descriptor:102 def __set_name__(self, owner, name):103 1/0104 with self.assertRaises(RuntimeError) as cm:105 class NotGoingToWork:106 attr = Descriptor()107 exc = cm.exception108 self.assertRegex(str(exc), r'\bNotGoingToWork\b')109 self.assertRegex(str(exc), r'\battr\b')110 self.assertRegex(str(exc), r'\bDescriptor\b')111 self.assertIsInstance(exc.__cause__, ZeroDivisionError)112 def test_set_name_wrong(self):113 class Descriptor:114 def __set_name__(self):115 pass116 with self.assertRaises(RuntimeError) as cm:117 class NotGoingToWork:118 attr = Descriptor()119 exc = cm.exception120 self.assertRegex(str(exc), r'\bNotGoingToWork\b')121 self.assertRegex(str(exc), r'\battr\b')122 self.assertRegex(str(exc), r'\bDescriptor\b')123 self.assertIsInstance(exc.__cause__, TypeError)124 def test_set_name_lookup(self):125 resolved = []126 class NonDescriptor:127 def __getattr__(self, name):128 resolved.append(name)129 class A:130 d = NonDescriptor()131 self.assertNotIn('__set_name__', resolved,132 '__set_name__ is looked up in instance dict')133 def test_set_name_init_subclass(self):134 class Descriptor:135 def __set_name__(self, owner, name):136 self.owner = owner137 self.name = name138 class Meta(type):139 def __new__(cls, name, bases, ns):140 self = super().__new__(cls, name, bases, ns)141 self.meta_owner = self.owner142 self.meta_name = self.name143 return self144 class A:145 def __init_subclass__(cls):146 cls.owner = cls.d.owner147 cls.name = cls.d.name148 class B(A, metaclass=Meta):149 d = Descriptor()150 self.assertIs(B.owner, B)151 self.assertEqual(B.name, 'd')152 self.assertIs(B.meta_owner, B)153 self.assertEqual(B.name, 'd')154 def test_set_name_modifying_dict(self):155 notified = []156 class Descriptor:157 def __set_name__(self, owner, name):158 setattr(owner, name + 'x', None)159 notified.append(name)160 class A:161 a = Descriptor()162 b = Descriptor()163 c = Descriptor()164 d = Descriptor()165 e = Descriptor()166 self.assertCountEqual(notified, ['a', 'b', 'c', 'd', 'e'])167 def test_errors(self):168 class MyMeta(type):169 pass170 with self.assertRaises(TypeError):171 class MyClass(metaclass=MyMeta, otherarg=1):...
bulkfood_v4.py
Source:bulkfood_v4.py
...39 # - self - descriptor instance,40 # - owner - the managed class,41 # - name - is the name of the attribute of owner, to which this42 # descriptor instance was assigned in the class body of owner.43 def __set_name__(self, owner, name):44 # This is what the __init__ did in implementation without __set_name__45 self.storage_name = name46 # Same implementation as in the non-__set_name__ case.47 def __set__(self, instance, value):48 if value > 0:49 instance.__dict__[self.storage_name] = value50 else:51 msg = f'{self.storage_name} must be > 0'52 raise ValueError(msg)53 54 # __get__ isn't needed - the name of the storage attribute matches55 # the name of the managed attribute. There's no risk of a mistake,56 # missuse or malicious intent.57 # def __get__(self, instance, owner):...
main.py
Source:main.py
1from material import Material2from simulator import Simulator3def main():4 iron = Material(0, 1)5 iron.__set_name__("iron")6 iron_bar = Material(0, 15)7 iron_bar.__set_name__("iron-bar_1")8 iron_bar.add_material_to_recipe(iron, 5)9 iron_bar2 = Material(0, 8)10 iron_bar2.__set_name__("iron-bar_2")11 iron_bar2.add_material_to_recipe(iron, 5)12 iron_bolt = Material(0, 10)13 iron_bolt.__set_name__("iron-bolt")14 iron_bolt.add_material_to_recipe(iron_bar, 2)15 copper = Material(0, 2)16 copper.__set_name__("copper")17 copper_bar = Material(0, 8)18 copper_bar.__set_name__("copper-bar")19 copper_bar.add_material_to_recipe(copper, 5)20 plate = Material(0, 30)21 plate.__set_name__("plate")22 plate.add_material_to_recipe(iron_bolt, 1)23 plate.add_material_to_recipe(copper_bar, 3)24 simulator = Simulator(120, [iron, iron_bar, iron_bar2, iron_bolt, copper, copper_bar, plate])25 simulator.simulate()26if __name__ == "__main__":...
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!!