通过 SWIG 从 Python 访问 UtcTimeStamp

发布于 2024-11-26 06:42:09 字数 1185 浏览 1 评论 0原文

我想这是一个 python 与 SWIG 的问题比其他任何问题都重要......

我正在使用带有 SWIG Python 绑定的 C++ 包。 我收到的对象之一是 UTC 时间戳,我试图从中提取时间戳。

该对象具有以下特征:

>>> print type(obj)
<type 'SwigPyObject'>

>>> print dir(obj)
['__class__', '__cmp__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__hex__', '__init__', '__int__', '__le__', '__long__', '__lt__', '__ne__', '__new__', '__oct__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'acquire', 'append', 'disown', 'next', 'own']

>>> print obj
<Swig Object of type 'UtcTimeStamp *' at 0x0379F320>

如何从中提取数据?


更新:
我找到了 UTCTimeStamp 类,该类派生自 DateTime 结构 - 它是开源 QuickFix 包的一部分。

但是我仍然不知道如何访问数据。 DateTime 有简单的 getter 函数,例如 getYear() - 但是,如何访问它们?

I guess this is a python vs SWIG question more than anything else...

I'm using a C++ package with SWIG Python bindings.
One of the objects I receive is a UTC time stamp from which I'm trying to extract the time stamp.

The object has the following characteristics:

>>> print type(obj)
<type 'SwigPyObject'>

>>> print dir(obj)
['__class__', '__cmp__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__hex__', '__init__', '__int__', '__le__', '__long__', '__lt__', '__ne__', '__new__', '__oct__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'acquire', 'append', 'disown', 'next', 'own']

>>> print obj
<Swig Object of type 'UtcTimeStamp *' at 0x0379F320>

How do I extract the data out of it?


UPDATE:
I've found the UTCTimeStamp class which is derived from a DateTime struct - it is part of the open source QuickFix package.

However I still don't know how to access the data. DateTime has simple getter functions such as getYear() - however, how do I access them?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白日梦 2024-12-03 06:42:09

不要在时间字段上使用 qfTimeField.getValue(),而是使用 qfTimeField.getString(),然后仅使用 strptime() 结果字符串。例如:

qfSendingTime = fix.SendingTime()
message.getHeader().getField(qfSendingTime)
my_datetime = datetime.strptime(qfSendingTime.getString(), '%Y%m%d-%H:%M:%S.%f')

Instead of using qfTimeField.getValue() on the time field, use qfTimeField.getString(), and then just strptime() the resulting string. For example:

qfSendingTime = fix.SendingTime()
message.getHeader().getField(qfSendingTime)
my_datetime = datetime.strptime(qfSendingTime.getString(), '%Y%m%d-%H:%M:%S.%f')
魂归处 2024-12-03 06:42:09

您尝试过 obj.getYear() 吗?从文档中可以看出,UTCTimeStamp 派生自 DateTime,因此您应该能够访问父类的方法。

如果这不起作用,那么如果您执行 newobj = obj.next() ,您会得到什么样的对象?您是否尝试过 print obj.__doc__?

编辑:来自 http ://www.swig.org/Doc1.3/Python.html#Python_nn27a

这个指针值可以自由地传递给期望接收类型对象的不同 C 函数...唯一不能做的就是从 Python 取消引用该指针。

因此,您需要将其传递给可以采用 DateTime 的 C++ 函数的包装器。我不知道具体是什么,因为我不知道你在包装什么。

在本例中,即 http://www.quickfixengine.org/quickfix/doc /html/struct_f_i_x_1_1_utc_time_stamp_convertor.html。我相信,虽然我无法测试这一点,但您可以这样称呼它:

import quickfix
converter = quickfix.UtcTimeStampConverter()
string = converter.convert(obj)

Did you try obj.getYear()? It appears from the documentation that UTCTimeStamp derives from DateTime, so you should be able to access methods of the parent class.

If that doesn't work, what kind of object do you get if you do newobj = obj.next()? Did you try print obj.__doc__?

Edit: from http://www.swig.org/Doc1.3/Python.html#Python_nn27a:

This pointer value can be freely passed around to different C functions that expect to receive an object of type ... The only thing you can't do is dereference the pointer from Python.

So you need to pass it to a wrapper for a C++ function that can take a DateTime. I don't know specifically what that is, as I don't know what you're wrapping.

In this case, that is http://www.quickfixengine.org/quickfix/doc/html/struct_f_i_x_1_1_utc_time_stamp_convertor.html. I believe, although I can't test this, that you call it with:

import quickfix
converter = quickfix.UtcTimeStampConverter()
string = converter.convert(obj)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文