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__', '__开发者_StackOverflowint__', '__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?
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')
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)
精彩评论