forked from lili10000/quant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataSource_base.py
174 lines (124 loc) · 4.51 KB
/
dataSource_base.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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# coding=utf-8
#!/usr/bin/python
from db.mysql import sqlMgr
import tushare as ts
import re
import csv
import os
class tushareBase:
__columnList = ['c1','c2','c3','c4','c5','c6','c7','c8','c9','c10','c11','c12']
__filename = 'tmp.csv'
def __init__(self):
self.sql = sqlMgr('localhost', 'root', '123456', 'stockdb')
def getStockBasics(self):
'''
获取股票基本面数据
'''
print 'getStockBasics work'
df = ts.get_stock_basics()
print 'download data ok'
for codeIndex in range(0, len(df.index)):
data = str(df.index[codeIndex])
for rowsIndex in range(0, len(df.columns)):
data += "," + str(df.ix[codeIndex, rowsIndex])
self.sql.insertBasicTable(data)
print 'get StockBasics data ok'
def getMainReport(self, year, season):
'''
获取股票业绩报告(主表)
'''
print 'getMainReport work'
df = ts.get_report_data(year, season)
df.to_csv(self.__filename)
self.__getDataComm(12, year, "MainReport")
def getProfit(self, year, season):
'''
获取股票盈利能力数据
'''
print 'getProfit work'
df = ts.get_profit_data(year, season)
df.to_csv(self.__filename)
self.__getDataCommSeason(10, year, season, "Profit")
def getGrowth(self, year, season):
'''
获取股票成长能力数据 ---待测试
'''
print 'getGrowth work'
df = ts.get_growth_data(year, season)
df.to_csv(self.__filename)
self.__getDataCommSeason(9, year, season, "Growth")
def getOperation(self, year, season):
'''
获取股票运营能力数据
'''
print 'getOperation work'
df = ts.get_operation_data(year, season)
df.to_csv(self.__filename)
self.__getDataCommSeason(9, year, season, "Operation")
def getDebtpaying(self, year, season):
'''
获取股票运营能力数据
'''
print 'getDebtpaying work'
df = ts.get_debtpaying_data(year, season)
df.to_csv(self.__filename)
self.__getDataCommSeason(9, year, season, "Debtpaying")
def getCashflow(self, year, season):
'''
获取股票运营能力数据
'''
print 'getCashflow work'
df = ts.get_cashflow_data(year, season)
df.to_csv(self.__filename)
self.__getDataCommSeason(8, year, season, "Cashflow")
def __getDataComm(self, size, year, dataName):
print ''
listIndex = size - 1
file = open(self.__filename)
file.readline()
reader = csv.reader(file)
for self.__columnList in reader:
data = ""
list = self.__columnList[1:listIndex]
for index in list:
data += index + ","
data += str(year) + "-" + self.__columnList[listIndex]
self.sql.insertMainReportTable(data)
print "get " + dataName +" OK"
file.close()
os.remove(self.__filename)
def __Debtpaying(self, data):
self.sql.insertDebtpayingTable(data)
def __Operation(self, data):
self.sql.insertOperationTable(data)
def __Growth(self, data):
self.sql.insertGrowthTable(data)
def __Profit(self, data):
self.sql.insertProfitTable(data)
def __Cashflow(self, data):
self.sql.insertCashflowTable(data)
__operator = {'Debtpaying':__Debtpaying, \
'Operation':__Operation, \
'Growth':__Growth, \
'Profit':__Profit, \
'Cashflow':__Cashflow}
def __getDataCommSeason(self, size, year, season, dataName):
print ''
file = open(self.__filename)
file.readline()
reader = csv.reader(file)
for self.__columnList in reader:
data = ""
list = self.__columnList[1:size]
for index in list:
data += index + ","
data += str(year) + "-" + str(season*3) + "-30"
self.__operator.get(dataName)(self, data)
print "get " + dataName +" OK"
file.close()
os.remove(self.__filename)
def test(self):
'''
test function work
'''
self.sql.test()