Best Python code snippet using green
test_geometry.py
Source:test_geometry.py
1import numpy as np2from impose.geometry import shapes3def test_getstate_circle():4 shp = shapes.Circle(x=11, y=10, r=8, point_um=.25)5 assert shp.__getstate__()["x"] == 116 assert shp.__getstate__()["y"] == 107 assert shp.__getstate__()["r"] == 88 assert shp.__getstate__()["point_um"] == 0.259def test_getstate_ellipse():10 shp = shapes.Ellipse(x=11, y=10, a=8, b=9, point_um=.25)11 assert shp.__getstate__()["x"] == 1112 assert shp.__getstate__()["y"] == 1013 assert shp.__getstate__()["a"] == 814 assert shp.__getstate__()["b"] == 915 assert shp.__getstate__()["point_um"] == 0.2516def test_getstate_polygon():17 points = [[20, 20], [40, 50], [80, 10]]18 shp = shapes.Polygon(points=points, point_um=1)19 assert np.allclose(shp.__getstate__()["points"], points)20 assert shp.__getstate__()["point_um"] == 121def test_getstate_rectangle():22 shp = shapes.Rectangle(x=11, y=10, a=8, b=9, point_um=.25)23 assert shp.__getstate__()["x"] == 1124 assert shp.__getstate__()["y"] == 1025 assert shp.__getstate__()["a"] == 826 assert shp.__getstate__()["b"] == 927 assert shp.__getstate__()["point_um"] == 0.2528def test_rotate_circle():29 shp = shapes.Circle(x=11, y=10, r=8, point_um=.25)30 mask1 = shp.to_mask(shape=(20, 20))31 shp.rotate(dphi=np.pi/2)32 assert shp.phi == np.pi/233 mask2 = shp.to_mask(shape=(20, 20))34 # rotation happens around the center (x, y)35 assert np.all(mask1 == mask2)36def test_rotate_ellipse():37 shp = shapes.Ellipse(x=10, y=10, a=8, b=9, point_um=.25)38 mask1 = shp.to_mask(shape=(20, 20))39 shp.rotate(dphi=np.pi/2)40 assert shp.phi == np.pi/241 mask2 = shp.to_mask(shape=(20, 20))42 assert np.all(mask1 == mask2.transpose())43def test_rotate_ellipse_origin():44 shp = shapes.Ellipse(x=10, y=0, a=8, b=9, point_um=.25)45 shp.rotate(dphi=np.pi/2, origin=(5, 0))46 assert shp.x == 547 assert shp.y == 548 assert shp.phi == np.pi/249def test_rotate_ellipse_origin2():50 shp = shapes.Ellipse(x=6, y=5, a=8, b=9, phi=-np.pi/4, point_um=.25)51 shp.rotate(dphi=np.pi/2, origin=(1, 0))52 assert shp.x == -453 assert shp.y == 554 assert shp.phi == np.pi/455def test_rotate_polygon():56 points = [[1, 1], [10, 1], [10, 10], [1, 10]]57 shp = shapes.Polygon(points=points, point_um=1)58 mask1 = shp.to_mask(shape=(20, 20))59 shp.rotate(dphi=np.pi / 2) # rotation around mean of points60 mask2 = shp.to_mask(shape=(20, 20))61 # this works because the center of the polygon is symmetric in x and y62 assert np.all(mask1 == mask2)63def test_rotate_polygon_2():64 points = [[4, 6], [12, 6], [12, 10], [4, 10]]65 shp = shapes.Polygon(points=points, point_um=1)66 mask1 = shp.to_mask(shape=(22, 22))67 shp.rotate(dphi=np.pi / 2) # rotation around mean of points68 mask2 = shp.to_mask(shape=(22, 22))69 # this works because the center of the polygon is symmetric in x and y70 assert np.all(mask1 == mask2.transpose())71def test_rotate_polygon_3():72 points = [[4, 8], [12, 8], [12, 10], [4, 10]]73 shp = shapes.Polygon(points=points, point_um=1)74 mask1 = shp.to_mask(shape=(22, 22))75 shp.rotate(dphi=np.pi / 2) # rotation around mean of points76 mask2 = shp.to_mask(shape=(22, 22))77 assert shp.x == np.array(points)[:, 0].mean()78 assert shp.y == np.array(points)[:, 1].mean()79 assert not np.all(mask1 == mask2.transpose())80def test_rotate_polygon_origin():81 points = [[2, 2], [4, 2], [3, 3]]82 shp = shapes.Polygon(points=points, point_um=1)83 shp.rotate(dphi=np.pi, origin=(1, 1))84 assert np.allclose(shp.points[0][0], 0)85 assert np.allclose(shp.points[0][1], 0)86 assert np.allclose(shp.points[1][0], -2)87 assert np.allclose(shp.points[1][1], 0)88 assert np.allclose(shp.points[2][0], -1)89 assert np.allclose(shp.points[2][1], -1)90def test_rotate_rectangle():91 shp = shapes.Rectangle(x=10, y=10, a=8, b=9, point_um=.25)92 mask1 = shp.to_mask(shape=(20, 20))93 shp.rotate(dphi=np.pi/2)94 assert shp.phi == np.pi/295 mask2 = shp.to_mask(shape=(20, 20))96 assert np.all(mask1 == mask2.transpose())97def test_rotate_rectangle_origin():98 shp = shapes.Ellipse(x=10, y=0, a=8, b=9, point_um=.25)99 shp.rotate(dphi=np.pi/2, origin=(5, 0))100 assert shp.x == 5101 assert shp.y == 5102 assert shp.phi == np.pi/2103def test_setstate_circle():104 shp = shapes.Circle(x=11, y=10, r=8, point_um=.25)105 state = shp.__getstate__()106 state["x"] = 16107 state["point_um"] = 32108 shp.__setstate__(state)109 assert shp.x == 16110 assert shp.point_um == 32111def test_setstate_ellipse():112 shp = shapes.Ellipse(x=11, y=10, a=8, b=9, point_um=.25)113 state = shp.__getstate__()114 state["b"] = 12115 state["x"] = 16116 state["point_um"] = 32117 shp.__setstate__(state)118 assert shp.b == 12119 assert shp.x == 16120 assert shp.point_um == 32121def test_setstate_polygon():122 points = [[4, 8], [12, 8], [12, 10], [4, 10]]123 shp = shapes.Polygon(points=points, point_um=1)124 state = shp.__getstate__()125 state["points"] = np.array(points) * 2126 state["point_um"] = 32127 shp.__setstate__(state)128 assert shp.point_um == 32129 assert np.all(shp.points == np.array(points) * 2)130def test_setstate_rectangle():131 shp = shapes.Rectangle(x=11, y=10, a=8, b=9, point_um=.25)132 state = shp.__getstate__()133 state["b"] = 12134 state["x"] = 16135 state["point_um"] = 32136 shp.__setstate__(state)137 assert shp.b == 12138 assert shp.x == 16139 assert shp.point_um == 32140def test_set_scale_circle():141 shp = shapes.Circle(x=0, y=1, r=8, point_um=1)142 shp.set_scale(point_um=2)143 assert shp.x == 0144 assert shp.y == .5145 assert shp.r == 4146 assert shp.a == 4...
OtherXslElement.py
Source:OtherXslElement.py
...21 return22 def __getinitargs__(self):23 return (None, self.namespaceURI, self.localName, self.prefix,24 self.baseUri)25 def __getstate__(self):26 base_state = XsltElement.__getstate__(self)27 return base_state28 def __setstate__(self, state):29 XsltElement.__setstate__(self, state)30 return31class IncludeElement(XsltElement):32 legalAttrs = ('href',)33 def __init__(self, doc, uri=xml.xslt.XSL_NAMESPACE, localName='include', prefix='xsl', baseUri=''):34 XsltElement.__init__(self, doc, uri, localName, prefix, baseUri)35 def setup(self):36 self.__dict__['_href'] = self.getAttributeNS(EMPTY_NAMESPACE, 'href')37 return38 def __getinitargs__(self):39 return (None, self.namespaceURI, self.localName, self.prefix,40 self.baseUri)41 def __getstate__(self):42 base_state = XsltElement.__getstate__(self)43 new_state = (base_state, self._href, )44 return new_state45 def __setstate__(self, state):46 XsltElement.__setstate__(self, state[0])47 self._href = state[1]48 return49class FallbackElement(XsltElement):50 legalAttrs = ()51 def __init__(self, doc, uri=xml.xslt.XSL_NAMESPACE, localName='fallback', prefix='xsl', baseUri=''):52 XsltElement.__init__(self, doc, uri, localName, prefix, baseUri)53 self.__dict__['_nss'] = xml.dom.ext.GetAllNs(self)54 return55 def setup(self):56 return57 def instantiate(self, context, processor):58 origState = context.copy()59 context.setNamespaces(self._nss)60 for child in self.childNodes:61 context = child.instantiate(context, processor)[0]62 context.set(origState)63 return (context,)64 def __getinitargs__(self):65 return (None, self.namespaceURI, self.localName, self.prefix,66 self.baseUri)67 def __getstate__(self):68 base_state = XsltElement.__getstate__(self)69 return base_state70 def __setstate__(self, state):71 XsltElement.__setstate__(self, state)72 return73class ImportElement(XsltElement):74 legalAttrs = ('href',)75 def __init__(self, doc, uri=xml.xslt.XSL_NAMESPACE, localName='import', prefix='xsl', baseUri=''):76 XsltElement.__init__(self, doc, uri, localName, prefix, baseUri)77 self.stylesheet = None78 def setup(self):79 self.href = self.getAttributeNS(EMPTY_NAMESPACE, 'href')80 def __getinitargs__(self):81 return (None, self.namespaceURI, self.localName, self.prefix,82 self.baseUri)83 def __getstate__(self):84 base_state = XsltElement.__getstate__(self)85 return base_state86 def __setstate__(self, state):87 XsltElement.__setstate__(self, state)88 return89class KeyElement(XsltElement):90 legalAttrs = ('name', 'match', 'use')91 def __init__(self, doc, uri=xml.xslt.XSL_NAMESPACE, localName='key', prefix='xsl', baseUri=''):92 XsltElement.__init__(self, doc, uri, localName, prefix, baseUri)93 def setup(self):94 pass95 def __getinitargs__(self):96 return (None, self.namespaceURI, self.localName, self.prefix,97 self.baseUri)98 def __getstate__(self):99 base_state = XsltElement.__getstate__(self)100 return base_state101 def __setstate__(self, state):102 XsltElement.__setstate__(self, state)103 return104class NamespaceAliasElement(XsltElement):105 legalAttrs = ('stylesheet-prefix', 'result-prefix')106 def __init__(self, doc, uri=xml.xslt.XSL_NAMESPACE, localName='namespace-alias', prefix='xsl', baseUri=''):107 XsltElement.__init__(self, doc, uri, localName, prefix, baseUri)108 def setup(self):109 pass110 def __getinitargs__(self):111 return (None, self.namespaceURI, self.localName, self.prefix,112 self.baseUri)113 def __getstate__(self):114 base_state = XsltElement.__getstate__(self)115 return base_state116 def __setstate__(self, state):117 XsltElement.__setstate__(self, state)118 return119class OutputElement(XsltElement):120 legalAttrs = ('method', 'version', 'encoding', 'omit-xml-declaration', 'standalone', 'doctype-public', 'doctype-system', 'cdata-section-elements', 'indent', 'media-type')121 def __init__(self, doc, uri=xml.xslt.XSL_NAMESPACE, localName='output', prefix='xsl', baseUri=''):122 XsltElement.__init__(self, doc, uri, localName, prefix, baseUri)123 def setup(self):124 pass125 def __getinitargs__(self):126 return (None, self.namespaceURI, self.localName, self.prefix,127 self.baseUri)128 def __getstate__(self):129 base_state = XsltElement.__getstate__(self)130 return base_state131 def __setstate__(self, state):132 XsltElement.__setstate__(self, state)133 return134class PreserveSpaceElement(XsltElement):135 legalAttrs = ('elements',)136 def __init__(self, doc, uri=xml.xslt.XSL_NAMESPACE, localName='preserve-space', prefix='xsl', baseUri=''):137 XsltElement.__init__(self, doc, uri, localName, prefix, baseUri)138 def setup(self):139 pass140 def __getinitargs__(self):141 return (None, self.namespaceURI, self.localName, self.prefix,142 self.baseUri)143 def __getstate__(self):144 base_state = XsltElement.__getstate__(self)145 return base_state146 def __setstate__(self, state):147 XsltElement.__setstate__(self, state)148 return149 150class StripSpaceElement(XsltElement):151 legalAttrs = ('elements',)152 def __init__(self, doc, uri=xml.xslt.XSL_NAMESPACE, localName='strip-space', prefix='xsl', baseUri=''):153 XsltElement.__init__(self, doc, uri, localName, prefix, baseUri)154 def setup(self):155 pass156 def __getinitargs__(self):157 return (None, self.namespaceURI, self.localName, self.prefix,158 self.baseUri)159 def __getstate__(self):160 base_state = XsltElement.__getstate__(self)161 return base_state162 def __setstate__(self, state):163 XsltElement.__setstate__(self, state)164 return165 166import urlparse167from xml.xslt import XsltException, Error...
serde_patch.py
Source:serde_patch.py
...24def apply_getsetstate(cls, ref):25 cls.__getstate__ = ref.__getstate__26 cls.__setstate__ = ref.__setstate__27class _DictState:28 def __getstate__(self):29 state = self.__dict__30 return state31 def __setstate__(self, state):32 for k, v in state.items():33 setattr(self, k, v)34try:35 # pygments isn't necessarily installed -- if it is we need to be able to pickle these modules36 from pygments.lexers import _automodule37 apply_getsetstate(_automodule, _DictState)38except ImportError:39 pass40apply_getsetstate(Config, _DictState)41def reinit(self, pluginmanager):42 self.pluginmanager = pluginmanager43 self.trace = self.pluginmanager.trace.root.get("config")44 self.hook = self.pluginmanager.hook45 self._warn = self.pluginmanager._warn46Config.__reinit__ = reinit47def getpystate(self):48 # don't take anything -- we should be able to reimport it.49 state = {}50 return state51def setpystate(self, state):52 import py53 for k, v in py.__dict__.items():54 setattr(self, k, v)55ApiModule.__getstate__ = getpystate56ApiModule.__setstate__ = setpystate57class _:58 def __getstate__(self):59 state = dict(name=self.name, mod=self.mod)60 return state61 __setstate__ = _DictState.__setstate__62 MovedModule.__reduce_ex__ = object.__reduce_ex__63apply_getsetstate(MovedModule, _)64class _CaptureIOGetstate:65 def __getstate__(self):66 assert isinstance(self, CaptureIO)67 return {'value': self.getvalue()}68 def __setstate__(self, state):69 assert isinstance(self, CaptureIO)70 self.write(state['value'])71apply_getsetstate(CaptureIO, _CaptureIOGetstate)72class _SysCaptureGetstate:73 def __getstate__(self):74 reverse_name = {v: k for k, v in patchsysdict.items()}75 return dict(fd=reverse_name[self.name])76 def __setstate__(self, state):77 self.__init__(state['fd'])78apply_getsetstate(SysCapture, _SysCaptureGetstate)79class _:80 def __getnewargs__(self):81 return (self.request, self.request.config._tmpdirhandler)82 def __getstate__(self):83 def promote_weak_dict(wd):84 assert isinstance(wd, WeakKeyDictionary)85 return dict(wd.items())86 def handle_value(v):87 if isinstance(v, WeakKeyDictionary):88 return promote_weak_dict(v)89 else:90 return v91 return {}92 Testdir.__getnewargs__ = __getnewargs__93 Testdir.__getstate__ = __getstate__94class _EncodedFileSerde:95 def __getstate__(self):96 assert isinstance(self, EncodedFile)97 current_position = self.buffer.seek(0, 1)98 self.buffer.seek(0)99 value = self.buffer.read()100 self.buffer.seek(current_position, 0)101 return {'value': value, 'encoding': self.encoding}102 def __setstate__(self, state):103 assert isinstance(self, EncodedFile)104 setattr(self, 'encoding', state['encoding'])105 buffer = BytesIO(state['value'])106 setattr(self, 'buffer', buffer)107apply_getsetstate(EncodedFile, _EncodedFileSerde)108class _ConfigState:109 def __getstate__(self):110 return {'config': self.config}111 def __setstate__(self, state):112 self.config = state['config']113 def __reinit__(self, pluginmanager):114 # sleep a bit and then init?115 self.__init__(self.config)116apply_getsetstate(TerminalReporter, _ConfigState)117TerminalReporter.__reinit__ = _ConfigState.__reinit__118apply_getsetstate(AssertionRewritingHook, _ConfigState)119AssertionRewritingHook.__reinit__ = _ConfigState.__reinit__120class _:121 def __getstate__(self):122 return {'_method': self._method}123 def __setstate__(self, state):124 self._method = state['_method']125 self.init_capturings()126 CaptureManager.__getstate__ = __getstate__127 CaptureManager.__setstate__ = __setstate__128class _:129 def __getnewargs__(self):130 assert isinstance(self, PluginManager)131 return self.project_name, self._implprefix132 def __getstate__(self):133 state = {'_1_plugins': self.get_plugins(), '_1_project_name': self.project_name,134 '_1_implprefix': self._implprefix}135 return state136 def __setstate__(self, state):137 for k, v in state.items():138 setattr(self, k, v)139 def __recreate__(self):140 new_pm = PytestPluginManager()141 for plugin in self._1_plugins:142 new_pm.register(plugin)143 if hasattr(plugin, '__reinit__'):144 plugin.__reinit__(new_pm)145 return new_pm146 PytestPluginManager.__getnewargs__ = __getnewargs__...
test_bitarray.py
Source:test_bitarray.py
...24 sa.set_range(0, 1)25 sa.clear_range(60, 2)26 ba = BitArray(sa)27 exp = 0b111110011111111111111111111111111111111111111111111111111111111100128 assert ba.__getstate__() == exp29 sa.invert_in_place()30 ba = BitArray(sa)31 assert ba.__getstate__() == ~exp32def test_bitarray_allon():33 assert BitArray.all_on().is_all_on()34 assert BitArray.all_on().get_highest_on_bit() == -135 assert BitArray.all_on().get_highest_off_bit() == -136def test_bitarray_nonzero():37 assert not BitArray()38 assert not BitArray(0)39 assert BitArray(1)40 assert BitArray.all_on()41def test_bitarray_invert():42 assert ~BitArray(0) != BitArray(0)43 assert (~BitArray(0)).is_all_on()44 assert ~~BitArray(0) == BitArray(0)45 assert ~~BitArray(123) == BitArray(123)46def test_bitarray_set_word():47 # Non-inverted48 expected = 9876 | (123456 << (BitArray.num_bits_per_word * 3))49 ba = BitArray(0)50 ba.set_word(0, 9876)51 ba.set_word(3, 123456)52 assert ba.__getstate__() == expected53 assert not ba.is_all_on()54 # Inverted55 ba = BitArray(0)56 ba.invert_in_place()57 ba.set_word(2, 1234)58 full_word = (1 << BitArray.num_bits_per_word) - 159 expected = ~((full_word & ~1234) << (BitArray.num_bits_per_word * 2))60 assert ba.__getstate__() == expected61 assert not ba.is_all_on()62def test_bitarray_clear():63 ba = BitArray(1234)64 ba.clear()65 assert ba.is_zero()66 assert not ba.is_all_on()67 assert ba.get_highest_on_bit() == -168 assert ba.get_highest_off_bit() == -169 ba = BitArray.all_on()70 ba.clear()71 assert ba.is_zero()72 assert not ba.is_all_on()73 assert ba.get_highest_on_bit() == -174 assert ba.get_highest_off_bit() == -175def test_bitarray_getstate():76 assert BitArray().__getstate__() == 077 assert BitArray(0).__getstate__() == 078 assert BitArray(100).__getstate__() == 10079 assert BitArray(9870000000000000000).__getstate__() == 987000000000000000080 assert BitArray.all_on().__getstate__() == -181 assert (~BitArray(100).__getstate__()) == ~10082 assert (~BitArray(812000000000000000).__getstate__()) == ~81200000000000000083def test_bitarray_pickle():84 ba = BitArray()85 assert ba == pickle.loads(pickle.dumps(ba, -1))86 ba = BitArray(0)87 assert ba == pickle.loads(pickle.dumps(ba, -1))88 ba = BitArray(123)89 assert ba == pickle.loads(pickle.dumps(ba, -1))90 ba = BitArray(94187049178237918273981729127381723)91 assert ba == pickle.loads(pickle.dumps(ba, -1))92 ba = ~BitArray(94187049178237918273981729127381723)...
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!!