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 codecs
|
||||||
import hashlib
|
import hashlib
|
||||||
|
|
||||||
|
from isso.utils import types
|
||||||
from isso.compat import string_types, text_type as str
|
from isso.compat import string_types, text_type as str
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -20,11 +21,6 @@ except ImportError:
|
|||||||
"to `werkzeug` 0.9 or install `passlib`.")
|
"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):
|
class Hash(object):
|
||||||
|
|
||||||
func = None
|
func = None
|
||||||
@ -37,29 +33,25 @@ class Hash(object):
|
|||||||
self.func = func
|
self.func = func
|
||||||
|
|
||||||
if salt is not None:
|
if salt is not None:
|
||||||
if not isinstance(salt, bytes):
|
types.require(salt, bytes)
|
||||||
raise _TypeError("salt", "bytes", salt)
|
|
||||||
self.salt = salt
|
self.salt = salt
|
||||||
|
|
||||||
def hash(self, val):
|
def hash(self, val):
|
||||||
"""Calculate hash from value (must be bytes)."""
|
"""Calculate hash from value (must be bytes).
|
||||||
|
"""
|
||||||
|
|
||||||
if not isinstance(val, bytes):
|
types.require(val, bytes)
|
||||||
raise _TypeError("val", "bytes", val)
|
|
||||||
|
|
||||||
rv = self.compute(val)
|
rv = self.compute(val)
|
||||||
|
|
||||||
if not isinstance(val, bytes):
|
types.require(rv, bytes)
|
||||||
raise _TypeError("val", "bytes", rv)
|
|
||||||
|
|
||||||
return rv
|
return rv
|
||||||
|
|
||||||
def uhash(self, val):
|
def uhash(self, val):
|
||||||
"""Calculate hash from unicode value and return hex value as unicode"""
|
"""Calculate hash from unicode value and return hex value as unicode
|
||||||
|
"""
|
||||||
if not isinstance(val, string_types):
|
types.require(val, string_types)
|
||||||
raise _TypeError("val", "str", val)
|
|
||||||
|
|
||||||
return codecs.encode(self.hash(val.encode("utf-8")), "hex_codec").decode("utf-8")
|
return codecs.encode(self.hash(val.encode("utf-8")), "hex_codec").decode("utf-8")
|
||||||
|
|
||||||
def compute(self, val):
|
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