oldDave
oldDave

Reputation: 405

PyYAML ConstructError: could not determine a constructor for the tag

I have several classes constructed with PyYAML using Python 2.7. But only the class defined below fails with:

ConstructorError: could not determine a constructor for the tag 
    '!ClassifyTransform'
        in "<unicode string>", line 1, column 5:
        --- !ClassifyTransform
            ^

The YAML string is as follows:

        self.yamlconfig =  dedent(u'''\
        --- !ClassifyTransform
        file_with_path: '/../../testdata/classify.csv'
        skip_header_lines: 1
        duplicates: []
        outtype: 'dataframe'
        client: 'test'
        classifycolumn: [ 
            { newcolumn: 'audit_status',
              conditions: [['', 'Status', '==', {scalar: 'F', column: None}], ['&', 'STATUS', '==', {scalar: 'C', column: None}]],
              value: [['', {scalar: 'Filled', column: None}]]
            },
            { newcolumn: 'audit_status',
              conditions: [['', 'Status', '%NA', {scalar: '', column: None}], ['&', 'STATUS', '==', {scalar: 'C', column: None}]],
              value: [['', {scalar: 'Filled - Order Missing', column: None}], ['+', {scalar: '', column: None}]]
            }
        ]
    ''')

I use the same pattern for the constructor as in all my other classes:

def transform_constructor(loader, node):
    instance = ClassifyTransform.__new__(ClassifyTransform)
    yield instance
    state = loader.construct_mapping(node, deep=True)
    instance.__init__(**state)

yaml.add_constructor(yaml_tag, transform_constructor)

I cannot identify what is wrong with the yamlconfig.

I discovered in experimenting I had used this import

import ruamel.yaml as yaml

in my class and the yaml import in my testcase, no surprise it did not work.

Upvotes: 2

Views: 4663

Answers (1)

Tejas Tank
Tejas Tank

Reputation: 1216

pip3 install PyYAML==3.13

Required latest version of PyYAML. New Version support it and stable

For python 2.7 similar latest upgraded version required too.

Upvotes: 1

Related Questions