为了方便起见,我使用了两个库,并在它们使用的某些类型/结构之间编写了一个转换器.
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语句来消除警告.
问题:什么是清除警告的干净方法?
假设我在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)
回答这个问题,事实证明,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) 以下代码:
>>> 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数组.如果我改变127为128两个返回一个np.int16数组.
问题:
编辑:好的,所有的编辑使问题的布局有点混乱,所以我将尝试重写问题(不改变内容,但改进其结构).
如果我将它编译为可执行文件,我有一个openCL程序可以正常工作.现在我尝试使用Python来调用它boost.python.但是,只要我退出Python(导入我的模块后),python就会崩溃.
原因似乎与某些事情有关
程序终止时,仅静态存储GPU CommandQueues及其释放机制
使用的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模块工作正常
#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) 有没有办法为ctest输出着色.例如,在100%成功的情况下,可能有类似于最后一行的东西是绿色的
100% tests passed, 0 test failed out of x
Run Code Online (Sandbox Code Playgroud)
或红色否则?
我在我的c ++项目中的每个头文件中使用#pragma once(或者你使用includeguardsàla #ifndef...).这是巧合还是你在大多数开源项目中找到的东西(避免只依赖于个人项目经验的答案).如果是这样,为什么不相反:如果我想要多次包含头文件,我使用一些特殊的预处理器命令,如果不是,我将文件保留原样.
我从这里运行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
但是,在上面发布的链接中,提到代码还应该捕获与打开文件相关的异常.什么地方出了错?
我运行了以下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) 我写我需要从外部调用一个包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 ×6
c++ ×4
numpy ×2
arrays ×1
boost ×1
boost-python ×1
ctest ×1
dictionary ×1
ifstream ×1
list ×1
opencl ×1
pandas ×1
pytest ×1
subprocess ×1
templates ×1