本文整理汇总了Python中openmdao.main.expreval.ExprEvaluator.is_valid_assignee方法的典型用法代码示例。如果您正苦于以下问题:Python ExprEvaluator.is_valid_assignee方法的具体用法?Python ExprEvaluator.is_valid_assignee怎么用?Python ExprEvaluator.is_valid_assignee使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类openmdao.main.expreval.ExprEvaluator
的用法示例。
在下文中一共展示了ExprEvaluator.is_valid_assignee方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _create
# 需要导入模块: from openmdao.main.expreval import ExprEvaluator [as 别名]
# 或者: from openmdao.main.expreval.ExprEvaluator import is_valid_assignee [as 别名]
def _create(self, target, low, high, scaler, adder, start, fd_step,
key, scope):
""" Create one Parameter or ArrayParameter. """
try:
expreval = ExprEvaluator(target, scope)
except Exception as err:
raise err.__class__("Can't add parameter: %s" % err)
if not expreval.is_valid_assignee():
raise ValueError("Can't add parameter: '%s' is not a"
" valid parameter expression"
% expreval.text)
try:
val = expreval.evaluate()
except Exception as err:
val = None # Let Parameter code sort out why.
name = key[0] if isinstance(key, tuple) else key
if isinstance(val, ndarray):
return ArrayParameter(target, low=low, high=high,
scaler=scaler, adder=adder,
start=start, fd_step=fd_step,
name=name, scope=scope,
_expreval=expreval, _val=val,
_allowed_types=self._allowed_types)
else:
return Parameter(target, low=low, high=high,
scaler=scaler, adder=adder,
start=start, fd_step=fd_step,
name=name, scope=scope,
_expreval=expreval, _val=val,
_allowed_types=self._allowed_types)
示例2: __init__
# 需要导入模块: from openmdao.main.expreval import ExprEvaluator [as 别名]
# 或者: from openmdao.main.expreval.ExprEvaluator import is_valid_assignee [as 别名]
def __init__(self, target, high=None, low=None,
scaler=None, adder=None, start=None,
fd_step=None, scope=None, name=None,
_expreval=None):
"""If scaler and/or adder are not None, then high, low, and start, if
not None, are assumed to be expressed in unscaled form. If high and low
are not supplied, then their values will be pulled from the target
variable (along with a start value), and are assumed to be in scaled
form, so their values will be unscaled prior to being stored in the
Parameter.
"""
if scaler is None:
scaler = 1.0
if adder is None:
adder = 0.0
self._scaling_required = scaler != 1. or adder != 0.
self.low = low
self.high = high
self.scaler = scaler
self.adder = adder
self.start = start
self.fd_step = fd_step
self.name = name or target
if _expreval is None:
try:
_expreval = ExprEvaluator(target, scope)
except Exception as err:
raise err.__class__("Can't add parameter: %s" % str(err))
if not _expreval.is_valid_assignee():
raise ValueError("Can't add parameter: '%s' is not a valid"
" parameter expression" % _expreval.text)
self._expreval = _expreval
try:
self._metadata = self._expreval.get_metadata()
except AttributeError:
raise AttributeError("Can't add parameter '%s' because it doesn't"
" exist." % target)
# 'raw' metadata is in the form [(varname, metadata)],
# so use [0][1] to get the actual metadata dict
metadata = self._metadata[0][1]
if 'iotype' in metadata and metadata['iotype'] == 'out':
raise RuntimeError("Can't add parameter '%s' because '%s' is an"
" output." % (target, target))
try:
# So, our traits might not have a vartypename?
self.vartypename = metadata['vartypename']
except KeyError:
self.vartypename = None
示例3: __init__
# 需要导入模块: from openmdao.main.expreval import ExprEvaluator [as 别名]
# 或者: from openmdao.main.expreval.ExprEvaluator import is_valid_assignee [as 别名]
def __init__(self, target, parent, high=None, low=None,
scaler=None, adder=None, start=None,
fd_step=None, scope=None, name=None):
self._metadata = None
if scaler is None and adder is None:
self._transform = self._do_nothing
self._untransform = self._do_nothing
else:
if scaler is None:
scaler = 1.0
else:
try:
scaler = float(scaler)
except (TypeError, ValueError):
msg = "Bad value given for parameter's 'scaler' attribute."
parent.raise_exception(msg, ValueError)
if adder is None:
adder = 0.0
else:
try:
adder = float(adder)
except (TypeError, ValueError):
msg = "Bad value given for parameter's 'adder' attribute."
parent.raise_exception(msg, ValueError)
self.low = low
self.high = high
self.start = start
self.scaler = scaler
self.adder = adder
self.fd_step = fd_step
if name is not None:
self.name = name
else:
self.name = target
try:
expreval = ExprEvaluator(target, scope)
except Exception as err:
parent.raise_exception("Can't add parameter: %s" % str(err),
type(err))
if not expreval.is_valid_assignee():
parent.raise_exception("Can't add parameter: '%s' is not a valid parameter expression" %
expreval.text, ValueError)
self._expreval = expreval
try:
# metadata is in the form (varname, metadata), so use [1] to get
# the actual metadata dict
metadata = self.get_metadata()[1]
except AttributeError:
parent.raise_exception("Can't add parameter '%s' because it doesn't exist." % target,
AttributeError)
if 'iotype' in metadata and metadata['iotype'] == 'out':
parent.raise_exception("Can't add parameter '%s' because '%s' is an output." % (target, target),
RuntimeError)
try:
# So, our traits might not have a vartypename?
self.vartypename = metadata['vartypename']
except KeyError:
self.vartypename = None
try:
val = expreval.evaluate()
except Exception as err:
parent.raise_exception("Can't add parameter because I can't evaluate '%s'." % target,
ValueError)
self.valtypename = type(val).__name__
if self.vartypename == 'Enum':
return # it's an Enum, so no need to set high or low
if not isinstance(val, real_types) and not isinstance(val, int_types):
parent.raise_exception("The value of parameter '%s' must be a real or integral type, but its type is '%s'." %
(target,type(val).__name__), ValueError)
meta_low = metadata.get('low') # this will be None if 'low' isn't there
if meta_low is not None:
if low is None:
self.low = self._untransform(meta_low)
elif low < self._untransform(meta_low):
parent.raise_exception("Trying to add parameter '%s', "
"but the lower limit supplied (%s) exceeds the "
"built-in lower limit (%s)." %
(target, low, meta_low), ValueError)
else:
if low is None:
parent.raise_exception("Trying to add parameter '%s', "
"but no lower limit was found and no "
"'low' argument was given. One or the "
"other must be specified." % target,ValueError)
meta_high = metadata.get('high') # this will be None if 'low' isn't there
if meta_high is not None:
if high is None:
self.high = self._untransform(meta_high)
#.........这里部分代码省略.........
示例4: test_assignee
# 需要导入模块: from openmdao.main.expreval import ExprEvaluator [as 别名]
# 或者: from openmdao.main.expreval.ExprEvaluator import is_valid_assignee [as 别名]
def test_assignee(self):
ex = ExprEvaluator('a1d[3]*a1d[2 ]', self.top.a)
self.assertEqual(ex.is_valid_assignee(), False)
ex.text = 'comp.contlist[1].a2d[2][1]'
self.assertEqual(ex.is_valid_assignee(), True)
示例5: __init__
# 需要导入模块: from openmdao.main.expreval import ExprEvaluator [as 别名]
# 或者: from openmdao.main.expreval.ExprEvaluator import is_valid_assignee [as 别名]
def __init__(self, target, parent, high=None, low=None, scaler=None, adder=None, fd_step=None, scope=None):
self._metadata = None
if scaler is None and adder is None:
self._transform = self._do_nothing
self._untransform = self._do_nothing
else:
if scaler is None:
scaler = 1.0
else:
try:
scaler = float(scaler)
except TypeError:
parent.raise_exception("bad scaler", TypeError)
if adder is None:
adder = 0.0
else:
try:
adder = float(adder)
except TypeError:
parent.raise_exception("bad adder", TypeError)
self.low = low
self.high = high
self.scaler = scaler
self.adder = adder
self.fd_step = fd_step
try:
expreval = ExprEvaluator(target, scope)
except Exception as err:
parent.raise_exception("Can't add parameter: %s" % str(err), type(err))
if not expreval.is_valid_assignee():
parent.raise_exception(
"Can't add parameter: '%s' is not a valid parameter expression" % expreval.text, ValueError
)
self._expreval = expreval
try:
# metadata is in the form [(varname, metadata)], so use [0][1] to get
# the actual metadata dict (since we're a Parameter we'll only be
# referencing one variable.
metadata = self.get_metadata()[0][1]
except AttributeError:
parent.raise_exception("Can't add parameter '%s' because it doesn't exist." % target, AttributeError)
try:
self.vartypename = metadata["vartypename"]
except KeyError:
self.vartypename = None
try:
val = expreval.evaluate()
except Exception as err:
parent.raise_exception("Can't add parameter because I can't evaluate '%s'." % target, ValueError)
if self.vartypename != "Enum" and not isinstance(val, (float, float32, float64, int, int32, int64)):
parent.raise_exception(
"The value of parameter '%s' must be of type float or int, but its type is '%s'."
% (target, type(val).__name__),
ValueError,
)
self.valtypename = type(val).__name__
meta_low = metadata.get("low") # this will be None if 'low' isn't there
if low is None:
self.low = meta_low
else:
if meta_low is not None and low < self._transform(meta_low):
parent.raise_exception(
"Trying to add parameter '%s', "
"but the lower limit supplied (%s) exceeds the "
"built-in lower limit (%s)." % (target, low, meta_low),
ValueError,
)
self.low = low
meta_high = metadata.get("high") # this will be None if 'high' isn't there
if high is None:
self.high = meta_high
else: # high is not None
if meta_high is not None and high > self._transform(meta_high):
parent.raise_exception(
"Trying to add parameter '%s', "
"but the upper limit supplied (%s) exceeds the "
"built-in upper limit (%s)." % (target, high, meta_high),
ValueError,
)
self.high = high
if self.vartypename == "Enum":
return # it's an Enum, so no need to set high or low
else:
if self.low is None:
parent.raise_exception(
"Trying to add parameter '%s', "
"but no lower limit was found and no "
"'low' argument was given. One or the "
"other must be specified." % target,
ValueError,
)
#.........这里部分代码省略.........