小编est*_*tan的帖子

在Python单元测试中修补多个方法的首选方法

在测试对我的单元测试中的第四个方法()的调用之前_send_reply,我需要使用mock方法修补三个方法(,_reset_watchdog_handle_set_watchdog)_handle_command.

从查看模拟包的文档,有几种方法可以解决它:

随着patch.multiple作为装饰

@patch.multiple(MBG120Simulator,
                _send_reply=DEFAULT,
                _reset_watchdog=DEFAULT,
                _handle_set_watchdog=DEFAULT,
                autospec=True)
def test_handle_command_too_short_v1(self,
                                     _send_reply,
                                     _reset_watchdog,
                                     _handle_set_watchdog):
    simulator = MBG120Simulator()
    simulator._handle_command('XA99')
    _send_reply.assert_called_once_with(simulator, 'X?')
    self.assertFalse(_reset_watchdog.called)
    self.assertFalse(_handle_set_watchdog.called)
    simulator.stop()
Run Code Online (Sandbox Code Playgroud)

随着patch.multiple如上下文管理器

def test_handle_command_too_short_v2(self):
    simulator = MBG120Simulator()

    with patch.multiple(simulator,
                        _send_reply=DEFAULT,
                        _reset_watchdog=DEFAULT,
                        _handle_set_watchdog=DEFAULT,
                        autospec=True) as mocks:
        simulator._handle_command('XA99')
        mocks['_send_reply'].assert_called_once_with('X?')
        self.assertFalse(mocks['_reset_watchdog'].called)
        self.assertFalse(mocks['_handle_set_watchdog'].called)
        simulator.stop()
Run Code Online (Sandbox Code Playgroud)

有多个patch.objectdecoratorations

@patch.object(MBG120Simulator, '_send_reply', autospec=True)
@patch.object(MBG120Simulator, '_reset_watchdog', autospec=True)
@patch.object(MBG120Simulator, '_handle_set_watchdog', autospec=True)
def test_handle_command_too_short_v3(self,
                                     _handle_set_watchdog_mock,
                                     _reset_watchdog_mock,
                                     _send_reply_mock):
    simulator = MBG120Simulator()
    simulator._handle_command('XA99')
    _send_reply_mock.assert_called_once_with(simulator, 'X?')
    self.assertFalse(_reset_watchdog_mock.called)
    self.assertFalse(_handle_set_watchdog_mock.called) …
Run Code Online (Sandbox Code Playgroud)

python unit-testing mocking

17
推荐指数
1
解决办法
1万
查看次数

检查双倍是否可以被C中的另一个双重整除?

如何检查双x是否可以被C中的另一个双y整除?使用整数我只会使用模数,但是用双精度执行它的正确/最佳方法是什么?

我知道浮点数带有不精确性,但我从标准输入得到了双倍.也许我不应该直接扫描它,而是两个整数,但是从那时起我会去哪里?

c floating-point

16
推荐指数
2
解决办法
9770
查看次数

自定义`sphinx-apidoc`的模板

我一直在使用最近尝试狮身人面像,apidoc狮身人面像,以帮助生成从Python项目的API狮身人面像的具体reStructuredText的.

但是,我得到的结果是:

<code>sphinx-api</code>用于其输出?具体来说,我想:</p>

<ul>
<li>摆脱所有

  • 我的__init__.py文件中的docstring结果直接显示在包下面,因此,如果我单击包名,我首先看到的是包文档.目前,本文档位于每个软件包部分末尾略微奇怪的"模块内容"标题下.
  • 我认为"Submodules"和"Subpackages"标题是多余的,因为包/模块的正常标题是"xxx.yyy package"和"xxx.yyy.zzz module".

    我希望上面这个小例子的结构是

    • orexplore.components包
      • orexplore.components.mbg120模块
    • orexplore.simulators包
      • orexplore.simulators.test包
        • orexplore.simulators.test.mbg120模块
      • orexplore.simulators.mbg120模块

    点击包的地方,我在页面上看到的第一件事就是包文档.

    或者甚至只是

    • orexplore.components
      • orexplore.components.mbg120
    • orexplore.simulators
      • orexplore.simulators.test
        • orexplore.simulators.test.mbg120
    • orexplore.simulators.mbg120

    如果有一些方法可以在视觉上区分包/模块(颜色?会徽?)而不是相当冗长的"包"和"模块".

    python python-sphinx

    14
    推荐指数
    3
    解决办法
    3323
    查看次数

    将alpha beta修剪的minimax转换为negamax

    我写了一个极小与算法的alpha beta剪枝的游戏跳棋,现在我想使用重写它negamax方法.我期待这两者是等价的,因为negamax只是一种编写极小极大的技巧.但由于某种原因,我的两种算法表现不同.当我在相同的输入上运行它们时,negamax版本似乎评估了更多的状态,所以我认为alpha beta修剪必定是错误的.

    下面的代码显示了算法(minimaxnegamax函数),并在底部显示了play我称之为的函数.该evaluate函数是我用来评估两种算法中的状态的基本启发式算法.

    任何有关发现错误的帮助都会受到很多关注.

    #include "player.hpp"
    #include <algorithm>
    #include <limits>
    #include <cstdlib>
    
    namespace checkers
    {
    
    int evaluatedStates = 0;
    
    int evaluate(const GameState &state)
    {
        // FIXME: Improve heuristics.
        int redScore = 0;
        int whiteScore = 0;
        int piece = 0;
        for (int i = 1; i <= 32; ++i)
        {
            piece = state.at(i);
            if (piece & CELL_RED) {
                ++redScore;
                if (piece & CELL_KING)
                    redScore += …
    Run Code Online (Sandbox Code Playgroud)

    c++ algorithm minimax alpha-beta-pruning

    5
    推荐指数
    1
    解决办法
    7737
    查看次数

    如何改进 Pollard 的 rho 算法来处理半大素数的乘积?

    下面是我对 Pollard 质因数分解的 rho 算法的实现:

    #include <vector>
    #include <queue>
    
    #include <gmpxx.h>
    
    // Interface to the GMP random number functions.
    gmp_randclass rng(gmp_randinit_default);
    
    // Returns a divisor of N using Pollard's rho method.
    mpz_class getDivisor(const mpz_class &N)
    {
        mpz_class c = rng.get_z_range(N);
        mpz_class x = rng.get_z_range(N);
        mpz_class y = x;
        mpz_class d = 1;
        mpz_class z;
    
        while (d == 1) {
            x = (x*x + c) % N;
            y = (y*y + c) % N;
            y = (y*y + c) % N; …
    Run Code Online (Sandbox Code Playgroud)

    c++ algorithm prime-factoring

    5
    推荐指数
    1
    解决办法
    2831
    查看次数

    在多个视图中固定大小的QGraphicsItems?

    我正在使用Qt的图形视图框架可视化图形。看起来像这样:

    程序中的示例视图

    现在,我希望将顶点绘制为较小的实心圆,使其相对于视图缩放保持恒定。目前,我不通过将顶点作为场景中的项来实现此目的,而是在视图的drawForeground(...)函数中绘制它们,如下所示:

    void View::drawForeground(QPainter *painter, const QRectF& rect) {
        painter->setMatrixEnabled(false);
        painter->setPen(Qt::NoPen);
        painter->setBrush(Qt::black);
        painter->setRenderHint(QPainter::Antialiasing);
    
        // Draw points.
        const QRectF biggerRect = rect.adjusted(-100, -100, 100, 100);
        const float radius = 2.0;
        foreach (const QPointF& point, m_points) {
            if (biggerRect.contains(point)) {
                painter->drawEllipse(QPointF(mapFromScene(point)), radius, radius);
            }
        }
    
        // ...
    }
    
    Run Code Online (Sandbox Code Playgroud)

    但这是糊涂的,而且是错误的,因为从概念上讲,这些顶点是我场景的一部分,并且应该是我场景中的项目QGraphicsScene。另外,例如,我可能想暂时隐藏一组顶点,或对其进行变换,如果它们是场景中的适当项目,则这样做会容易得多。最后但并非最不重要的一点是,让视图绘制点会破坏模型/视图的分离,并迫使视图知道它们正在显示的点(可能有多个视图)。

    使用这种方法时,render(...)在查看时,顶点(非项目)和边(项目)的相对位置也遇到了麻烦,这QSvgGenerator可能是由于我禁用画家变换的技巧而导致的。

    因此,我的问题是:QGraphicsItem在显示场景的每个视图中,如何将顶点添加到场景中,同时相对于视图仍保持其大小不变?

    我尝试在项目上设置QGraphicsItem :: ItemIgnoresTransformations标志。文档中有关于该标志的说明:

    该项目将忽略继承的转换(即,其位置仍固定在其父项上,但是将忽略父项或视图的旋转,缩放或剪切转换)。此标志对于保持文本标签项目水平和不缩放很有用,因此,如果对视图进行了转换,它们仍将可读。设置后,项目的视图几何和场景几何将分别维护。您必须调用deviceTransform()来映射坐标并检测视图中的碰撞。默认情况下,此标志是禁用的。这个标志是在Qt 4.3中引入的。

    但是设置此标志会导致所有视图转换都被忽略,因此当我放大时,点将不再显得进一步分开,这不是我想要的。我只希望项目大小(以屏幕像素为单位)保持不变。

    由于Merlin069要求这样做:我尝试使用此标志时使用的代码只是将每个顶点添加到场景中,如下所示:

    QGraphicsEllipseItem *item = scene->addEllipse(x - 2.0, y - 2.0, 4.0, 4.0);
    item->setPen(Qt::NoPen); …
    Run Code Online (Sandbox Code Playgroud)

    c++ qt qgraphicsview

    5
    推荐指数
    1
    解决办法
    947
    查看次数

    用于布尔/条件表达式的 ANTLR v3 语法

    我首先尝试为表达式创建语法,例如:

    (foo = bar or (bar = "bar" and baz = 45.43)) and test = true
    
    Run Code Online (Sandbox Code Playgroud)

    到目前为止,我的语法如下所示:

    grammar filter;
    
    tokens {
        TRUE = 'true';
        FALSE = 'false';
        AND = 'and';
        OR = 'or';
        LT = '<';
        GT = '>';
        EQ = '=';
        NEQ = '!=';
        PATHSEP = '/';
        LBRACK = '[';
        RBRACK = ']';
        LPAREN = '(';
        RPAREN = ')';
    }
    
    expression : or_expression EOF;
    
    or_expression : and_expression (OR or_expression)*;
    
    and_expression : term (AND term)*;
    
    term : atom ( operator …
    Run Code Online (Sandbox Code Playgroud)

    grammar antlr3

    2
    推荐指数
    1
    解决办法
    3124
    查看次数