Skip to content

Comments

[feature] Added utils file with example fns#5

Open
pushpitkamboj wants to merge 1 commit intomasterfrom
feature/demo_code
Open

[feature] Added utils file with example fns#5
pushpitkamboj wants to merge 1 commit intomasterfrom
feature/demo_code

Conversation

@pushpitkamboj
Copy link
Collaborator

Summary

Add utility module with common helper functions for the project.

Changes

New File: pushpit/utils.py

Added utility functions including:

  • generate_random_string() - Generate random alphanumeric strings
  • get_current_timestamp() - Get formatted current timestamp
  • celsius_to_fahrenheit() / fahrenheit_to_celsius() - Temperature conversions
  • is_prime() - Check if a number is prime
  • fibonacci() - Generate Fibonacci sequence
  • word_count() - Count word occurrences in text

Testing

Run the module directly to see a demo:

python pushpit/utils.py

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

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.py with 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.

Comment on lines +52 to +59
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
Copy link

Copilot AI Feb 16, 2026

Choose a reason for hiding this comment

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

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('.,!?;:')))

Copilot uses AI. Check for mistakes.
Comment on lines +8 to +11
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))
Copy link

Copilot AI Feb 16, 2026

Choose a reason for hiding this comment

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

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.

Copilot uses AI. Check for mistakes.
Comment on lines +29 to +36
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
Copy link

Copilot AI Feb 16, 2026

Choose a reason for hiding this comment

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

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.

Copilot uses AI. Check for mistakes.
Comment on lines +39 to +49
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
Copy link

Copilot AI Feb 16, 2026

Choose a reason for hiding this comment

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

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.

Copilot uses AI. Check for mistakes.
counts = {}
for word in words:
word = word.strip('.,!?;:')
counts[word] = counts.get(word, 0) + 1
Copy link

Copilot AI Feb 16, 2026

Choose a reason for hiding this comment

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

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

Suggested change
counts[word] = counts.get(word, 0) + 1
if word:
counts[word] = counts.get(word, 0) + 1

Copilot uses AI. Check for mistakes.
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))
Copy link

Copilot AI Feb 16, 2026

Choose a reason for hiding this comment

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

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))

Suggested change
return ''.join(random.choice(chars) for _ in range(length))
return ''.join(random.choices(chars, k=length))

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants