dips
dips

Reputation: 1637

PyDoIt: Sharing tasks definitions across modules

Here is a simpler version of the problem I am facing. I have a task in dodo.py file. This task has access to a module level variable name.

# dodo.py
name = 'John'

def task_hello():
    return {
        'actions': [f'echo Hello {name}!']
    }

Now I want to have different versions of this task. A naive way of doing this would be the following.

# dodo1.py
name = 'Tim'

def task_hello():
    return {
        'actions': [f'echo Hello {name}!']
    }

And

# dodo2.py
name = 'Julia'

def task_hello():
    return {
        'actions': [f'echo Hello {name}!']
    }

I do want multiple dodo modules. However, I do not want to copy the task definition.

Note that, although I have mentioned just one task here for simplicity, I have a set of inter-dependent tasks and subtasks. Also, I do not want to use command line parameters and prefer to have multiple config files.

Upvotes: 1

Views: 490

Answers (1)

dips
dips

Reputation: 1637

Finally I ended up doing this. This solves the problem. But I am open to more elegant solutions.

dodo1.py

def mk_task_hello(name):
    yield {
        'basename': 'hello',
        'name': 'world',
        'actions': [f'echo Hello {name}!']
    }

dodo2.py

from dodo1 import mk_task_hello

name = 'Tim'


def task_hello():
    yield from mk_task_hello(name)

dodo3.py

from dodo1 import mk_task_hello

name = 'Julia'


def task_hello():
    yield from mk_task_hello(name)

dodo1.py is the main task implementation module whereas dodo2.py and dodo3.py are different instantiations. Note the basename field in task dictionary. This is necessary for the task dependencies (not shown in the example above) to work.

Upvotes: 1

Related Questions