Skip to content
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

Open
wants to merge 8 commits into
base: 2023-01-26-no-more-py2
Choose a base branch
from

Conversation

jonathaneunice
Copy link

  • 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

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.:

size = bitmath.MiB(2.847598437)
print('size: {:.1f}'.format(size))

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.

size = bitmath.MiB(2.847598437)
print(f'size: {size:.1f}')

- 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
@jonathaneunice
Copy link
Author

The CI failures appear to be related to outdated configuration of the requirements, which I did not change, not test breakage.

@jonathaneunice
Copy link
Author

Using modern python3 versions (the 3.3 branch ended its official supported life on September 29, 2017), the tests pass.

@tbielawa
Copy link
Owner

tbielawa commented Jul 3, 2018

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
Copy link
Owner

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):
Copy link
Owner

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 😄

@tbielawa
Copy link
Owner

tbielawa commented Jul 3, 2018

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
Copy link
Owner

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?

Copy link
Author

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.

@tbielawa
Copy link
Owner

@jonathaneunice this is a nice PR, do you still feel like trying to get this cleaned up and able to merge?

@florisla
Copy link

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}"))
1.000 Byte
1.001  KiB
1.002  MiB
1.003  GiB
1.004  TiB

@tbielawa tbielawa changed the base branch from master to 2023-01-26-no-more-py2 February 5, 2023 23:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants