dataquant 1.1.6__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ # -*- coding: UTF-8 -*-
dataquant/sql/api.py ADDED
@@ -0,0 +1,61 @@
1
+ # -*- coding: UTF-8 -*-
2
+ import warnings
3
+
4
+ import pandas as pd
5
+
6
+ from dataquant.apis.base import get_data
7
+
8
+
9
+ __all__ = [
10
+ "get_sql_data"
11
+ ]
12
+
13
+
14
+ def get_sql_data(qry_db, qry_sql, page_no=-1, page_size=-1):
15
+ """
16
+ 获取SQL查询数据
17
+
18
+ """
19
+
20
+ if qry_db is None or qry_sql is None:
21
+ warnings.warn("函数[get_sql_data]的参数(qry_db, qry_sql)为必填项")
22
+ return None
23
+
24
+ try:
25
+ if page_no == -1:
26
+ page_no = 0
27
+ if page_size == -1:
28
+ page_size = 100000
29
+
30
+ params = {
31
+ "query_db": qry_db,
32
+ "query_sql": qry_sql,
33
+ "page_no": page_no,
34
+ "page_size": page_size,
35
+ "api_type": "sql",
36
+ }
37
+
38
+ _url = "sql/get_sql_data" + "/" + qry_db
39
+ result = get_data(_url, **params)
40
+ if result.empty:
41
+ return result
42
+
43
+ total_cnt = result['query_total'][0]
44
+ while total_cnt > result.shape[0]:
45
+ params['page_no'] += 1
46
+ page_result = get_data(_url, **params)
47
+ result = pd.concat([result, page_result], axis=0)
48
+
49
+ result.reset_index(drop=True, inplace=True)
50
+ result.drop(['query_total'], axis=1, inplace=True)
51
+ columns = result.columns.tolist()
52
+ new_columns = [x.lower() for x in columns]
53
+ result.columns = new_columns
54
+
55
+ return result
56
+ except Exception as ex:
57
+ import traceback
58
+ print(traceback.print_exc())
59
+ return None
60
+
61
+
@@ -0,0 +1,5 @@
1
+ # -*- coding: UTF-8 -*-
2
+
3
+ from dataquant.utils.client import init
4
+
5
+ init()
@@ -0,0 +1,192 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ import time
4
+ import random
5
+ import warnings
6
+
7
+ _CLIENT = None
8
+
9
+
10
+ def get_client():
11
+ global _CLIENT
12
+ if _CLIENT is None:
13
+ init()
14
+
15
+ return _CLIENT
16
+
17
+
18
+ def init(**kwargs):
19
+ """
20
+ :param str username: 用户名,license模式下为'license'
21
+ :param str password: 用户密码,license模式下为下发的license
22
+ :param str protocol:传输协议,默认HTTP
23
+ :param str url: 数据服务的URL
24
+ :param int connect_timeout: 连接建立超时时间,默认5秒
25
+ :param int request_timeout: 数据传输超时时间,默认300秒
26
+ :param str compressor: 数据传输过程中的压缩算法,默认不使用
27
+ :param int pool_size: 连接池大小,默认10
28
+ :param int page_size: 数据请求分页大小,默认100000
29
+ :return:
30
+ """
31
+
32
+ from dataquant.utils.config import read_config, write_config
33
+
34
+ # 获取系统配置
35
+ kwargs_num = len(kwargs)
36
+ retry_times = 0
37
+ load_succ = False
38
+ while not load_succ and retry_times <= 3:
39
+ try:
40
+ conf = read_config()['system']
41
+ load_succ = True
42
+ except Exception as ex:
43
+ time.sleep(random.random()*0.5+0.2)
44
+ retry_times = retry_times + 1
45
+ if not load_succ and retry_times > 3:
46
+ # 读取配置失败,使用默认配置启动
47
+ warnings.warn("读取配置失败,请检查dataquant.utils.config.yml")
48
+
49
+ user = kwargs.pop("username", None)
50
+ if user is None:
51
+ user = conf.get("username")
52
+ else:
53
+ conf["username"] = user
54
+ assert user
55
+
56
+ pwd = kwargs.pop("password", None)
57
+ if pwd is None:
58
+ pwd = conf.get("password")
59
+ else:
60
+ conf["password"] = pwd
61
+ assert pwd
62
+
63
+ url = kwargs.pop("url", None)
64
+ if url is None:
65
+ url = conf.get("url")
66
+ else:
67
+ conf["url"] = url
68
+ assert url
69
+
70
+ protocol = kwargs.pop("protocol", None)
71
+ if protocol is None:
72
+ protocol = conf.get("protocol", "HTTP")
73
+ else:
74
+ if protocol != "HTTP":
75
+ warnings.warn("protocol 当前只支持HTTP,设置的{}将不会生效".format(protocol))
76
+ else:
77
+ conf["protocol"] = "HTTP"
78
+
79
+ con_timeout = kwargs.pop("connect_timeout", None)
80
+ if con_timeout is None:
81
+ con_timeout = conf.get("connect_timeout", 5)
82
+ else:
83
+ if int(con_timeout) <= 0 or int(con_timeout) > 1000:
84
+ warnings.warn("connect_timeout 有效范围为(0, 1000],设置的{}将不会生效".format(con_timeout))
85
+ else:
86
+ conf["connect_timeout"] = con_timeout
87
+
88
+ timeout = kwargs.pop("request_timeout", None)
89
+ if timeout is None:
90
+ timeout = conf.get("request_timeout", 300)
91
+ else:
92
+ if int(timeout) <= 0 or int(timeout) > 100000:
93
+ warnings.warn("request_timeout 有效范围为(0, 100000],设置的{}将不会生效".format(timeout))
94
+ else:
95
+ conf["request_timeout"] = timeout
96
+
97
+ pool_size = kwargs.pop("pool_size", None)
98
+ if pool_size is None:
99
+ pool_size = conf.get("pool_size", 10)
100
+ else:
101
+ if int(pool_size) <= 0 or int(pool_size) > 100:
102
+ warnings.warn("pool_size 有效范围为(0, 100],设置的{}将不会生效".format(pool_size))
103
+ else:
104
+ conf["pool_size"] = pool_size
105
+
106
+ compressor = kwargs.pop("compressor", None)
107
+ if compressor is None:
108
+ compressor = conf.get("compressor", None)
109
+ else:
110
+ conf["compressor"] = compressor
111
+
112
+ page_size = kwargs.pop("page_size", None)
113
+ if page_size is None:
114
+ page_size = conf.get("page_size", 100000)
115
+ else:
116
+ if int(page_size) <= 0 or int(page_size) > 100000:
117
+ warnings.warn("page_size 有效范围为(0, 100000],设置的{}将不会生效".format(page_size))
118
+ else:
119
+ conf["page_size"] = page_size
120
+
121
+ quote_parallel = kwargs.pop("quote_parallel", None)
122
+ if quote_parallel is None:
123
+ quote_parallel = conf.get("quote_parallel", 50)
124
+ conf["quote_parallel"] = quote_parallel
125
+
126
+ quote_format = kwargs.pop("quote_format", None)
127
+ if quote_format is None:
128
+ quote_format = conf.get("quote_format", "arrow")
129
+ if quote_format.lower() in ('a', 'arrow'):
130
+ quote_format = "Arrow"
131
+ elif quote_format.lower() in ('p', 'parquet'):
132
+ quote_format = "Parquet"
133
+ else:
134
+ raise ValueError("Unknown quote_format, available values are:a for Arrow,p for Parquet")
135
+
136
+ conf["quote_format"] = quote_format
137
+
138
+ parallel_mode = kwargs.pop("parallel_mode", None)
139
+ if parallel_mode is None:
140
+ parallel_mode = conf.get("parallel_mode", "t")
141
+ if parallel_mode.lower() in ('t', 'thread'):
142
+ parallel_mode = "Thread"
143
+ elif parallel_mode.lower() in ('c', 'coroutine'):
144
+ parallel_mode = "Coroutine"
145
+ else:
146
+ raise ValueError("Unknown quote_format, available values are:t for Thread,c for Coroutine")
147
+
148
+ conf["parallel_mode"] = parallel_mode
149
+
150
+ config = {
151
+ "protocol": protocol,
152
+ "auth": {
153
+ "username": user,
154
+ "password": pwd,
155
+ },
156
+ "request_timeout": timeout,
157
+ "connect_timeout": con_timeout,
158
+ "url": url,
159
+ "pool_size": pool_size,
160
+ "compressor": compressor,
161
+ "page_size": page_size,
162
+ "quote_format": quote_format,
163
+ }
164
+
165
+ if protocol == "HTTP":
166
+ from dataquant.utils.connection_pool import ConnectionPool
167
+ global _CLIENT
168
+ _CLIENT = _CLIENT = ConnectionPool(config)
169
+ else:
170
+ raise RuntimeError("传输协议无效,got protocol[{}]".format(protocol))
171
+
172
+ # 保存配置文件
173
+ if kwargs_num > 0:
174
+ write_config({'system': conf})
175
+ #
176
+ from dataquant.apis.base.api import load_conf as load_base_conf
177
+ from dataquant.apis.quote.api import load_conf as load_quote_conf
178
+ load_base_conf()
179
+ load_quote_conf()
180
+ return
181
+
182
+
183
+ def environ():
184
+ """
185
+ 获取环境配置信息
186
+ :return:
187
+ """
188
+ global _CLIENT
189
+ if _CLIENT:
190
+ return _CLIENT.config
191
+
192
+ return {}
@@ -0,0 +1,23 @@
1
+ # -*- coding: utf-8 -*-
2
+ from enum import Enum
3
+
4
+ # 服务交互字段定义
5
+ ERROR_NO = "error_no" # 错误代码域
6
+ ERROR_INFO = "error_info" # 错误信息域
7
+ DATA = "data" # 数据域
8
+
9
+
10
+ class ConnectionStatus(Enum): #连接代码
11
+ Disconnected = 0x0000 # 未连接
12
+ Connecting = 0x0001, # 正在连接
13
+ Connected = 0x0002, # 已连接
14
+ SafeConnecting = 0x0004, # 正在建立安全连接
15
+ SafeConnected = 0x0008, # 已建立安全连接
16
+ Registering = 0x0010, # 正注册
17
+ Registered = 0x0020, # 已注册
18
+ Rejected = 0x0040 # 被拒绝,将被关闭
19
+
20
+
21
+ class Protocol(Enum): #连接类型
22
+ HTTP = "HttpConnection"
23
+ TCP = "TCPConnection"
@@ -0,0 +1,29 @@
1
+ # -*- coding: utf-8 -*-
2
+ import os
3
+ import yaml
4
+
5
+ from dataquant.utils.decorators import lru_cache
6
+
7
+
8
+ def load_yml(path):
9
+ with open(path, encoding='utf-8') as pf:
10
+ return yaml.safe_load(pf)
11
+
12
+
13
+ def save_yml(full_path, _yml):
14
+ with open(full_path, encoding='utf-8', mode='w') as pf:
15
+ return yaml.safe_dump(_yml, pf)
16
+
17
+
18
+ @lru_cache(128)
19
+ def read_config():
20
+ path = os.path.join(os.path.dirname(__file__), ".", "config.yml")
21
+ return load_yml(path)
22
+
23
+
24
+ def write_config(_config):
25
+ conf = read_config()['system']
26
+ import operator
27
+ if operator.ne(_config, conf):
28
+ path = os.path.join(os.path.dirname(__file__), ".", "config.yml")
29
+ return save_yml(path, _config)
@@ -0,0 +1,30 @@
1
+ system:
2
+ bind_method: socket
3
+ bind_port: 47317
4
+ compressor: null
5
+ connect_timeout: 5
6
+ page_size: 990000
7
+ parallel_mode: Thread
8
+ password: 19515beb
9
+ pool_size: 10
10
+ protocol: HTTP
11
+ quote_format: Arrow
12
+ quote_hf_bond_sort: scr_num,date_time
13
+ quote_hf_fund_sort: scr_num,date_time
14
+ quote_hf_future_sort: scr_num,date_time
15
+ quote_hf_stock_sort: scr_num,date_time
16
+ quote_hf_hkstock_sort: scr_num,date_time
17
+ quote_kd_bond_sort: scr_num,date_time
18
+ quote_kd_fund_sort: scr_num,date_time
19
+ quote_kd_future_sort: scr_num,date_time
20
+ quote_kd_stock_sort: scr_num,date_time
21
+ quote_km_bond_sort: scr_num,date_time,min_time
22
+ quote_km_fund_sort: scr_num,date_time,min_time
23
+ quote_km_future_sort: scr_num,date_time,min_time
24
+ quote_km_stock_sort: scr_num,date_time,min_time
25
+ quote_parallel: 20
26
+ request_timeout: 300
27
+ uote_hf_bond_sort: scr_num,date_time
28
+ uote_hf_fund_sort: scr_num,date_time
29
+ url: http://10.56.37.27:8082/bdataspi/spi/1.0/dataservice
30
+ username: license