def __init__(self, engine=None, scheduler=None, options=None): if options: self.size = Vector(*[int(_) for _ in options.size.split('x')]) self.width = self.size[0] self.height = self.size[1] self.num_frames = options.num_frames self.fps = options.fps self.background = Color(options.background) self.foreground = Color(options.foreground) self.grid = options.grid else: self.width = defaults.WIDTH self.height = defaults.HEIGHT self.size = Vector(self.width, self.height) self.num_frames = defaults.NUM_FRAMES self.fps = defaults.FPS self.background = Color(defaults.BACKGROUND) self.foreground = Color(defaults.FOREGROUND) self.grid = defaults.GRID self.engine = engine if engine else PyGameEngine( width=self.width, height=self.height, fps=self.fps) self.engine.fg_color = self.foreground self.engine.bgcolor = self.background self.scheduler = scheduler if scheduler else Scheduler() self.actors = [] self.refs = { 'center': self.size / 2, 'top_right': Vector(self.width, 0), 'top_left': Vector(0, 0), 'bottom_right': Vector(self.width, self.height), 'bottom_left': Vector(0, self.height), }
def test_create_sequence(self): s = Stage(PyGameEngine()) s.num_frames = 100 star = Star('star', color='gold', pos=(1280, 720), radius=20) s.add_actor(star) bob = Square('bob', pos=(0, s.height // 2)) s.add_actor(bob) dice = Dice('D1', pos=(400, 400)) s.add_actor(dice) mf = Bitmap('mf', 'mf.png', pos=(200, 200)) s.add_actor(mf) sch = Scheduler() sch.add_action(Move(star, 0, 100, (0, 0))) sch.add_action(Swing(mf, 0, 100, (1080, 520))) sch.add_action(Land(bob, 0, 100, Vector(s.width, s.height // 2))) for frame in range(0, 100): s.draw(frame) sch.next()
def test_colorize(self): sujeto = Actor('A', color='#0080FF') sch = Scheduler() sch.add_action(actions.Colorize(sujeto, 0, 4, '#FF9000')) frame = sch.next() # Frame 0 to 1, first step self.assertEqual(frame, 1) self.assertEqual(str(sujeto.color), '#4084bf') frame = sch.next() # Frame 1 to 2, second step self.assertEqual(frame, 2) self.assertEqual(str(sujeto.color), '#80887f') frame = sch.next() # Frame 2 to 3, third step self.assertEqual(frame, 3) self.assertEqual(str(sujeto.color), '#bf8c40') frame = sch.next() # Frame 3 to 4, fourth step self.assertEqual(frame, 4) self.assertEqual(str(sujeto.color), '#ff9000')
def test_calls(): sch = Scheduler() class TestAction(actions.Action): def start(self, frame): self.started_at_frame = frame self.called_on_frames = [] def step(self, frame): self.called_on_frames.append(frame) def end(self, frame): self.ended_at_frame = frame a = Actor('A') task = TestAction(a, 3, 6) sch.add_action(task) for f in range(15): sch.next() assert task.started_at_frame == 3 assert task.ended_at_frame == 6 assert task.called_on_frames == [3, 4, 5]
def test_fade_in_in_pygame(self): sch = Scheduler() engine = PyGameEngine() studio = Stage(engine) for row in range(50, 780, 100): for col in range(50, 1280, 100): from_frame = random.randint(5, 30) size = random.randint(10, 70) to_frame = from_frame + size name = 'bob{}x{}'.format(col, row) actor = Square( name, color=colors.random_color(), pos=(col, row), side=90, alpha=0.1, ) sch.add_action(actions.FadeIn(actor, from_frame, to_frame)) studio.add_actor(actor) for frame in range(75): studio.draw(frame) sch.next()
def test_easing(): move = Label('Move', width=190, pos=(100, 30), color='gold') fall = Label('Fall', width=190, pos=(300, 30), color='gold') land = Label('Land', width=190, pos=(500, 30), color='gold') ease_in = Label('EaseIn', width=190, pos=(700, 30), color='gold') ease_out = Label('EaseOut', width=190, pos=(900, 30), color='gold') swing = Label('Swing', width=190, pos=(1100, 30), color='gold') sch = Scheduler() sch.add_action(actions.Move(move, 5, 70, (100, 700))) sch.add_action(actions.Fall(fall, 5, 70, (300, 700))) sch.add_action(actions.Land(land, 5, 70, (500, 700))) sch.add_action(actions.EaseIn(ease_in, 5, 70, (700, 700))) sch.add_action(actions.EaseOut(ease_out, 5, 70, (900, 700))) sch.add_action(actions.Swing(swing, 5, 70, (1100, 700))) engine = PyGameEngine() stage = Stage(engine) stage.add_actors(move, fall, land, ease_in, ease_out, swing) for frame in range(75): stage.draw(frame) sch.next()
def main(): fps, debug = get_args() sch = Scheduler() f_1 = Rect("f_1", pos=(50, 0), color="brown", width=90, height=25) sch.add_action(EaseIn(f_1, 0, 150, Vector(50, 700))) f_2 = Rect("f_2", pos=(150, 0), color="white", width=90, height=25) sch.add_action(Move(f_2, 0, 150, Vector(150, 700))) f_3 = Rect("f_3", pos=(250, 0), color="#334588", width=90, height=25) sch.add_action(Land(f_3, 0, 150, Vector(250, 700))) f_4 = Rect("f_4", pos=(350, 0), color="#f0A371", width=90, height=25) sch.add_action(Swing(f_4, 0, 150, Vector(350, 700))) lbl_fall = Label("Fall", "Fall", pos=(450, 10), color="#333333") sch.add_action(Fall(lbl_fall, 0, 50, (450, 400))) sch.add_action(EaseIn(lbl_fall, 186, 196, (450, 10))) lbl_easing_in = Label("easing_in", "Easing in", pos=(600, 10), color="#333333") sch.add_action(EaseIn(lbl_easing_in, 0, 50, (600, 400))) sch.add_action(EaseIn(lbl_easing_in, 188, 198, (600, 10))) lbl_swing = Label("swing", "Swing", pos=(750, 10), color="#333333") sch.add_action(Swing(lbl_swing, 0, 50, (750, 400))) sch.add_action(EaseIn(lbl_swing, 190, 200, (750, 10))) pelota = Circle("Pelota", pos=(480, 10), color="gold") sch.add_action(Fall(pelota, 0, 25, (480, 670))) sch.add_action(Land(pelota, 25, 50, (480, 12))) sch.add_action(Fall(pelota, 50, 75, (480, 670))) sch.add_action(Land(pelota, 75, 100, (480, 10))) sch.add_action(Fall(pelota, 100, 125, (480, 670))) sch.add_action(Land(pelota, 125, 150, (480, 10))) sch.add_action(Fall(pelota, 150, 175, (480, 670))) sch.add_action(Land(pelota, 175, 200, (480, 10))) bob = RoundRect("Bob", color="cadetblue", width=60, height=50) bob.place(10, 400) sch.add_action(Land(bob, 1, 20, Vector(400, 400))) sch.add_action(Fall(bob, 21, 40, Vector(10, 400))) star = Star("Marylin", color="red", alpha=0.33) sch.add_action(Swing(star, 0, 100, (WIDTH, HEIGHT))) sch.add_action(Swing(star, 100, 200, (0, 0))) all_actors = [ pelota, bob, f_1, f_2, f_3, f_4, star, lbl_fall, lbl_easing_in, lbl_swing ] lbl_clock = Label("clock", "00:00.00", pos=(1120, 670), color="#CFCFCF", alpha=0.5) sch.add_action(Timer(lbl_clock, 0, 200)) all_actors.append(lbl_clock) in_stage = all_actors[:] engine = engines.PyGameEngine() clock = pygame.time.Clock() frame = 0 inside_loop = True while inside_loop: engine.clear(frame) if debug: engine.grid() for actor in in_stage: actor.start_draw(engine) for event in pygame.event.get(): if event.type == pygame.QUIT: inside_loop = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: inside_loop = False pygame.display.flip() clock.tick(fps) frame = sch.next() % MAX_FRAMES if frame == 0: sch.reset() for actor in all_actors: actor.reset()
#coding=utf-8 '''Workers处理任务''' # 因为比较简单的项目,就都在一起了,没有拆分 import multiprocessing # https://github.com/douban/CaoE, 父进程死掉杀掉子进程 import caoe caoe.install() import parse import models from schedulers import Message from log import error, warn from control import Scheduler, periodic, run terminating = None scheduler = Scheduler('worker') # Fixed 不能CTRL-C http://stackoverflow.com/questions/14579474/multiprocessing-pool-spawning-new-childern-after-terminate-on-linux-python2-7 def initializer(terminating_): # This places terminating in the global namespace of the worker subprocesses. # This allows the worker function to access `terminating` even though it is # not passed as an argument to the function. global terminating terminating = terminating_ class Worker(object): '''执行任务类''' def __init__(self, map_func, num_workers=None, **kwargs): self.map_func = map_func self.inputs = Message.objects(state__ne=2, inprocess__ne=True)
''' 把需要抓取的电影id发送给MQ, 他是一切任务的生成源 ''' from mongoengine.errors import NotUniqueError from parse import get_movie_ids, get_movie_pages from utils import get_unfinished, group, sleep2 from spider import Search from conf import (SEARCH_PAGE, SEARCH_API, MIN_YEAR, TASK_BEAT_NUM, TASK_BEAT, VERIFY_INTERVAL) from models import YearFinished, IdFinished from schedulers import Message from control import Scheduler, periodic, run from log import error, debug, warn scheduler = Scheduler('beat') def get_year(): '''根据年份从前向后,获取当前要执行的第一个年份(min)''' obj = YearFinished.objects if obj: c_year = obj.first() return c_year.year else: return MIN_YEAR - 1 def fetch(year, page): s = Search(params={'Ajax_CallBack': True, 'Ajax_CallBackType': 'Mtime.Channel.Pages.SearchService', # noqa
def test_sequence(self): sch = Scheduler() s = Stage(PyGameEngine()) s.num_frames = 80 middle = s.height // 2 dice1 = Dice('D1', num=1) dice1.place(1 * s.width // 7, middle) sch.add_action( Land(dice1, 0, 60, Vector(1 * s.width // 7, s.height - 50))) sch.add_action(Swing(dice1, 60, 80, dice1.initial_state['pos'])) s.add_actor(dice1) dice2 = Dice('D2', num=2) dice2.place(2 * s.width // 7, middle) sch.add_action( Fall(dice2, 0, 60, Vector(2 * s.width // 7, s.height - 50))) sch.add_action(Swing(dice2, 60, 80, dice2.initial_state['pos'])) s.add_actor(dice2) dice3 = Dice('D3', num=3) dice3.place(3 * s.width // 7, middle) sch.add_action(Fall(dice3, 0, 60, Vector(3 * s.width // 7, 50))) sch.add_action(Swing(dice3, 60, 80, dice3.initial_state['pos'])) s.add_actor(dice3) dice4 = Dice('D4', num=4) dice4.place(4 * s.width // 7, middle) sch.add_action(Land(dice4, 0, 60, Vector(4 * s.width // 7, 50))) sch.add_action(Swing(dice4, 60, 80, dice4.initial_state['pos'])) s.add_actor(dice4) dice5 = Dice('D5', num=5) dice5.place(5 * s.width // 7, middle) sch.add_action( Move(dice5, 0, 60, Vector(5 * s.width // 7, s.height - 50))) sch.add_action(Swing(dice5, 60, 80, dice5.initial_state['pos'])) s.add_actor(dice5) dice6 = Dice('D6', num=6) dice6.place(6 * s.width // 7, middle) sch.add_action(Move(dice6, 0, 60, Vector(6 * s.width // 7, 50))) sch.add_action(Swing(dice6, 60, 80, dice6.initial_state['pos'])) s.add_actor(dice6) t1 = Label( 'Texto1', pos=(s.width // 2, 50), color='gold', width=200, height=100, ) for i in range(0, 81, 20): fall_pos = vectors.get_random_position_vector(s.width, s.height) land_pos = vectors.get_random_position_vector(s.width, s.height) sch.add_action(Land(t1, i, i + 25, fall_pos)) sch.add_action(Fall(t1, i, i + 25, land_pos)) s.add_actors(t1) for frame in range(0, 100): s.draw(frame) sch.next()
def test_start(): sch = Scheduler() bob = Actor('Bob') sch.add_action(actions.Move(bob, 5, 10, Vector(72, 54))) sch.add_action(actions.Blink(bob, 8, 12)) assert len(sch.active_actions) == 0 # 0 sch.next() assert len(sch.active_actions) == 0 # 1 sch.next() assert len(sch.active_actions) == 0 # 2 sch.next() assert len(sch.active_actions) == 0 # 3 sch.next() assert len(sch.active_actions) == 0 # 4 sch.next() assert len(sch.active_actions) == 0 # 5 sch.next() assert len(sch.active_actions) == 1 # 6 sch.next() assert len(sch.active_actions) == 1 # 7 sch.next() assert len(sch.active_actions) == 1 # 8 sch.next() assert len(sch.active_actions) == 2 # 9 sch.next() assert len(sch.active_actions) == 2 # 10 sch.next() assert len(sch.active_actions) == 1 # 11 sch.next() assert len(sch.active_actions) == 1 # 12 sch.next() assert len(sch.active_actions) == 0 # 13 sch.next() assert len(sch.active_actions) == 0 # 14 sch.next() assert len(sch.active_actions) == 0 # 15
def test_show_calls(self): bob = Square('bob', alpha=0.0) show = actions.FadeIn(bob, 0, 10) sch = Scheduler() sch.add_action(show) self.assertEqual(bob.alpha, 0.0) sch.next() self.assertAlmostEqual(bob.alpha, 0.1) sch.next() self.assertAlmostEqual(bob.alpha, 0.2) sch.next() self.assertAlmostEqual(bob.alpha, 0.3) sch.next() self.assertAlmostEqual(bob.alpha, 0.4) sch.next() self.assertAlmostEqual(bob.alpha, 0.5) sch.next() self.assertAlmostEqual(bob.alpha, 0.6) sch.next() self.assertAlmostEqual(bob.alpha, 0.7) sch.next() self.assertAlmostEqual(bob.alpha, 0.8) sch.next() self.assertAlmostEqual(bob.alpha, 0.9) sch.next() self.assertAlmostEqual(bob.alpha, 1.0)
def test_fade_out(self): bob = Square('bob') hide = actions.FadeOut(bob, 0, 10) sch = Scheduler() sch.add_action(hide) self.assertEqual(bob.alpha, 1.0) sch.next() self.assertAlmostEqual(bob.alpha, 0.9) sch.next() self.assertAlmostEqual(bob.alpha, 0.8) sch.next() self.assertAlmostEqual(bob.alpha, 0.7) sch.next() self.assertAlmostEqual(bob.alpha, 0.6) sch.next() self.assertAlmostEqual(bob.alpha, 0.5) sch.next() self.assertAlmostEqual(bob.alpha, 0.4) sch.next() self.assertAlmostEqual(bob.alpha, 0.3) sch.next() self.assertAlmostEqual(bob.alpha, 0.2) sch.next() self.assertAlmostEqual(bob.alpha, 0.1) sch.next() self.assertAlmostEqual(bob.alpha, 0.0) self.assertEqual(sch.frame, 10) sch.next() self.assertAlmostEqual(bob.alpha, 0.0) self.assertEqual(sch.frame, 11)
def test(self): t = Text('timer', pos=(1000, 700), color='navy') e1 = Label('e1', text='Enter on 75', width=190, color='gold') bg = Rect('bg', width=30, height=600, pos=(620, 320), color='orange') fg = Rect('fg', width=30, height=600, pos=(660, 320), color='navy') e = Label('exit', text="I'll go on 2s", pos=(200, 200)) sch = Scheduler() sch.add_action(actions.Timer(t, 0, 150)) sch.add_action(actions.Enter(e1, 75, 76, (640, 320))) sch.add_action(actions.Background(bg, 100, 101)) sch.add_action(actions.Foreground(fg, 125, 126)) sch.add_action(actions.Exit(e, 50, 51)) engine = PyGameEngine() studio = Stage(engine) studio.add_actors(t, fg, e, e1, bg) for frame in range(150): studio.draw(frame) sch.next()
def test_uso(): sch = Scheduler() bob = Square('Bob') task = actions.Action(bob, 5, 10) sch.add_action(task) assert (task.actor.name, 5) in sch.actions