Repository navigation
Expand file tree
/
Copy pathxtextend.py
More file actions
133 lines (108 loc) · 3.88 KB
/
Copy pathxtextend.py
File metadata and controls
133 lines (108 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import json
import os
import time
from ctypes import *
class FileLock:
def __init__(this, path, auto_lock = False):
this.path = path
this.fhandle = None
if auto_lock:
this.lock()
return
def is_lock(this):
if os.path.exists(this.path):
try:
os.remove(this.path)
return False
except Exception as e:
return True
return False
def lock(this):
if this.fhandle:
raise this.fhandle
try:
this.fhandle = open(this.path, 'w')
except Exception as e:
return False
return True
def unlock(this):
if not this.fhandle:
raise this.fhandle
this.fhandle.close()
this.fhandle = None
return True
def clean(this):
if not os.path.exists(this.path):
return True
try:
if os.path.isfile(this.path):
os.remove(this.path)
return True
except Exception as e:
pass
return False
class Extender:
value_type = c_float
rank_type = c_short
def __init__(self, base_dir):
self.base_dir = os.path.join(base_dir, 'EP')
def read_config(self):
data = None
with open(os.path.join(self.file, 'config'), 'r', encoding='utf-8') as f:
data = json.loads(f.read())
if data:
self.stocklist = []
for i in range(1, len(data['stocklist']), 2):
for stock in data['stocklist'][i]:
self.stocklist.append("%s.%s" % (stock, data['stocklist'][i - 1]))
self.timedatelist = data['tradedatelist']
def read_data(self, data, time_indexs, stock_length):
res = {}
num = (sizeof(self.value_type) + sizeof(self.rank_type)) * stock_length
for time_index in time_indexs:
index = num * time_index
value_data = data[index: index + sizeof(self.value_type) * stock_length]
values = cast(value_data, POINTER(c_float))
rank_data = data[index + sizeof(self.value_type) * stock_length: index + num]
ranks = cast(rank_data, POINTER(c_short))
res[self.timedatelist[time_index]] = [(round(values[i], 3), ranks[i]) for i in range(stock_length)]
return res
def format_time(self, times):
if type(times) == str:
return int(time.mktime(time.strptime(times, '%Y%m%d'))) * 1000
elif type(times) == int:
if times < 0:
return self.timedatelist[times]
elif times < ((1 << 31) - 1):
return times * 1000
else:
return times
def show_extend_data(self, file, times):
self.file = os.path.join(self.base_dir, file + '_Xdat')
if not os.path.isdir(self.file):
return "No such file"
fs = FileLock(os.path.join(self.file, 'filelock'), False)
while fs.is_lock():
print('文件被占用')
time.sleep(1)
fs.lock()
self.read_config()
time_list = []
if not times:
time_list = self.timedatelist
elif type(times) == list:
time_list.extend([self.format_time(i) for i in times])
else:
time_list.append(self.format_time(times))
time_index = [self.timedatelist.index(time) for time in time_list if self.timedatelist.count(time) != 0]
stock_length = len(self.stocklist)
data = None
with open(os.path.join(self.file, 'data'), 'rb') as f:
data = f.read()
fs.unlock()
res = self.read_data(data, time_index, stock_length)
return self.stocklist, res
from . import xtdata as xd
def show_extend_data(file, times):
exd = Extender(os.path.join(xd.init_data_dir(), '..', 'datadir'))
return exd.show_extend_data(file, times)