def _stroke(self, pressure): if pressure is 'hard': self.total += 10 else: self.total += 1 # ~&% Kuài %&~ Kuai.emit('stroked')
def get(self, item): try: rv = self._innerData[item] except KeyError: return None else: # ~&% Kuài %&~ Kuai.emit("model-accessed", item) return rv
def __init__(self, model): self.activities = ["queryModel", "mutateModel"] self.data = model self.t = threading.Thread(target=self.run) self.t.daemon = True self.t.start() # ~&% Kuài %&~ Kuai.on('jobs-done', self.jobsDone, priority=1)
def confirm_exit(msg=None): if msg: print("Select y or n..") conf = input("Sure you want to exit? n [y] $ ").lower() or 'y' if conf not in ['y', 'n']: confirm_exit(msg=True) else: if conf == 'y': Kuai.emit('display-is-done')
def __init__(self): self.ego = Ego(1) # ~&% Kuài %&~ Kuai.on("model-changed", self.onModelChanged) Kuai.on("model-accessed", self.onModelAccessed) Kuai.on("jobs-done", self.onJobsDone) Kuai.on("stroked", self.onStroked)
def run(self): while not self.is_done: time.sleep(0.01) x, y = pygame.mouse.get_pos() events = pygame.event.get() for event in events: if event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: Kuai.emit('key-up', x, y) if event.key == pygame.K_DOWN: Kuai.emit('key-down', x, y) if event.key == pygame.K_LEFT: Kuai.emit('key-left', x, y) if event.key == pygame.K_RIGHT: Kuai.emit('key-right', x, y) if event.key == pygame.K_ESCAPE: Kuai.emit('key-escape') pass
def run(self): while True: job = random.choice(self.activities) if job == "queryModel": choice = random.choice("first second third".split(' ')) print( green( "Me: Better do some research on this {} one..".format( choice))) self.data.get(choice) print( green("Me: Okay so {} choice is the one then..".format( choice))) if job == "mutateModel": data = random.choice("first second third".split(' ')) this = random.randint(1, 100) print( green("Me: Hmm.. Think i'll change {} to {}..".format( data, this))) self.data.set(data, this) time.sleep(random.randint(3, 9)) # ~&% Kuài %&~ Kuai.emit("jobs-done", "Hm, a job well done I'd say!")
def test_priority_backend(): set_backend('priority') def increment(): global x x += 1 Kuai.on('test', increment) Kuai.emit('test') assert x == 1 Kuai.emit('test') assert x == 2
def test_kuai_events(): set_backend('threaded') def increment(): global x x += 1 Kuai.on('test', increment) Kuai.emit('test') sleep(0.01) assert x == 1 Kuai.emit('test') sleep(0.01) assert x == 2
def draw_squid(x, y): print("Draw squid!") Kuai.emit('circle', white, x, y, random.randrange(5, 15))
def draw_speck(x, y): print("Draw speck!") Kuai.emit('point', red, x, y) def confirm_exit(msg=None): if msg: print("Select y or n..") conf = input("Sure you want to exit? n [y] $ ").lower() or 'y' if conf not in ['y', 'n']: confirm_exit(msg=True) else: if conf == 'y': Kuai.emit('display-is-done') def greeting(): print(f"Welcome to the demo! Pygame is ready to go~") if __name__ == '__main__': Kuai.on('key-up', draw_squid) Kuai.on('key-down', draw_worm) Kuai.on('key-left', draw_turtle) Kuai.on('key-right', draw_speck) Kuai.on('key-escape', confirm_exit) Kuai.on('display-is-done', controller.stop) Kuai.on('view-loaded', greeting) controller.run()
def __init__(self, screen_res): pygame.init() self.screen = pygame.display.set_mode(screen_res) self.background = pygame.Surface(self.screen.get_size()) Kuai.on('square', self.draw_square) Kuai.on('line', self.draw_line) Kuai.on('circle', self.draw_circle) Kuai.on('point', self.draw_point) Kuai.on('update-view', self.update) Kuai.emit('view-loaded')
def draw_point(self, color, x, y): pygame.draw.rect(self.screen, color, (x, y, 1, 1)) Kuai.emit('update-view')
def draw_square(self, color, x, y, height, border=0): height = 20 pygame.draw.rect(self.screen, color, (x, y, height, height), border) Kuai.emit('update-view')
def draw_line(self, color, start_pos, end_pos, width=1): pygame.draw.line(self.screen, color, start_pos, end_pos, width) Kuai.emit('update-view')
def draw_circle(self, color, x, y, radius, width=0): pygame.draw.circle(self.screen, color, (x, y), radius, width) Kuai.emit('update-view')
def set(self, item, value): self._innerData[item] = value # ~&% Kuài %&~ Kuai.emit("model-changed", data=(item, value))
def draw_speck(x, y): print("Draw speck!") Kuai.emit('point', red, x, y)
def draw_worm(x, y): print("Draw worm!") Kuai.emit('line', white, (x, y), (random.randrange(0, 799), random.randrange(0, 599)), 1)
def draw_turtle(x, y): print("Draw turtle!") Kuai.emit('square', red, x, y, random.randrange(5, 20))
def test_kuai_singleton(): a = Kuai() b = Kuai() c = Kuai() assert (a == b == c)
from kuai import Kuai, set_backend set_backend('signal') def hello(name): print(f"Hello, {name}") signal = Kuai.on('signal', hello) signal("cymrow")
from kuai import Kuai def hello(world): print("Hello, {world}!".format(world=world)) Kuai.on('hello-world', hello) Kuai.emit('hello-world', 'Kuài')