Skip to content

Refactor: simplify scientific() and extract _SUPERSCRIPT_MAP constant#313

Merged
hugovk merged 5 commits into
python-humanize:mainfrom
maayanmatsliah-tech:main
May 14, 2026
Merged

Refactor: simplify scientific() and extract _SUPERSCRIPT_MAP constant#313
hugovk merged 5 commits into
python-humanize:mainfrom
maayanmatsliah-tech:main

Conversation

@maayanmatsliah-tech
Copy link
Copy Markdown
Contributor

Cool repo! I made a small cleanup to the scientific() function in number.py.

I extracted the exponents dict as a module-level constant so it's not recreated on every function call. I also simplified the character mapping loop into a generator expression.

I'm happy to make any changes if necessary. Hope this helps!

@hugovk hugovk added the changelog: Changed For changes in existing functionality label May 14, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented May 14, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.55%. Comparing base (55dc47c) to head (13e54db).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #313      +/-   ##
==========================================
- Coverage   99.56%   99.55%   -0.01%     
==========================================
  Files          12       12              
  Lines         911      907       -4     
==========================================
- Hits          907      903       -4     
  Misses          4        4              
Flag Coverage Δ
macos-latest 97.57% <100.00%> (-0.02%) ⬇️
ubuntu-latest 97.57% <100.00%> (-0.02%) ⬇️
windows-latest 95.70% <100.00%> (-0.02%) ⬇️

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

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Member

@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!

Comment thread src/humanize/number.py
@hugovk
Copy link
Copy Markdown
Member

hugovk commented May 14, 2026

I've just added benchmarking to the CI, so updated this PR from main to see what it looks like :)

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented May 14, 2026

Merging this PR will not alter performance

✅ 15 untouched benchmarks


Comparing maayanmatsliah-tech:main (13e54db) with main (55dc47c)

Open in CodSpeed

Comment thread src/humanize/number.py Outdated
@maayanmatsliah-tech
Copy link
Copy Markdown
Contributor Author

Thanks for the feedback! I moved _SUPERSCRIPT_MAP after the TYPE_CHECKING block and switched to a list comprehension. Let me know if I should make any other changes!

@hugovk hugovk changed the title refactor: extract _SUPERSCRIPT_MAP constant and simplify scientific() Refactor: simplify scientific() and extract _SUPERSCRIPT_MAP constant May 14, 2026
@hugovk
Copy link
Copy Markdown
Member

hugovk commented May 14, 2026

These are already small numbers, but nice little performance improvement now:

image image

https://codspeed.io/python-humanize/humanize/benchmarks/tests/test_benchmarks.py::test_scientific?runnerMode=Simulation&branchId=6a05aa6cad9a6239bf2b0269

Thanks!

@hugovk hugovk merged commit e7c04f4 into python-humanize:main May 14, 2026
46 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog: Changed For changes in existing functionality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants