-
Notifications
You must be signed in to change notification settings - Fork 39
Fix abbreviation formatting in visualizer #989
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
Conversation
Replaces the previous method of removing trailing '.0' with a more robust approach using rstrip to remove trailing zeros and periods.
Improves the formatting of abbreviated numbers by ensuring suffixes are preserved after trimming trailing zeros and decimal points.
| return str(n) | ||
| return s.replace(".0", "") | ||
| suffix = s[-1] | ||
| return s[:-1].rstrip("0").rstrip(".") + suffix |
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.
@OpenHands /codereview-roasted
Read the pr description, understand the problem, and see if we can use some existing library / simplify the code to make it more robust
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 on it! xingyaoww can track my progress at all-hands.dev
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.
🎯 Code Review Complete - PR #989 Updated
I've reviewed and improved the PR fixing the abbreviation formatting bug in visualizer.py.
🔴 Taste Rating: Good → Better
The PR correctly fixed the bug, but I simplified it further using better design.
📊 Problem Analysis
Original Bug:
s.replace(".0", "") # Incorrectly: "89.08K" → "898K" ❌The .replace(".0", "") removed ".0" from anywhere in the string, breaking numbers like 89.08K.
PR's Fix:
suffix = s[-1]
return s[:-1].rstrip("0").rstrip(".") + suffix # Works ✓ but complexThis worked by: format → extract suffix → strip → re-add suffix.
✨ My Improvement (Applied)
Simplified the data flow - extract value and suffix FIRST, then format:
if n >= 1_000_000_000:
val, suffix = n / 1_000_000_000, "B"
elif n >= 1_000_000:
val, suffix = n / 1_000_000, "M"
elif n >= 1_000:
val, suffix = n / 1_000, "K"
else:
return str(n)
return f"{val:.2f}".rstrip("0").rstrip(".") + suffixWhy this is better ("Good Taste" principles):
- ✅ Clearer data structure - Value and suffix are determined separately
- ✅ Single formatting operation - No format-extract-reformat dance
- ✅ More maintainable - Suffix logic is centralized in tuple assignments
- ✅ Same behavior - Produces identical correct results
🧪 Testing
Added comprehensive edge case tests covering:
- Below threshold: 999 → "999"
- Exact boundaries: 1000 → "1K", 1000000 → "1M", 1000000000 → "1B"
- Trailing zeros: 89000 → "89K" (not "89.00K")
- The bug case: 89080 → "89.08K" (not "898K") ✓
All 16 tests pass, including the new comprehensive abbreviation test.
📚 Library Investigation
Checked if we could use existing libraries like humanize:
- ❌ Not in current dependencies
- ❌ Would add dependency for a simple 11-line function
- ✅ Our implementation is clean, tested, and sufficient
✅ Changes Committed & Pushed
- Simplified the
abbr()function logic invisualizer.py - Updated existing test to expect correct output ("1.5K" not "1.50K")
- Added comprehensive edge case test (
test_metrics_abbreviation_formatting) - All pre-commit hooks pass (ruff, pyright, pycodestyle)
- Changes pushed to branch
stripupdating PR Fix abbreviation formatting in visualizer #989
Verdict: The PR is now simpler, more maintainable, and thoroughly tested. ✨
Refactor the abbr() function to use a cleaner approach: - Extract value and suffix determination into tuple assignment - Apply formatting and cleanup in a single return statement - More readable and maintainable code with same functionality This resolves the bug where '89.08K' was incorrectly displayed as '898K' by properly handling the suffix after stripping trailing zeros. Also add comprehensive test coverage for edge cases including: - Numbers below 1K (no abbreviation) - Exact boundaries (1K, 1M, 1B) - Numbers with trailing zeros that should be removed - The specific bug case (89.08K) Co-authored-by: openhands <[email protected]>
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.
@SmartManoj lmk if this changes looks good to you. if so we can merge this
|
merging this now as we are preparing for v1 release. LMK if you have concerns on the changes here @SmartManoj |
Replaces the previous method of removing trailing '.0' with a more robust approach using rstrip to remove trailing zeros and periods.
input 88k to 898k
Output: