英文:
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').
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 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
我把所有的股票信息都放入了一个列表中,然后只在列表的长度达到预期长度时清除屏幕,预期长度基于股票列表的长度。在这一点上,我实际上不需要单独的函数,我确信有更简洁的方法来做这个。在几个小时后再次查看。
import yfinance as yf
import time
import os
stocks = ["SPY", "TMUS", "DAL", "IEF"]
data = []
def check():
for stock in stocks:
stock = yf.Ticker(stock)
name = stock.info["symbol"]
data.append("Symbol: ")
data.append(name)
bid = stock.info["bid"]
data.append(" Current: ")
data.append(bid)
change = (stock.info["bid"] - stock.info["open"])
data.append(" Change: ")
data.append('%f\n' % change)
if len(data) == (len(stocks)*6):
os.system('clear')
print (''.join(map(str, data)))
time.sleep(1)
def main():
while True:
check()
data.clear()
if __name__ == '__main__':
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.
import yfinance as yf
import time
import os
stocks = ["SPY", "TMUS", "DAL", "IEF"]
data = []
def check():
for stock in stocks:
stock = yf.Ticker(stock)
name = stock.info["symbol"]
data.append("Symbol: ")
data.append(name)
bid = stock.info["bid"]
data.append(" Current: ")
data.append(bid)
change = (stock.info["bid"] - stock.info["open"])
data.append(" Change: ")
data.append('%f\n' % change)
if len(data) == (len(stocks)*6):
os.system('clear')
print (''.join(map(str, data)))
time.sleep(1)
def main():
while True:
check()
data.clear()
if __name__ == '__main__':
main()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论