清除屏幕,然后在循环中准备显示新数据之前执行?

huangapple go评论99阅读模式
英文:

Clear the screen right before new data is ready to be displayed while in a loop?

问题

I am using the yfinance library to build a stock ticker that checks a variety of stock prices and their change. I have no problem with that.

The problem I am having is a nicety: only clearing the screen after I have new data to display (both stocks showing, then cleared, and immediately updated). Here is my current code. I have tried os.system('clear') after time.sleep(1) but there is a delay before the new data has been obtained and displayed. If I try to 'clear' before the print, it clears the first stock before the second is displayed. I even went down the very interesting rabbithole of using ANSI code like print('\033[1A', end='\x1b[2K').

import yfinance as yf
import time
import os

stocks = ["SPY", "TMUS"]

def check():
for stock in stocks:
l = yf.Ticker(stock)
change = (l.info["regularMarketOpen"] - l.info["bid"])
if change is not None:
print(l.info["symbol"], ": ", l.info["bid"], f' Change: %f' % change)

def main():
while True:
check()
time.sleep(1)
os.system('clear')

if name == 'main':
main()

英文:

I am using the yfinance library to build a stock ticker that checks a variety of stock prices and their change. I have no problem with that.

The problem I am having is a nicety: only clearing the screen after I have new data to display (both stocks showing, then cleared, and immediately updated). Here is my current code. I have tried os.system('clear') after time.sleep(1) but there is a delay before the new data has been obtained and displayed. If I try to 'clear' before the print, it clears the first stock before the second is displayed. I even went down the very interesting rabbithole of using ANSI code like print('\033[1A', end='\x1b[2K').

  1. import yfinance as yf
  2. import time
  3. import os
  4. stocks = ["SPY", "TMUS"]
  5. def check():
  6. for stock in stocks:
  7. l = yf.Ticker(stock)
  8. change = (l.info["regularMarketOpen"] - l.info["bid"])
  9. if change is not None:
  10. print(l.info["symbol"],": ", l.info["bid"], f' Change: %f' % change)
  11. def main():
  12. while True:
  13. check()
  14. time.sleep(1)
  15. os.system('clear')
  16. if __name__ == '__main__':
  17. main()

`

I am guessing asynchronous might be the way to go? I haven't played with that yet, and would appreciate any reading if that is the answer.

答案1

得分: 0

我把所有的股票信息都放入了一个列表中,然后只在列表的长度达到预期长度时清除屏幕,预期长度基于股票列表的长度。在这一点上,我实际上不需要单独的函数,我确信有更简洁的方法来做这个。在几个小时后再次查看。

  1. import yfinance as yf
  2. import time
  3. import os
  4. stocks = ["SPY", "TMUS", "DAL", "IEF"]
  5. data = []
  6. def check():
  7. for stock in stocks:
  8. stock = yf.Ticker(stock)
  9. name = stock.info["symbol"]
  10. data.append("Symbol: ")
  11. data.append(name)
  12. bid = stock.info["bid"]
  13. data.append(" Current: ")
  14. data.append(bid)
  15. change = (stock.info["bid"] - stock.info["open"])
  16. data.append(" Change: ")
  17. data.append('%f\n' % change)
  18. if len(data) == (len(stocks)*6):
  19. os.system('clear')
  20. print (''.join(map(str, data)))
  21. time.sleep(1)
  22. def main():
  23. while True:
  24. check()
  25. data.clear()
  26. if __name__ == '__main__':
  27. main()
英文:

I ended up putting all of the stock info into a list, then only clearing the screen after the list was the expected length based on the length of the stocks list. I don't really need the separate functions at this point and I am sure there are cleaner ways to do this. I'll take another look after I have to get up in a couple hours.

  1. import yfinance as yf
  2. import time
  3. import os
  4. stocks = ["SPY", "TMUS", "DAL", "IEF"]
  5. data = []
  6. def check():
  7. for stock in stocks:
  8. stock = yf.Ticker(stock)
  9. name = stock.info["symbol"]
  10. data.append("Symbol: ")
  11. data.append(name)
  12. bid = stock.info["bid"]
  13. data.append(" Current: ")
  14. data.append(bid)
  15. change = (stock.info["bid"] - stock.info["open"])
  16. data.append(" Change: ")
  17. data.append('%f\n' % change)
  18. if len(data) == (len(stocks)*6):
  19. os.system('clear')
  20. print (''.join(map(str, data)))
  21. time.sleep(1)
  22. def main():
  23. while True:
  24. check()
  25. data.clear()
  26. if __name__ == '__main__':
  27. main()

huangapple
  • 本文由 发表于 2023年5月17日 10:48:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76268267.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定