使用 Boost Python 创建和访问 freezeset
我有一些 C++ 方法,它们将 std::set
作为参数或返回值。 我想将其映射到 Python frozenset
(或常规 set
),但似乎没有一种简单的方法可以做到这一点。 有谁知道如何完成这项任务。
I have some C++ methods that have std::set<std::string>
as argument or return value.
I would like to map this to a Python frozenset
(or regular set
) but there does not seem to be a straightforward way to do this.
Does anyone know how one may accomplish this task.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
或者您可以使用
std::map
而不是std::set
,该值可以是例如 0。 std::map 有与 std::set 相同的插入/搜索时间复杂度,它还保持键有序,只会使内存稍微膨胀。然后,您可以使用地图索引套件,并且在 python 中,如果需要,您可以隐藏某些包装类中的差异。缺点是您必须稍微修改现有的 C++ 代码。Or you can use
std::map<YourType, int>
instead ofstd::set<YourType>
, the value can be for example 0. std::map has the same insert/search time complexity as std::set, it also keeps the keys ordered, it will only bloat the memory a little. Then you can use map indexing suite and in python you can hide the difference in some wrapper class if needed. The disanvantage is that you have to modify your existing c++ code a little bit.不幸的是,Boost.Python 的标准
indexing_suite
不支持std::set
。有一个 indexing_suite v2,适用于所有 stl 容器。 (http://mail.python.org/pipermail/cplusplus-sig/2009-July/014704.html)它可能没有进入官方发行版,但你可以通过询问找到它。
(http://mail.python.org/pipermail/cplusplus-sig/2009-July/014691.html)
我发现它比原来的
indexing_suite
更难使用,但它可能适合您需要。如果这不起作用,您可以像任何其他类一样手动包装
std::set
。这将使您将std::set
转换为 python,您可以相当轻松地将其转换为 pythonset
。我认为这两者都需要做更多的工作。这是我要做的:
首先,用具有相同签名的函数将函数包装在 C++ 中,但将返回的数据填充到
std::vector
而不是std::set
。公开该函数而不是原始函数现在您已经有了 python 中的数据。
其次,将 c++ 函数包装在 python 函数中,该函数获取 std::vector中的数据并将其填充到 python
set
中。是的,从设计美学的角度来看,这相当愚蠢,而且不是世界上性能最高的代码,但它可以让你用最少的代码到达你要去的地方,而且它相当健壮。
Unfortunately, the standard
indexing_suite
from Boost.Python does not supportstd::set
. There is a indexing_suite v2, that works on all stl containers. (http://mail.python.org/pipermail/cplusplus-sig/2009-July/014704.html)It may not have made it to the official distribution, but you can find it by asking around.
(http://mail.python.org/pipermail/cplusplus-sig/2009-July/014691.html)
I found it to be harder to use then the original
indexing_suite
, but it might fit your needs.If that does not work, you can just manually wrap
std::set<std::string>
like you would any other class. This will get you astd::set<std::string>
into python, where you can turn it into a pythonset
fairly easily.I think that both of those are more work then is called for though. Here is what I would do:
First, wrap the function in C++ with one that has the same signature, but stuffs the returned data in to a
std::vector<std::string>
instead of astd::set<std::string>
. expose that function rather then the originalNow you have the data in python.
Second, wrap the c++ function in python function that takes the data in that
std::vector<std::string>
and stuffs it into a pythonset
.Yes, this is rather silly from a design aesthetics point of view, and not the most performant code in the world, but is gets you to where you are going with a minimum of code, and it is fairly robust.