T
Thomas Rachel
Am 25.04.2011 16:29, schrieb Thomas Rachel:
Or yet even better:
class Job(object):
def __init__(self, target, *args, **kwargs):
self.target = lambda: target(*args, **kwargs)
def __call__(self):
return self.target()
in order to do
actions.append(Job(function, val))
actions.append(Job(function, x=val))
and then (thanks, Chris...)
results = [function() for function in actions]
or maybe (additionally or alternatively)
class ActionQueue(list):
def addJob(self, target, *args, **kwargs):
self.append(lambda: target(*args, **kwargs))
def __call__(self):
if 0: # first thought
for job in self:
yield job()
else: # second thought - clean up...
while self:
job = self.pop(0)
yield job()
with
actions = ActionQueue()
actions.addJob(function, val)
actions.addJob(function, x=val)
results = list(actions()) # for collecting them and having them at once
# with generator, all call results can as well be emitted as soon as
they are available - depending what shall be done with the results
mmm... too much imagination I think... ;-)
Thomas
or maybe even better (taking care for closures):
function = bool
value = 'the well at the end of the world'
## ...
actions.append(lambda val=value: function(val))
## ...
for function in actions:
results.append(function())
Or yet even better:
class Job(object):
def __init__(self, target, *args, **kwargs):
self.target = lambda: target(*args, **kwargs)
def __call__(self):
return self.target()
in order to do
actions.append(Job(function, val))
actions.append(Job(function, x=val))
and then (thanks, Chris...)
results = [function() for function in actions]
or maybe (additionally or alternatively)
class ActionQueue(list):
def addJob(self, target, *args, **kwargs):
self.append(lambda: target(*args, **kwargs))
def __call__(self):
if 0: # first thought
for job in self:
yield job()
else: # second thought - clean up...
while self:
job = self.pop(0)
yield job()
with
actions = ActionQueue()
actions.addJob(function, val)
actions.addJob(function, x=val)
results = list(actions()) # for collecting them and having them at once
# with generator, all call results can as well be emitted as soon as
they are available - depending what shall be done with the results
mmm... too much imagination I think... ;-)
Thomas