python-effect / effect

effect isolation in Python, to facilitate more purely functional code
372 stars 16 forks source link

safe passing of kwargs to performers #60

Closed RonnyPfannschmidt closed 9 years ago

RonnyPfannschmidt commented 9 years ago

in order to pass utilities to all performes that use thme i propose supporting passing kwargs to perform, and passing those as named arguments to performers, filtering those the performer does not take

strawman example to bootstrap discussion:

@sync_performer
def upgrade_progress(dispatcher, intend, progress=None):
  if progress is not none:
   return progress.update_from(intend)

@sync_performer
def do_something(dispatcher, intent, context):
 ...
# dispatchers and intent classes here

if __name__ == '__main__':
  sync_perform(dispatcher, sequence(...), context=Appcontext(),   progress=SimpleProgressbarWithDbus())
radix commented 9 years ago

Hi Ronny,

What you want to do here is bind these parameters into the performers when you build your dispatcher.

Here, for example, given the same definitions of upgrade_progress and do_something

from functools import partial

dispatcher = TypeDispatcher({
    UpgradeProgress: partial(upgrade_progress, progress=SimpleProgressBarWithDbus()),
    DoSomething: partial(do_something, context=Appcontext())
})
sync_perform(dispatcher, sequence(...))

You can also use a more Object Oriented technique (which is really just an unnecessarily verbose way of doing the same kind of partial) like so:

class UpgradeProgressPerformer(object):
    def __init__(self, progress):
        self.progress = progress

    @sync_performer
    def upgrade_progress(self, dispatcher, intent):
        if self.progress is not None:
            return self.progress.update_from(intent)

upgrade_progress_performer = UpgradeProgressPerformer(progress)
dispatcher = TypeDispatcher({
    UpgradeProgress: upgrade_progress_performer.upgrade_progress,
    ...
})

...
sync_perform(dispatcher, sequence(...))

Does this make sense?

RonnyPfannschmidt commented 9 years ago

then i would need some kind of composed dispatcher anyway, i'll have to experiment

in part i want to avod too many callbacks, and composition issues

radix commented 9 years ago

Okay, I think this is resolved, please reopen if you think there's still a problem