小编seb*_*seb的帖子

to_sql 时“并非所有参数都在字符串格式化期间转换”

我正在尝试以下一段代码,这是我在 2016 年的一本书中找到的:

import MySQLdb
import pandas as pd

# database setup omitted for the sake of brevity

nr_customers = 100
colnames = ["movie%i" %i for i in range(1, 33)]
pd.np.random.seed(2015)
generated_customers = pd.np.random.randint(0,2,32 * nr_customers).reshape(nr_customers,32)
data = pd.DataFrame(generated_customers, columns = list(colnames))
data.to_sql('cust',mc,index=True,if_exists='replace',index_label='cust_id')
Run Code Online (Sandbox Code Playgroud)

它只是给了我以下错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~/anaconda3/envs/TestEnv/lib/python3.7/site-packages/MySQLdb/cursors.py in execute(self, query, args)
    242             try:
--> 243                 query = query % args
    244             except TypeError as m:

TypeError: not all arguments converted during string formatting

During handling …
Run Code Online (Sandbox Code Playgroud)

python mysql-python pandas

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

运行测试时 ElasticSearch Rails resource_already_exists_exception

我正在尝试运行我的一个测试,该测试进行搜索,试图断言搜索结果中包含记录,但与此同时,我收到一个Elasticsearch::Transport::Transport::Errors::BadRequest错误:

SearchTest#test_simple_test_returns_product:
Elasticsearch::Transport::Transport::Errors::BadRequest: [400] 

{
  "error":{
    "root_cause":[
      {
        "type":"resource_already_exists_exception",
        "reason":"index [app_application_test_products/FTt1YC6eQrCw2XwJuqjmDw] already exists",
        "index_uuid":"FTt1YC6eQrCw2XwJuqjmDw",
        "index":"app_application_test_products"
      }
    ],
    "type":"resource_already_exists_exception",
    "reason":"index [app_application_test_products/FTt1YC6eQrCw2XwJuqjmDw] already exists",
    "index_uuid":"FTt1YC6eQrCw2XwJuqjmDw",
    "index":"app_application_test_products"
  },
  "status":400
}
Run Code Online (Sandbox Code Playgroud)

当我在开发中执行搜索时,它按预期工作,但在测试中抛出此类错误,在测试中我添加了导入和索引刷新,仅此而已:

class SearchTest < ActiveSupport::TestCase
  setup do
    Product.import force: true
    Product.__elasticsearch__.refresh_index!
  end

  test "simple test returns product" do
    product = products(:one)
    I18n.locale = product.market.lang
    search = Search.new(
      category: product.category.custom_slug,
      page: 1,
      market_id: product.market_id,
      status: "active",
      seed: Date.today.to_time.to_i
    )
    assert_includes search.results.records, products(:one)
    assert_includes search.results.records, products(:two)
    assert_not_includes search.results.records, products(:three)
  end
end
Run Code Online (Sandbox Code Playgroud)

任何帮助以及改进代码的提示都将受到赞赏。

我在用着: …

ruby ruby-on-rails minitest elasticsearch elasticsearch-rails

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

模拟输入上的点击事件 - JavaScript

我正在尝试模拟对输入标签的点击,通过点击anchor标签,这样我可以隐藏输入并将图像包裹在锚标签内。

这可以使用 jQuery 触发器函数工作,但我无法仅使用“普通”Javascript:

jQuery 版本:

let fake = $('.fake')
fake.click(function(e) {
  e.preventDefault();
  $('#user_avatar').trigger('click');
})
Run Code Online (Sandbox Code Playgroud)
#user_avatar { display: none; }
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="file" name="file_field" id="user_avatar">
<a href="#" class="fake">
  <img src="https://fthmb.tqn.com/65lNzIRNfZY4xY02D17b1RcGvso=/960x0/filters:no_upscale()/kitten-looking-at-camera-521981437-57d840213df78c583374be3b.jpg" width="320" height="240">
</a>
Run Code Online (Sandbox Code Playgroud)

使用new Event和的 JavaScript 版本dispatchEvent

let fake = document.querySelector('.fake');
fake.addEventListener('click', function(e) {
  e.preventDefault();
  console.log('testing');
  let clickEvent = new Event('click');
document.getElementById('user_avatar').dispatchEvent(clickEvent)
})
Run Code Online (Sandbox Code Playgroud)
#user_avatar { display: none; }
Run Code Online (Sandbox Code Playgroud)
<input type="file" name="file_field" id="user_avatar">
<a href="#" class="fake">
  <img src="https://fthmb.tqn.com/65lNzIRNfZY4xY02D17b1RcGvso=/960x0/filters:no_upscale()/kitten-looking-at-camera-521981437-57d840213df78c583374be3b.jpg" width="320" height="240">
</a>
Run Code Online (Sandbox Code Playgroud)

已呈现 console.log,但未分派事件,我做错了什么?

html javascript

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

该模块导入错误

在尝试导入Date模块并在简单的Elm文件中使用它时,我收到以下错误:

-- UNKNOWN IMPORT ---------------------------- app/javascript/CountdownTimer.elm

The CountdownTimer module has a bad import:

    import Date

I cannot find that module! Is there a typo in the module name?

The "source-directories" field of your elm.json tells me to only look in the src
directory, but it is not there. Maybe it is in a package that is not installed
yet?
Run Code Online (Sandbox Code Playgroud)

CountdownTimer是文件和模块的名称,取自这里,似乎工作正常,但不适合我.

我正在使用Elm 0.19.0和Rails 6.0.0beta1,这似乎不是一个约束,因为如果我在任何地方进入Elm REPL,并尝试导入日期,我面临同样的错误:

> import Date
-- UNKNOWN IMPORT ---------------------------------------------------------- elm

The Elm_Repl module has a bad …
Run Code Online (Sandbox Code Playgroud)

date elm

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

Rails Enum 返回整数值,而不是字符串表示形式

我有一个表,其中包含一个基于整数的列(状态),我将其用于 Rails 模型中的枚举属性。

在做的那一刻:

Post.select(:id, ..., :status)
Run Code Online (Sandbox Code Playgroud)

定义为:

enum status: { inactive: 0, active: 1, ... }
Run Code Online (Sandbox Code Playgroud)

它按预期返回所有内容,但状态列在其字符串值中返回为inactiveactive等。但我需要它作为整数。

我怎样才能得到它?

我目前只是使用ActiveRecord::Base.connection.execute并传递原始查询:

ActiveRecord::Base.connection.execute('select id, ..., status from posts')
Run Code Online (Sandbox Code Playgroud)

postgresql ruby-on-rails ruby-on-rails-6

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

Haskell Aeson 返回空对象

如果不是Nothing,我试图返回一个JSON数据表示,如果Nothing则返回一个空的JSON对象;

我知道我可以做到:

encode ()
-- "[]"
Run Code Online (Sandbox Code Playgroud)

但是现在我想要一个空对象 ( "{}")。

我有这个,它可以根据给定的字段生成 JSON:

? data Person = Person { id :: Integer, height :: Float } deriving (Show)
? instance ToJSON Person where toJSON (Person { id = id, height = height }) = object [ "id" .= id, "height" .= height ]
? encode (Person 1 72.8)
-- "{\"height\":72.8,\"id\":1}"
Run Code Online (Sandbox Code Playgroud)

但最终没有 Person 将用 Nothing 表示,如果我这样做,encode (Nothing)我会收到一个错误:

<interactive>:11:1: error:
    • Ambiguous type variable ‘a0’ arising from a use of ‘encode’ …
Run Code Online (Sandbox Code Playgroud)

haskell ihp

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

Elasticsearch 6.3.2 术语匹配空数组“加上”其他

在我的数据库中,一个帖子可以有零 (0) 个或多个类别表示为一个数组。

当我进行查询时,要查看这些类别,传递一些值:

{
  "query": {
    "bool": {
      "should": {
        "terms": {
          "categories": ["First", "Second", "And so on"]
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

它运作良好,我有我期待的记录。但是当我想包含这些帖子时问题就来了,其中类别是一个空数组 ([])。

我现在正在从旧版本的 ES (1.4.5) 升级到版本 6.3.2,并且这段代码是使用“missing”制作的,它已被弃用。

我试过更改映射添加著名的"null_value": "NULL",然后查询,但没有奏效。还尝试了 should 和 must_not 的组合,如升级“missing”的建议,但没有奏效。

我怎样才能做到这一点?这意味着如果我已经索引:

Post.new(id: 1, title: '1st', categories: [])
Post.new(id: 2, title: '2nd', categories: ['news', 'tv'])
Post.new(id: 3, title: '3rd', categories: ['tv', 'trending'])
Post.new(id: 4, title: '4th', categories: ['movies'])
Post.new(id: 5, title: '5th', categories: ['technology', 'music'])
Run Code Online (Sandbox Code Playgroud)

结果应返回帖子编号 1, 2 y 3 - 具有“新闻”、“电视”或空数组作为类别的帖子。

ruby-on-rails elasticsearch elasticsearch-rails elasticsearch-ruby

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