Accessing UtcTimeStamp from Python via SWIG
Asked Answered
H

2

5

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?

Hardnosed answered 22/7, 2011 at 20:32 Comment(3)
Did you figure out what c function in your library could take the timestamp pointer from you?Wideranging
no I'm still a bit at a loss on how to continue...Hardnosed
Once you import quickfix you should be able to find the right way to call by looking at quickfix.__dict__ then the __dict__ of the class (in case I got the capitalization wrong or something) in my edit.Wideranging
N
4

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')
Newsman answered 7/2, 2013 at 16:21 Comment(1)
This is a very good answer, and is what I do myself. However, you should be aware that strptime can fail in certain cases. This is a known bug in python involving multithreading, see #16310150 When this happens you can workaround by just calling qfSendingTime.getString() and forgetting about strptime. Then cast your date string into a different type when you need to use it for somethingCornered
W
3

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)
Wideranging answered 22/7, 2011 at 20:47 Comment(2)
yes, I've tried obj.getYear() and received: AttributeError: 'SwigPyObject' object has no attribute 'getYear'Hardnosed
print obj.__doc__ returns Swig object carries a C/C++ instance pointerHardnosed

© 2022 - 2024 — McMap. All rights reserved.