diff options
author | maheshgudi | 2017-08-24 16:55:18 +0530 |
---|---|---|
committer | maheshgudi | 2017-08-24 16:55:18 +0530 |
commit | 3367cf1bef0b8856972f4e4125322194c699d69e (patch) | |
tree | 6a9cc17591b9561b68d2782f742bc886f3360682 | |
parent | 6ab383c02085936ecc20ffeb4b9f41ca642256c3 (diff) | |
download | online_test-3367cf1bef0b8856972f4e4125322194c699d69e.tar.gz online_test-3367cf1bef0b8856972f4e4125322194c699d69e.tar.bz2 online_test-3367cf1bef0b8856972f4e4125322194c699d69e.zip |
Adds alphabetical order during yaml serializing
-rw-r--r-- | yaksh/models.py | 8 |
1 files changed, 5 insertions, 3 deletions
diff --git a/yaksh/models.py b/yaksh/models.py index b4c665c..cd79268 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -3,6 +3,7 @@ from datetime import datetime, timedelta import json import ruamel.yaml from ruamel.yaml.scalarstring import PreservedScalarString +from ruamel.yaml.comments import CommentedMap from random import sample from collections import Counter from django.db import models @@ -119,8 +120,8 @@ def dict_to_yaml(dictionary, path_to_file=None): ruamel.yaml.round_trip_dump(dictionary, yaml_file, default_flow_style=False, explicit_start=True, - allow_unicode=True - ) + allow_unicode=True, + ) ############################################################################### @@ -510,7 +511,8 @@ class Question(models.Model): tmp_file_path = tempfile.mkdtemp() yaml_path = os.path.join(tmp_file_path, "questions_dump.yaml") for elem in q_dict: - dict_to_yaml(elem, yaml_path) + commented_map = CommentedMap(sorted(elem.items(), key=lambda x:x[0])) + dict_to_yaml(commented_map, yaml_path) zip_file.write(yaml_path, os.path.basename(yaml_path)) zip_file.close() shutil.rmtree(tmp_file_path) |