Best Python code snippet using localstack_python
test_objects.py
Source:test_objects.py
...30 def test_call_and_clear(self):31 mock = MagicMock()32 mock.return_value = "foobar"33 @singleton_factory34 def my_singleton():35 return mock()36 assert my_singleton() == mock.return_value37 mock.assert_called_once()38 assert my_singleton() == mock.return_value39 mock.assert_called_once()40 my_singleton.clear()41 assert my_singleton() == mock.return_value42 mock.assert_has_calls([(), ()])43 assert my_singleton() == mock.return_value44 mock.assert_has_calls([(), ()])45 def test_exception_does_not_set_a_value(self):46 mock = MagicMock()47 @singleton_factory48 def my_singleton():49 mock()50 raise ValueError("oh noes")51 with pytest.raises(ValueError):52 my_singleton()53 mock.assert_has_calls([()])54 with pytest.raises(ValueError):55 my_singleton()56 mock.assert_has_calls([(), ()])57 def test_set_none_value_does_not_set_singleton(self):58 mock = MagicMock()59 mock.return_value = None60 @singleton_factory61 def my_singleton():62 return mock()63 assert my_singleton() is None64 mock.assert_has_calls([()])65 assert my_singleton() is None66 mock.assert_has_calls([(), ()])67 def test_set_falsy_value_sets_singleton(self):68 mock = MagicMock()69 mock.return_value = False70 @singleton_factory71 def my_singleton():72 return mock()73 assert my_singleton() is False74 mock.assert_called_once()75 assert my_singleton() is False...
8_singleton.py
Source:8_singleton.py
1"""Implements a singleton design pattern in Python."""2import functools3# from https://realpython.com/primer-on-python-decorators/#creating-singletons4def singleton(cls):5 """make a class a singleton class (only one instance)"""6 @functools.wraps(cls)7 def wrapper_singleton(*args, **kwargs): # this is the decoration which checks for previous existence8 if not wrapper_singleton.instance:9 wrapper_singleton.instance = cls(*args, **kwargs)10 print("in decoration: no instance exists, so let's create one!")11 else:12 print("in decoration: instance already exists!")13 return wrapper_singleton.instance14 wrapper_singleton.instance = None15 return wrapper_singleton # return instance of class, rather than function with other decorators16@singleton17class MySingleton:18 def __init__(self, parameter):19 self.parameter = parameter20 def do_some_stuff(self):21 print("parameter =", self.parameter)22print("\ncreate a MySingleton...")23my_singleton = MySingleton('my_parameter_string') # MySingleton is really wrapper_singleton(MySingleton)24print("singleton at", id(my_singleton))25print("type(my_singleton) =", type(my_singleton)) # ..which returns a MySingleton instance26my_singleton.do_some_stuff()27print("\ncreate a another MySingleton...")28my_singleton_copy = MySingleton('my_other_parameter_string') # this is wrapper_singleton(MySingleton)29print("singleton at", id(my_singleton_copy))...
singlegon.py
Source:singlegon.py
1#!/usr/bin/env python2# -*- coding:utf-8 -*-3class Singleton1(object):4 """å®ç°æ¹å¼1ï¼ä½¿ç¨__new__"""5 def __new__(cls, *args, **kwargs):6 if not hasattr(cls, '_instance'):7 orig = super(Singleton1, cls)8 cls._instance = orig.__new__(cls, *args, **kwargs)9 return cls._instance10class Singleton2(object):11 """å建å®ä¾æææå®ä¾ç__dict__é½æååä¸ä¸ªåå
¸ï¼ä½¿å
·æç¸åçå±æ§åæ¹æ³12 伪åä¾13 """14 _state = {}15 def __new__(cls, *args, **kwargs):16 ob = super(Singleton2, cls).__new__(cls, *args, **kwargs)17 ob.__dict__ = cls._state18 return ob19def Singleton3(cls, *args, **kwargs):20 """è£
饰å¨çæ¬çåä¾"""21 instances = {}22 def getinstance():23 if cls not in instances:24 instances[cls] = cls(*args, **kwargs)25 return instances[cls]26 return getinstance27@Singleton328class Singleton(object):29 pass30'''31# æ¹æ³4ï¼ä½ä¸ºpythonç模åæ¯å¤©ç¶çåä¾æ¨¡å¼32# mysingleton.py33class My_Singleton(object):34 def foo(self):35 pass36my_singleton = My_Singleton()37# to use38from mysingleton import my_singleton39my_singleton.foo()40'''41def test_singleton():42 """use py.test for unit test"""43 s1 = Singleton()44 s2 = Singleton()45 assert id(s1) == id(s2)...
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!!