小编lll*_*lll的帖子

在使用axios进行经过身份验证的请求时,Jest返回"网络错误"

这对我来说似乎有点奇怪.我正在尝试使用Jest测试实际(即真实网络)请求.

这些是经过测试的场景:

  • 测试没有标题的外部API(fixer.io) <---这是有效的
  • 使用标题测试本地API服务器 <---这不起作用
  • 使用来自node终端的 标头测试相同的本地API <---这是有效的

这种行为背后的原因是什么?什么是解决方案?

//This WORKS
test('testing no headers', () => {
  return axios.get('http://api.fixer.io/latest')
        .then( res => console.log(res) )
});

//This DOES NOT work
test('testing no headers', () => {
  return axios.get('http://localhost:3000/users/4/profile', 
                      {headers:{authorization:`Bearer ${mytoken}`}})
        .then( res => console.log(res) )
});

//...

//Node Terminal
//This WORKS
> axios.get('http://localhost:3000/users/4/profile', 
                   {headers:{authorization:`Bearer ${mytoken}`}})
        .then( res => console.log(res) )
Run Code Online (Sandbox Code Playgroud)

javascript testing ecmascript-6 jestjs axios

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

用序列创建的numpy数组

我正在从MATLAB到scipy(+ numpy)+ matplotlib的过渡之旅.在实施某些事情时我一直遇到问题.我想在三个不同的部分创建一个简单的矢量数组.在MATLAB中,我会做类似的事情:

vector=[0.2,1:60,60.8];
Run Code Online (Sandbox Code Playgroud)

这导致62个位置的一维阵列.我正在尝试使用scipy来实现它.我现在最接近的是:

a=[[0.2],linspace(1,60,60),[60.8]]
Run Code Online (Sandbox Code Playgroud)

然而,这会创建一个列表,而不是一个数组,因此我无法将其重新整形为矢量数组.但是,当我这样做时,我得到一个错误

a=array([[0.2],linspace(1,60,60),[60.8]])
ValueError: setting an array element with a sequence.
Run Code Online (Sandbox Code Playgroud)

我相信我的主要障碍是我无法弄清楚如何在MATLAB中翻译这个简单的操作:

a=[1:2:20];
Run Code Online (Sandbox Code Playgroud)

笨拙 我知道如何访问数组中的位置,尽管不是在创建序列时.任何帮助将不胜感激,谢谢!

python arrays numpy sequence scipy

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

rails before_create not trigering方法

我正在尝试在保存实例之前触发方法.我有这个User型号:

class User < ActiveRecord::Base

    has_secure_password
    attr_accessible :name, :first_surname,:second_surname,:email, :password, :password_confirmation,:number,:credit

    before_save{ self.email.downcase! }
    before_create :generate_auth_token

    default_scope order: 'users.created_at ASC'

    has_many :operations

    def consume(what,price,agent)
        self.operations.create(category:what,price:price, agent_id:agent)
    end
end
Run Code Online (Sandbox Code Playgroud)

而每User有许多Operation(注意使用的通过debuger binding.pry:

class Operation < ActiveRecord::Base
  attr_accessible :agent_id, :comment, :postcredit, :precredit, :category, :user_id,:price
  validates_presence_of :user_id
  validates_presence_of :agent_id
  validates_presence_of :price
  validates_presence_of :precredit
  validates_presence_of :postcredit
  validates_presence_of :category
  #before_save :compute_prices, :on => :create
  before_create :compute_prices
  belongs_to :user

  private

  def compute_prices
      binding.pry
      user=User.find(self.user_id)
      self.precredit=user.credit
      #select whether adding …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails callback

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

运行脚本后保持图形"保持"状态

我有这个Python代码:

from pylab import *
from numpy import *

time=linspace(-pi,pi,10000)
ycos=cos(time)
ysin=sin(time)

plot(time,ycos)
plot(time,ysin)

show()
Run Code Online (Sandbox Code Playgroud)

如果我通过Ipython终端执行所有这些步骤,我可以保持图形打开并与之交互.但是,如果我通过$python script.py图形运行脚本打开并立即关闭.

我怎么能有与Ipython终端相同的行为但是当作为脚本运行时?

python numpy matplotlib

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

Flexbox的.避免高于父母的孩子(栏目方向)

我正在使用flexbox为一个站点搭建基本UI.与通常的电子邮件布局非常相似.

出于某种原因,第二个孩子(类.main-body,海藻绿柱,layouted Flexbox的容器的颜色)( .content-main,李子色)比其母公司,这一点我一个更大的高度希望它.

我制作了一个codepen代码段来显示这种行为.

html,
body {
  width: 100%;
  height: 100%;
  background: lightblue;
  box-sizing: border-box;
}
* {
  box-sizing: border-box;
}
.container {
  height: inherit;
  background: PeachPuff;
  padding: 1em;
  display: flex;
  max-height: 500px;
}
.sidebar {
  flex: 0 1 15%;
  background: khaki;
}
.content {
  flex: 1 1 auto;
  background: plum;
  padding: 0.8em;
  display: flex;
  flex-direction: column;
}
.content-header {
  background: lightgreen;
  padding: 0.3em;
  flex: 0 0 7%; …
Run Code Online (Sandbox Code Playgroud)

html css flexbox

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

元组作为多维数组的索引

我找到了一个与我非常相似的问题,但不完全相同.这一个:这里 但是在ntimes的情况下,数组的大小与元组指向的维度的数量相匹配.在我的情况下,我有一个4维数组和一个二维元组,就像这样:

from numpy.random import rand
big_array=rand(3,3,4,5)
tup=(2,2)
Run Code Online (Sandbox Code Playgroud)

我想使用元组作为前两个维度的索引,并手动索引最后两个维度.就像是:

big_array[tup,3,2]
Run Code Online (Sandbox Code Playgroud)

但是,我沿着第四维获得了索引= 2的第一维的重复(因为它在技术上没有被索引).这是因为这个索引将双索引解释为第一维而不是每个维的一个值,

eg. 
| dim 0:(index 2 AND index 2) , dim 1:(index 3), dim 2:(index 2), dim 3:(no index)|
instead of 
|dim 0(index 2), dim 1(index 2), dim 2:(index 3), dim 3:(index 2)|.
Run Code Online (Sandbox Code Playgroud)

那我怎么能"打开"这个元组呢?有任何想法吗?谢谢!

python arrays tuples numpy scipy

6
推荐指数
2
解决办法
6987
查看次数

理解"attribute_will_change!" 方法

我想覆盖这个store_accessor吸气剂.哪个可以在这里找到.代码在这里:

# File activerecord/lib/active_record/store.rb, line 74
      def store_accessor(store_attribute, *keys)
        keys = keys.flatten

        _store_accessors_module.module_eval do
          keys.each do |key|
            define_method("#{key}=") do |value|
              write_store_attribute(store_attribute, key, value)
            end

            define_method(key) do
              read_store_attribute(store_attribute, key)
            end
          end
        end

        self.stored_attributes[store_attribute] ||= []
        self.stored_attributes[store_attribute] |= keys
      end
Run Code Online (Sandbox Code Playgroud)

我实现了我所追求的功能,但是如果我还要覆盖setter,那么有一种方法对我来说并不清楚,这是在write_store_attribute(...)方法中(在这里找到).

代码在这里:

# File activerecord/lib/active_record/store.rb, line 108
      def write_store_attribute(store_attribute, key, value)
        attribute = initialize_store_attribute(store_attribute)
        if value != attribute[key]
          send :"#{store_attribute}_will_change!"
          attribute[key] = value
        end
      end
Run Code Online (Sandbox Code Playgroud)

我不明白的方法是"#{whatever_store_att}_will_change!".

如果我要覆盖我会使用的设置器update_attributesupdate_column.这种方法是否使分配attribute[key]=value …

ruby postgresql activerecord ruby-on-rails hstore

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

node.js脚本无法获取静态文件

我有一个带有少量容器的基本HTML index.html,我在文件的部分添加了bootstrapfont awesome文件夹<head>.如:

<link href="../bootstrap/css/bootstrap.css" rel="stylesheet">
<link href="../bootstrap/css/bootstrap-responsive.css" rel="stylesheet">
<link href="font-awesome/css/font-awesome.css" rel="stylesheet">
Run Code Online (Sandbox Code Playgroud)

然后,我编写了一个web.js脚本来首先初始化服务器,然后以这种方式添加包含静态文件的文件夹:

var express = require('express');
var app = express();
var fs = require('fs');

var buf = fs.readFileSync("index.html");
app.use(express.logger());

app.get('/', function(request, response) {
  response.send(buf.toString());
});

app.configure(function(){
    app.use(express.static(__dirname + '/assets'));
    app.use(express.static(__dirname + '/bootstrap'));
    app.use(express.static(__dirname + '/font-awesome'));
});

var port = process.env.PORT || 8080;
app.listen(port, function() {
  console.log("Listening on " + port);
});
Run Code Online (Sandbox Code Playgroud)

但是,当我去的时候,http://localhost:8080我从GET命令中获取404错误,试图获取bootstrap.css等等.有什么帮助吗?我尝试了从stackoverflow获得的不同脚本,但我似乎无法做到正确.谢谢!

html node.js express

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

Ruby/Rails 将字符串转换为类属性

假设我有一个 class Article,这样:

class Article

  attr_accessor :title, :author

  def initialize(title, author)
    @title = title
    @author= author
  end

end
Run Code Online (Sandbox Code Playgroud)

此外,变量atribString包含属性名称的。我怎么能把这个字符串变成一个变量来用作吸气剂?

a = Article.new
atrib='title'
puts a.eval(atrib)     # <---- I want to do this
Run Code Online (Sandbox Code Playgroud)

扩展

假设我现在有一篇Array文章,我想按标题对它们进行排序。有没有办法使用以下方式进行紧凑版本&

col = Article[0..10]
sorted_one = col.sort_by{|a| a.try('title') }   #This works
sorted_two = col.sort_by(&:try('title'))   #This does not work
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails

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

FactoryGirl.create不会增加`.count`字段

我和Nick Rutten在这个线程Rails教程中遇到了同样的问题:RSpec测试解耦.我怀疑问题是FactoryGirl.create()保存到数据库,感谢@prusswan我修复了它.但是我不明白为什么这些puts并不表示不同User.count.

这是我感兴趣的片段:

describe "with valid information" do

  puts "Number of users pre-Factory:" + User.count.to_s
  let(:user){FactoryGirl.create(:user)}
  puts "Number of users post-Factory:" + User.count.to_s

  (...)

end
Run Code Online (Sandbox Code Playgroud)

我明白了:

Number of users pre-Factory:0
Number of users post-Factory:0

但我不应该得到吗?:

Number of users pre-Factory:0
Number of users post-Factory:1

顺便说一下,工厂的定义(尽管现在不那么重要)如下:

FactoryGirl.define do
  factory :user do
    name "Example User"
    email "user@example.com"
    password "foobar"
    password_confirmation "foobar"
  end
end
Run Code Online (Sandbox Code Playgroud)

我一直试图理解为什么计数器没有增加,这不是它开始增加的问题吗?谢谢

使用@ derekyau的建议编辑:

现在我按顺序排列:

it "print counter before let"
  puts …
Run Code Online (Sandbox Code Playgroud)

rspec ruby-on-rails factory-bot

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