Skip to content

Commit

Permalink
replaced _swig_repr
Browse files Browse the repository at this point in the history
  • Loading branch information
keli committed Jun 12, 2019
1 parent 9e47c74 commit 7f2992a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ [email protected]:keli/ctp-python.git
cd ctp-python
```

安装
```
python setup.py install
```
Expand All @@ -17,7 +18,7 @@ python setup.py install

1. 修改setup.py中的API_VER的值为'6.3.15'

2. 最好swig重新生成一下源码
2. 用swig重新生成一下源码

```
cd ctp-python
Expand All @@ -34,6 +35,7 @@ pytest -s tests/test_trader.py --front=tcp://180.168.146.187:13030 --broker=<bro
## 其他事项

- 本项目中CTP返回的GBK编码字符串已经全部自动转换为UTF-8
- 市场数据中的极大值代表无数据,为可读性起见打印整个结构体时会显示为None
- 目前只支持了Python 3,测试环境Linux
- 目前只在simnow上的终端厂商测试环境测通了trader。
- simnow以及大部分券商的测试环境是用的6.3.13版本,版本不同将无法调用OnFrontConnected。
Expand Down
23 changes: 23 additions & 0 deletions ctp.i
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,29 @@
//#define SWIG_PYTHON_STRICT_BYTE_CHAR
//%}

%pythonbegin %{
from sys import float_info
%}

%pythoncode %{
def _swig_repr(self):
values = []
for k in vars(self.__class__):
if not k.startswith('_'):
v = getattr(self, k)
if isinstance(v, float):
if v == float_info.max:
values.append("%s: None" % k)
else:
values.append("%s: %.2f" % (k, v))
elif isinstance(v, int):
values.append("%s: %i" % (k, v))
else:
values.append('%s: "%s"' % (k, v))

return "<%s.%s; %s>" % (self.__class__.__module__, self.__class__.__name__, ', '.join(values))
%}

%{
#define SWIG_FILE_WITH_INIT
#include "ThostFtdcUserApiDataType.h"
Expand Down
21 changes: 21 additions & 0 deletions ctp.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
# the SWIG interface file instead.


from sys import float_info





Expand Down Expand Up @@ -105,6 +108,24 @@ class _object:
weakref_proxy = lambda x: x



def _swig_repr(self):
values = []
for k in vars(self.__class__):
if not k.startswith('_'):
v = getattr(self, k)
if isinstance(v, float):
if v == float_info.max:
values.append("%s: None" % k)
else:
values.append("%s: %.2f" % (k, v))
elif isinstance(v, int):
values.append("%s: %i" % (k, v))
else:
values.append('%s: "%s"' % (k, v))

return "<%s.%s; %s>" % (self.__class__.__module__, self.__class__.__name__, ', '.join(values))

THOST_TERT_RESTART = _ctp.THOST_TERT_RESTART
THOST_TERT_RESUME = _ctp.THOST_TERT_RESUME
THOST_TERT_QUICK = _ctp.THOST_TERT_QUICK
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
API_VER='6.3.13'
API_DIR='api/' + API_VER
API_LIBS=glob.glob(API_DIR + '/*.so')
API_NAMES=[os.path.basename(path)[3:-3] for path in API_LIBS]

def get_install_data_dir():
d = dist.Distribution()
Expand All @@ -28,7 +29,7 @@ def run(self):
library_dirs=[API_DIR],
extra_link_args=['-Wl,-rpath,$ORIGIN'],
#libraries=['thostmduserapi', 'thosttraderapi'],
libraries=['thostmduserapi_se', 'thosttraderapi_se'],
libraries=API_NAMES,
language='c++',
#swig_opts=['-py3', '-c++', '-threads', '-I./' + API_DIR],
)
Expand Down

0 comments on commit 7f2992a

Please sign in to comment.