Design Architecture for Utility Functions #2
9 changed files with 107 additions and 1 deletions
3
.forge/iss-1ac610bafd4c.md
Normal file
3
.forge/iss-1ac610bafd4c.md
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
# iss-1ac610bafd4c
|
||||||
|
|
||||||
|
Forge 이슈 작업 브랜치 `forge/iss-1ac610bafd4c`.
|
||||||
|
|
@ -1,2 +1,8 @@
|
||||||
# ssot-live
|
# Utility Functions
|
||||||
|
|
||||||
|
This repository contains a collection of utility functions that can be used across various projects.
|
||||||
|
|
||||||
|
## Modules
|
||||||
|
- `math_utils`: Contains basic mathematical operations.
|
||||||
|
- `string_utils`: Provides string manipulation functions.
|
||||||
|
- `date_utils`: Handles date-related utilities.
|
||||||
16
tests/test_date_utils.py
Normal file
16
tests/test_date_utils.py
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
import unittest
|
||||||
|
from utils.date_utils import get_current_date, format_date
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
|
||||||
|
class TestDateUtils(unittest.TestCase):
|
||||||
|
def test_get_current_date(self):
|
||||||
|
self.assertEqual(get_current_date(), datetime.now().strftime('%Y-%m-%d'))
|
||||||
|
|
||||||
|
def test_format_date(self):
|
||||||
|
date = datetime(2023, 1, 1)
|
||||||
|
self.assertEqual(format_date(date, '%d-%m-%Y'), '01-01-2023')
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
||||||
22
tests/test_math_utils.py
Normal file
22
tests/test_math_utils.py
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
import unittest
|
||||||
|
from utils.math_utils import add, subtract, multiply, divide
|
||||||
|
|
||||||
|
|
||||||
|
class TestMathUtils(unittest.TestCase):
|
||||||
|
def test_add(self):
|
||||||
|
self.assertEqual(add(1, 2), 3)
|
||||||
|
|
||||||
|
def test_subtract(self):
|
||||||
|
self.assertEqual(subtract(5, 3), 2)
|
||||||
|
|
||||||
|
def test_multiply(self):
|
||||||
|
self.assertEqual(multiply(3, 4), 12)
|
||||||
|
|
||||||
|
def test_divide(self):
|
||||||
|
self.assertEqual(divide(10, 2), 5)
|
||||||
|
with self.assertRaises(ValueError):
|
||||||
|
divide(10, 0)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
||||||
15
tests/test_string_utils.py
Normal file
15
tests/test_string_utils.py
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
import unittest
|
||||||
|
from utils.string_utils import capitalize_first_letter, reverse_string
|
||||||
|
|
||||||
|
|
||||||
|
class TestStringUtils(unittest.TestCase):
|
||||||
|
def test_capitalize_first_letter(self):
|
||||||
|
self.assertEqual(capitalize_first_letter('hello'), 'Hello')
|
||||||
|
self.assertEqual(capitalize_first_letter(''), '')
|
||||||
|
|
||||||
|
def test_reverse_string(self):
|
||||||
|
self.assertEqual(reverse_string('hello'), 'olleh')
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
||||||
3
utils/__init__.py
Normal file
3
utils/__init__.py
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
# Utility Functions Package
|
||||||
|
|
||||||
|
# This package contains various utility functions for different purposes.
|
||||||
11
utils/date_utils.py
Normal file
11
utils/date_utils.py
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
|
||||||
|
def get_current_date():
|
||||||
|
"""Returns the current date as a string in YYYY-MM-DD format."""
|
||||||
|
return datetime.now().strftime('%Y-%m-%d')
|
||||||
|
|
||||||
|
|
||||||
|
def format_date(date, format='%Y-%m-%d'):
|
||||||
|
"""Formats a given date to the specified format."""
|
||||||
|
return date.strftime(format)
|
||||||
20
utils/math_utils.py
Normal file
20
utils/math_utils.py
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
def add(a, b):
|
||||||
|
"""Returns the sum of two numbers."""
|
||||||
|
return a + b
|
||||||
|
|
||||||
|
|
||||||
|
def subtract(a, b):
|
||||||
|
"""Returns the difference of two numbers."""
|
||||||
|
return a - b
|
||||||
|
|
||||||
|
|
||||||
|
def multiply(a, b):
|
||||||
|
"""Returns the product of two numbers."""
|
||||||
|
return a * b
|
||||||
|
|
||||||
|
|
||||||
|
def divide(a, b):
|
||||||
|
"""Returns the quotient of two numbers. Raises ValueError on division by zero."""
|
||||||
|
if b == 0:
|
||||||
|
raise ValueError('Cannot divide by zero')
|
||||||
|
return a / b
|
||||||
10
utils/string_utils.py
Normal file
10
utils/string_utils.py
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
def capitalize_first_letter(s):
|
||||||
|
"""Capitalizes the first letter of a string."""
|
||||||
|
if not s:
|
||||||
|
return s
|
||||||
|
return s[0].upper() + s[1:]
|
||||||
|
|
||||||
|
|
||||||
|
def reverse_string(s):
|
||||||
|
"""Reverses the given string."""
|
||||||
|
return s[::-1]
|
||||||
Loading…
Add table
Add a link
Reference in a new issue