Patchwork [3,of,4] templatefilters: use list comprehension in json()

login
register
mail settings
Submitter Yuya Nishihara
Date April 2, 2017, 3:28 a.m.
Message ID <eba702fdc933ed84d5f5.1491103703@mimosa>
Download mbox | patch
Permalink /patch/19903/
State Accepted
Headers show

Comments

Yuya Nishihara - April 2, 2017, 3:28 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1491101664 -32400
#      Sun Apr 02 11:54:24 2017 +0900
# Node ID eba702fdc933ed84d5f53225bfe87ebc40c7831e
# Parent  54017aea6a3b5c7a668f44df4399102b94a9eecd
templatefilters: use list comprehension in json()

Not important, but the code slightly looks better.

Patch

diff --git a/mercurial/templatefilters.py b/mercurial/templatefilters.py
--- a/mercurial/templatefilters.py
+++ b/mercurial/templatefilters.py
@@ -230,15 +230,11 @@  def json(obj):
     elif isinstance(obj, str):
         return '"%s"' % encoding.jsonescape(obj, paranoid=True)
     elif util.safehasattr(obj, 'keys'):
-        out = []
-        for k, v in sorted(obj.iteritems()):
-            s = '%s: %s' % (json(k), json(v))
-            out.append(s)
+        out = ['%s: %s' % (json(k), json(v))
+               for k, v in sorted(obj.iteritems())]
         return '{' + ', '.join(out) + '}'
     elif util.safehasattr(obj, '__iter__'):
-        out = []
-        for i in obj:
-            out.append(json(i))
+        out = [json(i) for i in obj]
         return '[' + ', '.join(out) + ']'
     else:
         raise TypeError('cannot encode type %s' % obj.__class__.__name__)