From fc917e55a8b22e8df7b0e9a2dad7c6a01c9132b9 Mon Sep 17 00:00:00 2001 From: Jonathan Eunice Date: Fri, 29 Jun 2018 07:12:55 -0400 Subject: [PATCH 1/8] Added __format__ method - allows specifying precision in inline string formatting - e.g. 'size: {:0.1f}'.format(s) - using standard string formatting, not just bitmath.format - adds corresponding tests --- bitmath/__init__.py | 13 +++++++++++++ tests/test_representation.py | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/bitmath/__init__.py b/bitmath/__init__.py index 1198fd0..db0a284 100644 --- a/bitmath/__init__.py +++ b/bitmath/__init__.py @@ -413,6 +413,19 @@ def __str__(self): global format_string return self.format(format_string) + def __format__(self, format_spec): + """String representation of this object with custom formatting, such as specified number of digits.""" + global format_string + try: + # replace any format spec added into global format_string with the format_spec used in this invocation + value_end = format_string.index('{value') + 6 + brace_index = format_string.index('}', value_end) + format_string_custom = format_string[:value_end] + ':' + format_spec + format_string[brace_index:] + except ValueError: + # no {value} found in format_string, so cannot customize; fall back to global format_string + format_string_custom = format_string + return self.format(format_string_custom) + def format(self, fmt): """Return a representation of this instance formatted with user supplied syntax""" diff --git a/tests/test_representation.py b/tests/test_representation.py index 1403eb0..170c878 100644 --- a/tests/test_representation.py +++ b/tests/test_representation.py @@ -146,3 +146,39 @@ def test_print_byte_singular(self): one_Byte = bitmath.Byte(1.0) actual_result = one_Byte.format(fmt_str) self.assertEqual(expected_result, actual_result) + + def test_inline_format(self): + """Inline string formatting interpolates default format_string values""" + expected_result = 'size: 3.1215 MiB' + size = bitmath.MiB(3.1215) + actual_result = 'size: {size}'.format(size=size) + self.assertEqual(expected_result, actual_result) + + def test_inline_format_customized(self): + """Inline formats obey inline format specifications""" + expected_result = 'size: 3.1 MiB' + size = bitmath.MiB(3.1215) + actual_result = 'size: {size:.1f}'.format(size=size) + self.assertEqual(expected_result, actual_result) + + def test_inline_format_override(self): + """Inline formats use module defaults, overriding only format spec""" + orig_fmt_str = bitmath.format_string + bitmath.format_string = "{unit} {value:.3f}" + expected_result = 'size: MiB 3.1' + size = bitmath.MiB(3.1215) + actual_result = 'size: {size:.1f}'.format(size=size) + self.assertEqual(expected_result, actual_result) + bitmath.format_string = orig_fmt_str + + def test_inline_format_cant_override(self): + """Inline formats use module defaults, changing nothing if no {value} component""" + orig_fmt_str = bitmath.format_string + bitmath.format_string = "{power} {binary}" + expected_result = 'size: 20 0b1100011111000110101001111' + size = bitmath.MiB(3.1215) + # will not obey instant formatting, because global format_string doesn't allow that; + # obeys the global format_string instead + actual_result = 'size: {size:.1f}'.format(size=size) + self.assertEqual(expected_result, actual_result) + bitmath.format_string = orig_fmt_str \ No newline at end of file From 7f43eef0278151966d01843ebbe1532121e16f82 Mon Sep 17 00:00:00 2001 From: Jonathan Eunice Date: Tue, 3 Jul 2018 05:51:15 -0400 Subject: [PATCH 2/8] Tweaked test for trailing whitespace - and added modern py36 target --- .travis.yml | 2 ++ tests/test_representation.py | 10 +++++----- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index 6e50ec3..05b7f38 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,5 +12,7 @@ matrix: env: CI=ci2 - python: "3.3" env: CI=ci3 + - python: "3.6" + env: CI=ci3 script: make $CI install: "" diff --git a/tests/test_representation.py b/tests/test_representation.py index 170c878..a1cf37c 100644 --- a/tests/test_representation.py +++ b/tests/test_representation.py @@ -152,14 +152,14 @@ def test_inline_format(self): expected_result = 'size: 3.1215 MiB' size = bitmath.MiB(3.1215) actual_result = 'size: {size}'.format(size=size) - self.assertEqual(expected_result, actual_result) + self.assertEqual(expected_result, actual_result) def test_inline_format_customized(self): """Inline formats obey inline format specifications""" expected_result = 'size: 3.1 MiB' size = bitmath.MiB(3.1215) actual_result = 'size: {size:.1f}'.format(size=size) - self.assertEqual(expected_result, actual_result) + self.assertEqual(expected_result, actual_result) def test_inline_format_override(self): """Inline formats use module defaults, overriding only format spec""" @@ -168,7 +168,7 @@ def test_inline_format_override(self): expected_result = 'size: MiB 3.1' size = bitmath.MiB(3.1215) actual_result = 'size: {size:.1f}'.format(size=size) - self.assertEqual(expected_result, actual_result) + self.assertEqual(expected_result, actual_result) bitmath.format_string = orig_fmt_str def test_inline_format_cant_override(self): @@ -180,5 +180,5 @@ def test_inline_format_cant_override(self): # will not obey instant formatting, because global format_string doesn't allow that; # obeys the global format_string instead actual_result = 'size: {size:.1f}'.format(size=size) - self.assertEqual(expected_result, actual_result) - bitmath.format_string = orig_fmt_str \ No newline at end of file + self.assertEqual(expected_result, actual_result) + bitmath.format_string = orig_fmt_str From 61a87aa410f3739f464b6e6a836e257438f0575a Mon Sep 17 00:00:00 2001 From: Jonathan Eunice Date: Tue, 3 Jul 2018 05:56:06 -0400 Subject: [PATCH 3/8] Updated travis config - to trigger brnach build --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 05b7f38..b809354 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,6 +4,7 @@ notifications: branches: only: - master + - format-method after_success: - coveralls matrix: From b7e3b61ba4d3d78ce6c21178e7336ed94b839219 Mon Sep 17 00:00:00 2001 From: Jonathan Eunice Date: Tue, 3 Jul 2018 06:02:23 -0400 Subject: [PATCH 4/8] Extend python versions tested --- .travis.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index b809354..d5039b2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,7 +11,9 @@ matrix: include: - python: "2.7" env: CI=ci2 - - python: "3.3" + - python: "3.4" + env: CI=ci3 + - python: "3.5" env: CI=ci3 - python: "3.6" env: CI=ci3 From 89d73c03e36ebc1ae6a4e36fc0db1c97effaf77d Mon Sep 17 00:00:00 2001 From: Jonathan Eunice Date: Tue, 3 Jul 2018 06:05:02 -0400 Subject: [PATCH 5/8] Adding python 3.7 to testing matrix --- .travis.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.travis.yml b/.travis.yml index d5039b2..b2713fb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,5 +17,7 @@ matrix: env: CI=ci3 - python: "3.6" env: CI=ci3 + - python: "3.7" + env: CI=ci3 script: make $CI install: "" From ffd8eaac227771f5612203afa25b76804aeb8523 Mon Sep 17 00:00:00 2001 From: Jonathan Eunice Date: Tue, 3 Jul 2018 06:10:41 -0400 Subject: [PATCH 6/8] correct py3.7 invocation on travis --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index b2713fb..9164a99 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,7 +17,7 @@ matrix: env: CI=ci3 - python: "3.6" env: CI=ci3 - - python: "3.7" + - python: "3.7-dev" env: CI=ci3 script: make $CI install: "" From 1b8e8fc3da1dd8bbe002dfcaf1eb5ab0098fee1c Mon Sep 17 00:00:00 2001 From: Jonathan Eunice Date: Tue, 3 Jul 2018 06:15:09 -0400 Subject: [PATCH 7/8] back out not yet working py3.7 CI --- .travis.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 9164a99..d5039b2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,7 +17,5 @@ matrix: env: CI=ci3 - python: "3.6" env: CI=ci3 - - python: "3.7-dev" - env: CI=ci3 script: make $CI install: "" From f3a8793c406c0e8f1b9c73bd4dece5b10bdfc1d8 Mon Sep 17 00:00:00 2001 From: Jonathan Eunice Date: Tue, 3 Jul 2018 06:22:38 -0400 Subject: [PATCH 8/8] Updated setup.py to show current testable Python versions --- setup.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/setup.py b/setup.py index c723c69..6872216 100644 --- a/setup.py +++ b/setup.py @@ -59,9 +59,9 @@ 'Programming Language :: Python :: 2.6', 'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 3', - 'Programming Language :: Python :: 3.1', - 'Programming Language :: Python :: 3.2', - 'Programming Language :: Python :: 3.3', + 'Programming Language :: Python :: 3.4', + 'Programming Language :: Python :: 3.5', + 'Programming Language :: Python :: 3.6', 'Programming Language :: Python', 'Topic :: Scientific/Engineering :: Mathematics', 'Topic :: Software Development :: Libraries :: Python Modules',