在倭黑猩猩中使用@use装饰器

2024-06-26 00:18:38 发布

您现在位置:Python中文网/ 问答频道 /正文

在bonobo文档中,他们有以下配置服务依赖关系的示例:

from bonobo.config import use
@use('orders_database')
def select_all(database):
yield from database.query('SELECT * FROM foo;')

我试着做类似的事情,结果出错了。下面是我的脚本的一个非常简化的版本:

import bonobo
from bonobo.config import use
from ftplib import FTP

def get_services(**options):
    ftp_1 =  FTP('ftp.gnu.org')
    ftp_1.login()
    ftp_1.cwd('gnu/emacs')
    return{
        'ftp1': ftp_1,
    }

@use('ftp1')
def listen_for_file(ftp):
    test = ftp.nlst('README.olderversions')
    if test:
        print( 'Found file')
        return True
    else:
        print('File not found in ftp')
        return False

def get_graph(**options):
    graph = bonobo.Graph()
    graph.add_chain(listen_for_file)
    return graph

if __name__ == '__main__':
    parser = bonobo.get_argument_parser()
    with bonobo.parse_args(parser) as options:
        bonobo.run(get_graph(**options), services=get_services(**options))

如果尝试运行此操作,则会出现以下错误:

CRITICAL:bonobo.execution.contexts.base:<NodeExecutionContext(+listen_for_file) in=1 err=1> Traceback (most recent call last): File "C:\Users\mfrants\AppData\Local\Programs\Python\Python37\lib\site-packages\bonobo\config\processors.py", line 102, in call bound = self._bind(_input) File "C:\Users\mfrants\AppData\Local\Programs\Python\Python37\lib\site-packages\bonobo\config\processors.py", line 89, in _bind return bind(*self.args, *_input, **self.kwargs) File "C:\Users\mfrants\AppData\Local\Programs\Python\Python37\lib\inspect.py", line 3015, in bind return args[0]._bind(args[1:], kwargs) File "C:\Users\mfrants\AppData\Local\Programs\Python\Python37\lib\inspect.py", line 2930, in _bind raise TypeError(msg) from None TypeError: missing a required argument: 'ftp'

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "C:\Users\mfrants\AppData\Local\Programs\Python\Python37\lib\site-packages\bonobo\execution\contexts\node.py", line 102, in loop self.step() File "C:\Users\mfrants\AppData\Local\Programs\Python\Python37\lib\site-packages\bonobo\execution\contexts\node.py", line 129, in step results = self._stack(input_bag) File "C:\Users\mfrants\AppData\Local\Programs\Python\Python37\lib\site-packages\bonobo\config\processors.py", line 114, in call ) from exc bonobo.errors.UnrecoverableTypeError: Input of <function listen_for_file at 0x000002A591047F78> does not bind to the node signature. Args: () Input: () Kwargs: {'ftp1': <ftplib.FTP object at 0x000002A5910CF708>} Signature: (ftp)

我能让它运行的唯一方法是在listen_for_file函数中将ftp更改为ftp1。我做错了什么


Tags: infrompybindliblocallineftp
1条回答
网友
1楼 · 发布于 2024-06-26 00:18:38

bonobo对变量命名很严格——尽管不应该这样

要使用config.use,最好在函数签名中使用确切的服务名称,并将其保留为最后一个参数

要更改要使用的实际服务提供商,需要在get_services函数中更改映射

import bonobo
from bonobo.config import use
from ftplib import FTP

def get_services(**options):
    ftp_1 =  FTP(options.get('ftp_server') or 'ftp.gnu.org')
    ftp_1.login()
    ftp_1.cwd('gnu/emacs')
    return{
        'ftp': ftp_1,
    }

@use('ftp')
def listen_for_file(ftp):
    test = ftp.nlst('README.olderversions')
    if test:
        print( 'Found file')
        return True
    else:
        print('File not found in ftp')
        return False

def get_graph(**options):
    graph = bonobo.Graph()
    graph.add_chain(listen_for_file)
    return graph

if __name__ == '__main__':
    parser = bonobo.get_argument_parser()
    parser.add(' ftp_server')
    with bonobo.parse_args(parser) as options:
        bonobo.run(get_graph(**options), services=get_services(**options))

相关问题 更多 >