- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
上下文
首先,感谢您的假设。它既非常强大又非常有用!
我编写了一个假设策略来生成以下形式的单调(ANDS 和 OR)策略表达式:
(A and (B or C))
这可以被认为是一个树结构,其中 A、B 和 C 是叶子节点的属性,而 'and' 和 'or' 是非叶子节点。
该策略似乎可以根据需要生成表达式。
>>> find(policy_expressions(), lambda x: len(x.split()) > 3)
'(A or (A or A))'
(也许可以提高样本的统计多样性,但这不是这个问题的本质)。
不等式也是有效的。例如:
(N or (WlIorO and (nX <= 55516 and e)))
我想约束或过滤示例,以便生成具有指定数量的叶节点(即属性)的策略表达式。
对于性能测试,我试过将 data.draw()
与 filter
一起使用,如下所示:
@given(data=data())
def test_keygen_encrypt_proxy_decrypt_decrypt_execution_time(data, n):
"""
:param n: the input size n. Number of attributes or leaf nodes in policy tree.
"""
policy_str = data.draw(strategy=policy_expressions().filter(lambda x: len(extract_attributes(group, x)) == n),
label="policy string")
其中 extract_attributes()
计算表达式中叶节点的数量,n
是所需的叶节点数。
这个解决方案的问题在于,当 n
> 16 时,假设会抛出一个:
hypothesis.errors.Unsatisfiable: Unable to satisfy assumptions of hypothesis test_keygen_encrypt_proxy_decrypt_decrypt_execution_time.
我想生成具有 100 个叶节点的有效策略表达式。
该方法的另一个缺点是假设报告 HealthCheck.filter_too_much
和 HealthCheck.too_slow
并且 @settings
变得丑陋。
我宁愿有一个参数说 policy_expressions(leaf_nodes=4)
来得到这样的例子:
(N or (WlIorO and (nX <= 55516 and e)))
我最初避免了这种情况,因为我看不到如何使用递归策略代码来做到这一点。
问题
您能否建议一种重构此策略的方法,以便它可以针对叶节点的数量进行参数化?
这是策略代码(无论如何它在 Charm Crypto 中是开源的)
from hypothesis.strategies import text, composite, sampled_from, characters, one_of, integers
def policy_expressions():
return one_of(attributes(), inequalities(), policy_expression())
@composite
def policy_expression(draw):
left = draw(policy_expressions())
right = draw(policy_expressions())
gate = draw(gates())
return u'(' + u' '.join((left, gate, right)) + u')'
def attributes():
return text(min_size=1, alphabet=characters(whitelist_categories='L', max_codepoint=0x7e))
@composite
def inequalities(draw):
attr = draw(attributes())
oper = draw(inequality_operators())
numb = draw(integers(min_value=1))
return u' '.join((attr, oper, str(numb)))
def inequality_operators():
return sampled_from((u'<', u'>', u'<=', u'>='))
def gates():
return sampled_from((u'or', u'and'))
def assert_valid(policy_expression):
assert policy_expression # not empty
assert policy_expression.count(u'(') == policy_expression.count(u')')
https://github.com/JHUISI/charm/blob/dev/charm/toolbox/policy_expression_spec.py
最佳答案
我建议明确地将叶数构建到数据的构建方式中,然后传入您想要的叶数:
from hypothesis.strategies import text, composite, sampled_from, characters, one_of, integers
def policy_expressions_of_size(num_leaves):
if num_leaves == 1:
return attributes()
elif num_leaves == 2:
return one_of(inequalities(), policy_expression(num_leaves))
else:
return policy_expression(num_leaves)
policy_expressions = integers(min_value=1, max_value=500).flatmap(policy_expressions_of_size)
@composite
def policy_expression(draw, num_leaves):
left_leaves = draw(integers(min_value=1, max_value=num_leaves - 1))
right_leaves = num_leaves - left_leaves
left = draw(policy_expressions_of_size(left_leaves))
right = draw(policy_expressions_of_size(right_leaves))
gate = draw(gates())
return u'(' + u' '.join((left, gate, right)) + u')'
def attributes():
return text(min_size=1, alphabet=characters(whitelist_categories='L', max_codepoint=0x7e))
@composite
def inequalities(draw):
attr = draw(attributes())
oper = draw(inequality_operators())
numb = draw(integers(min_value=1))
return u' '.join((attr, oper, str(numb)))
def inequality_operators():
return sampled_from((u'<', u'>', u'<=', u'>='))
def gates():
return sampled_from((u'or', u'and'))
然后您可以准确选择您希望策略表达式的大小:
>>> policy_expressions.example()
'((((((oOjFo or (((cH and (Q or (uO > 18 and byy))) and kS) or pqKUUZ > 74)) and (gi or mwsrU <= 4115)) and qLkVSTqXZxgScTj) and (vNJ > 969 and (Drwvh or (((xhmsWhHpc or hQSMnfgyiYnblLFJ) or sesfHbQ) and jt)))) or xS) and ((V and (mArqYR or qY)) or (((uVf and bbtKUCnecMKjRJD > 18944) and nerVkPSs < 29292) and (UlOJebfbgcJz or (bxfVfjgmfulSB > 71 or (jqGLlr or (zQqj and zqUGwc < 24845)))))))'
>>>
>>> policy_expressions_of_size(1).example()
'Eo'
>>>
>>> policy_expressions_of_size(2).example()
'KJAitOKC > 18179'
>>> policy_expressions_of_size(10).example()
'(((htjdVy or (((XTfZil or (rqZw and DEOeER)) and xGVsdeQJLTJxLsC < 388312303) or LxLfUPljUTH)) or (Kb or EoipoYzjncAGKTE)) or bc)'
>>> policy_expressions_of_size(100).example()
'(((((CxySeUrNW or bZG) or (gzSUGgTG and (((V or n) or wqA) or veuTEnjGKwIpkDDDBiQkMwsNbxrBv))) or (((SKgQSXtAg or ChCHcEsVavy) and (((Yxj and xcCX) or QrILGAWxVKXWRb > 98817811688973569232860005374239659122) or JD <= 28510)) and KhrGfZciz > 4057857855522854443)) and (ZMIzFELKAKDMrH and (((MOmAZ and J <= 22052) or (Scy >= 17563 and (VCS and ((FFLa and EtZvqwNymnZNnjlREM) or pU)))) or A))) and ((((kaYzzIXIu and (lwos and (vp and GqG))) and ((Nh and lb) or ((TbNZWYOpYmj and (AQs or w)) or NjFYLBr > 228431293))) or ((((FTSXkXGZyKXD or zXeVEqNgkyXI) or mNGI) or ((cGOGK or gjcI) and DQzYonXszfSrZMB)) and JI > 3802)) or (((jIREd and IVzFB >= 28149) and (UdCBg < 20 or (VSGxr or XBuiS <= 1615))) and (rE > 10511139808015932 and ((((((((W and u) or yslVZ) or (eVGlz < 7033 or UiE)) and ((trOmArBc and Zx) or mPKva)) or ((qqDmKUpAnW or yvSkhTgqXQaLnxL) or Z)) or snXcMDhhf) and ((Wu or XSjbKdsZqEiXXvOb) and (DNZg and qv >= 7503))) and ((rnffxTLThwvw >= 24460 and ((oO or y <= 24926) and (NjM and vEHukii))) or ((((BTdpW and rP) or (rjUylCZwJzGobXZR or MNoBdEEIuLbTRvZHMb < 7958346708112664935)) and ((YU or gY >= 15498) and (s and GnOydthO > 103))) or ((caumKPjp < 27 and OQoFXscbD) or ((qaxYwfnelmetYqHKnatQ or P) and (ixzsvX and mYROpqoHAqeEy))))))))))'
关于python - 我如何重构这个递归策略表达式 Strategy 来参数化它的长度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55498549/
在大多数示例中,策略模式通常适用于压缩算法/编解码器算法,其中这些算法可能具有完全不同的代码。 但是,在我的例子中,我想重构为策略模式的统计算法有 50% 的代码相似或完全相同。我想知道人们如何处理这
git merge选项有什么区别--strategy-option(短-X)和--strategy(短-s)? 有很多关于 merge strategies 的问题.但是没有人解释这些选项之间的区别。
耦合策略和非耦合策略有什么区别? 谢谢回答。 最佳答案 当策略类知道上下文时,或者当客户端知道策略具体类时,策略是耦合的。 我们可以阅读 here (有一些小的修改): Usually each st
我是 hibernate 新手。我不明白以下两种主键生成策略: 身份 顺序 有人能解释一下这两者是如何工作的吗?这两者有什么区别? 最佳答案 引用 Java Persistence/Identity
我正在尝试调试我在编写内核时遇到的链接器问题。 问题是我有一个变量 SCAN_CODE_MAPPING我无法使用 - 它似乎是空的或其他东西。我可以通过改变我链接程序的方式来解决这个问题,但我不知道为
今天,当我找到一种方法来破坏我们程序中的某些功能时,我正在为客户做一些事情。 (代码实际上是遗留代码,它已经开发了大约 10 年,而我在这里只工作了大约一年。) 它不会导致错误或导致程序崩溃,但如果用
我想使用 Strategy Pattern为第三方类中的成员实现一些定制的格式化行为。我希望第三方只需注释类成员以指示他们是否应该使用格式策略(A、B 或 C),然后使用他们类的实例调用我的“格式”函
我试图将 CopyOnWriteArrayList 理解为我的代码: 我的代码是: public class AuditService { private CopyOnWriteArrayLi
在 git 中,我有一个 master 分支,我在其中开发我的应用程序。除了应用程序,我还需要开发它的演示版本。 我考虑过为 demo 创建一个永远不会 merge 到 master 中的单独分支。主
1、意图 定义一系列的算法,把它们一个个封装起来,并且使它们可相互替换。策略模式可以使算法可独立于使用它的客户而变化 策略模式变化的是算法 2、策略模式结构图 3、策略模式中主要
抽象策略(Strategy)角色:定义所有支持的算法的公共接口。通常是以一个接口或抽象来实现。Context使用这个接口来调用其ConcreteStrategy定义的算法。 具体策略(Concre
模板模式(Template Pattern)中,一个抽象类公开定义了执行它的方法的方式/模板。它的子类可以按需要重写方法实现,但调用将以抽象类中定义的方式进行 模板模式属于行为型模式 摘要 1、
我希望这是一个可以接受的问题。我理解递归的思维模式,我想考虑基本情况,然后是递归情况,但是对于一些更困难的 BST 问题,我只是画了空白,感觉就像我迷失了方向而没有一个好的方向。 例如,对于链表,似乎
是否有人考虑过为 Azure 存储队列实现策略,允许以任意顺序(除了先进先出)使消息出队。例如,有些人可能对 LIFO 感兴趣,有些人可能希望在不太重要的消息之前将“重要”消息出队,等等。 就我个人而
虽然多线程在某些情况下速度更快,但有时我们只想生成多个工作进程来完成工作。这样做的好处是,如果其中一个 worker 崩溃,主应用程序不会崩溃,而且用户不需要太担心互锁问题。 COM+ 的 Appli
我的应用程序将对我的对象有不同的策略。实现这一目标的最佳方法是什么?理想情况下,我想从某些关系数据库动态加载策略实现。但我不知道该怎么做。最好的方法是什么? 例如,假设我们要将策略 Strategy1
上下文 首先,感谢您的假设。它既非常强大又非常有用! 我编写了一个假设策略来生成以下形式的单调(ANDS 和 OR)策略表达式: (A and (B or C)) 这可以被认为是一个树结构,其中 A、
问题 我有一个大类(大约 1500 LOC),它使用不同的“策略”将数据从一个对象转换到另一个对象。我在这里有那个类的表示: public class FooService implements Fo
我正在阅读 Effective Java。在讨论使用函数对象作为策略的部分中,存在以下段落。 Because the strategy interface serves as a type for a
我是 C# 4.0 和 WPF 的新手,我正准备开始一个新的应用程序。 来自 C++/MFC 世界,我对测试与最新技术一起使用的策略很感兴趣。 例如: 单元测试 功能测试 界面测试 其他? 如有任何建
我是一名优秀的程序员,十分优秀!