Skip to content
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

Add callable sort key functionality to sort_keys #666

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/yaml/representer.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,10 @@ def represent_mapping(self, tag, mapping, flow_style=None):
mapping = list(mapping.items())
if self.sort_keys:
try:
mapping = sorted(mapping)
if callable(self.sort_keys):
mapping = sorted(mapping, key=self.sort_keys)
else:
mapping = sorted(mapping)
except TypeError:
pass
for item_key, item_value in mapping:
Expand Down
6 changes: 6 additions & 0 deletions tests/data/mapping.sort_vowels_first
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
u: 1
c: 2
v: 3
d: 4
w: 5
e: 6
6 changes: 6 additions & 0 deletions tests/data/mapping.sorted_vowels_first
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
e: 6
u: 1
c: 2
d: 4
v: 3
w: 5
41 changes: 41 additions & 0 deletions tests/lib/test_sort_keys_function.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import yaml
import sys
import functools

_vowels = set(('a','e','i','o','u'))

def sort_keys_vowels_first(a, b):
# params are (key,value) pairs
a=a[0]
b=b[0]
if (a in _vowels) == (b in _vowels):
return (a>b)-(a<b) # cmp(a,b)
else:
return (b in _vowels)*2-1

def test_sort_keys_function(input_filename, sorted_filename, verbose=False):
with open(input_filename, 'rb') as file:
input = file.read().decode('utf-8')
with open(sorted_filename, 'rb') as file:
sorted = file.read().decode('utf-8')
data = yaml.load(input, Loader=yaml.FullLoader)
dump_sorted = yaml.dump(data, default_flow_style=False, sort_keys=functools.cmp_to_key(sort_keys_vowels_first))
dump_unsorted = yaml.dump(data, default_flow_style=False, sort_keys=False)
dump_unsorted_safe = yaml.dump(data, default_flow_style=False, sort_keys=False, Dumper=yaml.SafeDumper)
if verbose:
print("INPUT:")
print(input)
print("DATA:")
print(data)

assert dump_sorted == sorted

if sys.version_info>=(3,7):
assert dump_unsorted == input
assert dump_unsorted_safe == input

test_sort_keys_function.unittest = ['.sort_vowels_first', '.sorted_vowels_first']

if __name__ == '__main__':
import test_appliance
test_appliance.run(globals())
1 change: 1 addition & 0 deletions tests/lib/test_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from test_recursive import *
from test_input_output import *
from test_sort_keys import *
from test_sort_keys_function import *
from test_multi_constructor import *

from test_schema import *
Expand Down