本文整理汇总了Python中mathics.core.expression.Expression.get_option_values方法的典型用法代码示例。如果您正苦于以下问题:Python Expression.get_option_values方法的具体用法?Python Expression.get_option_values怎么用?Python Expression.get_option_values使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mathics.core.expression.Expression
的用法示例。
在下文中一共展示了Expression.get_option_values方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: OptionsPattern
# 需要导入模块: from mathics.core.expression import Expression [as 别名]
# 或者: from mathics.core.expression.Expression import get_option_values [as 别名]
class OptionsPattern(PatternObject):
"""
<dl>
<dt>'OptionsPattern[$f$]'
<dd>is a pattern that stands for a sequence of options given
to a function, with default values taken from 'Options[$f$]'.
The options can be of the form '$opt$->$value$' or
'$opt$:>$value$', and might be in arbitrarily nested lists.
<dt>'OptionsPattern[{$opt1$->$value1$, ...}]'
<dd>takes explicit default values from the given list. The
list may also contain symbols $f$, for which 'Options[$f$]' is
taken into account; it may be arbitrarily nested.
'OptionsPattern[{}]' does not use any default values.
</dl>
The option values can be accessed using 'OptionValue'.
>> f[x_, OptionsPattern[{n->2}]] := x ^ OptionValue[n]
>> f[x]
= x ^ 2
>> f[x, n->3]
= x ^ 3
Delayed rules as options:
>> e = f[x, n:>a]
= x ^ a
>> a = 5;
>> e
= x ^ 5
Options might be given in nested lists:
>> f[x, {{{n->4}}}]
= x ^ 4
#> {opt -> b} /. OptionsPattern[{}] -> t
= t
#> Clear[f]
#> Options[f] = {Power -> 2};
#> f[x_, OptionsPattern[f]] := x ^ OptionValue[Power]
#> f[10]
= 100
#> f[10, Power -> 3]
= 1000
#> Clear[f]
#> Options[f] = {Power -> 2};
#> f[x_, OptionsPattern[]] := x ^ OptionValue[Power]
#> f[10]
= 100
#> f[10, Power -> 3]
= 1000
#> Clear[f]
"""
arg_counts = [0, 1]
def init(self, expr):
super(OptionsPattern, self).init(expr)
try:
self.defaults = expr.leaves[0]
except IndexError:
# OptionsPattern[] takes default options of the nearest enclosing
# function. Set to not None in self.match
self.defaults = None
def match(self, yield_func, expression, vars, evaluation, **kwargs):
if self.defaults is None:
self.defaults = kwargs.get('head')
if self.defaults is None:
# we end up here with OptionsPattern that do not have any
# default options defined, e.g. with this code:
# f[x:OptionsPattern[]] := x; f["Test" -> 1]
# set self.defaults to an empty List, so we don't crash.
self.defaults = Expression('List')
values = self.defaults.get_option_values(
evaluation, allow_symbols=True, stop_on_error=False)
sequence = expression.get_sequence()
for options in sequence:
option_values = options.get_option_values(evaluation)
if option_values is None:
return
values.update(option_values)
new_vars = vars.copy()
for name, value in values.items():
new_vars['_option_' + name] = value
yield_func(new_vars, None)
def get_match_count(self, vars={}):
return (0, None)
def get_match_candidates(self, leaves, expression, attributes, evaluation,
vars={}):
def _match(leaf):
return (leaf.has_form(('Rule', 'RuleDelayed'), 2) or
leaf.has_form('List', None))
return [leaf for leaf in leaves if _match(leaf)]