PyYAML shows "ScannerError: mapping values are not allowed here" in my unittest

PyYAML shows "ScannerError: mapping values are not allowed here" in my unittest

我正在尝试使用 unittest 测试一些 Python 2.7 classes。

这里是例外:

ScannerError: mapping values are not allowed here
    in "<unicode string>", line 3, column 32:
            ... file1_with_path: '../../testdata/concat1.csv'

这是与错误消息相关的示例:

class TestConcatTransform(unittest.TestCase):

def setUp(self):
    filename1 = os.path.dirname(os.path.realpath(__file__)) + '/../../testdata/concat1.pkl'
    self.df1 = pd.read_pickle(filename1)
    filename2 = os.path.dirname(os.path.realpath(__file__)) + '/../../testdata/concat2.pkl'
    self.df2 = pd.read_pickle(filename2)

    self.yamlconfig =  u'''
        --- !ConcatTransform
        file1_with_path: '../../testdata/concat1.csv'
        file2_with_path: '../../testdata/concat2.csv'
        skip_header_lines: [0]
        duplicates: ['%allcolumns']
        outtype: 'dataframe'
        client: 'testdata'
        addcolumn: []
    '''
    self.testconcat = yaml.load(self.yamlconfig)

问题是什么?

我不清楚的是我的目录结构是:

app
app/etl
app/tests

ConcatTransformapp/etl/concattransform.py 中,TestConcatTransformapp/tests 中。我使用此导入将 ConcatTransform 导入 TestConcatTransform 单元测试:

from app.etl import concattransform

PyYAML 如何将 class 与 yamlconfig 中定义的相关联?

YAML 文档可以以文档开始标记 --- 开始,但它必须位于一行的开头,而您的文档在输入的第二行缩进八个位置。这导致 --- 被解释为多行普通(即非引号)标量的开头,并且在这样的标量中你不能有 : (冒号 + space ).您只能在带引号的标量中包含 :。如果您的文档在根级别没有映射或序列,就像您的一样,整个文档只能由一个标量组成。

如果你想像现在一样保持源代码的良好缩进,我建议你使用 textwrap 中的 dedent

以下运行没有错误:

import ruamel.yaml
from textwrap import dedent

yaml_config = dedent(u'''\
        --- !ConcatTransform
        file1_with_path: '../../testdata/concat1.csv'
        file2_with_path: '../../testdata/concat2.csv'
        skip_header_lines: [0]
        duplicates: ['%allcolumns']
        outtype: 'dataframe'
        client: 'testdata'
        addcolumn: []
''')

yaml = ruamel.yaml.YAML()
data = yaml.load(yaml_config)

您应该养成在第一个三重引号末尾加上反斜杠 (\) 的习惯,因此您的 YAML 文档。如果这样做,您的错误实际上会指示第 2 行,因为文档不再以空行开头。


在加载 YAML 解析器期间遇到标签 !ConcatTransform。对象的构造函数可能已在 PyYAML 加载程序中注册,并在导入期间将该标记与使用 PyYAML 的 add_constructor 相关联。

不幸的是,他们使用默认的、非安全的加载程序注册了构造函数,这不是必需的,他们本可以使用 SafeLoader 注册的,因此不会强迫用户冒非受控输入问题的风险.