Add utils (2)#47
Conversation
Test - A tiny utility library
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
WalkthroughIntroduces a new Python module python/simple_utils.py containing three independent utility functions: reverse_string, count_words, and celsius_to_fahrenheit. Each function includes a brief docstring and returns a computed value without additional error handling or side effects. Changes
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3 minutes Poem
Tip 🔌 Remote MCP (Model Context Protocol) integration is now available!Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats. ✨ Finishing Touches
🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (3)
python/simple_utils.py (3)
7-9: Clarify “word” semantics;split()counts tokens with punctuationIf “words” should exclude punctuation (e.g., "hello," -> 1), consider a regex-based approach. If current whitespace tokenization is intended, feel free to ignore.
Apply this diff if you want regex-based word counting:
def count_words(sentence): - """Return the number of whitespace-separated words in the sentence.""" - return len(sentence.split()) + """Return the number of words in the sentence (alphanumeric word chars).""" + return len(re.findall(r'\b\w+\b', sentence))And add this import at the top of the module:
import re
1-14: Nit: add a module docstring, explicit exports, and basic type hintsThis improves readability and helps type checkers without changing behavior.
Apply this diff:
-# simple_utils.py - A tiny utility library +"""A tiny utility library with simple string, text, and temperature helpers.""" + +__all__ = ["reverse_string", "count_words", "celsius_to_fahrenheit"] -def reverse_string(text): +def reverse_string(text: str) -> str: """Return the reverse of the input string.""" return text[::-1] - - -def count_words(sentence): +def count_words(sentence: str) -> int: """Return the number of whitespace-separated words in the sentence.""" return len(sentence.split()) - - -def celsius_to_fahrenheit(celsius): +def celsius_to_fahrenheit(celsius: float) -> float: """Convert Celsius to Fahrenheit.""" return (celsius * 9 / 5) + 32
1-14: Optional: add a couple of quick unit tests to lock behaviorHelpful both for edge cases and to document intended semantics (especially for
count_words).Here’s a minimal pytest-style test file you can drop into
tests/test_simple_utils.py:import pytest from python.simple_utils import reverse_string, count_words, celsius_to_fahrenheit def test_reverse_string_basic(): assert reverse_string("abc") == "cba" assert reverse_string("") == "" assert reverse_string("racecar") == "racecar" def test_count_words_whitespace_tokens(): assert count_words("one two three\tfour\nfive") == 5 assert count_words("") == 0 assert count_words(" hello ") == 1 def test_celsius_to_fahrenheit_precision(): assert celsius_to_fahrenheit(0.0) == 32.0 assert celsius_to_fahrenheit(100.0) == 212.0 assert celsius_to_fahrenheit(-40.0) == -40.0If you prefer
unittest, I can provide that instead. Want me to open a follow-up PR with tests?
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled by default for public repositories
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (1)
python/simple_utils.py(1 hunks)
🔇 Additional comments (2)
python/simple_utils.py (2)
2-4:reverse_stringimplementation is clean and idiomatic — LGTMSlicing with
[::-1]is the right choice here.
12-14: Celsius→Fahrenheit conversion looks correct — LGTMFormula and operator precedence are correct; returns a float as expected.
|
|
|
Tip For best results, initiate chat on the files or code changes. @Ruffelsforralph Thank you for sharing the review summary! I can see this is CodeRabbit's automated walkthrough of the Is there anything specific you'd like me to help you with regarding this PR or the utility functions? For example:
Let me know how I can assist you! 🐰 |
Adding second test to be able to highlight commit suggestions
Summary by CodeRabbit
These capabilities are lightweight and consistent across the app, improving basic text handling and temperature presentation in areas that surface these interactions.