From 77fe8a78ed4dd15aa0106dcab8b01ca469297fbe Mon Sep 17 00:00:00 2001 From: nillst Date: Thu, 7 Dec 2017 17:45:43 +0000 Subject: [PATCH 1/4] initial commit --- gpkitmodels/tools/qstr.py | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 gpkitmodels/tools/qstr.py diff --git a/gpkitmodels/tools/qstr.py b/gpkitmodels/tools/qstr.py new file mode 100644 index 00000000..c705b1fb --- /dev/null +++ b/gpkitmodels/tools/qstr.py @@ -0,0 +1,5 @@ +from gpkit.small_scripts import mag + +def qstr(qty): + 'Converts a pint quantity to a nice string for display' + return "%-.4g %s" % (qty.magnitude, u"{:~}".format(qty.units)) if hasattr(qty, "magnitude") else (qty, "") From 4056fbc1686263de757f81250f7598f9e9e02e96 Mon Sep 17 00:00:00 2001 From: bqpd Date: Thu, 7 Dec 2017 12:56:31 -0500 Subject: [PATCH 2/4] Update qstr.py --- gpkitmodels/tools/qstr.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gpkitmodels/tools/qstr.py b/gpkitmodels/tools/qstr.py index c705b1fb..bcd58608 100644 --- a/gpkitmodels/tools/qstr.py +++ b/gpkitmodels/tools/qstr.py @@ -2,4 +2,4 @@ def qstr(qty): 'Converts a pint quantity to a nice string for display' - return "%-.4g %s" % (qty.magnitude, u"{:~}".format(qty.units)) if hasattr(qty, "magnitude") else (qty, "") + return "%-.4g%s" % (qty.magnitude, u" {:~}".format(qty.units)) if hasattr(qty, "magnitude") else (qty, "") From 2a6c0797a25de64d32388474caf828c57a29e96a Mon Sep 17 00:00:00 2001 From: bqpd Date: Thu, 7 Dec 2017 13:00:02 -0500 Subject: [PATCH 3/4] Update qstr.py --- gpkitmodels/tools/qstr.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/gpkitmodels/tools/qstr.py b/gpkitmodels/tools/qstr.py index bcd58608..f02f0ad6 100644 --- a/gpkitmodels/tools/qstr.py +++ b/gpkitmodels/tools/qstr.py @@ -1,5 +1,12 @@ from gpkit.small_scripts import mag -def qstr(qty): - 'Converts a pint quantity to a nice string for display' - return "%-.4g%s" % (qty.magnitude, u" {:~}".format(qty.units)) if hasattr(qty, "magnitude") else (qty, "") +def qstr(qty, fmtstr="%-.4g"): + 'Converts a number/pint quantity to a nice string for display' + if hasattr(qty, "magnitude"): + magstr = fmt % qty.magnitude + unitstr = u" {:~}".format(qty.units)) + if "dimensionless" in unitstr: # boring and long way to put it + unitstr = "" + return magstr + unitstr + else: + return fmt % qty # it's just a number From d0a9444ee98dadf3cf81a7475158ae7ce1a017f5 Mon Sep 17 00:00:00 2001 From: nillst Date: Fri, 15 Dec 2017 18:20:57 +0000 Subject: [PATCH 4/4] fixed paren mismatch --- gpkitmodels/tools/qstr.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gpkitmodels/tools/qstr.py b/gpkitmodels/tools/qstr.py index f02f0ad6..b0f14d81 100644 --- a/gpkitmodels/tools/qstr.py +++ b/gpkitmodels/tools/qstr.py @@ -4,7 +4,7 @@ def qstr(qty, fmtstr="%-.4g"): 'Converts a number/pint quantity to a nice string for display' if hasattr(qty, "magnitude"): magstr = fmt % qty.magnitude - unitstr = u" {:~}".format(qty.units)) + unitstr = u" {:~}".format(qty.units) if "dimensionless" in unitstr: # boring and long way to put it unitstr = "" return magstr + unitstr