-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added __format__ method #76
base: 2023-01-26-no-more-py2
Are you sure you want to change the base?
Changes from all commits
fc917e5
7f43eef
61a87aa
b7e3b61
89d73c0
ffd8eaa
1b8e8fc
f3a8793
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -413,6 +413,19 @@ def __str__(self): | |
global format_string | ||
return self.format(format_string) | ||
|
||
def __format__(self, format_spec): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the purpose of updating the documentation later, can you tell me some of the limitations of this? Additional more complex examples would be even better. Even just a gist w/ you playing around with this in a python interpreter showing example input and output would be helpful. Like, as a user, still you use the various instance attributes in formatting? I think so, since the |
||
"""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""" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see it repeated in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm reading your comments in the PR thread and seeing that message about CI failures. Once this is in a state ready to merge we can remove this extra branch, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I'll squash the commits, rebase out the development branch, and provide some examples.