question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add support to load a yaml from a file

See original GitHub issue

While ruamel.yaml supports any kind of stream for its load method, this is not the case for strictyaml, which currently checks the following:

    if stream_type not in ("<type 'unicode'>", "<type 'str'>", "<class 'str'>"):
        raise TypeError("StrictYAML can only read a string of valid YAML.")

I think strictyaml.load should implicitly accept all the types that are supported by the underlying call to ruamel.yaml.load.

I also strongly believe you should avoid using this kind of type comparisons and use isinstance instead. Also consider adapting validators.py where types are checked using == which is clearly a bad idea 😉

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
AlexandreDecancommented, Sep 5, 2016

What if I don’t want to store a whole document in a string? What if I want to read a stream from the network? Why should I use an explicit str, not any other (lazy?) iterable? The stream api is quite common in Python, and ruamel.yaml provides support for it. It is expected that your load function also supports it.

I believe you should either focus on (1) bringing a replacement of ruamel.yaml API with type safety support (built on top of ruamel.yaml for instance), or (2) bringing a tool that allows to validate a json (before it is loaded in ruamel) or to validate the structure returned by ruamel.yaml.load.

This is an ugly line of code but it’s necessary to be python 2/3 compatible.

I don’t understand what is wrong about using isinstance in py2/3.

2reactions
crdoconnorcommented, Sep 5, 2016

I’m sorry, in this case, I don’t see any good reason to break the duck typing philosophy of Python.

This philosophy is in direct contradiction to line 2 of the zen of python: explicit is better than implicit.

I don’t personally believe that either philosophy should be used dogmatically - there is a case by case trade off to be made between both. In this case I believe there’s enough complication under the hood as it is to justify not putting even more implicit behavior in.

For objects that implement __ str __ and return YAML it isn’t a problem just do strictyaml.load(str(object)). For file handles it’s not a huge deal to just do .read().

I was expecting strictyaml to handle the same kind of cases than ruamel.yaml.

It’s stricter.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I include a YAML file inside another? - Stack Overflow
Files are loaded from classpath (Maven resources directory): /** * Custom YAML loader. It adds support to the custom !include tag which allows...
Read more >
How to Load, Read, and Write YAML - Python Land
This article teaches you how to load, read, and write YAML files with PyYAML. In addition, you'll learn how to install it on...
Read more >
Python YAML – Read, Write, Parse YAML - PYnative
We can add application-specific tags and assign default values to certain tags while parsing the YAML file using the load() method. The steps ......
Read more >
How to use a YAML file or environment variables to populate ...
Decide where you would like to save the desired credentials or config values - in a YAML file, environment variables, or a combination...
Read more >
Writing YAML files with python - Towards Data Science
Another advantage is enum support, by adding comments to a yaml file, ... Importing ruamel into python and loading the yaml file. #...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found