小编Str*_*yer的帖子

为什么('0'?'a':'b')的行为不同于('0'== true?'a':'b')

为什么以下两个语句的结果不同?

('0' ? 'a' : 'b') /* -> 'a' */
('0' == true ? 'a' : 'b') /* -> 'b' */
Run Code Online (Sandbox Code Playgroud)

jsFiddle测试用例

编辑:

我应该补充一点,我怀疑'0'第一个语句要转换为boolean来进行比较 - 这应该与"'0'== true"完全相同"显然这不是真的.

javascript

111
推荐指数
2
解决办法
6936
查看次数

使用Qt编译项目后,将文件复制到构建目录

我有一个文件"settings.ini",它需要驻留在Qt可执行文件旁边.

我可以在Qt Creator中添加一个自定义构建步骤,调用类似这样的东西:

copy %{sourceDir}/settings.ini %{buildDir}/settings.ini
Run Code Online (Sandbox Code Playgroud)

到目前为止这种方法很有用,但是我想将它包含在*.pro文件中,这样我就可以把它放在我们的SVN中了.

我怎么能只用qmake/.pro文件呢?

qmake

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

在子路径中通过nginx + uwsgi运行django app

我想在我们的开发服务器上的子目录别名中运行一个简单的测试项目.基本设置是一个nginx,其位置将子目录中的所有内容传递给wsgi应用程序.

Django显然不明白它运行在子目录别名中,这完全破坏了URL的生成和解析.
我在文档中找不到任何类似前缀的设置,而我的谷歌也没有那么多帮助...所以我在这里问.

我找到的唯一一件事是设置FORCE_SCRIPT_NAME,它至少修复了URL的生成.(参见:http:
//docs.webfaction.com/software/django/config.html#mounting-a-django-application-on-a-subpath )遗憾的是,这并不能修复urlconf解析,即使提到的网站建议那.

是否可以在子目录别名中运行django应用程序,如果是,如何?

nginx配置:

server {
        location /fancyprojectname/static {
                alias /srv/fancyprojectname/static;
        }

        location /fancyprojectname/ {
                uwsgi_pass unix://var/run/uwsgi/app/fancyprojectname/socket;
                include uwsgi_params;
        }
}
Run Code Online (Sandbox Code Playgroud)

编辑

所以,设置"uwsgi_param SCRIPT_NAME/fancyprojectname;" 在nginx位置使FORCE_SCRIPT_NAME变得不必要 - 遗憾的是,URL匹配仍然不起作用.

from django.conf.urls import patterns, include, url

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    url(r'^admin/', …
Run Code Online (Sandbox Code Playgroud)

django nginx

15
推荐指数
3
解决办法
8437
查看次数

我可以将libcurls CURLOPT_WRITEFUNCTION与C++ 11 lambda表达式一起使用吗?

我尝试使用带有CURLOPT_WRITEFUNCTION的C++ 11 lambda表达式,但程序在运行时因访问冲突而崩溃.由于缺乏C++ 11知识,我不确定如何进一步研究这个问题,但也许其他人知道如何使这项工作.

功能:

#ifndef CURL_GET_H
#define CURL_GET_H

#include <curl/curl.h>
#include <curl/easy.h>
#include <vector>
#include <string>

std::vector<std::string> curl_get(const char* url)
{
    CURL *curl;
    CURLcode res;

    std::vector<std::string> content;
    auto curl_callback = [](void *ptr, size_t size, size_t nmemb, void *stream) -> size_t {
        // does nothing at the moment due to testing...
        return size * nmemb;
    };

    curl = curl_easy_init();
    if (curl)
    {
        curl_easy_setopt(curl, CURLOPT_URL, "http://localhost/aaa.txt");
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_callback);
        res = curl_easy_perform(curl);
        curl_easy_cleanup(curl);
    }

    return content;
}

#endif // CURL_GET_H
Run Code Online (Sandbox Code Playgroud)

错误: …

c++ lambda libcurl c++11

9
推荐指数
3
解决办法
3655
查看次数

将PrimaryKey添加到具有大量重复数据的Realm

我需要添加@PrimaryKey两个由于白痴而缺少它的Realm模型.通过直接关系或RealmLists在多个其他模型中引用模型,两个模型中的一个也引用另一个模型.

我的第一个想法是在迁移中重命名模式并手动复制数据,但随后Realm抱怨模式在其他模式中链接,无法重命名.

这两个模式包含大约15000个可以压缩到大约100个的对象,它们完全相同并且由于缺失而被复制@PrimaryKey.

模型本身有点简单:

class ModelA extends RealmObject {
     String primaryKey; // Is missing the @PrimaryKey annotation
     String someField;
     String someOtherField;
     Date someDate;
     ModelB relationToTheOtherProblematicModel;
}

class ModelB extends RealmObject {
    String primaryKey; // Is also missing the @PrimaryKey annotation
    // this class only contains String fields and one Date field
}
Run Code Online (Sandbox Code Playgroud)

当我添加@PrimaryKey到两个类的primaryKey字段时,如何迁移数据?

编辑澄清:

两个模式都包含多个完全相同的项目.

primaryKey | someField | someOtherField
------     | ------    | ------
A          | foo       | bar
A          | …
Run Code Online (Sandbox Code Playgroud)

java android database-migration realm realm-migration

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

期望同一类的多个 Rails Active Jobs 使用不同的参数排队

我有一个 Rails Active Job,它创建多个具有不同参数的相同类型的其他作业。我想测试该作业是否使用正确的参数将另一个作业排入队列。

这基本上就是我想要实现的目标:

require 'rails_helper'

RSpec.describe TriggerJob, type: :job do
  include ActiveJob::TestHelper

  after do
    clear_enqueued_jobs
  end

  it 'enqueues jobs for all model instances' do
    model1 = create(:model)
    model2 = create(:model)
    model3 = create(:model)

    expect { described_class.perform_now }
      .to(have_enqueued_job(ModelJob).with { |arg| expect(arg.id).to be == model1.id }.exactly(:once))
      .and(have_enqueued_job(ModelJob).with { |arg| expect(arg.id).to be == model2.id }.exactly(:once))
      .and(have_enqueued_job(ModelJob).with { |arg| expect(arg.id).to be == model3.id }.exactly(:once))
  end
end
Run Code Online (Sandbox Code Playgroud)

这不起作用,因为 RSpec 似乎只是匹配作业类类型,然后尝试将第一个作业参数与我的块进行比较。根据队列中的顺序,此操作会失败。我希望 RSpec 匹配任何排队的 ModelJob 作业,并且只有在队列中找不到任何匹配项时才会失败。

此外,我想测试是否存在其他具有不同参数的 ModelJob 作业,但这并不是真正需要的。

ruby rspec ruby-on-rails rails-activejob

8
推荐指数
2
解决办法
4058
查看次数

如何使用QWaitCondition实现一个永远运行的QThread但仍然需要在执行此操作时捕获另一个Slot

我实现了一个类,它可以通过QQueue将数据写入串口,并通过插槽读取.我使用QAsyncSerial来反过来使用boost :: asio和回调.该类被移动到一个线程,当QThread发出"started()"时,它的start()方法被执行

问题是我使用forever {}和QWaitCondition在start() - 方法中使QQueue出列.当这个运行时(显然会永远运行),连接到QAsyncSerial的dataReceived信号的插槽无法被调用,因此我从未从串口读取任何内容.

解决这个问题的常用方法是什么?

SerialPortHandler::SerialPortHandler(SerialPort serialPort, QObject *parent) : QObject(parent), serialPort(serialPort)
{
    m_enqueueMessageMutex = new QMutex();
    m_messageQueue = new QQueue<BaseMessage*>();
    m_waitCondition = new QWaitCondition();
    serial.open(serialPort.deviceName(), 2400);
    connect(&serial, SIGNAL(dataReceived(QByteArray)), this, SLOT(serialSlotReceivedData(QByteArray)));
}

void SerialPortHandler::serialSlotReceivedData(QByteArray line)
{
    qDebug() << QString(line).toAscii();
}

void SerialPortHandler::sendTestPing()
{
    PingMessage *msg = new PingMessage();
    enqueueMessage(msg);
}

void SerialPortHandler::enqueueMessage(BaseMessage *msg)
{
    QMutexLocker locker(m_enqueueMessageMutex);
    m_messageQueue->enqueue(msg);
    m_waitCondition->wakeAll();
}

void SerialPortHandler::start()
{
    if (!serial.isOpen())
        return;

    forever {
        m_enqueueMessageMutex->lock();
        if (m_messageQueue->isEmpty())
            m_waitCondition->wait(m_enqueueMessageMutex);
        BaseMessage *msg = m_messageQueue->dequeue();
        serial.write(msg->encodeForWriting());
        m_enqueueMessageMutex->unlock(); …
Run Code Online (Sandbox Code Playgroud)

c++ qt boost-asio qthread

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

Django:根据最新的子模型字段对QuerySet进行排序

让我们假设我想显示按其最新冲刺时间排序的跑步者列表.

class Runner(models.Model):
    name = models.CharField(max_length=255)

class Sprint(models.Model):
    runner = models.ForeignKey(Runner)
    time = models.PositiveIntegerField()
    created = models.DateTimeField(auto_now_add=True)
Run Code Online (Sandbox Code Playgroud)

这是我在SQL中做的快速草图:

SELECT runner.id, runner.name, sprint.time
FROM runner
LEFT JOIN sprint ON (sprint.runner_id = runner.id)
WHERE 
  sprint.id = (
    SELECT sprint_inner.id
    FROM sprint as sprint_inner
    WHERE sprint_inner.runner_id = runner.id
    ORDER BY sprint_inner.created DESC
    LIMIT 1
  )
  OR sprint.id = NULL
ORDER BY sprint.time ASC
Run Code Online (Sandbox Code Playgroud)

Django的查询集文档指出:

允许指定多值字段以按结果排序(例如,ManyToManyField字段).通常这不是一件明智的事情,它确实是一种高级的使用功能.但是,如果您知道您的查询集的过滤或可用数据意味着您选择的每个主要项目只有一个订购数据,那么排序可能正是您想要做的.谨慎使用多值字段的排序,并确保结果符合您的预期.

我想我需要在这里应用一些过滤器,但我不确定Django究竟是什么期望......

一个注意事项,因为在这个例子中并不明显:Runner表将有几百个条目,sprint也将有几百个,在一些晚些时候可能有几千个条目.数据将以分页视图显示,因此无法在Python中进行排序.

我看到的唯一另一种可能性是自己编写SQL,但我想不惜一切代价避免这种情况.

django django-models django-queryset

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

如何告诉 Traefik 2.1 为路由器使用用户定义的通配符证书?

我试图让 Traefik 为所有具有匹配主机规则的路由器使用手动配置的通配符证书。我认为 Traefik 会尝试根据主机规则中使用的域查找证书,但它始终使用默认生成的证书。

traefik.yml

global:
  checkNewVersion: false
  sendAnonymousUsage: false

log:
  level: DEBUG

entryPoints:
  web:
    address: ":80"
  web-secure:
    address: ":443"

providers:
  file:
    directory: /etc/traefik/conf
    watch: true
Run Code Online (Sandbox Code Playgroud)

动态配置:

http:
  routers:
    test:
      rule: "Host(`subdomain.wildcard.domain.tld`)"
      entryPoints: ["web"]
      service: service-test
      middlewares: ["https_redirect"]
    test-secure:
      rule: "Host(`subdomain.wildcard.domain.tld`)"
      entryPoints: ["web-secure"]
      service: service-test
      tls: {}

  services:
    service-test:
      loadBalancer:
        servers:
          - url: "http://helloworld"

  middlewares:
    https_redirect:
      redirectScheme:
        scheme: https
        permanent: true
Run Code Online (Sandbox Code Playgroud)

Traefik 在 Docker 容器内运行,基于它可以看到的日志并使用挂载的证书文件:

time="2020-03-04T10:44:13Z" level=debug msg="No store is defined to add the certificate <...>, it will be …
Run Code Online (Sandbox Code Playgroud)

ssl traefik

7
推荐指数
0
解决办法
823
查看次数

带有RegExp的QTreeView,QFileSystemModel,setRootPath和QSortFilterProxyModel用于过滤

我需要显示特定目录的QTreeView,并且我想让用户使用RegExp过滤文件.

据我了解Qt文档,我可以使用标题中提到的类来实现这一点,如下所示:

// Create the Models
QFileSystemModel *fileSystemModel = new QFileSystemModel(this);
QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel(this);

// Set the Root Path
QModelIndex rootModelIndex = fileSystemModel->setRootPath("E:\\example");

// Assign the Model to the Proxy and the Proxy to the View
proxyModel->setSourceModel(fileSystemModel);
ui->fileSystemView->setModel(proxyModel);

// Fix the TreeView on the Root Path of the Model
ui->fileSystemView->setRootIndex(proxyModel->mapFromSource(rootModelIndex));

// Set the RegExp when the user enters it
connect(ui->nameFilterLineEdit, SIGNAL(textChanged(QString)),
        proxyModel, SLOT(setFilterRegExp(QString)));
Run Code Online (Sandbox Code Playgroud)

启动程序时,TreeView正确地固定到指定的目录.但是一旦用户更改了RegExp,看起来TreeView就会忘记它的RootIndex.删除RegExp LineEdit中的所有文本(或输入类似"."的RegExp)后,它再次显示所有目录(在Windows上,这意味着所有驱动器等)

我究竟做错了什么?:/

c++ qt

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