Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Global float rounding for prettier output

My solution is kind of hacky but it seems like there is no better option.
See http://stackoverflow.com/questions/1447287/format-floats-with-standard-json-module
  • Loading branch information...
commit f0f9b2ac99118f513f5bf0fd5f7628d1f6ddb590 1 parent c7dcf9a
Simon Marti authored April 04, 2012
38  burger/roundedfloats.py
... ...
@@ -0,0 +1,38 @@
  1
+#!/usr/bin/env python
  2
+# -*- coding: utf8 -*-
  3
+"""
  4
+Copyright (c) 2011 Tyler Kenendy <tk@tkte.ch>
  5
+
  6
+Permission is hereby granted, free of charge, to any person obtaining a copy
  7
+of this software and associated documentation files (the "Software"), to deal
  8
+in the Software without restriction, including without limitation the rights
  9
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10
+copies of the Software, and to permit persons to whom the Software is
  11
+furnished to do so, subject to the following conditions:
  12
+
  13
+The above copyright notice and this permission notice shall be included in
  14
+all copies or substantial portions of the Software.
  15
+
  16
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22
+THE SOFTWARE.
  23
+"""
  24
+
  25
+
  26
+def transform_floats(o):
  27
+    if isinstance(o, float):
  28
+        return RoundedFloat(o)
  29
+    elif isinstance(o, dict):
  30
+        return dict((k, transform_floats(v)) for k, v in o.iteritems())
  31
+    elif isinstance(o, (list, tuple)):
  32
+        return map(transform_floats, o)
  33
+    return o
  34
+
  35
+
  36
+class RoundedFloat(float):
  37
+    def __repr__(self):
  38
+        return "%.5g" % self
5  munch.py
@@ -36,6 +36,7 @@
36 36
 from solum import JarFile
37 37
 
38 38
 from burger.website import Website
  39
+from burger.roundedfloats import transform_floats
39 40
 
40 41
 
41 42
 def import_toppings():
@@ -239,9 +240,9 @@ def __repr__(self):
239 240
         summary.append(aggregate)
240 241
 
241 242
     if not compact:
242  
-        json.dump(summary, output, sort_keys=True, indent=4)
  243
+        json.dump(transform_floats(summary), output, sort_keys=True, indent=4)
243 244
     else:
244  
-        json.dump(summary, output)
  245
+        json.dump(transform_floats(summary), output)
245 246
 
246 247
     # Cleanup temporary downloads
247 248
     if download_fresh_jar:

0 notes on commit f0f9b2a

Please sign in to comment.
Something went wrong with that request. Please try again.