-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStock_config_kit.py
103 lines (84 loc) · 3.02 KB
/
Stock_config_kit.py
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
import json
import os
import codecs
absfilep=os.path.dirname(__file__)
'''
{
"code": [
"abcdcd",
"sadfasdfsdaf",
"asdfasdf"
],
"instruction_mode": 1,
"modifytime": "",
"DefaultPath": "C:/Users/macbook/Desktop/",
"author": "aurora",
"MacroSavingPath": "C:/Users/macbook/Desktop/研究数据/个股/AutoMacro/",
"StockSavingpath": "C:/Users/macbook/Desktop/研究数据/股票历史/",
"DataSavingtype": "excel",
"Force": 0
}
'''
class configfile(json.JSONEncoder):
configf=None
configstr=None
path=None
configjson=None
def __init__(self,filepath):
if os.path.isfile(filepath):
self.path=filepath
elif os.path.isfile(os.path.join(absfilep,filepath)):
self.path=os.path.join(absfilep,filepath)
else:
raise Exception( 'the format of filepath is not correct or the file in:%s, %s, %s is not existed'%(filepath,os.path.join(os.getcwd(),filepath),os.path.join(absfilep,filepath) ) )
def _open(self):
enc='utf-8'
self.configf=codecs.open(self.path,encoding=enc,mode='r+')
self.configstr=self.configf.read()
self.configjson=json.loads(self.configstr)
#print(self.configjson)
def KW_modify(self,**arg):
#或用迭代器arg.iteritems(), 迭代字典
self._open()
for Na, Va in arg.items():
if 'code DataSavingtype instruction_mode Force StockSavingpath MacroSavingPath DefaultPath DataSavingtype'.find(Na) != -1 and Na!=' ':
self.configjson[Na]=Va
def KW_save_config(self):
# use with self.configf as f:
if not self.configf.closed:
self.configstr=json.dumps(self.configjson,ensure_ascii=False,indent=4,sort_keys=True)
self.configf=deleteContent(self.configf)
self.configf.write(self.configstr)
self.configf.close()
else:
raise Exception('Make sure we iniial the file')
def Set_Stock_Download(self):
self._open()
RE=[]
Keysets='code DataSavingtype instruction_mode Force StockSavingpath MacroSavingPath DefaultPath'
for key in Keysets.split(' '):
RE.append(self.configjson[key])
self.configf.close()
return RE
def deleteContent(pfile):
pfile.seek(0)
pfile.truncate()
return pfile
def test():
path='C:/Users/macbook/Anaconda3/MyCode/StockP_config.json'
path1='StockP_config.json'
a=configfile(path)
b=configfile(path1)
a.KW_modify(code=['abcdcd','sadfasdfsdaf','asdfasdf'])
a.KW_save_config()
[code,DataSavingtype,I_Mode,Force,StockSavingpath,MacroSavingPath,DefaultPath]=a.Set_Stock_Download()
print(code)
print(DataSavingtype)
print(DefaultPath)
print(I_Mode)
print(Force)
print(StockSavingpath)
print(MacroSavingPath)
if __name__ =='__main__':
test()
print("----------------------Clear----------------------")