Code Monkey home page Code Monkey logo

Comments (1)

mk-fg avatar mk-fg commented on May 27, 2024

It's happening because you're using "safe=True" there, which ensures that PyYAML's more robust serialization is used, and de-serialized data will be same as original, at the cost of some prettiness.

Reason why that one string is singled-out for escaping is simple - only 0.51 will be de-serialized as a number (0.51), while 0.5.1 and 0.7.1 will be interpreted as strings anyway (due to two dots in there), so no need for quotes there.

If you won't be passing safe=True, you should get desired output, but when de-serialized by any sane YAML parser, it will not match the original types, as mentioned.

Example:

% python3 -m pyaml <<< "{'y': ['0.5.1', '0.51', '0.7.1']}"
y:
  - 0.5.1
  - 0.51
  - 0.7.1

% python3 -c 'import yaml; print(yaml.safe_load("{y: [0.5.1, 0.51, 0.7.1]}"))'
{'y': ['0.5.1', 0.51, '0.7.1']}

Note missing quotes around 0.51 in the last output, indicating that it indeed was parsed as a float object instead of string.

It's an intended behavior that "unsafe" (default) serializer may produce such quirks (though hopefully not many), with human-readability preferred over correctness.
Same thing also mentioned in #20, I believe.

from pretty-yaml.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.