今さらPython

Rubyのコンソールで羽根をクルクル回すのPython版。時計に組み込んでみた。

code

#!/usr/bin/env python

import datetime
import time

class Hane():
   def __init__(self):
       self.s = ["/","-","\\","|"]
       self.si = 0
   def next(self):
       self.si = self.si + 1
       if(self.si > 3):
           self.si = 0
       return(str(self.s[self.si]))

h = Hane()
ttx = ""
try:
   while 1:
       tx = datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S")
       if(tx != ttx):
           ttx = tx
           print("\r",flush=True,end="")
           print(tx+" "+h.next(),flush=True,end="")
       time.sleep(0.1)
except KeyboardInterrupt:
   print("\nBye!")
   exit

動作

2023/07/10 13:10:43 -

なお、CTRL-Cすると「Bye!」と出して終わります。


トップ   編集 凍結 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2023-07-10 (月) 13:11:26