ENH: Add `coerce` preprocessor.

This commit is contained in:
Scott Sanderson
2016-01-12 17:36:36 -05:00
parent d3d362f56f
commit 43b6344d5f
+25
View File
@@ -11,6 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from functools import partial
from operator import attrgetter
from numpy import dtype
@@ -283,6 +284,30 @@ def expect_element(*_pos, **named):
return preprocess(**valmap(_expect_element, named))
def coerce(from_, to, **to_kwargs):
"""
A preprocessing decorator that coerces inputs of a given type by passing
them to a callable.
Usage
-----
>>> @preprocess(x=coerce(float, int), y=coerce(float, int))
... def floordiff(x, y):
... return x - y
...
>>> floordiff(3.2, 2.5)
1
"""
def preprocessor(func, argname, arg):
if isinstance(arg, from_):
return to(arg, **to_kwargs)
return arg
return preprocessor
coerce_string = partial(coerce, string_types)
def _expect_element(collection):
template = (
"%(funcname)s() expected a value in {collection} "