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

Adds the ability for intword to use user provided mapping. #187

Closed
wants to merge 2 commits into from

Conversation

rjgpacheco
Copy link
Contributor

Adds the ability for intword to use user provided mapping.

Also fixes a bug where not conversions would be made if only one mapping is provided.

I was looking for the ability to humanize into "thousands" in addition to "millions", etc. and noticed it wasn't possible.

This StackOverflow comment lead me to expand intword.

I also saw issues like #160, which lead me to believe I wouldn't be the only one to benefit from this.

Changes proposed in this pull request:

  • Changes powers and human_powers to be defined from a list of pairs
  • Fixes an edge case where conversions wouldn't happen if only one (power, human_power) pair is passed to intword
  • Replaces powers and human_powers with l_powers and l_human_powers to avoid scope conflicts
  • intword can receive a list of (power, human_power) pairs when called, to enable custom configurations.

If no custom configurations are passed, then it defaults to the existing ones.

For example:

import humanize

conversions = [
    (3, "K"),
    (6, "M"),
    (9, "B"),
    (12, "T"),
]


for power in range(0, 10):
	x = 10**power
	print(f"{x:>10} -> {humanize.intword(x, '%.0f', conversions=conversions):>5}")

outputs

         1 ->     1
        10 ->    10
       100 ->   100
      1000 ->   1 K
     10000 ->  10 K
    100000 -> 100 K
   1000000 ->   1 M
  10000000 ->  10 M
 100000000 -> 100 M
1000000000 ->   1 B

This lets users expand conversions as needed (eg "thousand") or use custom abbreviations (eg, "k", "MM").

… a bug where not conversions would be made if only one mapping is provided.
@codecov-io
Copy link

codecov-io commented Jan 31, 2021

Codecov Report

Merging #187 (fb804b1) into master (a0f03c1) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master      #187   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            9         9           
  Lines          586       592    +6     
=========================================
+ Hits           586       592    +6     
Flag Coverage Δ
GHA_Ubuntu 100.00% <100.00%> (ø)
GHA_Windows 100.00% <100.00%> (ø)
GHA_macOS 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/humanize/number.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a0f03c1...fb804b1. Read the comment docs.

Copy link
Collaborator

@hugovk hugovk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!

Please could you include tests, for both the bug fix and the new feature?

The bug test should fail on master and pass with the fix. If you like, it could be another PR.

N_("nonillion"),
N_("decillion"),
N_("googol"),
default_conversions = [
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is default_conversions an internal thing? Should we name it _ default_conversions?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

default_conversions is now where powers and human_powers are defined. If powers and human_powers were also internal, then maybe default_conversions should also be internal?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The existing powers and human_powers are already "public", so we shouldn't change those to _powers and _human_powers in case people are using them.

But do you think people would or should be able to change the default conversions?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing the defaults might not make sense, but people may want to expand them, eg.

from humanize.numbers import intword, default_conversions

intword(1023, conversions=default_conversions.append((3,"Thousands"))

But I'm not sure how useful that would be in practice.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, let's keep it as default_conversions

@@ -172,16 +178,27 @@ def intword(value, format="%.1f"):
except (TypeError, ValueError):
return value

if value < powers[0]:
if conversions is None:
l_powers, l_human_powers = powers, human_powers
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does the l_ prefix mean?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

l_ as in local.

I might have done it the other way around, by renaming the default ones, but tests were specifically looking for powers and human_powers in the body of number. I'll change this and adjust the tests as well.


```
Args:
value (int, float, str): Integer to convert.
format (str): To change the number of decimal or general format of the number
portion.
conversions [(int, str)]: Power to suffix conversion
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
conversions [(int, str)]: Power to suffix conversion
conversions [(int, str)]: Power to suffix conversion.

@rjgpacheco
Copy link
Contributor Author

@hugovk thank you for the comments and suggestions.

I agree with them, and I'll be changing the PR accordingly.

Depending on my personal availability it might take a few days.

@hugovk hugovk added the enhancement New feature or request label Apr 30, 2021
@hugovk
Copy link
Collaborator

hugovk commented May 3, 2022

🚀 Development has moved to https://github.com/python-humanize/humanize 🚀

Please open new PRs at https://github.com/python-humanize/humanize/pulls

@hugovk hugovk closed this May 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants