Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions pushpit/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
"""Utility functions for common operations."""

import random
import string
from datetime import datetime


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))
Comment on lines +8 to +11
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.
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.


def get_current_timestamp() -> str:
"""Get the current timestamp as a formatted string."""
return datetime.now().strftime("%Y-%m-%d %H:%M:%S")


def celsius_to_fahrenheit(celsius: float) -> float:
"""Convert Celsius to Fahrenheit."""
return (celsius * 9/5) + 32


def fahrenheit_to_celsius(fahrenheit: float) -> float:
"""Convert Fahrenheit to Celsius."""
return (fahrenheit - 32) * 5/9


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
Comment on lines +29 to +36
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.


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
Comment on lines +39 to +49
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.


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
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.
return counts
Comment on lines +52 to +59
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.


if __name__ == "__main__":
print("=== Utils Demo ===\n")

print(f"Random string: {generate_random_string(8)}")
print(f"Timestamp: {get_current_timestamp()}")
print(f"25°C = {celsius_to_fahrenheit(25):.1f}°F")
print(f"77°F = {fahrenheit_to_celsius(77):.1f}°C")
print(f"Is 17 prime? {is_prime(17)}")
print(f"Fibonacci(10): {fibonacci(10)}")
print(f"Word count: {word_count('hello world hello python')}")