本文整理汇总了Python中ovs.dal.helpers.Toolbox.check_type方法的典型用法代码示例。如果您正苦于以下问题:Python Toolbox.check_type方法的具体用法?Python Toolbox.check_type怎么用?Python Toolbox.check_type使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ovs.dal.helpers.Toolbox
的用法示例。
在下文中一共展示了Toolbox.check_type方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: verify_required_params
# 需要导入模块: from ovs.dal.helpers import Toolbox [as 别名]
# 或者: from ovs.dal.helpers.Toolbox import check_type [as 别名]
def verify_required_params(required_params, actual_params):
error_messages = []
for required_key, key_info in required_params.iteritems():
expected_type = key_info[0]
expected_value = key_info[1]
optional = len(key_info) == 3 and key_info[2] is False
if required_key not in actual_params:
error_messages.append('Missing required param "{0}"'.format(required_key))
continue
actual_value = actual_params[required_key]
if HelperToolbox.check_type(actual_value, expected_type)[0] is False:
error_messages.append('Required param "{0}" is of type "{1}" but we expected type "{2}"'.format(required_key, type(actual_value), expected_type))
continue
if expected_value is None or (optional is True and actual_value in ('', None)):
continue
if expected_type == list:
if type(expected_value) == Toolbox.compiled_regex_type: # List of strings which need to match regex
for item in actual_value:
if not re.match(expected_value, item):
error_messages.append('Required param "{0}" has an item "{1}" which does not match regex "{2}"'.format(required_key, item, expected_value.pattern))
else:
if HelperToolbox.check_type(expected_value, list)[0] is True and actual_value not in expected_value:
error_messages.append('Required param "{0}" with value "{1}" should be 1 of the following: {2}'.format(required_key, actual_value, expected_value))
elif HelperToolbox.check_type(expected_value, Toolbox.compiled_regex_type)[0] is True and not re.match(expected_value, actual_value):
error_messages.append('Required param "{0}" with value "{1}" does not match regex "{2}"'.format(required_key, actual_value, expected_value.pattern))
if error_messages:
raise RuntimeError('\n' + '\n'.join(error_messages))
示例2: verify_required_params
# 需要导入模块: from ovs.dal.helpers import Toolbox [as 别名]
# 或者: from ovs.dal.helpers.Toolbox import check_type [as 别名]
def verify_required_params(required_params, actual_params, exact_match=False):
"""
Verify whether the actual parameters match the required parameters
:param required_params: Required parameters which actual parameters have to meet
:param actual_params: Actual parameters to check for validity
:param exact_match: Keys of both dictionaries must be identical
:return: None
"""
error_messages = []
if not isinstance(required_params, dict) or not isinstance(actual_params, dict):
raise RuntimeError('Required and actual parameters must be of type dictionary')
if exact_match is True:
for key in set(actual_params.keys()).difference(required_params.keys()):
error_messages.append('Missing key "{0}" in required_params'.format(key))
for required_key, key_info in required_params.iteritems():
expected_type = key_info[0]
expected_value = key_info[1]
optional = len(key_info) == 3 and key_info[2] is False
if optional is True and (required_key not in actual_params or actual_params[required_key] in ('', None)):
continue
if required_key not in actual_params:
error_messages.append('Missing required param "{0}" in actual parameters'.format(required_key))
continue
actual_value = actual_params[required_key]
if HelperToolbox.check_type(actual_value, expected_type)[0] is False:
error_messages.append('Required param "{0}" is of type "{1}" but we expected type "{2}"'.format(required_key, type(actual_value), expected_type))
continue
if expected_value is None:
continue
if expected_type == list:
if type(expected_value) == Toolbox.compiled_regex_type: # List of strings which need to match regex
for item in actual_value:
if not re.match(expected_value, item):
error_messages.append('Required param "{0}" has an item "{1}" which does not match regex "{2}"'.format(required_key, item, expected_value.pattern))
elif expected_type == dict:
Toolbox.verify_required_params(expected_value, actual_params[required_key])
elif expected_type == int:
if isinstance(expected_value, list) and actual_value not in expected_value:
error_messages.append('Required param "{0}" with value "{1}" should be 1 of the following: {2}'.format(required_key, actual_value, expected_value))
if isinstance(expected_value, dict):
minimum = expected_value.get('min', sys.maxint * -1)
maximum = expected_value.get('max', sys.maxint)
if not minimum <= actual_value <= maximum:
error_messages.append('Required param "{0}" with value "{1}" should be in range: {2} - {3}'.format(required_key, actual_value, minimum, maximum))
else:
if HelperToolbox.check_type(expected_value, list)[0] is True and actual_value not in expected_value:
error_messages.append('Required param "{0}" with value "{1}" should be 1 of the following: {2}'.format(required_key, actual_value, expected_value))
elif HelperToolbox.check_type(expected_value, Toolbox.compiled_regex_type)[0] is True and not re.match(expected_value, actual_value):
error_messages.append('Required param "{0}" with value "{1}" does not match regex "{2}"'.format(required_key, actual_value, expected_value.pattern))
if error_messages:
raise RuntimeError('\n' + '\n'.join(error_messages))
示例3: _backend_property
# 需要导入模块: from ovs.dal.helpers import Toolbox [as 别名]
# 或者: from ovs.dal.helpers.Toolbox import check_type [as 别名]
def _backend_property(self, function, dynamic):
"""
Handles the internal caching of dynamic properties
"""
caller_name = dynamic.name
cache_key = '{0}_{1}'.format(self._key, caller_name)
mutex = volatile_mutex(cache_key)
try:
cached_data = self._volatile.get(cache_key)
if cached_data is None:
if dynamic.locked:
mutex.acquire()
cached_data = self._volatile.get(cache_key)
if cached_data is None:
function_info = inspect.getargspec(function)
if 'dynamic' in function_info.args:
cached_data = function(dynamic=dynamic) # Load data from backend
else:
cached_data = function()
if cached_data is not None:
correct, allowed_types, given_type = Toolbox.check_type(cached_data, dynamic.return_type)
if not correct:
raise TypeError('Dynamic property {0} allows types {1}. {2} given'.format(
caller_name, str(allowed_types), given_type
))
if dynamic.timeout > 0:
self._volatile.set(cache_key, cached_data, dynamic.timeout)
return cached_data
finally:
mutex.release()
示例4: _set_property
# 需要导入模块: from ovs.dal.helpers import Toolbox [as 别名]
# 或者: from ovs.dal.helpers.Toolbox import check_type [as 别名]
def _set_property(self, prop, value):
"""
Setter for a simple property that will validate the type
"""
self.dirty = True
if value is None:
self._data[prop.name] = value
else:
correct, allowed_types, given_type = Toolbox.check_type(value, prop.property_type)
if correct:
self._data[prop.name] = value
else:
raise TypeError('Property {0} allows types {1}. {2} given'.format(
prop.name, str(allowed_types), given_type
))
示例5: verify_required_params
# 需要导入模块: from ovs.dal.helpers import Toolbox [as 别名]
# 或者: from ovs.dal.helpers.Toolbox import check_type [as 别名]
def verify_required_params(required_params, actual_params):
error_messages = []
for required_key, key_info in required_params.iteritems():
expected_type = key_info[0]
expected_value = key_info[1]
optional = len(key_info) == 3 and key_info[2] is False
if optional is True and (required_key not in actual_params or actual_params[required_key] in ("", None)):
continue
if required_key not in actual_params:
error_messages.append('Missing required param "{0}"'.format(required_key))
continue
actual_value = actual_params[required_key]
if HelperToolbox.check_type(actual_value, expected_type)[0] is False:
error_messages.append(
'Required param "{0}" is of type "{1}" but we expected type "{2}"'.format(
required_key, type(actual_value), expected_type
)
)
continue
if expected_value is None:
continue
if expected_type == list:
if type(expected_value) == Toolbox.compiled_regex_type: # List of strings which need to match regex
for item in actual_value:
if not re.match(expected_value, item):
error_messages.append(
'Required param "{0}" has an item "{1}" which does not match regex "{2}"'.format(
required_key, item, expected_value.pattern
)
)
elif expected_type == dict:
Toolbox.verify_required_params(expected_value, actual_params[required_key])
elif expected_type == int:
if isinstance(expected_value, list) and actual_value not in expected_value:
error_messages.append(
'Required param "{0}" with value "{1}" should be 1 of the following: {2}'.format(
required_key, actual_value, expected_value
)
)
if isinstance(expected_value, dict):
minimum = expected_value.get("min", sys.maxint * -1)
maximum = expected_value.get("max", sys.maxint)
if not minimum <= actual_value <= maximum:
error_messages.append(
'Required param "{0}" with value "{1}" should be in range: {2} - {3}'.format(
required_key, actual_value, minimum, maximum
)
)
else:
if HelperToolbox.check_type(expected_value, list)[0] is True and actual_value not in expected_value:
error_messages.append(
'Required param "{0}" with value "{1}" should be 1 of the following: {2}'.format(
required_key, actual_value, expected_value
)
)
elif HelperToolbox.check_type(expected_value, Toolbox.compiled_regex_type)[0] is True and not re.match(
expected_value, actual_value
):
error_messages.append(
'Required param "{0}" with value "{1}" does not match regex "{2}"'.format(
required_key, actual_value, expected_value.pattern
)
)
if error_messages:
raise RuntimeError("\n" + "\n".join(error_messages))