谎言是实例

时间:2017-04-06 15:32:09

标签: python isinstance

所以,这是一个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(对象)

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)

# ==============================================================================

2 个答案:

答案 0 :(得分: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)。

如果在target内构建MyTargetInfo.__init__并不适合您的自定义类的需求(例如,您需要提前创建它或多次重复使用同一个实例),您可以将已存在的LocalTarget传递给构造函数,只需将其分配给self.target,而不是创建新对象。我不太了解您所使用的图书馆,以判断这是不是一个好主意。

答案 1 :(得分:1)

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

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

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