numpy的C-API:转换类型的对象键入号码 [英] NumPy C-API: convert type object to type number

查看:297
本文介绍了numpy的C-API:转换类型的对象键入号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

功能

 的PyObject * PyArray_TypeObjectFromType(INT);

转换为一个numpy的标量类型(NPY_BOOL,NPY_BYTE,...),以相应的类型的对象的类型数目

你怎么做相反的转换,从类型对象为numpy的标量类型到相应类型号?

编辑:以下code基于kwatford的回答。它接受两种类型的对象,如int和numpy.int16和字符串,如诠释UInt和INT16。

  INT numpyScalarTypeNumber(*的PyObject OBJ)
{
    PyArray_Descr * DTYPE;
    如果(PyArray_DescrConverter(OBJ,和放大器;!DTYPE))返回NPY_NOTYPE;
    INT typeNum = dtype-> type_num;
    Py_DECREF(DTYPE);
    返回typeNum;
}


解决方案

如果你能得到 PyArray_Descr 结构,而不是 PyArray_TypeObject S,你可以简单地看一下<一个href=\"http://docs.scipy.org/doc/numpy/reference/c-api.types-and-structures.html#PyArray_Descr.type_num\"相对=nofollow> type_num 领域。该DESCR结构可以通过使用类型号<一个被收购href=\"http://docs.scipy.org/doc/numpy/reference/c-api.array.html?highlight=numpy%20pyarray_typeobjectfromtype#PyArray_DescrFromType\"相对=nofollow> PyArray_DescrFromType 。如果你看一下这个链接,也有用于把各种东西放到DESCR结构多了一些功能。他们很可能在比一般类型的对象更加有用,它们包含自己的类型的引用也是如此。

The function

PyObject* PyArray_TypeObjectFromType(int);

converts the type number for a NumPy scalar type (NPY_BOOL, NPY_BYTE, ...) to the corresponding type object.

How do you do the opposite conversion, from the type object for a NumPy scalar type to the corresponding type number?

Edit: The following code is based on kwatford's answer. It accepts both type objects such as int and numpy.int16, and strings such as "int", u"int" and "int16".

int numpyScalarTypeNumber(PyObject* obj)
{
    PyArray_Descr* dtype;
    if(!PyArray_DescrConverter(obj, &dtype)) return NPY_NOTYPE;
    int typeNum = dtype->type_num;
    Py_DECREF(dtype);
    return typeNum;
}

解决方案

If you can get PyArray_Descr structs rather than PyArray_TypeObjects, you can simply look at the type_num field. The descr structs can be acquired via the type number using PyArray_DescrFromType. If you look at that link, there are also a few more functions for converting various things into descr structs. They're probably more useful in general than the type objects, and they contain references to their types as well.

这篇关于numpy的C-API:转换类型的对象键入号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆