xtquant.xtextend

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