Custom properties on task.

21 views
Skip to the first unread message

Arman Ohanian

unread,
31 Aug 2017, 10:15:3931/08/2017
to python-doit
Hi,

Is it possible to add custom properties to 'task'?

Since 'task' is passed to the action, I think it would be useful to be able to attach a custom dictionary to it.

Here is a modified example from the documentation:
def who(task):
   
print('my name is', task.name)
   
print(task.targets)
    print('Foo is ', task.attributes['foo']

def task_x():
   
return {
       
'actions': [who],
       
'targets': ['asdf'],
       
'verbosity': 2,
        'attributes' : { 'foo' : 'bar' },
        }


Of course, it doesn't have to be named 'attributes'.   This will be the user a way to parametrize the task.


Thanks,
-Arman.


Arman Ohanian

unread,
31 Aug 2017, 10:31:0931/08/2017
to python-doit
I am starting to think that the answer may is to use "args"...  :)

Please advise if there is an otherwise better way.

Eduardo Schettino

unread,
31 Aug 2017, 10:43:5531/08/2017
to python-doit

> Is it possible to add custom properties to 'task'?

No. This is something I plan to add for ages but never really needed myself.

Use "args" would be an ugly hack...

--
You received this message because you are subscribed to the Google Groups "python-doit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-doit+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jan Vlčinský (gmail)

unread,
1 Sept 2017, 15:02:1101/09/2017
to Arman Ohanian, pytho...@googlegroups.com

Hi Arman

I have created study on how can be the parametrization done by means of TaskContext class.

See my repository https://github.com/vlcinsky/doit-task-context-study

Jan


On 1.9.2017 18:19, Arman Ohanian wrote:
Hi Jan,

ok, I look into passing the info as arguments and here is the "ugliness" (as Eduardo pointed out) of the solution.  Say, we have multiple 'actions' on the same task, we have repeated pass the same arguments into each function.  That makes it a but hard to manage the dodo file.

It will work, but it ain't pretty.  :)

I thought I should share this with you.

Thanks,
-Arman.



On Thu, Aug 31, 2017 at 9:25 PM Arman Ohanian <arman....@gmail.com> wrote:

Ok, I see your point. 
I will give it a try.

Thank you.


On Thu, Aug 31, 2017, 5:03 PM Jan Vlčinský (gmail) <jan.vl...@gmail.com> wrote:

Hi Arman

Passing a dictionary into task (resp. action of a task, there can be multiple actions in one task) really allows complex logic.

You have to decide somewhere in the code, what action shall use which value from the dictionary.

You already can do that by means of calling python functions as action and passing it arguments and keywords.

Adding extra keyword to returned/yielded dictionary does not improve the solution, in fact it adds extra complexity.

My example with string substitution was just an example, a task (this time command line call) can get some parameter. I had better to provide an example with calling a python function. Tutorial provides such examples.

Jan


On 31.8.2017 23:12, Arman Ohanian wrote:
Thanks Jan.


I don't see how it's the same thing.

Passing a dictionary allows me to implement complex logic in the function.  
If I understand it correctly, you are suggesting using string substitution?





On Thu, Aug 31, 2017 at 12:56 PM Jan Vlčinský (gmail) <jan.vl...@gmail.com> wrote:

In short: "Luke, use the power" (you already have).

One of beauties of doit is it allows using full power of python code.

You are free to define the attributes in the code above returning (or yielding) the "plan" dictionary as shown below.

def who(task):
    print('my name is', task.name)
    print(task.targets)
    print('Foo is ', task.attributes['foo']

def task_x
():
    foo = "bar"
    return {
        'actions': [who, "rm -rf %s" % foo],

        'targets': ['asdf'],
        'verbosity': 2,
        }



With best regards

Jan Vlčinský


Arman Ohanian

unread,
1 Sept 2017, 22:10:4601/09/2017
to python-doit
Oh yeah, I see what you are doing there!

I will take a closer look when I get a chance.

Huge thanks for making the effort.

-Arman.

Reply all
Reply to author
Forward
0 new messages