From 123e7bc05478eb448a1239a4efdd5029606a09c8 Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Sun, 15 Jul 2018 17:38:09 +0800 Subject: [PATCH] pyon: sort string dicts by key when pretty-printing. Closes #1010 --- artiq/protocols/pyon.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/artiq/protocols/pyon.py b/artiq/protocols/pyon.py index 092e46b52..932e95996 100644 --- a/artiq/protocols/pyon.py +++ b/artiq/protocols/pyon.py @@ -17,6 +17,7 @@ function call syntax to express special data types. """ +from operator import itemgetter import base64 from fractions import Fraction from collections import OrderedDict @@ -113,15 +114,20 @@ class _Encoder: return r def encode_dict(self, x): + if self.pretty and all(k.__class__ == str for k in x.keys()): + items = lambda: sorted(x.items(), key=itemgetter(0)) + else: + items = x.items + r = "{" if not self.pretty or len(x) < 2: r += ", ".join([self.encode(k) + ": " + self.encode(v) - for k, v in x.items()]) + for k, v in items()]) else: self.indent_level += 1 r += "\n" first = True - for k, v in x.items(): + for k, v in items(): if not first: r += ",\n" first = False