What's the difference between! and!! in the barley?

I am trying to load YAML that looks like this:

dist: !!opencv-matrix
   rows: 380
   cols: 380
   dt: f
   data: [ 0., 0., -1.88644529e+18, 2.45423365e+00, 11698176.,
       2.03862047e+00, -8.85501460e+10, 2.54738545e+00, 1.71208843e+20,
       ...
       2.44447327e+00 ]

Download Code:

import yaml
y = yaml.load(s)

where s is YAML loaded into the string.

I get this error:

yaml.constructor.ConstructorError: could not determine a constructor for the tag 'tag:yaml.org,2002:opencv-matrix'
  in "<string>", line 382, column 7:
    dist: !!opencv-matrix

This is fair enough, so I am adding a constructor for this tag:

def opencv_matrix(loader, node):
    mapping = loader.construct_mapping(node)
    mat = np.array(mapping["data"])
    mat.resize(mapping["rows"], mapping["cols"])
    return mat

yaml.add_constructor(u"!!opencv-matrix", opencv_matrix)
y = yaml.load(s)

I am still getting the error. However, if I replace! Opencv_matrix with! Opencv_matrix, then everything will work.

What's going on here?

+5
source share
1 answer

Secondary tags, such as !!opencv-matrix, are actually abbreviated for tag:yaml.org,2002:opencv-matrix(mentioned in the reference map). It seems that the PyYAML method does add_constructornot properly handle this shortened notation.

, , (. ). , , , .

, !opencv-matrix, , , , PyYAML.

, !!opencv-matrix tag:yaml.org,2002:opencv-matrix add_constructor.


, AFAIK (!) , (!!) ( ).

OpenCV, , , .

+4
source

All Articles