Modify ↓
Ticket #23 (closed enhancement: fixed)
Dictionary output not sorted.
| Reported by: | tim.hochberg@… | Owned by: | xi |
|---|---|---|---|
| Priority: | normal | Component: | pyyaml |
| Severity: | normal | Keywords: | |
| Cc: |
Description
It is convenient to have YAML's output be consistent across runs. Some representers in PyYaml? do this already, but the dictionaries do not. I implemented a quick fix by replacing represent_dict with:
def represent_dict(self, data):
items = data.items()
items.sort()
return self.represent_mapping(u'tag:yaml.org,2002:map', items)
It might also be convenient to have custom sort orders as PyYaml? legacy does, but that's a bigger change.
Attachments
Change History
Note: See
TracTickets for help on using
tickets.

Fixed in [222].
Now represent_mapping converts a dictionary to a list of pairs and sorts it.
The easiest way to have custom sort is to define a custom representer and pass a suitably sorted list of pairs to represent_mapping.