本文整理汇总了Python中cf_units.Unit.is_convertible方法的典型用法代码示例。如果您正苦于以下问题:Python Unit.is_convertible方法的具体用法?Python Unit.is_convertible怎么用?Python Unit.is_convertible使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cf_units.Unit
的用法示例。
在下文中一共展示了Unit.is_convertible方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: units_convertible
# 需要导入模块: from cf_units import Unit [as 别名]
# 或者: from cf_units.Unit import is_convertible [as 别名]
def units_convertible(units1, units2, reftimeistime=True):
"""Return True if a Unit representing the string units1 can be converted
to a Unit representing the string units2, else False."""
try:
u1 = Unit(units1)
u2 = Unit(units2)
except ValueError:
return False
return u1.is_convertible(units2)
示例2: _check_variable_attrs
# 需要导入模块: from cf_units import Unit [as 别名]
# 或者: from cf_units.Unit import is_convertible [as 别名]
def _check_variable_attrs(dataset, var_name, required_attributes=None):
'''
Convenience method to check a variable attributes based on the
expected_vars dict
'''
score = 0
out_of = 0
messages = []
if var_name not in dataset.variables:
# No need to check the attrs if the variable doesn't exist
return (score, out_of, messages)
var = dataset.variables[var_name]
# Get the expected attrs to check
check_attrs = required_attributes or required_var_attrs.get(var_name, {})
var_attrs = set(var.ncattrs())
for attr in check_attrs:
if attr == 'dtype':
# dtype check is special, see above
continue
out_of += 1
score += 1
# Check if the attribute is present
if attr not in var_attrs:
messages.append('Variable {} must contain attribute: {}'
''.format(var_name, attr))
score -= 1
continue
# Attribute exists, let's check if there was a value we need to compare against
if check_attrs[attr] is not None:
if getattr(var, attr) != check_attrs[attr]:
# No match, this may be an error, but first an exception for units
if attr == 'units':
msg = ('Variable {} units attribute must be '
'convertible to {}'.format(var_name, check_attrs[attr]))
try:
cur_unit = Unit(var.units)
comp_unit = Unit(check_attrs[attr])
if not cur_unit.is_convertible(comp_unit):
messages.append(msg)
score -= 1
except ValueError:
messages.append(msg)
score -= 1
else:
messages.append('Variable {} attribute {} must be {}'.format(var_name, attr, check_attrs[attr]))
score -= 1
else:
# Final check to make sure the attribute isn't an empty string
try:
# try stripping whitespace, and return an error if empty
att_strip = getattr(var, attr).strip()
if not att_strip:
messages.append('Variable {} attribute {} is empty'
''.format(var_name, attr))
score -= 1
except AttributeError:
pass
return (score, out_of, messages)