我们从Python开源项目中,提取了以下8个代码示例,用于说明如何使用hashlib.__dict__()。
def test_get_builtin_constructor(self): get_builtin_constructor = hashlib.__dict__[ '__get_builtin_constructor'] self.assertRaises(ValueError, get_builtin_constructor, 'test') try: import _md5 except ImportError: pass # This forces an ImportError for "import _md5" statements sys.modules['_md5'] = None try: self.assertRaises(ValueError, get_builtin_constructor, 'md5') finally: if '_md5' in locals(): sys.modules['_md5'] = _md5 else: del sys.modules['_md5']
def test_get_builtin_constructor(self): get_builtin_constructor = hashlib.__dict__[ '__get_builtin_constructor'] self.assertRaises(ValueError, get_builtin_constructor, 'test') try: import _md5 except ImportError: pass # This forces an ImportError for "import _md5" statements sys.modules['_md5'] = None try: self.assertRaises(ValueError, get_builtin_constructor, 'md5') finally: if '_md5' in locals(): sys.modules['_md5'] = _md5 else: del sys.modules['_md5'] self.assertRaises(TypeError, get_builtin_constructor, 3)
def _closure(): hash = hashlib.__dict__[_algo] def file(p): h = hash() fd = open(p) while True: s = fd.read(4096) if not s: break h.update(s) fd.close() return h def sum(s): return hash(s) filef = lambda x: file(x).digest() filef.__doc__ = 'Calculates the %s sum of a file' % _algo sumf = lambda x: sum(x).digest() sumf.__doc__ = 'Calculates the %s sum of a string' % _algo fileh = lambda x: file(x).hexdigest() fileh.__doc__ = 'Calculates the %s sum of a file; returns hex-encoded' % _algo sumh = lambda x: sum(x).hexdigest() sumh.__doc__ = 'Calculates the %s sum of a string; returns hex-encoded' % _algo return filef, sumf, fileh, sumh