小编NOh*_*Ohs的帖子

使用static_assert时如何避免关于无返回表达式的警告?

为了方便起见,我使用了两个库,并在它们使用的某些类型/结构之间编写了一个转换器.

template<typename T>
struct unsupportedType : std::false_type
{};

template<typename T>
FormatB getFormat()
{
    static_assert(
        unsupportedType<T>::value, "This is not supported!");
}

template<>
FormatB getFormat<FormatA::type1>()
{
    return FormatB(//some parameters);
}

template<>
FormatB getFormat<FormatA::type2>()
{
    return FormatB(//some other parameters);
}
Run Code Online (Sandbox Code Playgroud)

现在由于unsupportedType结构,编译器不会立即看到断言将始终失败,因此如果未在某处调用非专用版本,则不会抛出编译错误.但是,编译器因此也不知道返回语句之后static_assert是不必要的.我不只是想在断言之后放置一个任意的return语句来消除警告.

问题:什么是清除警告的干净方法?

c++ templates compiler-warnings

20
推荐指数
3
解决办法
1157
查看次数

如何使dict键弃用?

问题

假设我在python中有一个函数,它返回带有一些对象的dict.

class MyObj:
    pass

def my_func():
    o = MyObj()
    return {'some string' : o, 'additional info': 'some other text'}
Run Code Online (Sandbox Code Playgroud)

在某些时候,我注意到重命名密钥'some string'是有意义的,因为它具有误导性,并且没有很好地描述用这个密钥实际存储的内容.但是,如果我只是更改密钥,使用这些代码的人会非常恼火,因为我没有给他们时间通过弃用期来调整他们的代码.

目前的尝试

所以我考虑实现弃用警告的方式是在dict周围使用一个瘦的包装器:

from warnings import warn

class MyDict(dict):
    def __getitem__(self, key):
        if key == 'some string':
             warn('Please use the new key: `some object` instead of `some string`')
        return super().__getitem__(key)
Run Code Online (Sandbox Code Playgroud)

这样我就可以用旧键和新键指向同一个对象来创建dict

class MyObj:
    pass

def my_func():
    o = MyObj()
    return MyDict({'some string' : o, 'some object' : o, 'additional info': 'some other text'})
Run Code Online (Sandbox Code Playgroud)

问题:

  • 如果我添加此更改,代码可能会破坏的方式有哪些?
  • 是否有更简单的方法(使用现有解决方案,使用常见模式,更少量的更改)实现此目的?

python dictionary

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

df.groupby(...).agg(set)与df.groupby(...)相比产生不同的结果.agg(lambda x:set(x))

回答这个问题,事实证明,df.groupby(...).agg(set)并且df.groupby(...).agg(lambda x: set(x))正在产生不同的结果.

数据:

df = pd.DataFrame({
       'user_id': [1, 2, 3, 4, 1, 2, 3], 
       'class_type': ['Krav Maga', 'Yoga', 'Ju-jitsu', 'Krav Maga', 
                      'Ju-jitsu','Krav Maga', 'Karate'], 
       'instructor': ['Bob', 'Alice','Bob', 'Alice','Alice', 'Alice','Bob']})
Run Code Online (Sandbox Code Playgroud)

演示:

In [36]: df.groupby('user_id').agg(lambda x: set(x))
Out[36]:
                    class_type    instructor
user_id
1        {Krav Maga, Ju-jitsu}  {Alice, Bob}
2            {Yoga, Krav Maga}       {Alice}
3           {Ju-jitsu, Karate}         {Bob}
4                  {Krav Maga}       {Alice}

In [37]: df.groupby('user_id').agg(set)
Out[37]:
                                class_type                         instructor
user_id
1        {user_id, class_type, instructor}  {user_id, class_type, instructor}
2 …
Run Code Online (Sandbox Code Playgroud)

python pandas pandas-groupby

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

将np.int8数组与127相乘会产生不同的numpy数组类型,具体取决于平台

以下代码:

>>> import numpy as np
>>> np.arange(2).astype(np.int8) * 127
Run Code Online (Sandbox Code Playgroud)

产生numpy 1.13.3

# On Windows
array([0, 127], dtype=int16)
# On Linux
array([0, 127], dtype=int8)
Run Code Online (Sandbox Code Playgroud)

但是,如果我将其更改127为a 126,则都返回一个np.int8数组.如果我改变127128两个返回一个np.int16数组.

问题:

  • 这是预期的行为吗?
  • 为什么这两个平台的情况有所不同?

python arrays numpy

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

静态openCL类没有使用boost.python在python模块中正确发布

编辑:好的,所有的编辑使问题的布局有点混乱,所以我将尝试重写问题(不改变内容,但改进其结构).

这个问题简而言之

如果我将它编译为可执行文件,我有一个openCL程序可以正常工作.现在我尝试使用Python来调用它boost.python.但是,只要我退出Python(导入我的模块后),python就会崩溃.

原因似乎与某些事情有关

程序终止时,仅静态存储GPU CommandQueues及其释放机制

MWE和设置

建立

  • 使用的IDE:Visual Studio 2015

  • 使用的操作系统:Windows 7 64位

  • Python版本:3.5

  • AMD OpenCL APP 3.0标头

  • cl2.hpp直接来自Khronos,如下所示:空openCL程序抛出弃用警告

  • 此外,我有一个带有集成图形硬件的Intel CPU,没有其他专用显卡

  • 我使用版本1.60的boost库编译为64位版本

  • 我使用的boost dll称为: boost_python-vc140-mt-1_60.dll

  • 没有python的openCL程序运行正常

  • 没有openCL的python模块工作正常

MWE

#include <vector>

#define CL_HPP_ENABLE_EXCEPTIONS
#define CL_HPP_TARGET_OPENCL_VERSION 200
#define CL_HPP_MINIMUM_OPENCL_VERSION 200 // I have the same issue for 100 and 110
#include "cl2.hpp"
#include <boost/python.hpp>

using namespace std;

class TestClass
{
private:
    std::vector<cl::CommandQueue> queues;
    TestClass();

public:
    static const TestClass& getInstance()
    {
        static TestClass instance;
        return instance;
    } …
Run Code Online (Sandbox Code Playgroud)

c++ python boost opencl boost-python

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

ctest输出可以着色吗?

有没有办法为ctest输出着色.例如,在100%成功的情况下,可能有类似于最后一行的东西是绿色的

100% tests passed, 0 test failed out of x
Run Code Online (Sandbox Code Playgroud)

或红色否则?

ctest

11
推荐指数
0
解决办法
394
查看次数

为什么不在c ++中包含守卫默认值?

我在我的c ++项目中的每个头文件中使用#pragma once(或者你使用includeguardsàla #ifndef...).这是巧合还是你在大多数开源项目中找到的东西(避免依赖于个人项目经验的答案).如果是这样,为什么不相反:如果我想要多次包含头文件,我使用一些特殊的预处理器命令,如果不是,我将文件保留原样.

c++

9
推荐指数
2
解决办法
1271
查看次数

没有捕到异常使用C++打开一个不存在的文件

我从这里运行MWE:http://www.cplusplus.com/reference/ios/ios/exceptions/ 在我的机器上它没有捕获异常.这是我的代码

#include <iostream>
#include <fstream>

int main()
{
    std::ifstream file;
    file.exceptions( std::ifstream::failbit | std::ifstream::badbit );
    try
    {
        file.open("IDoNotExist.txt");
    }
    catch(const std::ifstream::failure& e)
    {
        std::cout << "Bad luck!" << std::endl;
    }
}
Run Code Online (Sandbox Code Playgroud)

使用gcc 6.2.1拱Linux的,我得到:

在抛出'std :: ios_base :: failure'的实例后终止调用

what():basic_ios :: clear

但是,在上面发布的链接中,提到代码还应该捕获与打开文件相关的异常.什么地方出了错?

c++ ifstream

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

Numpy 随机选择不适用于二维列表

我运行了以下python代码:

import numpy as np
a_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 1, 2]]
np.random.choice(a_list, size=20, 
    replace=True)
Run Code Online (Sandbox Code Playgroud)

期待这样的结果:

[[7, 8, 9], [1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 1, 2], [1, 2, 3], [1, 2, 3], [10, 1, 2], [1, 2, 3], [7, 8, 9], [1, 2, 3], [1, 2, 3], [10, 1, 2], [4, 5, 6], [4, 5, 6], [10, 1, 2], [10, 1, 2], [7, 8, 9], [1, 2, 3], …
Run Code Online (Sandbox Code Playgroud)

python numpy list

7
推荐指数
2
解决办法
3217
查看次数

如何用自定义字符串完全替换测试的pytest输出?

设想

我写我需要从外部调用一个包pytest从内pytest通过运行subprocess。很明显,从子进程捕获的输出正是我想要显示的错误,因为它具有 pytest 提供的所有漂亮的格式和信息。不幸的是,目前主要的 pytest 调用只显示我的包装器的内部代码,而不是漂亮的子进程输出,在我打印它之后,只显示在 pytest 的捕获标准输出部分。我想格式化失败和错误的输出,就好像直接调用代码一样,并隐藏进行了子进程调用。因此,我基本上想用不同的字符串完全替换一个测试函数的输出。这可能吗?

移动电源

让我们看一个简单包装函数的 MWE(没有做任何有用的事情,但是我能想到的最短的 MWE):

import functools
from subprocess import Popen, PIPE
import sys


def call_something(func):
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        # for the sake of simplicity, a dummy call
        p = Popen([sys.executable, '-c', 'import numby'], stderr=PIPE, stdout=PIPE)
        # let's imagine this is the perfect string output we want
        # to print instead of the actual output generated from...
        error = p.communicate()[1].decode("utf-8")

        if p.returncode != 0:
            # ... …
Run Code Online (Sandbox Code Playgroud)

python subprocess pytest

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