本文整理匯總了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)