3

所以,这是一个kluge。我正在与 luigi 和 sciluigi 合作。

isinstance支票在 sciluigi 包中,我宁愿把它弄好,然后必须为这个小问题分支整个 sciluigi :)

简而言之,我必须继承包的一个类 (luigi.LocalTarget) - 以添加额外的功能。该功能效果很好,但是 sciluigi 包中有一个对象检查...

sciluigi.dependencies.DependencyHelpers._parse_outputitem()

...这会导致运行失败,因为某isinstance行设置为仅检查“TargetInfo”对象。

我想做的只是告诉我的孩子类“撒谎”,isinstance以便它作为一个TargetInfo对象报告并通过:D

事先请求宽恕:D

def _parse_outputitem(self, val, targets):
    '''
    Recursively loop through lists of TargetInfos, or
    callables returning TargetInfos, or lists of ...
    (repeat recursively) ... and return all targets.
    '''
    if callable(val):
        val = val()
    if isinstance(val, TargetInfo):
        targets.append(val.target)
    elif isinstance(val, list):
        for valitem in val:
            targets = self._parse_outputitem(valitem, targets)
    elif isinstance(val, dict):
        for _, valitem in iteritems(val):
            targets = self._parse_outputitem(valitem, targets)
    else:
        raise Exception('Input item is neither callable, TargetInfo, nor list: %s' % val)
    return targets

错误信息:

2017-04-06 22:26:09,753 - PipeineTest1 - DEBUG - RunSubprocess:Traceback (most recent call last):
2017-04-06 22:26:09,754 - PipeineTest1 - DEBUG - RunSubprocess:  File "/Library/Python/2.7/site-packages/luigi/worker.py", line 305, in check_complete
2017-04-06 22:26:09,754 - PipeineTest1 - DEBUG - RunSubprocess:    is_complete = task.complete()
2017-04-06 22:26:09,754 - PipeineTest1 - DEBUG - RunSubprocess:  File "/Library/Python/2.7/site-packages/luigi/task.py", line 482, in complete
2017-04-06 22:26:09,754 - PipeineTest1 - DEBUG - RunSubprocess:    outputs = flatten(self.output())
2017-04-06 22:26:09,754 - PipeineTest1 - DEBUG - RunSubprocess:  File "/Library/Python/2.7/site-packages/sciluigi/dependencies.py", line 99, in output
2017-04-06 22:26:09,754 - PipeineTest1 - DEBUG - RunSubprocess:    return self._output_targets()
2017-04-06 22:26:09,755 - PipeineTest1 - DEBUG - RunSubprocess:  File "/Library/Python/2.7/site-packages/sciluigi/dependencies.py", line 111, in _output_targets
2017-04-06 22:26:09,755 - PipeineTest1 - DEBUG - RunSubprocess:    output_targets = self._parse_outputitem(attrval, output_targets)
2017-04-06 22:26:09,755 - PipeineTest1 - DEBUG - RunSubprocess:  File "/Library/Python/2.7/site-packages/sciluigi/dependencies.py", line 132, in _parse_outputitem
2017-04-06 22:26:09,755 - PipeineTest1 - DEBUG - RunSubprocess:    raise Exception('Input item is neither callable, TargetInfo, nor list: %s' % val)
2017-04-06 22:26:09,755 - PipeineTest1 - DEBUG - RunSubprocess:Exception: Input item is neither callable, TargetInfo, nor list: <Bioproximity.common.luigi_extensions.local_target.ToppasLocalTarget object at 0x110e48190>

...不幸的是,这是 Sciluigi 作为输出提供的 100% 的错误回溯。

sciluigi.dependencies.TargetInfo(object)

class TargetInfo(object):
    '''
    Class to be used for sending specification of which target, from which
    task, to use, when stitching workflow tasks' outputs and inputs together.
    '''
    task = None
    path = None
    target = None

    def __init__(self, task, path, format=None, is_tmp=False):
        self.task = task
        self.path = path
        self.target = luigi.LocalTarget(path, format, is_tmp)

    def open(self, *args, **kwargs):
        '''
        Forward open method, from luigi's target class
        '''
        return self.target.open(*args, **kwargs)

# ==============================================================================
4

2 回答 2

2

TargetInfo我认为除了LocalTarget已经拥有的子类之外,您还需要创建一个子类。看起来您当前正在尝试将当前的子类用作前者,而它是后者的实例。传递自定义类的实例不起作用,因为LocalTarget在同一个地方传递常规也不起作用。

尝试这样的事情:

class MyTargetInfo(TargetInfo):            # pick your own name
    def __init__(self, task, path, *args): # you might want to explicitly name the args here
        self.task = task
        self.path = path
        self.target = ToppasLocalTarget(*args)

当您给它一个子类的实例时,您需要将此类的一个实例传递给给您错误的函数LocalTarget。正如我评论的那样,你应该给这个类一个更好的名字,也许给你需要显式传递给你的其他类的参数命名(也许给默认值)(而不是使用*args)。

如果构造targetinsideMyTargetInfo.__init__不能很好地满足您的自定义类的需求(例如,您需要提前创建它或多次重复使用相同的实例),您可以将一个已经存在的实例传递LocalTarget给构造函数,并将其分配给self.target而不是创建一个新对象。我对您用来判断这是否是一个好主意的库知之甚少。

于 2017-04-06T21:45:25.680 回答
1

看起来您只需要子类TargetInfo化,以便您的对象通过isinstance检查。你可以这样:

class Foo(<whatever other base classes you have>, TargetInfo):
    ...

如果您有TargetInfo作为根后代,那么它不应该干扰类的功能,因为其他基类将覆盖任何冲突的方法。

于 2017-04-06T20:51:01.920 回答