1. Georg Brandl
  2. ipython-physics

Commits

Anders Lehmann  committed 3a999a3

Adding a __format__method. With a few tests

  • Participants
  • Parent commits c4a1a08
  • Branches default

Comments (0)

Files changed (2)

File physics.py

View file
  • Ignore whitespace
         return self.__class__(new_value, num + denom)
 
     # implementations of special functions, used by numpy ufuncs
+    def __format__(self, *args, **kw):
+        formatstring = "{1:{0}} {2}".format(args[0],self.value, self.unit)
+        return formatstring
 
     def sqrt(self):
         return pow(self, 0.5)

File test_prefilter.py

View file
  • Ignore whitespace
+from physics import Q
+
+def test_format():
+    a = Q('1.45 m')
+    s = "{}".format(a)
+    assert s == '1.45 m'
+#    s = "{:.4e}".format(a)
+#    assert s == '1.4500E+0 m'
+    s = "{.value}".format(a)
+    assert s == '1.45'
+