1

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.

dips
  • 1,627
  • 14
  • 25

1 Answers1

1

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.

dips
  • 1,627
  • 14
  • 25
  • Tha's the whole point of class, since you can easily wrap `task_hello` in a class, import that class and call it like i did in my example @dips – Devesh Kumar Singh Jun 18 '19 at 19:35
  • `pydoit` automatically extracts tasks from the module. (all methods with prefix `task_`). This doesn't work with class. This is more of a pydoit question than a python question. But I can not add `pydoit` tag due to my low reputation. – dips Jun 19 '19 at 02:53
  • I don’t think there is a pydoit tag to begin with otherwise you would have seen it, anyways seems like I cannot help you much out here – Devesh Kumar Singh Jun 19 '19 at 04:09