-
-
Save vkgtaro/4064636 to your computer and use it in GitHub Desktop.
CodeIQ 用問題
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import sys | |
import time | |
import datetime | |
class Observable(): | |
def __init__(self): | |
self.__observers = [] | |
self.__state = False | |
def is_changed (self): | |
return self.__state | |
def changed (self): | |
self.__state = True | |
def notify_observers (self, args): | |
if not self.is_changed(): | |
return | |
self.__state = False | |
for observer in self.__observers: | |
try: | |
observer.update(args) | |
except Exception, inst: | |
print type(inst) | |
print inst.args | |
print inst | |
def add_observer(self, obj): | |
self.__observers.append(obj) | |
class Tick(Observable): | |
def start (self): | |
while True: | |
now = datetime.datetime.now() | |
self.changed() | |
self.notify_observers([now.hour, now.minute, now.second]) | |
time.sleep( 1.0 - (datetime.datetime.now().microsecond / 1000000.0) ) | |
class TextClock(): | |
def update(self, time): | |
sys.stdout.write( "\r%02d:%02d:%02d" % tuple(time) ) | |
sys.stdout.flush() | |
if __name__ == '__main__': | |
tick = Tick() | |
tick.add_observer( TextClock() ) | |
tick.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment