Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
T
titan
概览
Overview
Details
Activity
Cycle Analytics
版本库
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
问题
0
Issues
0
列表
Board
标记
里程碑
合并请求
0
Merge Requests
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
Snippets
成员
Collapse sidebar
Close sidebar
活动
图像
聊天
创建新问题
作业
提交
Issue Boards
Open sidebar
改变
titan
Commits
eb531ce4
Commit
eb531ce4
authored
Feb 03, 2021
by
jscat
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update
1. 添加update_log功能
parent
dbbe6cf2
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
23 行增加
和
10 行删除
+23
-10
titan_data/titan_data_function.py
+22
-9
titan_data/titan_table_structure.py
+1
-1
没有找到文件。
titan_data/titan_data_function.py
查看文件 @
eb531ce4
...
...
@@ -2,11 +2,14 @@ import pandas as pd
import
tushare
as
ts
import
baostock
as
bs
import
pymysql
import
time
import
calendar
from
datetime
import
datetime
import
titan_data_settings
as
settings
from
urllib
import
parse
from
sqlalchemy
import
create_engine
from
titan_table_structure
import
Base
from
dateutil.relativedelta
import
relativedelta
'''
author: jscat 2021/02/02
...
...
@@ -22,20 +25,25 @@ class Data:
def
__init__
(
self
,
token
):
# 创建请求数据线程
self
.
token
=
token
self
.
engine
=
create_engine
(
settings
.
mysql_url
,
encoding
=
'utf-8'
,
echo
=
Tru
e
)
self
.
engine
=
create_engine
(
settings
.
mysql_url
,
encoding
=
'utf-8'
,
echo
=
Fals
e
)
# truncate数据并且append
def
truncate_append
(
self
,
data
,
table_name
):
"""删除mysql表所有数据,to_sql追加新数据"""
conn
=
self
.
engine
.
connect
()
conn
.
execute
(
'truncate '
+
table_name
)
print
(
data
)
data
.
to_sql
(
table_name
,
self
.
engine
,
if_exists
=
'append'
,
index
=
False
)
self
.
engine
.
dispose
()
# keep数据并且append
def
keep_append
(
self
,
data
,
table_name
):
"""保留mysql表所有数据,to_sql追加新数据"""
data
.
to_sql
(
table_name
,
self
.
engine
,
if_exists
=
'append'
,
index
=
False
)
self
.
engine
.
dispose
()
# 同步更新日志表
def
update_log
(
self
,
table_name
,
info
):
data
=
pd
.
DataFrame
({
'TARGET_TABLE'
:
table_name
,
'UPDATE_INFO'
:
info
,
'CREATE_DT'
:
str
(
datetime
.
now
())},
index
=
[
0
])
data
.
to_sql
(
"tbl_update_log"
,
self
.
engine
,
if_exists
=
'append'
,
index
=
False
)
'''
start_date='20210101', end_date='20311231'
...
...
@@ -83,6 +91,8 @@ class Data:
self
.
truncate_append
(
df_all
,
'tbl_AShareCalendar'
)
else
:
self
.
keep_append
(
df_all
,
'tbl_AShareCalendar'
)
self
.
update_log
(
'tbl_AShareCalendar'
,
"update record: "
+
str
(
start_date
)
+
"_"
+
str
(
end_date
))
self
.
engine
.
dispose
();
# print('{}成功导入数据库'.format(date))
# Deprecated
...
...
@@ -137,6 +147,7 @@ class Data:
:param date:
:return:本月第一天日期和本月最后一天日期
"""
date
=
str
(
date
)
if
date
.
count
(
'-'
)
!=
2
:
raise
ValueError
(
'- is error'
)
year
,
month
=
str
(
date
)
.
split
(
'-'
)[
0
],
str
(
date
)
.
split
(
'-'
)[
1
]
...
...
@@ -148,10 +159,12 @@ class Data:
if
__name__
==
'__main__'
:
data
=
Data
(
settings
.
ts_token
)
Base
.
metadata
.
create_all
(
data
.
engine
)
data
.
get_AShareCalendar
(
"2015-01-01"
,
"2015-12-31"
,
0
)
data
.
get_AShareCalendar
(
"2016-01-01"
,
"2016-12-31"
,
0
)
data
.
get_AShareCalendar
(
"2017-01-01"
,
"2017-12-31"
,
0
)
data
.
get_AShareCalendar
(
"2018-01-01"
,
"2018-12-31"
,
0
)
data
.
get_AShareCalendar
(
"2019-01-01"
,
"2019-12-31"
,
0
)
data
.
get_AShareCalendar
(
"2020-01-01"
,
"2020-12-31"
,
0
)
start
=
"2019-02-01"
for
i
in
range
(
11
+
2
):
date
=
pd
.
to_datetime
(
start
)
+
relativedelta
(
months
=+
i
)
# 当前日期往后推i个月
date_str
=
date
.
strftime
(
"
%
Y-
%
m-
%
d"
)
start_date
,
end_date
=
data
.
get_current_month_start_and_end
(
date
)
print
(
start_date
,
end_date
)
data
.
get_AShareCalendar
(
start_date
=
start_date
,
end_date
=
end_date
,
type
=
1
)
time
.
sleep
(
10
)
titan_data/titan_table_structure.py
查看文件 @
eb531ce4
...
...
@@ -9,7 +9,7 @@ from sqlalchemy import Column, Integer, String
import
titan_data_settings
as
settings
# 创建连接
engine
=
create_engine
(
settings
.
mysql_url
,
encoding
=
'utf-8'
,
echo
=
Tru
e
)
engine
=
create_engine
(
settings
.
mysql_url
,
encoding
=
'utf-8'
,
echo
=
Fals
e
)
# 生成orm基类
Base
=
declarative_base
()
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论