for index in xrange(max((len(header) for header in timeline_header))): if any([header[index] == 0 for header in timeline_header]): break for ind, header in enumerate(timeline_header): for i in xrange(1, len(header) + 1): if header[-i] == 1: timeline_header[ind] = header[index:len(header) - i + 2] break return timeline_header, [max(0, r - index) for r in reqs] if __name__ == "__main__": # this is a declaration of tasks with different blocks of active/inactive time t1 = Task(timeblocks=[Active(1), Inactive(2), Active(1)]) t2 = Task(timeblocks=[Active(1), Inactive(2), Active(2)]) t3 = Task(timeblocks=[Active(1), Inactive(2), Active(1)]) t4 = Task(timeblocks=[Active(2), Inactive(2), Active(1)]) t5 = Task(timeblocks=[Active(3), Variable(), Active(3)]) t6 = Task(timeblocks=[Active(4), Inactive(1), Active(2)]) t7 = Task(timeblocks=[Active(2), Inactive(2), Active(2)]) t8 = Task(timeblocks=[Active(1), Inactive(3), Active(2)]) t9 = Task(timeblocks=[Active(1), Inactive(1), Active(1)]) # dictionary declaring which tasks need to happen before which ## i.e. t2 needs to happen before t4 & t5 graph = { t1.name: (t4, ), t2.name: (t4, t5), t3.name: (t5, ),
import pytest import tasks from tasks import Task def test_add_1(): """tasks.get() using id returend from add() works.""" task = Task('breathe', 'BRIAN', True) task_id = tasks.add(task) t_from_db = tasks.get(task_id) assert equivalent(t_from_db, task) @pytest.mark.parametrize('task', [Task('sleep', done=True), Task('wake', 'brian'), Task('breathe', 'BRIAN', True), Task('exercise', 'BrIaN', False)]) def test_add_2(task): """Demonstrate parameterize with one parameter.""" task_id = tasks.add(task) t_from_db = tasks.get(task_id) assert equivalent(t_from_db, task) @pytest.mark.parametrize('summary, owner, done', [('sleep',None, False), ('wake', 'brian', False), ('breath', 'BRIAN', True), ('eat eggs', 'BrIaN', False), ]) def test_add_3(summary, owner, done): """Demonstate parameterize with multiple parameters.""" task = Task(summary, owner, done)
def test_task_quality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') assert t1 == t2
def test_member_access(): """Check .field functionality of namedtuple.""" t = Task('buy milk', 'brian') assert t.summary == 'buy milk' assert t.owner == 'brian' assert (t.done, t.id) == (False, None)
def test_dict_equality(): t1_dict = Task("make sandwich", "okken")._asdict() t2_dict = Task("make sandwich", "okkem")._asdict() assert t1_dict == t2_dict
def test_add_returns_valid_id(): new_task = Task('do something') task_id = tasks.add(new_task) assert isinstance(task_id, int)
def tasks_just_a_few(): return (Task('Write some code', 'Brian', True), Task("Code rebiew Brian's code", 'Katie', False), Task('Fix what Brian did', 'Michelle', False))
def test_dict_equality(): """different tasks compare should fail""" t1_dict = Task('make sandwich', 'brain')._asdict() t2_dict = Task('make sandwich', 'ken')._asdict() assert t1_dict == t2_dict
def test_task_equality(): """different task should not be equal""" t1 = Task('sit there', 'brain') t2 = Task('do something', 'ken') assert t1 == t2
task = Task("breathe", "BRIAN", True) task_id = tasks.add(task) t_from_db = tasks.get(task_id) assert equivalent(t_from_db, task) def equivalent(t1, t2): """Check two tasks for equivalence.""" return ((t1.summary == t2.summary) and (t1.owner == t2.owner) and (t1.done == t2.done)) @pytest.mark.parametrize( "task", [ Task("sleep", done=True), Task("wake", "brian"), Task("breathe", "BRIAN", True), Task("exercise", "BrIaN", False), ], ) def test_add_2(tasks_db, task): """Demonstrate parametrize with one parameter.""" task_id = tasks.add(task) t_from_db = tasks.get(task_id) assert equivalent(t_from_db, task) @pytest.mark.parametrize( "summary, owner, done", [
def test_add_1(tasks_db): """tasks.get() using id returned from add() works.""" task = Task("breathe", "BRIAN", True) task_id = tasks.add(task) t_from_db = tasks.get(task_id) assert equivalent(t_from_db, task)
def decision(): TestTask( description='python lint & tests', variant='coverage', clone=False, command=[ '(cd repo &&' ' nosetests --all-modules --with-coverage --cover-tests tests)', '(cd repo && flake8 --ignore E402 $(git ls-files \*\*.py' ' git-cinnabar git-remote-hg))', ], ) for env in ('linux', 'mingw64', 'osx10_10'): TestTask(task_env=env) for env in ('linux', 'mingw64', 'osx10_11'): task_env = TaskEnvironment.by_name('{}.test'.format(env)) Task( task_env=task_env, description='download helper {} {}'.format(task_env.os, task_env.cpu), command=list(chain( Git.install('{}.{}'.format(env, GIT_VERSION)), Hg.install('{}.{}'.format(env, MERCURIAL_VERSION)), Task.checkout(), [ '(cd repo ; ./git-cinnabar download --dev)', 'rm -rf repo/.git', '(cd repo ; ./git-cinnabar download --dev)', '(cd repo ; ./git-cinnabar download)', ], )), dependencies=[ Helper.by_name(env), ], ) # Because nothing is using the x86 windows helper, we need to manually # touch it. Helper.by_name('mingw32') for upgrade in UPGRADE_FROM: TestTask( extra_desc='upgrade-from-{}'.format(upgrade), variant='coverage', clone=upgrade, env={ 'UPGRADE_FROM': upgrade, }, ) for git in ('1.8.5', '2.7.4'): TestTask(git=git) for hg in SOME_MERCURIAL_VERSIONS: if hg != MERCURIAL_VERSION: TestTask(hg=hg) for env in ('linux', 'osx10_11'): TestTask( task_env=env, variant='asan', env={ 'GIT_CINNABAR_EXPERIMENTS': 'true', }, ) TestTask( variant='coverage', extra_desc='graft', env={ 'GRAFT': '1', }, ) TestTask( variant='old', env={ 'GIT_CINNABAR_OLD_HELPER': '1', }, ) TestTask( variant='old', extra_desc='graft', env={ 'GIT_CINNABAR_OLD_HELPER': '1', 'GRAFT': '1', }, ) rev = old_compatible_python() TestTask( commit=rev, clone=rev, extra_desc='old python', ) TestTask( commit=rev, clone=rev, extra_desc='old python graft', env={ 'GRAFT': '1', }, ) TestTask( variant='coverage', extra_desc='experiments', env={ 'GIT_CINNABAR_EXPERIMENTS': 'true', }, ) TestTask( variant='coverage', extra_desc='experiments graft', env={ 'GIT_CINNABAR_EXPERIMENTS': 'true', 'GRAFT': '1', }, )
'codecov --name "{}" --commit {} --branch {}'.format( task.task['metadata']['name'], TC_COMMIT, TC_BRANCH), ('find . \( -name .coverage -o -name coverage.xml -o -name \*.gcda' ' -o -name \*.gcov \) -delete'), ]) if upload_coverage: Task( task_env=TaskEnvironment.by_name('linux.codecov'), description='upload coverage', scopes=['secrets:get:repo:github.com/glandium.git-cinnabar:codecov'], command=list(chain( Task.checkout(), [ 'set +x', ('export CODECOV_TOKEN=$(curl -sL http://taskcluster/secrets' '/v1/secret/repo:github.com/glandium.git-cinnabar:codecov | ' 'python -c "import json, sys; print(json.load(sys.stdin)' '[\\"secret\\"][\\"token\\"])")'), 'set -x', 'cd repo', ], upload_coverage, )), ) for t in Task.by_id.itervalues(): t.submit() if not TC_ACTION and 'TC_GROUP_ID' in os.environ: actions = { 'version': 1,
def test_missing_summary(self): """Should raise an exception if summary missing.""" with pytest.raises(ValueError): tasks.add(Task(owner='bob'))
def test_task_equality(): """Different tasks shouldn't be equal""" t1 = Task("sit here", "brian") t2 = Task("do something", "okken") assert t1 == t2
import pytest import tasks from tasks import Task def test_add_1(): """tasks.get() using id returned from add() works.""" task = Task('breathe', 'BRIAN', True) task_id = tasks.add(task) t_from_db = tasks.get(task_id) # everything but the id should be the same assert equivalent(t_from_db, task) @pytest.mark.parametrize('task', [ Task('sleep', done=True), Task('wake', 'brian'), Task('breathe', 'BRIAN', True), Task('exercise', 'BrIaN', False), ]) def test_add_2(task): """Demonstrate parametrize with one param.""" task_id = tasks.add(task) t_from_db = tasks.get(task_id) assert equivalent(t_from_db, task) @pytest.mark.parametrize('summary, owner, done', [ ('sleep', None, False), ('wake', 'brian', False), ('breathe', 'BRIAN', True),
def test_done_not_bool(self): """Should rise an exception if done is not boo.""" with pytest.raises(ValueError): tasks.add(Task(summary='summary', done = 'True'))
task_from_db = tasks.get(task_id) # THEN task_idはidフィールドと一致する assert task_from_db.id == task_id def test_add_1(): """tasks.get() using id returned from add() works.""" task = Task('breathe', 'Brian', True) task_id = tasks.add(task) t_from_db = tasks.get(task_id) assert equivalent(t_from_db, task) @pytest.mark.parametrize('task', [ Task('sleep', done=True), Task('wake', 'Brian'), Task('breathe', 'Tom', True), Task('exercise', 'Susan', False) ]) def test_add_2(task): """Demonstrate parametrize with one parameter""" task_id = tasks.add(task) t_from_db = tasks.get(task_id) assert equivalent(t_from_db, task) def equivalent(t1, t2): """Checks two tasks for equivalence""" return ((t1.summary == t2.summary) and (t1.owner == t2.owner) and (t1.done == t2.done))
def tasks_just_a_few(): """All summaries and owners are unique.""" return ( Task('Write some code', 'Brian', True), Task("Code review Brian's code", 'Katie', False), Task('Fix what Brian did', 'Michelle', False))
def test_add_1(): """tasks.get() using id returned from add() works.""" task = Task('breathe', 'Brian', True) task_id = tasks.add(task) t_from_db = tasks.get(task_id) assert equivalent(t_from_db, task)
def test_replace(): """replace() should change passed in f""" t_before = Task('finish book', 'brian', False) t_after = t_before._replace(id=10, done=True) t_expected = Task('finish book', 'brian', True, 10) assert t_after == t_expected
import pytest import tasks from tasks import Task tasks_to_try = (Task('fancify', done=True), Task('gargle', 'gary'), Task('harrass', 'GARY', True), Task('ignore', 'Gary', False)) task_ids = [f'Task({t.summary}, {t.owner}, {t.done})' for t in tasks_to_try] def equivalent(t1, t2): '''Check two tasks for equivalence''' return ((t1.summary == t2.summary) and (t1.owner == t2.owner) and (t1.done == t2.done)) @pytest.fixture(params=tasks_to_try) def a_task(request): '''Using no ids''' return request.param def test_add_a(tasks_db, a_task): '''Using a_task fixture (no ids)''' task_id = tasks.add(a_task) t_from_db = tasks.get(task_id) assert equivalent(t_from_db, a_task) @pytest.fixture(params=tasks_to_try, ids=task_ids) def b_task(request):
def test_task_equality(): t1 = Task("sit there", "brian") t2 = Task("do something", "okken") assert t1 == t2
import pytest import tasks from tasks import Task tasks_to_try = (Task('sleep', done=True), Task('wake', 'brian'), Task('wake', 'brian'), Task('breathe', 'Brian', True), Task('execrise', 'BrIaN', False)) task_ids = [ 'Task({},{},{})'.format(t.summary, t.owner, t.done) for t in tasks_to_try ] def test_add_1(): """tasks.get() using id returend from add() works.""" task = Task('breathe', 'BRIAN', True) task_id = tasks.add(task) t_from_db = tasks.get(task_id) # ID意外は同じはず assert equivalent(t_from_db, task) @pytest.mark.parametrize('task', [ Task('sleep', done=True), Task('wake', 'brian'), Task('breathe', 'BRIAN', True), Task('exercise', 'BriaN', False) ]) def test_add_2(task: tasks.Task):
def test_defaults(): """Using no parameters should invoke defaults.""" t1 = Task() t2 = Task(None, None, False, None) assert t1 == t2
import pytest import tasks.api as tasks from tasks import Task tasks_to_try = [ Task('sleep', done=True), Task('wake', 'brian'), Task('breathe', 'Brian', True), Task('excercise', 'BRIan', False), ] def test_add_1(): task = Task('breath', 'Andrew', True) task_id = tasks.add(task) t_from_db = tasks.get(task_id) assert equivalent(t_from_db, task) @pytest.mark.parametrize('task', tasks_to_try) def test_add_2(task): task_id = tasks.add(task) t_from_db = tasks.get(task_id) assert equivalent(task, t_from_db) def equivalent(t1, t2):
def test_add_3(summary, owner, done): """Demonstate parameterize with multiple parameters.""" task = Task(summary, owner, done) task_id = tasks.add(task) t_from_db = tasks.get(task_id) assert equivalent(t_from_db, task)
def test_dict_equality(): """Different tasks compared as dicts shouldn't be equal""" t1_dict = Task("make sandwich", "okken")._asdict() t2_dict = Task("make sandwich", "okkem")._asdict() assert t1_dict == t2_dict
def test_dict_equality(): """Different tasks compared as dicts should note be equal.""" t1_dict = Task('Make sandwich', 'brian')._asdict() t2_dict = Task('Make sandwich', 'okken')._asdict() assert t1_dict == t2_dict
def main(): # this is a declaration of tasks with different blocks of active/inactive time t1 = Task(timeblocks = [Active(1),Inactive(2),Active(1)]) t2 = Task(timeblocks = [Active(1),Inactive(2),Active(2)]) t3 = Task(timeblocks = [Active(1),Inactive(2),Active(1)]) t4 = Task(timeblocks = [Active(2),Inactive(2),Active(1)]) t5 = Task(timeblocks = [Active(3),Variable(),Active(3)]) t6 = Task(timeblocks = [Active(4),Inactive(1),Active(2)]) t7 = Task(timeblocks = [Active(2),Inactive(2),Active(2)]) t8 = Task(timeblocks = [Active(1),Inactive(3),Active(2)]) t9 = Task(timeblocks = [Active(1),Inactive(1),Active(1)]) # dictionary declaring which tasks need to happen before which ## i.e. t2 needs to happen before t4 & t5 graph = { t1.name:(t4,), t2.name:(t4,t5), t3.name:(t5,), t4.name:(t6,), t5.name:(t6,), t5.name:(t6,), t6.name:(t7,t8,t9), } # create schedule, with any number of workers # be aware time scaling is rough as you increase schedule = Schedule(worker_count = 1) schedule.add_tasks(t1,t2,t3,t4,t5,t6,t7,t8,t9) # add tasks to your schedule schedule.add_dependencies(graph) # add dependencies between tasks ### BORING ALGORITHMIC CODE ### ### DON'T LOOK HERE IF YOU WANT TO THINK ABOUT THIS PROBLEM WITHOUT BIAS ### ### THAT MEANS YOU JOSEPH ### queue = MyPriorityQueue() finished_nodes = { schedule.history: schedule.get_cost() } iterations = 0 while not _is_schedule_complete(schedule): iterations += 1 for new_state in schedule.get_next_states(): schedule.load_state(new_state) if schedule.tag in finished_nodes: #print 'Caught you!' continue queue.put(new_state,schedule.get_cost()) # pull next node based on priority current_state,current_cost = queue.get() schedule.load_state(current_state) finished_nodes[schedule.tag] = current_cost if iterations % 10000 == 0: print schedule.task_start_time print schedule.tag print schedule.history print schedule.nullblocks.values() print schedule print '' ''' print 'History:',current_state['history'] print schedule print '' ''' print 'Iterations used: {}'.format(iterations) print 'Ending timepoint: {}'.format(schedule.timepoints.values()[0]) schedule.plot()