Guest User

Untitled

a guest
May 20th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.46 KB | None | 0 0
  1. def retry(f, n_attempts=3):
  2. # 包装函数,在函数异常时尝试多次调用,这里是尝试3次
  3. def wrapper(*args, **kwargs):
  4. for i in range(n_attempts):
  5. try:
  6. return f(*args, **kwargs)
  7. except Exception:
  8. if i == n_attempts - 1:
  9. raise
  10. return wrapper
  11.  
  12. # 金融数据包
  13. import tushare as ts
  14. # 避免某次调用get_hs300s方法失败
  15. hs300 = retry(ts.get_hs300s)()
Add Comment
Please, Sign In to add comment