add type checking utils
This commit is contained in:
parent
b2bc582f92
commit
bbd9e1b523
27
isso/tests/test_utils_types.py
Normal file
27
isso/tests/test_utils_types.py
Normal file
@ -0,0 +1,27 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
|
||||
from __future__ import unicode_literals
|
||||
|
||||
try:
|
||||
import unittest2 as unittest
|
||||
except ImportError:
|
||||
import unittest
|
||||
|
||||
from isso.compat import string_types
|
||||
from isso.utils import types
|
||||
|
||||
|
||||
class TestTypes(unittest.TestCase):
|
||||
|
||||
def test_require(self):
|
||||
|
||||
try:
|
||||
types.require("foo", string_types)
|
||||
except TypeError:
|
||||
self.assertTrue(False)
|
||||
|
||||
def test_require_raises(self):
|
||||
|
||||
self.assertRaises(TypeError, types.require, 1, bool)
|
||||
self.assertRaises(TypeError, types.require, 1, str)
|
||||
|
@ -5,6 +5,7 @@ from __future__ import unicode_literals
|
||||
import codecs
|
||||
import hashlib
|
||||
|
||||
from isso.utils import types
|
||||
from isso.compat import string_types, text_type as str
|
||||
|
||||
try:
|
||||
@ -20,11 +21,6 @@ except ImportError:
|
||||
"to `werkzeug` 0.9 or install `passlib`.")
|
||||
|
||||
|
||||
def _TypeError(name, expected, val):
|
||||
return TypeError("'{0}' must be {1}, not {2}".format(
|
||||
name, expected, val.__class__.__name__))
|
||||
|
||||
|
||||
class Hash(object):
|
||||
|
||||
func = None
|
||||
@ -37,29 +33,25 @@ class Hash(object):
|
||||
self.func = func
|
||||
|
||||
if salt is not None:
|
||||
if not isinstance(salt, bytes):
|
||||
raise _TypeError("salt", "bytes", salt)
|
||||
types.require(salt, bytes)
|
||||
self.salt = salt
|
||||
|
||||
def hash(self, val):
|
||||
"""Calculate hash from value (must be bytes)."""
|
||||
"""Calculate hash from value (must be bytes).
|
||||
"""
|
||||
|
||||
if not isinstance(val, bytes):
|
||||
raise _TypeError("val", "bytes", val)
|
||||
types.require(val, bytes)
|
||||
|
||||
rv = self.compute(val)
|
||||
|
||||
if not isinstance(val, bytes):
|
||||
raise _TypeError("val", "bytes", rv)
|
||||
types.require(rv, bytes)
|
||||
|
||||
return rv
|
||||
|
||||
def uhash(self, val):
|
||||
"""Calculate hash from unicode value and return hex value as unicode"""
|
||||
|
||||
if not isinstance(val, string_types):
|
||||
raise _TypeError("val", "str", val)
|
||||
|
||||
"""Calculate hash from unicode value and return hex value as unicode
|
||||
"""
|
||||
types.require(val, string_types)
|
||||
return codecs.encode(self.hash(val.encode("utf-8")), "hex_codec").decode("utf-8")
|
||||
|
||||
def compute(self, val):
|
||||
|
15
isso/utils/types.py
Normal file
15
isso/utils/types.py
Normal file
@ -0,0 +1,15 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
|
||||
|
||||
def _TypeError(expected, val):
|
||||
if isinstance(expected, (list, tuple)):
|
||||
expected = ", ".join(expected.__name__)
|
||||
else:
|
||||
expected = expected.__name__
|
||||
return TypeError("Expected {0}, not {1}".format(
|
||||
expected, val.__class__.__name__))
|
||||
|
||||
|
||||
def require(val, expected):
|
||||
if not isinstance(val, expected):
|
||||
raise _TypeError(expected, val)
|
Loading…
Reference in New Issue
Block a user