通过SWIG从Python访问UtcTimeStamp

2024-09-24 02:17:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我想这是一个python vs 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>

如何从中提取数据?在


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

但是我还是不知道如何访问数据。DateTime有一些简单的getter函数,比如getYear()——但是,如何访问它们呢?在


Tags: 数据对象objreducedatetimetypedir时间
2条回答

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

如果这不起作用,你得到什么样的物体?你试过print obj.__doc__?

编辑:来自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.

因此,您需要将其传递给C++函数的包装器,该函数可以使用^ {< CD3>}。我不知道具体是什么,因为我不知道你在包装什么。在

在本例中,即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)

不要在时间字段上使用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')

相关问题 更多 >