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

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

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()

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:

确定