Comments (9)
Sure, I'd love to see it.
Custom scalar parameters were something I was going to work on soon - is that kind of what you were talking about?
from strictyaml.
I am not sure what you mean by "custom scalar parameter", just see my code here.
from strictyaml.
Ok, I think I understand. Yes, I very much want something like this.
Ideally, stricyaml.load(str) would return an object rather than a dict/list. You could treat this object purely as a dict or a list or a scalar (by overriding the relevant magic methods), but you could also use it to look up the line number of a scalar (for instance) as well as being able to use it to trivially load, change a value and roundtrip the YAML with comments included.
from strictyaml.
Hi @kshpytsya
This feature is now more or less implemented on the master branch.
The way it functions is documented in this test: https://github.com/crdoconnor/strictyaml/blob/master/tests/whatline.test
I haven't fully decided on the API so I haven't released it on to pypi yet. Any feedback would be very welcome.
from strictyaml.
As of this commit the functionality seems to be non-obvious or inconsistent.
> s = '\n\na:\n -\n A: 1\n -\n B: 2\n'
> v = strictyaml.load(s)
> [i.start_line for i in (v, v["a"], v["a"][0], v["a"][1])]
[1, 1, 3, 4]
> print(v.lines())
a:
- A: '1'
- B: '2'
>
So, the start_line
values does not seem to correspond to line numbers in the input string but rather in some transformed string.
Apparently missing is the way to access position of mapping keys:
> [i[0].start_line for i in v.items()]
AttributeError: 'str' object has no attribute 'start_line'
Also, I would like to be able to get info on columns and not just line numbers. Moreover, please take a look at my feature request for ruamel.yaml
.
To reiterate: I want to be able to obtain, for any YAML element (mapping, sequence or scalar), a precise position in the input string (i.e. line and column) that can be used e.g. to position cursor in an editor. For string scalars I would like to be able to obtain a precise position corresponding to any character (identified by its index) of the string, and not just the start and end positions. Please see the above-mentioned feature request for rationale.
The author of ruamel.yaml
appears to be unconvinced that the feature belongs to the library despite my explanations that doing so outside the library would require considerable piece of library code to be copied.
Of course, I could implement all this functionality I desire (see also issue 16) myself, but where should it go? A fork of strictyaml
? Yet another something-yaml
library having strictyaml
as a dependency? Definitely not as a part of the final project using this functionality, as there may be more than one such project.
from strictyaml.
Ah, I think you found a bug with ruamel.yaml::
ruamelyaml.dump(ruamelyaml.load(s, Loader=ruamelyaml.RoundTripLoader), Dumper=ruamelyaml.RoundTripDumper)
a:
- A: 1
- B: 2
from strictyaml.
To reiterate: I want to be able to obtain, for any YAML element (mapping, sequence or scalar), a precise position in the input string (i.e. line and column) that can be used e.g. to position cursor in an editor.
Fair enough. This only goes half way in that case.
For string scalars I would like to be able to obtain a precise position corresponding to any character (identified by its index) of the string, and not just the start and end positions.
Ok. In that case if you had start_line and start_column, deriving the precise character position outside of strictyaml would be straightforward.
Of course, I could implement all this functionality I desire (see also issue 16) myself, but where should it go? A fork of strictyaml?
I'd be very happy to merge in a PR that did start_column, end_column, start_position (character position in the YAML string) and end_position provided it came with a test.
From that point, I think your specific use case (getting the exact position of o in x: hello) should be pretty trivial (I think that part should probably live outside of strictyaml).
from strictyaml.
Apparently missing is the way to access position of mapping keys
Noted. I will fix this. Thanks for pointing it out.
from strictyaml.
Access position of mapping keys is now in 4b38cc6
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 HOT 1
- 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.