我有一个C++库,目前有一些方法在其中返回一个std::vector
定义的类似
public:
const std::vector<uint32_t>& getValues() const;
Run Code Online (Sandbox Code Playgroud)
我目前正在使用SWIG为Python包装整个库,到目前为止这种方法运行良好.
SWIG将此getValues()
函数包装好,以便返回Python元组.问题出在我的Python端代码中,我希望将其转换为NumPy数组.我当然可以这样做:
my_array = np.array(my_object.getValues(), dtype='uint32')
Run Code Online (Sandbox Code Playgroud)
但是这会导致原始向量中的所有条目首先被SWIG复制到Python元组中,然后由我再次复制到numpy数组中.由于这个向量可能非常大,我宁愿避免制作这两个副本,并希望有一种方法让SWIG在内存中的原始向量数据周围创建一个numpy.array包装器.
我已经阅读了numpy.i的文档,但明确提到输出数组不受支持,因为它们似乎是在C样式数组而不是C++向量的假设下工作的.
numpy.array的底层数据结构只是一个C风格的数组,就像C++ std :: vector一样,所以我希望在内存中访问相同的数据是可行的.
有没有办法让SWIG返回一个不复制原始数据的numpy.array?
我有以下代码,只需在屏幕上绘制一个绿色三角形.它使用顶点数组对象和索引缓冲区绘制,并具有我可以制作的最简单的着色器.
起初我没有使用索引缓冲区,只是简单地进行绘制调用,glDrawArrays
但是当我将其更改为使用时glDrawElements
,屏幕上没有任何内容(它完全是黑色).
from OpenGL.GL import shaders
from OpenGL.arrays import vbo
from OpenGL.GL import *
from OpenGL.raw.GL.ARB.vertex_array_object import glGenVertexArrays, \
glBindVertexArray
import pygame
import numpy as np
def run():
pygame.init()
screen = pygame.display.set_mode((800,600), pygame.OPENGL)
#Create the Vertex Array Object
vertexArrayObject = GLuint(0)
glGenVertexArrays(1, vertexArrayObject)
glBindVertexArray(vertexArrayObject)
#Create the VBO
vertices = np.array([[0,1,0],[-1,-1,0],[1,-1,0]], dtype='f')
vertexPositions = vbo.VBO(vertices)
#Create the index buffer object
indices = np.array([0,1,2], dtype='uint16')
indexPositions = vbo.VBO(indices, target=GL_ELEMENT_ARRAY_BUFFER)
indexPositions.bind()
vertexPositions.bind()
glEnableVertexAttribArray(0) # from 'location = 0' in …
Run Code Online (Sandbox Code Playgroud) 我正在尝试datetime.datetime
使用一些额外的方法来扩展Python的类.所以,例如我在做:
import datetime
class DateTime(datetime.datetime):
def millisecond(self):
return self.microsecond/1000
Run Code Online (Sandbox Code Playgroud)
但是如果我这样做的话
>>> d = DateTime(2010, 07, 11, microsecond=3000)
>>> print d.millisecond()
3
>>> delta = datetime.timedelta(hours=4)
>>> newd = d + delta
>>> print newd.millisecond()
AttributeError: 'datetime.datetime' object has no attribute 'millisecond'
Run Code Online (Sandbox Code Playgroud)
这显然是因为d + delta
调用datetime.datetime.__add__()
返回datetime.datetime
对象的方法.
有什么办法可以让这个datetime.datetime
对象转换成一个DateTime
对象吗?或者我是否必须重新实现DateTime
子类中的所有运算符才能返回正确的类型?
我有一个像以下类似的C++类:
template< template<typename> class ContainerType, typename MemberType>
class MyClass
{
public:
MyClass(ContainerType<MemberType>* volData);
}
Run Code Online (Sandbox Code Playgroud)
我试图用SWIG包装.我的MyClass.i看起来像:
%module MyClass
%{
#include "SimpleContainer.h"
#include "MyClass.h"
%}
%include "SimpleContainer.h"
%include "MyClass.h"
%template(MyClass_SimpleContainer_Int) MyClass<SimpleContainer, int>;
Run Code Online (Sandbox Code Playgroud)
但是,SWIG似乎在模板模板参数方面存在问题.编译时会报错,并显示错误消息:
MyClassPYTHON_wrap.cxx:30545:3: error: ‘ContainerType’ was not declared in this scope
Run Code Online (Sandbox Code Playgroud)
在生成的代码中查看该行,它包含以下行:
ContainerType< int > *arg1 = (ContainerType< int > *) 0 ;
Run Code Online (Sandbox Code Playgroud)
由于某种原因,它使用逐字的虚拟模板名称作为类的名称,即使我已经告诉它该类的实例化应该具有SimpleContainer的ContainterType.
有什么方法可以绕过这个bug吗?我在SWIG跟踪器中发现了它的提及,但是我无法理解上一篇文章中提到的解决方法,而且这个bug已经有4年了.
我在openSUSE 11.4上使用SWIG 1.3.40和GCC 4.5.1
OpenGL 有一些函数,例如glGetString
和glGetShaderInfoLog
返回字符串。这些使用什么形式的文本编码?
我假设,鉴于它们作为 a 返回GLchar*
,它是返回值中包含的 ASCII 编码文本,但这是在任何地方指定的吗?
glShaderSource
作为第二个相关点,诸如和之类的函数期望什么文本编码glBindAttribLocation
。GLSL 程序必须使用 ASCII 编码还是可以使用 UTF-8 编码?
查看随机选择的着名Python包,为什么总体趋势是不在#!/usr/bin/env python
顶部包含一行setup.py
?我知道通常推荐的与文件交互的方式是这样的:
python setup.py install
Run Code Online (Sandbox Code Playgroud)
而不是
./setup.py install
Run Code Online (Sandbox Code Playgroud)
但这有充分的理由吗?
这些包不包括shebang:pytest,lxml,six,virtualenv,pip
但这些:请求,simplejson,setuptools
我正在维护一个旧的Python代码库,其中包含一些非常奇怪的习惯用法.我遇到的一件事是使用百分比编码样式的字符串格式:
'Error %s: %s' % (str(err), str(message))
Run Code Online (Sandbox Code Playgroud)
忽略.format()
现代字符串插值的存在,我的问题是:
是否有必要显式转换%s
参数str()
或者究竟是什么%s
呢?
这是我需要处理的txt文件:
chr8 148401 153100 duplication
chr8 206001 207100 deletion
chr8 584401 589500 deletion
chr8 615101 616600 deletion
chr8 842601 843200 deletion
chr8 868901 869700 deletion
Run Code Online (Sandbox Code Playgroud)
基本上我想提取两个数字,并做减法.我的代码如下:
#!/usr/bin/python
import os,sys
file = open('/home/xxx/sge_jobs_output/rCEU.bed','r')
for line in file.readlines():
num1 = line.split()[1].split()[0]
num2 = line.split()[1].split()[1].split()[0]
num = int(num2)-int(num1)
print num
Run Code Online (Sandbox Code Playgroud)
我可以成功打印出num1; 但是num2不起作用.所以我们不能连续使用两个以上的.split?
而错误就像:
Traceback (most recent call last):
File "CNV_length_cal.py", line 8, in <module>
num2 = line.split()[1].split()[1].split()[0]
IndexError: list index out of range
Run Code Online (Sandbox Code Playgroud)
这有什么不对?我对.split命令感到很困惑......但是我找不到关于那个...的教程