python - 如何使用 SWIG 从 C++ 无序映射生成 Python 字典?
问题描述
我正在尝试使用 swig 将无序映射包装在 python 字典中:
// UsingAnUnorderedMap.h
#ifndef LEARNSWIG_USINGUNORDEREDMAP_H
#define LEARNSWIG_USINGUNORDEREDMAP_H
#include <unordered_map>
#include <string>
std::unordered_map<std::string, int> makeStringToIntMap(){
return std::unordered_map<std::string, int>{
{"first", 4},
{"second", 5},
};
}
#endif //LEARNSWIG_USINGUNORDEREDMAP_H
//UsingAnUnorderedMap.i
%module UsingUnorderedMap
%{
#include "UsingUnorderedMap.h"
#include <iostream>
#include <unordered_map>
%}
%include "std_string.i"
%include "std_pair.i"
%include "std_unordered_map.i"
%template(StringToIntMap) std::unordered_map<std::string,int>;
%include "UsingUnorderedMap.h"
%typemap(out) StringToIntMap {
PyDictObject* dict = PyDict_New($input);
for (auto &item: StringToIntMap){
PyDict_SetItem(dict, PyUnicode_FromString(item.first), item.second);
}
$result = dict;
}
# testUsingAnUnorderedMap.py
import sys
sys.path += [
r"D:\LearnSwig\install-msvc\UsingUnorderedMap"
]
import UsingUnorderedMap
print(type(UsingUnorderedMap.makeStringToIntMap()))
这产生
<class 'UsingUnorderedMap.StringToIntMap'>
即它只是忽略了类型映射。从技术上讲,它的StringToIntMap
行为与 Python 字典几乎相同——据我所知,但我认为 Python 字典中的 Python 用户很舒服,所以如果这是一本直截了当的字典会更好。有人对如何实现这一目标有任何指示吗?
为方便起见,您可以使用以下 CMake 代码构建此代码。请注意,我使用命令构建它-DSWIG_EXECUTABLE=/path/to/swig.exe
。
# CMakeLists.txt
set(Python_ROOT_DIR "C:/Miniconda3")
find_package(Python COMPONENTS Interpreter Development NumPy)
message("Python_EXECUTABLE ${Python_EXECUTABLE}")
find_package(SWIG 4.0.0 REQUIRED
COMPONENTS python
)
include(UseSWIG)
set_property(SOURCE UsingUnorderedMap.i PROPERTY CPLUSPLUS ON)
swig_add_library(UsingUnorderedMap
LANGUAGE python
TYPE MODULE
SOURCES UsingUnorderedMap.i UsingUnorderedMap)
set_target_properties(UsingUnorderedMap
PROPERTIES LANGUAGE CXX)
target_include_directories(UsingUnorderedMap PUBLIC
${Python_INCLUDE_DIRS}
${CMAKE_CURRENT_SOURCE_DIR}
)
target_link_libraries(UsingUnorderedMap PUBLIC ${Python_LIBRARIES})
install(TARGETS UsingUnorderedMap DESTINATION UsingUnorderedMap)
install(FILES
${CMAKE_CURRENT_BINARY_DIR}/UsingUnorderedMap.py
${CMAKE_CURRENT_SOURCE_DIR}/testUsingUnorderedMap.py
DESTINATION UsingUnorderedMap)
解决方案
这是一个小例子,展示了转换为std::unordered_map
python 字典
%module dictmap
%{
#include "test.h"
%}
%typemap(out) std::unordered_map<std::string, std::string> (PyObject* obj) %{
obj = PyDict_New();
for (const auto& n : $1) {
PyObject* strA = PyUnicode_FromString(n.first.c_str());
PyObject* strB = PyUnicode_FromString(n.second.c_str());
PyDict_SetItem(obj, strA, strB);
Py_XDECREF(strA);
Py_XDECREF(strB);
}
$result = SWIG_Python_AppendOutput($result, obj);
%}
%include "test.h"
小内联函数
#pragma once
#include <string>
#include <unordered_map>
std::unordered_map<std::string, std::string> makeStringMap() {
return std::unordered_map<std::string, std::string> {
{"first", "hello"},
{"second", "world"},
};
}
我希望你能看到你做错了什么。您正在返回一个使用空创建的空字典$input
。我不确定您是否需要定义一个PyObject
参数,但我总是这样做,以防它与另一个类型映射一起需要。
推荐阅读
- python - 网页抓取但属性返回为无?
- python - 这个递归函数如何产生所有排列?
- r - Install.packages 在 Rstudio 控制台中安装源代码,但在使用 Rscript 时安装二进制文件
- python-3.x - 设置变量的类型,使其与集合的内容相同
- ajax - 如何在 Django 中设置 AJAX 调用的视图/路径?
- c# - 我如何解决 Assert.Is.True (这并不意味着是 False btw)在我的测试驱动开发中失败
- ruby-on-rails - 你可以为同一个模型有两个序列化器来显示不同的东西吗?
- python - 在 Pandas 中合并两个 DataFrame 会在新合并的 DF 中产生 NaN
- python - 如何从要用作变量的字符串中删除引号 - python
- javascript - 命名 JavaScript 函数表达式的“名称”属性返回分配函数的变量名称的原因是什么?