2010/5/13 Guido Trotter <[email protected]>:
> On Wed, May 12, 2010 at 7:08 PM, Michael Hanselmann <[email protected]> wrote:
>> --- a/lib/rapi/baserlib.py
>> +++ b/lib/rapi/baserlib.py
>> +def EnforceType(value, exptype, what):
>
> I'd say Ensure rather than Enforce, as you're just checking/aborting,
> rather than trying any conversion/enforcement.
Renamed to “CheckType”.
>> + �...@param value: Value
>> + �...@type exptype: type
>> + �...@param exptype: Expected type
>> + �...@type what: string
>> + �...@param what: Description of value
>> + �...@return: Value (allows inline usage)
>> +
>> + """
>
> value, type, what? :)
> mmm... value, type, value_desc, perhaps?
“type” is a built-in name (and I couldn't call type(value) anymore).
Michael
---
--- a/lib/rapi/baserlib.py
+++ b/lib/rapi/baserlib.py
@@ -217,21 +217,20 @@ def FeedbackFn(ts, log_type, log_msg): # pylint:
disable-msg=W0613
logging.info("%s: %s", log_type, log_msg)
-def EnforceType(value, exptype, what):
+def CheckType(value, exptype, descr):
"""Abort request if value type doesn't match expected type.
@param value: Value
@type exptype: type
@param exptype: Expected type
- @type what: string
- @param what: Description of value
+ @type descr: string
+ @param descr: Description of value
@return: Value (allows inline usage)
"""
if not isinstance(value, exptype):
- raise http.HttpBadRequest("%s: Type is '%s', but '%s'"
- " is expected" %
- (what, type(value).__name__, exptype.__name__))
+ raise http.HttpBadRequest("%s: Type is '%s', but '%s' is expected" %
+ (descr, type(value).__name__, exptype.__name__))
return value
@@ -262,7 +261,7 @@ def CheckParameter(data, name, default=_DEFAULT,
exptype=_DEFAULT):
if exptype is _DEFAULT:
return value
- return EnforceType(value, exptype, "The '%s' parameter" % name)
+ return CheckType(value, exptype, "'%s' parameter" % name)
class R_Generic(object):