在返回 'self' 之前调用 setattr

calling setattr before 'self' is returned

我怀疑这对我来说是一种 klugefest,但我正在使用 Luigi 和 Sciluigi 模块,它们在 'self' 被 return 编辑之前设置了许多关键参数一个初始化。并且如果我在 self 被 returned 之后尝试手动处理这些参数,Luigi.Parameter 对象会以我无法做我需要做的事情的方式掩盖它们。

luigi 和 sciluigi classes(按照使用方式)不包含 __init__。如果我尝试插入 __init__ 或调用 super(ChildClass, self).__init__(*args, **kwargs) ...我会收到奇怪的错误 'unexpected parameter' 错误。

所以 Sciluigi class 看起来像这样...

class MyTask(sciluigi.task):
    param1 = sciluigi.Parameter(default='Yes') #String only
    param2 = sciluigi.Parameter(default='No')  #String only

    def out_target(self):
        return sciluigi.TargetInfo(self, self.out)

    def run(self):
        with self.out_target().open('w') as foofile:
            foofile.write('foo\n')

所以...我希望我可以在 'self' 实际上被 returned 之前通过 setattr 动态设置一些参数。但是 setattr 需要对象。

我希望我能做类似...

setattr(inspect.stack()[?][?], 'DynamicVar', sciluigi.Parameter(default='Yes') )

编辑:@Charles Duffy

嗯,我不确定哪些信息最有帮助。

第一期是;我无法添加初始化。实际代码如下,添加了 __init__ 方法。如果我尝试 运行 它,我已经包括了由此产生的错误。它同样的错误是如果我 tr 运行 对 __init__

的超级调用
class FileConverter(sciluigi.Task):
    """
    """
    in_target   = None # Mandatory
    out = sciluigi.Parameter() # <file>
    exepath = sciluigi.Parameter(default = "") 

    def __init__(self):
        self.var = 'anything'

    def out_target(self):
        log.debug("In 'FileConverter.out_target'... ")
        return sciluigi.TargetInfo(self, self.out)

    def run(self):
        result = None
        command = ''.join([
                           self.exepath, _slash, "FileConverter ",
                           " -in ", self.in_target().path,
                           " -out ", self.out_target().path, 
                           " -out_type ", self.file_type
                           ])
        log.info("RUNNING COMMAND: " + command)
        result = self.ex(command)
        log.info("result: " + result[1])

错误

2017-02-24 17:01:48 |  WARNING | Will not run MyWorkflow(instance_name=sciluigi_workflow) or any dependencies due to error in deps() method:
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/luigi/worker.py", line 697, in _add
    deps = task.deps()
  File "/Library/Python/2.7/site-packages/luigi/task.py", line 572, in deps
    return flatten(self._requires())
  File "/Library/Python/2.7/site-packages/luigi/task.py", line 544, in _requires
    return flatten(self.requires())  # base impl
  File "/Library/Python/2.7/site-packages/sciluigi/workflow.py", line 105, in requires
    workflow_output = self.workflow()
  File "/Users/mikes/Documents/Eclipseworkspace/Bioproximity/OpenMS-Python-Luigi/site-packages/Bioproximity/sciluigi_tasks/PipelineTest1.py", line 33, in workflow
    exepath = "/Applications/OpenMS-2.1.0/TOPP"
  File "/Library/Python/2.7/site-packages/sciluigi/workflow.py", line 145, in new_task
    newtask = sciluigi.new_task(instance_name, cls, self, **kwargs)
  File "/Library/Python/2.7/site-packages/sciluigi/task.py", line 37, in new_task
    newtask = cls.from_str_params(kwargs)
  File "/Library/Python/2.7/site-packages/luigi/task.py", line 412, in from_str_params
    return cls(**kwargs)
  File "/Library/Python/2.7/site-packages/luigi/task_register.py", line 99, in __call__
    h[k] = instantiate()
  File "/Library/Python/2.7/site-packages/luigi/task_register.py", line 80, in instantiate
    return super(Register, cls).__call__(*args, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'instance_name'

第二期是:

如果我等待自己到 return,我将无法再区分(例如使用上面的代码)...

in_target   = None # Mandatory
out = sciluigi.Parameter() # <file>

如果我执行 type(out),类型报告参数只是一个字符串(不是 sciluigi.Parameter 对象),所以如果我尝试使用 `ìsinstance(out, sciluigi.Parameter)...它 return 是错误的。

底线是:

我需要能够动态地(以编程方式)设置 sciluigi.Parameter 对象,然后能够区分 sciluigi.Parameter() 对象变量(如 out)和'real' str() 对象(如 in_target

我希望这是有道理的。

在构造函数中接受并静默丢弃参数,如下所示:

class FileConverter(sciluigi.Task):
    def __init__(self, *_args, **_kwargs):
        self.var = 'anything'

仅供将来参考,孤立问题“如何...

的答案
setattr(<thisClassObject>, 'DynamicVar', sciluigi.Parameter(default='Yes') )

就是使用locals()内置函数。即

locals()['DynamicVar'] = sciluigi.Parameter(default='Yes') #String only

这是我如何解决我的特定问题的片段;)

deleteme.py

import sciluigi

class MyFooWriter(sciluigi.Task):
    locals()['outfile'] = sciluigi.Parameter(default='./foo.txt') #String only
    locals()['normalvar'] = 'Normalstring'
    print "pre-self-returned outfile type =", type(outfile)
    print "pre-self-returned normalvar type =", type(normalvar)
#     locals()['param1'] = 
    def out_foo(self):
        # raw_input("Enter...")
        return sciluigi.TargetInfo(self, self.outfile)

    def run(self):
        print "self.outfile type =", type(self.outfile)
        print "self.normalvar type =", type(self.normalvar)
        # raw_input("Enter...")        
        with self.out_foo().open('w') as foofile:
            foofile.write('foo\n')            


class MyWorkflow(sciluigi.WorkflowTask):
    def workflow(self):
        print 'Starting workflow...'
        foowriter = self.new_task('foowriter', MyFooWriter, outfile = 'testfile.txt')
        return foowriter        

if __name__ == '__main__':
    sciluigi.run_local(main_task_cls=MyWorkflow)  

输出

pre-self-returned outfile type = <class 'sciluigi.parameter.Parameter'>
pre-self-returned normalvar type = <type 'str'>
Starting workflow...
2017-02-27 12:08:37 |     INFO | --------------------------------------------------------------------------------
2017-02-27 12:08:37 |     INFO | SciLuigi: MyWorkflow Workflow Started (logging to log/workflow_myworkflow_started_20170227_110837_278707.log)
2017-02-27 12:08:37 |     INFO | --------------------------------------------------------------------------------
2017-02-27 12:08:37 |     INFO | Task foowriter started
self.outfile type = <type 'str'>
self.normalvar type = <type 'str'>
2017-02-27 12:08:37 |     INFO | Task foowriter finished after 0.001s
Starting workflow...
2017-02-27 12:08:37 |     INFO | --------------------------------------------------------------------------------
2017-02-27 12:08:37 |     INFO | SciLuigi: MyWorkflow Workflow Finished (workflow log at log/workflow_myworkflow_started_20170227_110837_278707.log)
2017-02-27 12:08:37 |     INFO | --------------------------------------------------------------------------------