[feature] Added utils file with example fns#5
Conversation
There was a problem hiding this comment.
Pull request overview
This PR adds a new utility module (pushpit/utils.py) containing common helper functions for string generation, timestamp formatting, temperature conversion, prime number checking, Fibonacci sequence generation, and word counting. The module follows the project's existing conventions for type hints and documentation style.
Changes:
- Added
pushpit/utils.pywith six utility functions covering various common operations - Included demo code in
__main__block for testing the utilities
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| def word_count(text: str) -> dict[str, int]: | ||
| """Count occurrences of each word in a string.""" | ||
| words = text.lower().split() | ||
| counts = {} | ||
| for word in words: | ||
| word = word.strip('.,!?;:') | ||
| counts[word] = counts.get(word, 0) + 1 | ||
| return counts |
There was a problem hiding this comment.
The word_count() function could be more efficient using collections.Counter instead of manually maintaining a dictionary: from collections import Counter; return dict(Counter(word.strip('.,!?;:') for word in text.lower().split() if word.strip('.,!?;:')))
| def generate_random_string(length: int = 10) -> str: | ||
| """Generate a random alphanumeric string.""" | ||
| chars = string.ascii_letters + string.digits | ||
| return ''.join(random.choice(chars) for _ in range(length)) |
There was a problem hiding this comment.
The random.choice() function is not cryptographically secure and should not be used for generating tokens, passwords, or other security-sensitive strings. Consider using secrets.choice() instead for secure random string generation, or add documentation warning that this function should not be used for security purposes.
| def is_prime(n: int) -> bool: | ||
| """Check if a number is prime.""" | ||
| if n < 2: | ||
| return False | ||
| for i in range(2, int(n ** 0.5) + 1): | ||
| if n % i == 0: | ||
| return False | ||
| return True |
There was a problem hiding this comment.
The is_prime() function doesn't validate that the input is actually an integer. If a float is passed (e.g., is_prime(2.5)), the function will execute but may produce unexpected behavior in the range iteration. Consider adding type validation or relying on type hints being enforced by the caller.
| def fibonacci(n: int) -> list[int]: | ||
| """Generate Fibonacci sequence up to n numbers.""" | ||
| if n <= 0: | ||
| return [] | ||
| if n == 1: | ||
| return [0] | ||
|
|
||
| seq = [0, 1] | ||
| while len(seq) < n: | ||
| seq.append(seq[-1] + seq[-2]) | ||
| return seq |
There was a problem hiding this comment.
The fibonacci() function doesn't validate negative inputs. Calling fibonacci(-5) will return an empty list, which may be correct behavior, but the docstring doesn't specify what happens with negative or zero inputs. Consider clarifying the expected behavior in the docstring or adding explicit validation.
| counts = {} | ||
| for word in words: | ||
| word = word.strip('.,!?;:') | ||
| counts[word] = counts.get(word, 0) + 1 |
There was a problem hiding this comment.
The word_count() function may produce empty string keys if a word consists only of punctuation (e.g., "..." or "!!!"). After stripping all punctuation, these become empty strings. Consider filtering out empty strings: if word: counts[word] = counts.get(word, 0) + 1
| counts[word] = counts.get(word, 0) + 1 | |
| if word: | |
| counts[word] = counts.get(word, 0) + 1 |
| def generate_random_string(length: int = 10) -> str: | ||
| """Generate a random alphanumeric string.""" | ||
| chars = string.ascii_letters + string.digits | ||
| return ''.join(random.choice(chars) for _ in range(length)) |
There was a problem hiding this comment.
The generate_random_string() function uses a list comprehension with random.choice() in a loop, which is inefficient for larger strings. Consider using random.choices() (plural) which can generate multiple random selections in a single call: return ''.join(random.choices(chars, k=length))
| return ''.join(random.choice(chars) for _ in range(length)) | |
| return ''.join(random.choices(chars, k=length)) |
Summary
Add utility module with common helper functions for the project.
Changes
New File:
pushpit/utils.pyAdded utility functions including:
generate_random_string()- Generate random alphanumeric stringsget_current_timestamp()- Get formatted current timestampcelsius_to_fahrenheit()/fahrenheit_to_celsius()- Temperature conversionsis_prime()- Check if a number is primefibonacci()- Generate Fibonacci sequenceword_count()- Count word occurrences in textTesting
Run the module directly to see a demo: