Best Python code snippet using pandera_python
test_ctypes.py
Source:test_ctypes.py
1import unittest2from test.test_support import run_unittest, import_module, due_to_ironpython_bug3#Skip tests if _ctypes module does not exist4import_module('_ctypes')5if due_to_ironpython_bug("http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=22393"):6 import System, nt7 try:8 System.IO.File.Copy(nt.getcwd() + r"\..\Python27.dll", nt.getcwd() + r"\Python27.dll", True)9 except:10 pass11IRONPYTHON_DISABLED_LIST = [12 #ERROR13 "ctypes.test.test_as_parameter.AsParamPropertyWrapperTestCase.test_callbacks",14 "ctypes.test.test_byteswap.Test.test_endian_double",15 "ctypes.test.test_byteswap.Test.test_endian_float",16 "ctypes.test.test_byteswap.Test.test_endian_int",17 "ctypes.test.test_byteswap.Test.test_endian_longlong",18 "ctypes.test.test_byteswap.Test.test_endian_short",19 "ctypes.test.test_byteswap.Test.test_struct_fields_1",20 "ctypes.test.test_byteswap.Test.test_struct_fields_2",21 "ctypes.test.test_byteswap.Test.test_unaligned_nonnative_struct_fields",22 "ctypes.test.test_errno.Test.test_GetLastError",23 "ctypes.test.test_errno.Test.test_open",24 "ctypes.test.test_numbers.NumberTestCase.test_init",25 "ctypes.test.test_parameters.SimpleTypesTestCase.test_noctypes_argtype",26 "ctypes.test.test_pep3118.Test.test_endian_types",27 "ctypes.test.test_pickling.PickleTest.test_simple",28 "ctypes.test.test_pickling.PickleTest.test_struct",29 "ctypes.test.test_pickling.PickleTest.test_unpickable",30 "ctypes.test.test_pickling.PickleTest.test_wchar",31 "ctypes.test.test_pickling.PickleTest_1.test_simple",32 "ctypes.test.test_pickling.PickleTest_1.test_struct",33 "ctypes.test.test_pickling.PickleTest_1.test_unpickable",34 "ctypes.test.test_pickling.PickleTest_1.test_wchar",35 "ctypes.test.test_pickling.PickleTest_2.test_simple",36 "ctypes.test.test_pickling.PickleTest_2.test_wchar",37 "ctypes.test.test_pointers.PointersTestCase.test_c_void_p",38 "ctypes.test.test_prototypes.CharPointersTestCase.test_POINTER_c_char_arg",39 "ctypes.test.test_prototypes.CharPointersTestCase.test_c_char_p_arg",40 "ctypes.test.test_prototypes.CharPointersTestCase.test_instance",41 "ctypes.test.test_prototypes.CharPointersTestCase.test_int_pointer_arg",42 "ctypes.test.test_prototypes.CharPointersTestCase.test_paramflags",43 "ctypes.test.test_prototypes.WCharPointersTestCase.test_POINTER_c_wchar_arg",44 "ctypes.test.test_slicing.SlicesTestCase.test_char_ptr",45 "ctypes.test.test_slicing.SlicesTestCase.test_char_ptr_with_free",46 "ctypes.test.test_slicing.SlicesTestCase.test_wchar_ptr",47 #FAIL48 "ctypes.test.test_parameters.SimpleTypesTestCase.test_byref_pointer",49 "ctypes.test.test_parameters.SimpleTypesTestCase.test_byref_pointerpointer",50 "ctypes.test.test_parameters.SimpleTypesTestCase.test_cstrings",51 "ctypes.test.test_parameters.SimpleTypesTestCase.test_cw_strings",52 "ctypes.test.test_pickling.PickleTest_2.test_struct",53 "ctypes.test.test_pickling.PickleTest_2.test_unpickable",54 "ctypes.test.test_pointers.PointersTestCase.test_pointer_crash",55 "ctypes.test.test_prototypes.CharPointersTestCase.test_c_void_p_arg",56 "ctypes.test.test_prototypes.WCharPointersTestCase.test_c_wchar_p_arg",57 "ctypes.test.test_unicode.StringTestCase.test_ascii_ignore",58 "ctypes.test.test_unicode.StringTestCase.test_ascii_replace",59 "ctypes.test.test_unicode.StringTestCase.test_ascii_strict",60 "ctypes.test.test_unicode.StringTestCase.test_buffers",61 "ctypes.test.test_unicode.UnicodeTestCase.test_ascii_ignore",62 "ctypes.test.test_unicode.UnicodeTestCase.test_ascii_strict",63 "ctypes.test.test_unicode.UnicodeTestCase.test_buffers",64 ]65def test_main():66 import ctypes.test67 skipped, testcases = ctypes.test.get_tests(ctypes.test, "test_*.py", verbosity=0)68 suites = [unittest.makeSuite(t) for t in testcases]69 70 if due_to_ironpython_bug("http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=374"):71 for suite in suites:72 length = len(suite._tests)73 i = 074 while i < length:75 if suite._tests[i].id() in IRONPYTHON_DISABLED_LIST:76 suite._tests.pop(i)77 i -= 178 length -= 179 i += 180 81 try:82 run_unittest(unittest.TestSuite(suites))83 finally:84 if due_to_ironpython_bug("http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=22393"):85 try:86 System.IO.File.Delete(nt.getcwd() + r"\Python26.dll")87 except:88 pass89 print "%d of these test cases were disabled under IronPython." % len(IRONPYTHON_DISABLED_LIST)90if __name__ == "__main__":...
pickle.py
Source:pickle.py
1"""This is a copy-pasted chunk of cloudpickle. It modifies _is_importable to2redirect pickling of local-dir code from the standard pickling machinery (which3only records the name and file) into the cloudpickle machinery (which takes a full4copy of the code).5"""6from cloudpickle.cloudpickle_fast import (7 CloudPickler, subimport, dynamic_subimport,8 _BUILTIN_TYPE_NAMES, _builtin_type, _dynamic_class_reduce,9 types)10from cloudpickle.cloudpickle import _lookup_module_and_qualname11from collections import ChainMap12import sys13import os14from io import BytesIO15def is_library(obj, name):16 mod_qualname = _lookup_module_and_qualname(obj, name=name)17 if mod_qualname is None:18 return False19 else:20 mod, qualname = mod_qualname21 if hasattr(mod, '__file__'):22 return not mod.__file__.startswith(os.getcwd())23 return True24def _is_importable(obj, name=None):25 """This is the only function we've modified; everything else here is 26 just needed to nestle it into the CloudPickler's machinery"""27 if isinstance(obj, types.FunctionType):28 return is_library(obj, name)29 elif issubclass(type(obj), type):30 return is_library(obj, name)31 elif isinstance(obj, types.ModuleType):32 if hasattr(obj, '__file__'):33 if obj.__file__.startswith(os.getcwd()):34 return False35 return obj.__name__ in sys.modules36 else:37 raise TypeError(38 "cannot check importability of {} instances".format(39 type(obj).__name__)40 )41def _module_reduce(obj):42 if _is_importable(obj):43 return subimport, (obj.__name__,)44 else:45 obj.__dict__.pop('__builtins__', None)46 return dynamic_subimport, (obj.__name__, vars(obj))47def _class_reduce(obj):48 """Select the reducer depending on the dynamic nature of the class obj"""49 if obj is type(None): # noqa50 return type, (None,)51 elif obj is type(Ellipsis):52 return type, (Ellipsis,)53 elif obj is type(NotImplemented):54 return type, (NotImplemented,)55 elif obj in _BUILTIN_TYPE_NAMES:56 return _builtin_type, (_BUILTIN_TYPE_NAMES[obj],)57 elif not _is_importable(obj):58 return _dynamic_class_reduce(obj)59 return NotImplemented60class LocalPickler(CloudPickler):61 _dispatch_table = ChainMap({types.ModuleType: _module_reduce}, CloudPickler._dispatch_table)62 def _function_reduce(self, obj):63 if _is_importable(obj):64 return NotImplemented65 else:66 return self._dynamic_function_reduce(obj)67 def reducer_override(self, obj):68 t = type(obj)69 try:70 is_anyclass = issubclass(t, type)71 except TypeError: # t is not a class (old Boost; see SF #502085)72 is_anyclass = False73 if is_anyclass:74 return _class_reduce(obj)75 elif isinstance(obj, types.FunctionType):76 return self._function_reduce(obj)77 else:78 # fallback to save_global, including the Pickler's79 # distpatch_table80 return NotImplemented81def dump(obj, file, protocol=None, buffer_callback=None):82 LocalPickler(file, protocol=protocol, buffer_callback=buffer_callback).dump(obj)83def dumps(obj, protocol=None, buffer_callback=None):84 with BytesIO() as file:85 cp = LocalPickler(file, protocol=protocol, buffer_callback=buffer_callback)86 cp.dump(obj)87 return file.getvalue()88### TESTS89OLD = """90from torch import nn91class TestNetwork(nn.Module):92 def __init__(self):93 super().__init__()94 self.w = nn.Linear(1, 1)95 96 def forward(self, x):97 return self.w(x)*0 + 198"""99NEW = """100from torch import nn101class TestNetwork(nn.Module):102 def __init__(self):103 super().__init__()104 self.w = nn.Linear(2, 2)105 106 def forward(self, x):107 return self.w(x)*0 + 2108"""109def reload_recreate(path, contents):110 import importlib111 from pathlib import Path112 #TODO: Is there a better way to temporarily ignore the bytecode cache?113 Path('__pycache__/test_pickling.cpython-38.pyc').unlink(True)114 path.write_text(contents)115 import test_pickling116 importlib.reload(test_pickling)117 from test_pickling import TestNetwork118 return TestNetwork()119def test():120 import os121 import pickle122 import torch123 from pathlib import Path124 try:125 path = Path('test_pickling.py')126 old = reload_recreate(path, OLD)127 with open('test_pickling.pkl', 'wb+') as f:128 LocalPickler(f).dump(old)129 new = reload_recreate(path, NEW)130 with open('test_pickling.pkl', 'rb+') as f:131 old = pickle.load(f)132 133 assert old.w.weight.shape[0] == 1134 assert new.w.weight.shape[0] == 2135 assert old(torch.zeros(1))[0] == 1136 assert new(torch.zeros(2))[0] == 2137 finally:138 Path('test_pickling.pkl').unlink(True)...
test__exceptions.py
Source:test__exceptions.py
...6import numpy as np7_ArrayMemoryError = np.core._exceptions._ArrayMemoryError8_UFuncNoLoopError = np.core._exceptions._UFuncNoLoopError9class TestArrayMemoryError:10 def test_pickling(self):11 """ Test that _ArrayMemoryError can be pickled """12 error = _ArrayMemoryError((1023,), np.dtype(np.uint8))13 res = pickle.loads(pickle.dumps(error))14 assert res._total_size == error._total_size15 def test_str(self):16 e = _ArrayMemoryError((1023,), np.dtype(np.uint8))17 str(e) # not crashing is enough18 # testing these properties is easier than testing the full string repr19 def test__size_to_string(self):20 """ Test e._size_to_string """21 f = _ArrayMemoryError._size_to_string22 Ki = 102423 assert f(0) == '0 bytes'24 assert f(1) == '1 bytes'25 assert f(1023) == '1023 bytes'26 assert f(Ki) == '1.00 KiB'27 assert f(Ki+1) == '1.00 KiB'28 assert f(10*Ki) == '10.0 KiB'29 assert f(int(999.4*Ki)) == '999. KiB'30 assert f(int(1023.4*Ki)) == '1023. KiB'31 assert f(int(1023.5*Ki)) == '1.00 MiB'32 assert f(Ki*Ki) == '1.00 MiB'33 # 1023.9999 Mib should round to 1 GiB34 assert f(int(Ki*Ki*Ki*0.9999)) == '1.00 GiB'35 assert f(Ki*Ki*Ki*Ki*Ki*Ki) == '1.00 EiB'36 # larger than sys.maxsize, adding larger prefixes isn't going to help37 # anyway.38 assert f(Ki*Ki*Ki*Ki*Ki*Ki*123456) == '123456. EiB'39 def test__total_size(self):40 """ Test e._total_size """41 e = _ArrayMemoryError((1,), np.dtype(np.uint8))42 assert e._total_size == 143 e = _ArrayMemoryError((2, 4), np.dtype((np.uint64, 16)))44 assert e._total_size == 102445class TestUFuncNoLoopError:46 def test_pickling(self):47 """ Test that _UFuncNoLoopError can be pickled """48 assert isinstance(pickle.dumps(_UFuncNoLoopError), bytes)49@pytest.mark.parametrize("args", [50 (2, 1, None),51 (2, 1, "test_prefix"),52 ("test message",),53])54class TestAxisError:55 def test_attr(self, args):56 """Validate attribute types."""57 exc = np.AxisError(*args)58 if len(args) == 1:59 assert exc.axis is None60 assert exc.ndim is None61 else:62 axis, ndim, *_ = args63 assert exc.axis == axis64 assert exc.ndim == ndim65 def test_pickling(self, args):66 """Test that `AxisError` can be pickled."""67 exc = np.AxisError(*args)68 exc2 = pickle.loads(pickle.dumps(exc))69 assert type(exc) is type(exc2)70 for name in ("axis", "ndim", "args"):71 attr1 = getattr(exc, name)72 attr2 = getattr(exc2, name)...
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!!