Comments (8)
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.
from strictyaml.
I don't like this behavior actually. There's a clear trade off to be made between explicitness/loose coupling and less code written. I don't think the trade off favors implicit behavior of reading a file.
- Reading a file into a string is, in any case, a one or two liner so you're not typically saving a lot of characters.
- Reading files has a wide variety of tricky edge cases encodings, file not found, filesystem errors, links, etc.) which I believe should be exposed to and handled by the programmer or a separate library instead of overloading the parser with that behavior.
- Not all YAML will necessarily come directly from a file.
My code that does this typically works like so (using the lovely python library path.py)::
from path import Path
import strictyaml
config = strictyaml.load(Path("/path/to/file.yml").bytes().decode('utf8'))
I believe this balances explicitness, a balance of responsibilities between libraries and conciseness nicely.
I also strongly believe you should avoid using this kind of type comparisons and use isinstance instead.
This is an ugly line of code but it's necessary to be python 2/3 compatible.
Also consider adapting validators.py where types are checked using == which is clearly a bad idea
I'll look into this.
from strictyaml.
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.
from strictyaml.
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?
I would probably use a new explicit API call for that e.g. load_stream. There's a large number of tricky issues with handling that use case which are absent from the use case of just loading a string.
I haven't considered that a priority since I'm focused on readability of the YAML used as a DSL or config file. Readable DSL/config file very much implies your file isn't a million lines long and fits in a string.
The stream api is quite common in Python
It is for other use cases but I don't see much of a need for loading short-ish config files or DSLs via the stream API. Where's the downside to loading streams into a string and parsing that?
I don't understand what is wrong about using isinstance in py2/3.
Try doing isinstance(u"x", unicode) in python 3.
from strictyaml.
I'm sorry, in this case, I don't see any good reason to break the duck typing philosophy of Python.
The strict type check is to make sure that you are sending a type that strictyaml will parse.
I was expecting strictyaml to handle the same kind of cases than ruamel.yaml.
from strictyaml.
I would probably use a new explicit API call for that e.g. load_stream. There's a large number of tricky issues with handling that use case which are absent from the use case of just loading a string.
I didn't look at the code, but why do you have to consider more tricky issues that the one that are currently handled by ruamel.yaml? Just pass the parameter to ruamel.yaml.load, and work with the returned value, no?
Try doing isinstance(u"x", unicode) in python 3.
Right! Exactly a good reason to remove this check :-D
(Or to use six
, or to define unicode
in Python 2 if needed, or...) ;)
from strictyaml.
I didn't look at the code, but why do you have to consider more tricky issues that the one that are currently handled by ruamel.yaml?
It has implications for validation as well. It might be possible to get it to work but it would require a pretty significant refactor.
Which, for a feature that as yet doesn't have a presented use case, I'm skeptical of the need for.
Right! Exactly a good reason to remove this check :-D (Or to use six, or to define unicode in Python 2 if needed, or...) ;)
The strict type check is to make sure that you are sending a type that strictyaml will parse. It won't parse integers, lists, file streams or anything else and so it should fail fast if you try.
I could introduce six but for a minor clean up one line of code I'd prefer to avoid the heaviness of the dependency.
from strictyaml.
That's unfortunate... :(
from strictyaml.
Related Issues (20)
- Problem loading document with a single string
- Repeated revalidate() raises Invalid state HOT 2
- Failed revalidation leads to inconsistant state
- Fasjo HOT 3
- Website fails to acknowledge that the Norway problem was fixed in YAML 1.2 HOT 2
- strictyaml does not act as "a near-drop in replacement for pyyaml" HOT 4
- The type order of optional arguments affects the results HOT 2
- Date vs Datetime HOT 2
- Support python2.7.18 HOT 2
- 1.7.0 is broken (cannot import name 'ruamel' from partially initialized module 'strictyaml') HOT 5
- Map containing only Optionals does not validate an empty dict
- as_document and load have dissimilar results for CommaSeparated(Int())
- Doesn't Work At All For Lists / Arrays? HOT 4
- New lines after comment line causes `NotImplementedError`
- Feature request/Question: "Optionalize" maps HOT 1
- Thoughts regarding case-sensitivity of keys HOT 6
- Why not: non-Turing complete configuration languages
- Cannot iterate on emtpy list
- BUG: MapCombined documentation different traceback
- Feature: strictyaml.scalar.Time
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from strictyaml.