#include <vector>
#include <memory>
using namespace std;
class A {
public:
A(): i(new int) {}
A(A const& a) = delete;
A(A &&a): i(move(a.i)) {}
unique_ptr<int> i;
};
class AGroup {
public:
void AddA(A &&a) { a_.emplace_back(move(a)); }
vector<A> a_;
};
int main() {
AGroup ag;
ag.AddA(A());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
不编译...(说unique_ptr的拷贝构造函数被删除)
我尝试用前锋替换移动.不确定我是否做得对,但它对我不起作用.
[~/nn/src] g++ a.cc -o a -std=c++0x
/opt/local/include/gcc44/c++/bits/unique_ptr.h: In member function 'A& A::operator=(const A&)':
a.cc:6: instantiated from 'void std::vector<_Tp, _Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> >, _Args&& ...) [with _Args = …
Run Code Online (Sandbox Code Playgroud) 我有大约20个简单的C++类来表示各种概率分布.我想用Python提供这些.最简单的方法是什么?我不介意让程序自动生成包装器,但我不想重新键入类.
此外,我的一些类使用Boost uBLAS库.有没有办法从Python自动传入numpy数组?
在Python中,可以使用itertools.chain
以下方式以惰性方式扩展列表:
L = itertools.chain(L1, L2)
Run Code Online (Sandbox Code Playgroud)
是否有一个懒惰的地图"粘合"操作员?也就是说,
M = glue(M1, M2)
Run Code Online (Sandbox Code Playgroud)
哪里
M['blah']
Run Code Online (Sandbox Code Playgroud)
回报
M1['blah'] if 'blah' in M1 else M2['blah']
Run Code Online (Sandbox Code Playgroud)
并且,M
有适当的发电机keys()
和values()
.
我无法弄清楚为什么这个程序失败了.
#!/usr/bin/env python
from __future__ import division, print_function
from future_builtins import *
import types
import libui as ui
from PyQt4 import QtCore
import sip
p = ui.QPoint()
q = QtCore.QPoint()
def _q_getattr(self, attr):
print("get %s" % attr)
value = getattr(sip.wrapinstance(self.myself(), QtCore.QPoint), attr)
print("get2 %s returned %s" % (attr, value))
return value
p.__getattr__ = types.MethodType(_q_getattr, p)
print(p.__getattr__('x')()) # Works! Prints "0"
print(p.x()) # AttributeError: 'QPoint' object has no attribute 'x'
Run Code Online (Sandbox Code Playgroud)
我使用Boost.Python来创建libui,它暴露了类QPoint.我包括PyQt4,它有一个sip暴露的QPoint.我正在尝试完成两种类型之间的映射.
我检查过这p
是一个新式的课程,为什么不__getattr__
被要求p.x()
?
如何使用Boost.Python公开以下类?
class C {
public:
static void F(int) {}
static void F(double) {}
};
Run Code Online (Sandbox Code Playgroud)
我试过这样的事情:
bp::class_<C>("C")
.def("F", (void (C::*)(int))&C::F).staticmethod("F")
.def("F", (void (C::*)(double))&C::F).staticmethod("F")
;
Run Code Online (Sandbox Code Playgroud)
但是,它在Python(SystemError: initialization of libdistributions raised unreported exception
)中引发了一个异常.如果我从中删除其中一个重载bp::class_
,那么一切正常.我知道Boost.Python可以自动处理重载的构造函数,所以我想知道静态方法是否有类似的东西.
bp::class_<C>("C")
.def("F", (void (C::*)(int))&C::F) // Note missing staticmethod call!
.def("F", (void (C::*)(double))&C::F).staticmethod("F")
;
Run Code Online (Sandbox Code Playgroud) 在 Python 中,给定一组可比较的、可散列的元素,散列或s
哪个更好。frozenset(s)
tuple(sorted(s))
假设我有一个上下文管理器:
@contextmanager
def cm(x):
y = f(x)
z = yield y
g(z)
Run Code Online (Sandbox Code Playgroud)
如何发送z
到上下文管理器?
我试过:
my_cm = cm()
with my_cm:
my_cm.gen.send(123)
Run Code Online (Sandbox Code Playgroud)
但我得到 StopIteration,这可能是因为send
产量?
安装Yosemite后,我不得不升级numpy,PyOpenGL等.
现在,一个以前工作的程序给了我以下堆栈跟踪:
file "latebind.pyx", line 44, in OpenGL_accelerate.latebind.Curry.__call__ (src/latebind.c:1201)
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/OpenGL/GL/VERSION/GL_1_5.py", line 89, in glBufferData
return baseOperation( target, size, data, usage )
File "latebind.pyx", line 32, in OpenGL_accelerate.latebind.LateBind.__call__ (src/latebind.c:989)
File "wrapper.pyx", line 314, in OpenGL_accelerate.wrapper.Wrapper.__call__ (src/wrapper.c:6505)
File "wrapper.pyx", line 311, in OpenGL_accelerate.wrapper.Wrapper.__call__ (src/wrapper.c:6439)
ctypes.ArgumentError: ("argument 3: <class 'OpenGL.error.CopyError'>: from_param received a non-contiguous array! []", (GL_ARRAY_BUFFER, 0, array([],
dtype=[('time', '<f8'), ('data', [('cluster_index', '<i4'), ('delta_diag_out', '<f8')])]), GL_STREAM_DRAW))
Run Code Online (Sandbox Code Playgroud)
看起来PyOpenGL希望我的数组是连续的.在PyOpenGL中查看numpy_formathandler.pyx中的源代码:
if not PyArray_ISCARRAY( instance ):
raise CopyError(
"""from_param received a non-contiguous array! %s"""%(
working, …
Run Code Online (Sandbox Code Playgroud) 我正在尝试这样做,但我不确定如何指定类型签名:
def initialize_signals(
self,
command: InitializeCommand,
initializers: Iterable[Union[
Tuple[SignalNode],
Tuple[SignalNode, Any, ...]
]]):
for x, *args in initializers:
potential_update = command.create_potential_update(x, *args)
Run Code Online (Sandbox Code Playgroud) 如果我键入以下内容
void main(int blah,
Run Code Online (Sandbox Code Playgroud)
然后按回车键,我想继续这里:
float blah);
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
python ×8
c++ ×3
boost-python ×2
generator ×2
python-3.x ×2
c++11 ×1
ctypes ×1
cython ×1
hash ×1
indentation ×1
numpy ×1
opengl ×1
pyopengl ×1
pyqt ×1
pyrex ×1
python-sip ×1
swig ×1
unique-ptr ×1
vim ×1