def test_first(): array = [0, 2, 7, None] assert first(lambda x: x > 3, array) is 7 assert first(lambda x: x >= 0, array) is 0 assert first(lambda x: x, array) is 2 assert first(lambda x: x is None, array, default='default') is None assert first(lambda x: x is not None and x > 10, array, default='default') is 'default'
def __init__(self, queue=None, timeout=60, jobdesc=None, jobflags=None): ''' Constructor. ''' self.queue = queue self.timeout = timeout self.jobdesc = jobdesc self.jobflags = co.first(jobflags, lambda: list)
def enqueue(*args, **kwargs): jobdesc = co.first(kwargs.pop('jobdesc', None), self.jobdesc) jobflags = co.first(kwargs.pop('jobflags', None), self.jobflags) queue = co.first(kwargs.pop('queue', None), self.queue) timeout = co.first(kwargs.pop('timeout', None), self.timeout) if queue is None: raise ValueError('This job has no queue defined.') job = queue.enqueue_call(fn, args=args, kwargs=kwargs, timeout=timeout) job.meta['description'] = jobdesc job.meta['flags'] = list(jobflags) job.save() return job