小编Muk*_*215的帖子

AttributeError:使用pytest的monkeypatch时

src/mainDir/mainFile.py

mainFile.py 的内容

import src.tempDir.tempFile as temp

data = 'someData'
def foo(self):
    ans = temp.boo(data)
    return ans
Run Code Online (Sandbox Code Playgroud)

src/tempDir/tempFile.py

def boo(data):

   ans = data
   return ans
Run Code Online (Sandbox Code Playgroud)

现在我想测试foo()并在方法中src/tests/test_mainFile.py模拟temp.boo(data)方法foo()

 import src.mainDir.mainFile as mainFunc

  testData = 'testData'
  def test_foo(monkeypatch):
     monkeypatch.setattr('src.tempDir.tempFile', 'boo', testData)
     ans = mainFunc.foo()
     assert ans == testData
Run Code Online (Sandbox Code Playgroud)

但我收到错误

属性错误:“src.tempDir.tempFile”没有属性“boo”

我期望 ans = testData.

我想知道我是否正确地模拟了我的 tempDir.boo() 方法,或者我应该使用 pytest 的模拟程序而不是 Monkeypatch。

python monkeypatching pytest

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

have_selector 测试失败

我遇到了问题,包括 rspec 的 should have_selector 问题:

这是我的代码:

describe "GET 'home'" do
  it "returns http success" do
    get 'home'
    expect(response).to be_success
  end

  it "should have the right title" do
    should have_selector("title", 
          :content => "Ruby on Rails Tutorial Sample App | Home")
  end
end
Run Code Online (Sandbox Code Playgroud)

我在顶部添加了以下内容:

RSpec.describe PagesController, :type => :controller do
  render_views
Run Code Online (Sandbox Code Playgroud)

我的html5有以下内容:

<title>Ruby on Rails Tutorial Sample App | Home</title>
Run Code Online (Sandbox Code Playgroud)

我收到一条错误消息:

失败:

1) PagesController GET 'home' should have the right title
 Failure/Error: should have_selector("title",
   expected #<PagesController:0x007fceef586a90> to respond to `has_selector?`
 # …
Run Code Online (Sandbox Code Playgroud)

rspec ruby-on-rails ruby-on-rails-4

4
推荐指数
1
解决办法
2115
查看次数

使用Cowplot使用paste0获得绘图列表

我从1:10 ggplots所谓的名单plot_1plot_2...... plot_10

我想使用Cowplot一起显示所有图。

如何plot.grid()调用所有地块?即我想写点东西

plot.grid(paste0("plot",1:10)) 
Run Code Online (Sandbox Code Playgroud)

但这不起作用-我收到错误消息:

ggplot_to_gtable(x)中的错误:参数必须为“ ggplot”或“ gtable”类*

r paste cowplot

3
推荐指数
1
解决办法
1760
查看次数

等待用户单击而不是页面加载以启动功能

我正在尝试制作倒计时时钟,当用户点击按钮时,它会启动计时器.但是,现在,只要页面加载,倒计时就会开始.我怎么能等到用户真正按下id='startCountdown'按钮才能启动countdown.我是JavaScript新手,非常感谢一些反馈.

.js代码:

// Wait for webpage to load
$(document).ready(function(){
  var sessionTime = 1;
  var breakTime = 5;

  // Update session and break times with pre-defined variables
  $('#session-time').text(sessionTime);
  $('#rest-time').text(breakTime);

  // jQuery click functions for increasing and decreasing time
  $('#decrease-session').on('click', downSession);
  $('#increase-session').on('click', upSession);
  $('#decrease-rest').on('click', downBreak);
  $('#increase-rest').on('click', upBreak);


  // Decrease the length of each session
  function downSession() {
    sessionTime--;
    $('#session-time').text(sessionTime);
    $('#countdown-time').text(sessionTime);
  }

  // Increase the length of each session
  function upSession() {
    sessionTime++;
    $('#session-time').text(sessionTime);
    $('#countdown-time').text(sessionTime);
  }

  // Decrease the …
Run Code Online (Sandbox Code Playgroud)

javascript jquery

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

Scikit-learn ValueError:使用混淆矩阵时不支持未知

我使用 fusion_matrix 模块来可视化类预测结果与实际值的比较。

val= ... #shape (3000,1,30) dtype float32
pred = ... #shape (3000,1,30) dtype float32

cnf_matrix = confusion_matrix(val, pred) #ERROR HERE
Run Code Online (Sandbox Code Playgroud)

我收到这个错误:

回溯(最近一次调用最后一次):文件“vis.py”,第 757 行,在 cnf_matrix = fusion_matrix(y_test, y_pred) 文件“C:\Anaconda\envs\nn35\lib\site-packages\sklearn\metrics\classification. py”,第 240 行,confusion_matrix y_type, y_true, y_pred = _check_targets(y_true, y_pred) 文件“C:\Anaconda\envs\nn35\lib\site-packages\sklearn\metrics\classification.py”,第 89 行,在_check_targets raise ValueError("不支持{0}".format(y_type)) ValueError: 不支持未知

我做错了什么?

scikit-learn

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

使用幂函数时float = int

我试图运行这个,似乎无法找出它为什么不会运行.先感谢您.请知道我是新人.

#include <stdio.h>
#include <math.h>

int main(void)
{
float IR;
float invested;
float time;

printf("Please enter the following to be calculated:");
scanf("Interest rate: %f\n", &IR);
scanf("Amount invested: %f\n", &invested);
scanf("Time: %f\n", &time);

float ans;
ans = (invested(((float)1+((IR)^(time)))));

printf("%f", ans);
}
Run Code Online (Sandbox Code Playgroud)

c floating-point

0
推荐指数
1
解决办法
192
查看次数