# -*- coding: UTF-8 -*-
import pymysql
import datetime

def con_sql(sql):
	#从数据库的表里获取数据
	"""
	:type sql : str
	:rtype : tuple
	"""
	db = pymysql.connect(host='10.66.157.22', port=4000, user='root', passwd='3SYz54LS9#^9sBvC', db='jerry_prod')
	cursor = db.cursor()
	cursor.execute(sql)
	result = cursor.fetchall()
	db.close()
	return result

def tuple2dict(tuple_result):
	#把sql结果从tuple(tuple,tuple)格式转换成dict格式
	"""
	:type tuple_result : tuple
	:rtype : dict
	"""
	dict_result = {}
	for i in range(len(tuple_result)):
		dict_result[tuple_result[i][0]] = tuple_result[i][1]
	return dict_result

def get_yesterday_date():
	#自动获取昨天的日期,如"2018-08-08"
	"""
	:rtype : str
	"""
	today = datetime.date.today()
	yesterday = today - datetime.timedelta(days=1)
	yesterday = yesterday.strftime("%Y-%m-%d")
	return yesterday

def get_between_day(begin_date,end_date):
	#获取指定时间范围内的date
	"""
	type begin_date : str  eg:'2018-08-29'
	type end_date : str  eg:'2018-09-01'
	rtype : list  eg:['2018-08-29','2018-08-30','2018-08-31','2018-09-01']
	"""
	date_list = []  
	begin_date = datetime.datetime.strptime(begin_date, "%Y-%m-%d")
	end_date = datetime.datetime.strptime(end_date, "%Y-%m-%d")
	while begin_date <= end_date:  
		date_str = begin_date.strftime("%Y-%m-%d")  
		date_list.append(date_str)  
		begin_date += datetime.timedelta(days=1)  
	return date_list