From 5f44063755ff29dcf7752318722a3fdbf4b03eee Mon Sep 17 00:00:00 2001 From: Wei guangjing Date: Thu, 9 Jun 2022 12:03:03 +0800 Subject: [PATCH] init --- README.md | 40 ++++++++++++ pyTSLPy.py | 175 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 215 insertions(+) create mode 100644 README.md create mode 100644 pyTSLPy.py diff --git a/README.md b/README.md new file mode 100644 index 0000000..2b9cad4 --- /dev/null +++ b/README.md @@ -0,0 +1,40 @@ +# pyTSLPy + +用pyTSL模拟TSLPy。 + + + +## 范例 + + + +```python +import pyTSLPy as ts + +ts.SetConnectConfig('tslclient.ini') +dl = ts.DefaultConnectAndLogin('test') +if dl[0] == 0: + print("登陆成功") + data = ts.RemoteExecute("return 'return a string';", {}) # 执行一条语句 + print("数据:", data) + Close = ts.RemoteExecute("return close();", {"StockID": "SH000001"}) + print("Close:", Close) + ts.Disconnect() # 断开连接 +else: + print(dl[1]) +``` + +```python +import pyTSLPy as ts + +ts.ConnectServer('tsl.tinysoft.com.cn', 443) +dl = ts.LoginServer('user', 'password') +if dl[0] == 0: + print("登陆成功") + data = ts.RemoteExecute("return 'return a string';", {}) # 执行一条语句 + print("数据:", data) + ts.Disconnect() # 断开连接 +else: + print(dl[1]) +``` + diff --git a/pyTSLPy.py b/pyTSLPy.py new file mode 100644 index 0000000..479df00 --- /dev/null +++ b/pyTSLPy.py @@ -0,0 +1,175 @@ +# coding: utf8 +import pyTSL +import datetime +import configparser + +connectConfig = {} +defaultConnection = None +connectConfigFile = None + + +def SetConnectConfig(fn): + global connectConfigFile + connectConfigFile = fn + + +def connectOptions(alias): + global connectConfigFile + if connectConfigFile: + f = connectConfigFile + else: + f = 'tslclient.ini' + config = configparser.ConfigParser() + config.read(f) + if alias in config: + return config[alias] + return {} + + +def DefaultConnectAndLogin(alias): + global defaultConnection + opts = connectOptions(alias) + if opts: + defaultConnection = pyTSL.Client( + opts['LoginName'] + , opts['LoginPass'] + , opts['Address'] + , int(opts['Port']) + ) + if defaultConnection.login(): + return 0, '' + else: + return defaultConnection.last_error() + + +def ConnectServer(host, port, proxy={}): + global connectConfig + connectConfig['host'] = host + connectConfig['port'] = port + connectConfig['proxy'] = proxy + + +def LoginServer(user, passwd): + global defaultConnection + host = connectConfig['host'] + port = connectConfig['port'] + defaultConnection = pyTSL.Client(user, passwd, host, port) + if defaultConnection.login(): + return 0, '' + else: + return defaultConnection.last_error() + + +def Disconnect(): + global defaultConnection + if defaultConnection: + defaultConnection.logout() + + +def Logined(): + global defaultConnection + if defaultConnection: + return defaultConnection.login() + return 0 + + +def SetService(service): + global defaultConnection + if defaultConnection: + defaultConnection.default_service(service) + + +def SetComputeBitsOption(opt): + pass + + +def GetComputeBitsOption(): + pass + + +def GetService(): + global defaultConnection + if defaultConnection: + defaultConnection.default_service() + + +def parse_params(params): + pp = {} + if 'StockID' in params: + pp['stock'] = params['StockID'] + if 'Cycle' in params: + pp['cycle'] = params['Cycle'] + if 'CurrentDate' in params: + pp['time'] = params['CurrentDate'] + if 'bRate' in params: + pp['rate'] = params['bRate'] + if 'RateDay' in params: + pp['rateday'] = params['RateDay'] + if 'nDay' in params: + pp['nday'] = params['nDay'] + if 'Precision' in params: + pp['precision'] = params['Precision'] + if 'ReportMode' in params: + pp['reportmode'] = params['ReportMode'] + if 'EmptyMode' in params: + pp['emptymode'] = params['EmptyMode'] + if 'viewpoint' in params: + pp['viewpoint'] = params['viewpoint'] + return pp + + +def RemoteExecute(script, params): + global defaultConnection + if defaultConnection: + pp = parse_params(params) + r = defaultConnection.exec(script, **pp) + return r.error(), r.value() + return -1, '连接错误' + + +def RemoteCallFunc(func, args, params): + global defaultConnection + if defaultConnection: + pp = parse_params(params) + r = defaultConnection.clas(func, *args, **pp) + return r.error(), r.value() + return -1, '连接错误' + + +def SetSysParam(): + assert 0 + + +def GetSysParam(): + assert 0 + + +def EncodeDate(y, m, d): + return pyTSL.DatetimeToDouble(datetime.datetime(y, m, d)) + + +def EncodeTime(h, m, s, ss): + return h / 24.0 + m / 24.0 / 60.0 + s / 24.0 / 3600.0 + ss / 24.0 / 3600000.0 + + +def EncodeDateTime(Y, M, D, h, m, s, ss): + return pyTSL.DatetimeToDouble(datetime.datetime(Y, M, D, h, m, s, ss * 1000)) + + +def DecodeDate(dt): + d = pyTSL.DoubleToDatetime(dt) + return d.year, d.month, d.day + + +def DecodeTime(dt): + d = pyTSL.DoubleToDatetime(dt) + return d.hour, d.minute, d.second, d.microsecond * 1000 + + +def DecodeDateTime(dt): + d = pyTSL.DoubleToDatetime(dt) + return d.year, d.month, d.day, d.hour, d.minute, d.second, d.microsecond * 1000 + + +if __name__ == '__main__': + pass