-
Notifications
You must be signed in to change notification settings - Fork 98
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
to_yaml() does not honor ordered_box #104
Comments
Ouh, good idea. I think it will require passing the object through the python builtin OrderedDict somehow, so will have to look into this. Thanks! |
FWIW, PyYaml 5.1+ can handle this with the In [1]: from box import Box
In [2]: b = Box(last="1", first="2", an_arbitrarily_long_key_that_should_absolutely_be_last="3", ordered_box=True)
In [3]: b.keys()
Out[3]: ['last', 'first', 'an_arbitrarily_long_key_that_should_absolutely_be_last']
In [4]: print(b.to_yaml()) # this gets sorted alphabetically >_<
an_arbitrarily_long_key_that_should_absolutely_be_last: '3'
first: '2'
last: '1'
In [8]: print(b.to_yaml(sort_keys=False)) # this is ordered correctly!!!
last: '1'
first: '2'
an_arbitrarily_long_key_that_should_absolutely_be_last: '3' So unless you want to automatically add |
That's great to know! I will have to see if such a simple thing also exists with ruamel.yaml and if so easy enough to include on ordered boxes all the time. Thanks for figuring that out and glad you can get it to work for what you need already! |
Sadly it looks like doing this in ruamel is much more cumbersome https://stackoverflow.com/questions/40226610/ruamel-yaml-equivalent-of-sort-keys and I don't think is worth setting as the default method for everyone's use case. |
Sorry to update a long closed issue, but... now that In [1]: from box import Box
In [2]: b = Box(last="1", first="2", an_arbitrarily_long_key_that_should_absolutely_be_last="3")
In [3]: b.keys()
Out[3]: ['last', 'first', 'an_arbitrarily_long_key_that_should_absolutely_be_last']
In [4]: print(b.to_yaml()) # this gets sorted alphabetically >_<
an_arbitrarily_long_key_that_should_absolutely_be_last: '3'
first: '2'
last: '1'
In [5]: from ruamel.yaml import RoundTripDumper
In [6]: print(b.to_yaml(Dumper=RoundTripDumper)) # this is ordered correctly!!!
last: '1'
first: '2'
an_arbitrarily_long_key_that_should_absolutely_be_last: '3' Sadly, using the RoundTripLoader/RoundTripDumper doesn't retain comments, which is one of the major reasons for using |
First of all love the concept of normalizing different data intake formats for conversion. Thanks for your efforts! Have been experimenting with lots of different packages and once its time to get to the yml output step So when I got to using this package I was stuck w sorted data but thank you to @philipsd6 for this tip. One of the things that makes python so awesome is to be able to quickly drop in packages and move right along. IMO the basic usecase / expectation is to preserve original order and should be the default. As is this is a rough edge in the "easy drop in" department and I think really should be addressed as a higher priority. |
YAML really looks better when ordered logically, not arbitrarily!
The text was updated successfully, but these errors were encountered: