Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^2: python YAML output, perl Loader, which version?

by mgwmgw (Acolyte)
on Apr 16, 2014 at 21:53 UTC ( [id://1082563]=note: print w/replies, xml ) Need Help??


in reply to Re: python YAML output, perl Loader, which version?
in thread python YAML output, perl Loader, which version?

Here is an example of a string I created in perl that reproduces one of the problems:

$out = "Things:\n - Name: thing_1\n Sub:\n key1: value1\n + key2: very very very very very very very very very very very very + very very\n long line wrapped value2\nkey3: value3\n";

When it prints out in the log, it looks like this:

Things: - Name: thing_1 Sub: key1: value1 key2: very very very very very very very very very very very ver +y very very long line wrapped value2 key3: value3

If I paste this into yamlint it is valid, but the Loader complains:

YAML Error: Inconsistent indentation level

Replies are listed 'Best First'.
Re^3: python YAML output, perl Loader, which version?
by Anonymous Monk on Apr 16, 2014 at 22:03 UTC

    You want this?

    #!/usr/bin/perl -- use strict; use warnings; use YAML qw/ Load Dump /; use Data::Dump qw/ dd /; my $it = { Things => [ { Name => "thing_1", Sub => { key1 => "value1", key2 => "very very very very very very very very very +very very very very very long line\nwrapped value2", key3 => "value3", }, }, ], }; dd( Dump( $it ) ); print Dump( $it ), "\n"; __END__ "---\nThings:\n - Name: thing_1\n Sub:\n key1: value1\n +key2: |-\n very very very very very very very very very very v +ery very very very long line\n wrapped value2\n key3: val +ue3\n" --- Things: - Name: thing_1 Sub: key1: value1 key2: |- very very very very very very very very very very very very ve +ry very long line wrapped value2 key3: value3
Re^3: python YAML output, perl Loader, which version?
by Anonymous Monk on Apr 16, 2014 at 21:59 UTC
    How did you create the string? Why is your indentation level inconsistent? Perl module YAML also complains
    YAML Error: Inconsistent indentation level Code: YAML_PARSE_ERR_INCONSISTENT_INDENTATION Line: 6 Document: 1

      Let me explain again: I am testing product code that is being written in python. The product outputs what it outputs, WITH the inconsistent indentation, and I do not control that. The String is to show the nature of the problem without having to discuss details of the product, which do not matter for this problem and solution. Because the YAML output is considered to be valid by yamlint, the people writing the product code do not consider their code to be broken. I need to write perl tests which extract data from the output, in order to make sure that the content of the output is correct. In order to do this, I need to parse the output WITH the inconsistent indentation, which yamlint can do. The YAML Loader in perl's YAML::Old cannot parse the output and considers the output to be invalid. I strongly suspect that some newer YAML Loaders for perl would parse the output just fine, but there are so many newer YAML Loaders, and I am not sure which are fully working and finished.

      I guess I could blindly try whatever YAML Loaders for perl I can find until one of them works on this peculiarity, but since I expect to need to parse other YAML for these tests, with other peculiarities, I would rather choose the YAML Loader which is in the best shape, which I suspect means the Loader which is most compatible with the most recent or inclusive YAML spec.

      So, which Loader do I want?

      Am I making the problem clear yet?

      As to why the python behaves as it does, I suspect that this has to do with how PyYAML works.

        ...

        Thats all fine and well, but doesn't tell me what the data structure :) how am I supposed to guess what the real data structure is?

        YAML, great in theory :)

        I think you could try all of them to see if you can find one that works, and I think you wouldn't be able to find one ... something to do with the spec

        Maybe you want to copy/paste/rename YAML::Tiny and stop it from dying on errors

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1082563]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (8)
As of 2024-04-23 16:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found