def start(self): # 设置读取数据的开始、结束日期 start_date = '' end_date = Dater.today() # 获取本地数据查看是否需要更新 record = session.query(StockDaily).order_by( StockDaily.id.desc()).first() if record: start_date = Dater.offset(record.trade_date, 1) if start_date > end_date: return # 获取交易日历中开始到结束日期范围内的开市 records = session.query(TradeCalendar).filter( and_(TradeCalendar.cal_date >= start_date, TradeCalendar.cal_date <= end_date, TradeCalendar.is_open == 1)).all() # 根据日期记录循环读取 api 数据,存入本地数据库 for row in records: ta_data = self.__get_data(row.cal_date) if not ta_data.empty: ta_data.to_sql('stock_daily', engine, if_exists='append', index=False)
def start(self): """ 更新 trade_calendar 表 """ # 设置读取数据的开始、结束日期 start_date = '' end_date = Dater.month_end() # 获取本地数据查看数据是否需要更新 record = session.query(TradeCalendar).order_by( TradeCalendar.id.desc()).first() if record: start_date = Dater.offset(record.cal_date, 1) if start_date > end_date: return # 对比最新日期和目标日期,继续补足目标数据 ta_data = pd.DataFrame() while start_date < Dater.offset(end_date, 7): ts_data = self.__get_data(start_date, Dater.offset(end_date, 7)) if not ts_data.empty: ta_data = pd.concat([ta_data, ts_data], ignore_index=True) latest_date = ta_data['cal_date'].iloc[-1] start_date = Dater.offset(latest_date, 1) pass # 将还需更新的数据存入数据库 if not ta_data.empty: ta_data = self.update_weekly_monthly(ta_data, end_date) ta_data.to_sql('trade_calendar', engine, if_exists='append', index=False)
def inner_wrapper(*args, **kargs): # 如果记录中对应表近日已更新直接返回 record = session.query(UpdateRecord).filter( UpdateRecord.table == model.__tablename__).first() if record and record.last_updating == Dater.today(): return # 执行被包装的表更新任务 fn(*args, **kargs) # 表更新完毕在记录更新日期的表中修改最近更新日期 if record: record.last_updating = Dater.today() else: record = UpdateRecord(table=model.__tablename__, last_updating=Dater.today()) session.add(record) session.commit()
def suggest(fulltext=None, line=None, station=None, order=None, gowild=lambda s: s if str(s).count('%') else '%%%s%%'%s): """ Returns matching line and stations. Search uses a "LIKE %string%" comparator. :param fulltext: filters all searchable fields with an OR operator :param line: filters on line name :param station: filters on station name """ # Querying q = s.query(m.stop).join(m.line, m.station) searchable = { 'line': m.line.name, 'station': m.station.name } for arg,column in searchable.items(): if locals().get(arg): string = locals().get(arg) if arg not in ['line']: string = gowild(string) q = q.filter(column.like(string)) if fulltext: q = q.filter(or_(*[column.like(gowild(fulltext)) for column in searchable.values()])) if not order: order = (m.station.name, m.line.name, m.stop.direction) q = q.order_by(*order) # Results processing return [{ 'line': r.line.name, 'station': r.station.name, 'root': r.station.root, 'direction': r.direction, #FIXME: must be the terminus stop station name 'orientation': r.orientation, 'lon': r.geostop_collection[0].lon if r. geostop_collection else None, 'lat': r.geostop_collection[0].lat if r. geostop_collection else None } for r in q.all()]
def get_movie_by_id(id): return session.query(Movie).filter(Movie.movie_id==id).first()
def get_all_names(): return session.query(Name).all()
def get_all_users(): return session.query(User).all()