-
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?
Added __format__ method #76
Conversation
- 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
The CI failures appear to be related to outdated configuration of the requirements, which I did not change, not test breakage. |
- and added modern py36 target
- to trigger brnach build
Using modern python3 versions (the 3.3 branch ended its official supported life on September 29, 2017), the tests pass. |
Thanks for the PR! I'll try to take a gander at this today. I'm excited about this one. |
# 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 comment
The reason will be displayed to describe this comment to others. Learn more.
I see it repeated in test_inline_format_override
as well. Do you think this duplication could be cleaned up by adding this to a tearDown() method? Seems like a cheap and safe way to ensure we don't forget to do that again in later tests.
@@ -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 comment
The 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 __format__
method ultimately calls out to self.format
, but I'd like some more examples and such 😄
I'd like this squashed down into a shorter number of commits if you can please. One or a couple, whichever makes more sense to you. |
@@ -4,13 +4,18 @@ notifications: | |||
branches: | |||
only: | |||
- master | |||
- format-method |
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.
@jonathaneunice this is a nice PR, do you still feel like trying to get this cleaned up and able to merge? |
It would be great to include this in bitmath. Especially with Python 3.6's f-strings. byte_counts = [1, 1025, 1026*1024, 1027*1024*1024, 1028*1024*1024*1024]
for byte_count in byte_counts:
size = bitmath.Byte(byte_count).best_prefix()
print(f"{size.value:8.3f} {size.unit:>4}"))
|
Short description: Adds a format method to enable in-line string interpolation
If you have a bitmath object (of whatever unit), it's handy to be able to interpolate that value directly in a format string. E.g.:
A
__format__
method makes this possible. It's even neater in Python 3.6 and following, with its ability to automatically interpolate variables into strings.