Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pydocstyle + Merge pull request #550 from iKostanOrg/master #551

Merged
merged 51 commits into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
bb73951
Merge pull request #550 from iKostanOrg/master
ikostan Dec 5, 2024
d9ec5de
Create pydocstyle_kyu4.yml
ikostan Dec 5, 2024
aa72e90
__init__ docstring
ikostan Dec 6, 2024
0ec0eee
Update next_smaller.py
ikostan Dec 6, 2024
61946fe
Update test_next_smaller.py
ikostan Dec 6, 2024
9c1d133
Update format_duration.py
ikostan Dec 6, 2024
5f867a0
Update test_format_duration.py
ikostan Dec 6, 2024
8065547
Update solution.py
ikostan Dec 6, 2024
04e42e7
Update test_top_3_words.py
ikostan Dec 6, 2024
2e6dde6
Update next_bigger.py
ikostan Dec 6, 2024
df80c28
Update sudoku.py
ikostan Dec 6, 2024
bbda025
Update test_sudoku.py
ikostan Dec 6, 2024
59302cd
Update test_battle.py
ikostan Dec 6, 2024
e6033c0
Update sum_of_intervals.py
ikostan Dec 6, 2024
148da83
Update test_sum_of_intervals.py
ikostan Dec 6, 2024
0f932e0
Update sum_for_list.py
ikostan Dec 6, 2024
a4bc2c3
Update test_battle.py
ikostan Dec 6, 2024
90d3293
Update test_valid_solution.py
ikostan Dec 7, 2024
feb9d58
Update valid_solution.py
ikostan Dec 7, 2024
01775ee
Update solution.py
ikostan Dec 7, 2024
76d6d86
Update test_solution.py
ikostan Dec 7, 2024
92ef00e
Update solution.py
ikostan Dec 7, 2024
ba92654
Update snail_sort.py
ikostan Dec 7, 2024
6f66d53
Update test_snail.py
ikostan Dec 7, 2024
6c815e1
Update solution.py
ikostan Dec 7, 2024
cd76383
Update solution.py
ikostan Dec 7, 2024
0679925
Update permutations.py
ikostan Dec 7, 2024
c8e5477
Update test_permutations.py
ikostan Dec 7, 2024
2737838
Update solution.py
ikostan Dec 7, 2024
f90c9b2
Update solution.py
ikostan Dec 7, 2024
b367e2c
Update solution.py
ikostan Dec 7, 2024
dd4c341
Update sudoku.py
ikostan Dec 7, 2024
519769a
Update test_mix.py
ikostan Dec 7, 2024
d39ff04
Update sudoku.py
ikostan Dec 7, 2024
224ba11
Update test_solution.py
ikostan Dec 7, 2024
5df87a0
Update test_solution.py
ikostan Dec 7, 2024
12a2a46
Update test_next_bigger.py
ikostan Dec 7, 2024
b6f7688
Update next_bigger.py
ikostan Dec 7, 2024
4580080
Update next_bigger.py
ikostan Dec 8, 2024
56f7755
Update test_warrior.py
ikostan Dec 8, 2024
25323b5
Update warrior.py
ikostan Dec 8, 2024
f38677d
Update warrior.py
ikostan Dec 8, 2024
4770714
Update warrior.py
ikostan Dec 8, 2024
3735998
Update warrior.py
ikostan Dec 8, 2024
97c69d8
Update test_sum_for_list.py
ikostan Dec 8, 2024
9a5a836
Update test_sum_for_list.py
ikostan Dec 8, 2024
0d2b08c
Update format_duration.py
ikostan Dec 8, 2024
25af3b2
Update valid_solution.py
ikostan Dec 8, 2024
e53e400
Update valid_solution.py
ikostan Dec 8, 2024
5f99616
Update sum_of_intervals.py
ikostan Dec 8, 2024
fc29afe
Update sum_of_intervals.py
ikostan Dec 8, 2024
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
46 changes: 46 additions & 0 deletions .github/workflows/pydocstyle_kyu4.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
name: pydocstyle for kyu4

on: # yamllint disable-line rule:truthy
push:
branches:
- 'kyu4'

permissions:
contents: read
pull-requests: read

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.x"]
steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
# This is the version of the action for setting up Python,
# not the Python version.
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
# You can test your matrix by printing the current Python version
- name: Display Python version
run: python -c "import sys; print(sys.version)"
- name: Install dependencies
run: |
python -m pip install --upgrade pip setuptools wheel
pip install -r requirements.txt
pip install pydocstyle
pip install types-requests
- name: Check to make sure that the module is in your Python path
run: |
echo $PYTHONPATH
- name: Check pydocstyle version
run: |
pydocstyle --version
- name: Doc style checking with pydocstyle
# Pydocstyle testing (Guide)
# https://www.pydocstyle.org/en/stable/usage.html#cli-usage
run: |
pydocstyle --verbose --explain --count kyu_4
1 change: 1 addition & 0 deletions kyu_4/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Codewars kyu_4 package."""
1 change: 1 addition & 0 deletions kyu_4/human_readable_duration_format/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Human readable duration format package."""
143 changes: 72 additions & 71 deletions kyu_4/human_readable_duration_format/format_duration.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""
Format duration.

A function which formats a duration, given as a
number of seconds, in a human-friendly way.
Created by Egor Kostan.
Expand All @@ -8,6 +10,8 @@

def format_duration(seconds: int) -> str:
"""
format_duration function.

A function which formats a duration, given as a
number of seconds, in a human-friendly way.

Expand Down Expand Up @@ -67,34 +71,36 @@ def format_duration(seconds: int) -> str:
return result


def format_days(days: int, day: str, result: str) -> str:
def format_days(days: int,
Copy link
Contributor

Choose a reason for hiding this comment

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

issue (complexity): Consider consolidating the format_days, format_hours, format_minutes and format_seconds functions into a single helper function.

The formatting functions can be consolidated into a single helper function to reduce duplication while maintaining all functionality. Here's how:

def format_component(value: int,
                    component: str,
                    result: str,
                    is_final: bool = False,
                    has_seconds: bool = False) -> str:
    """Format a time component with appropriate conjunction.

    Args:
        value: The time value
        component: The formatted string (e.g. "1 day")
        result: The accumulated result string
        is_final: Whether this is the final component
        has_seconds: Whether seconds exist (for minute special case)
    """
    if value > 0:
        if not result:
            return component
        if is_final or (has_seconds and component.startswith('1 minute')):
            return f'{result} and {component}'
        return f'{result}, {component}'
    return result

# Usage example:
result = format_component(days, day, result)
result = format_component(hours, hour, result)
result = format_component(minutes, minute, result, seconds == 0, seconds > 0)
result = format_component(seconds, second, result, True)

This consolidates the duplicate logic while handling the special cases for minutes and final components. The code becomes more maintainable as formatting changes only need to be made in one place.

day: str,
result: str) -> str:
"""
Format days for the final string
:param days:
:param day:
:param result:
:return:
Format days for the final string.

:param days: int
:param day: str
:param result: str
:return: str
"""
if days > 0 and result != '':
result += f', {day}'
elif days > 0:
result += f'{day}'
if days > 0:
result += f', {day}' if result else f'{day}'

return result


def format_hours(hours: int, hour: str, result: str) -> str:
def format_hours(hours: int,
hour: str,
result: str) -> str:
"""
Format hours for the final string
:param hours:
:param hour:
:param result:
:return:
Format hours for the final string.

:param hours: int
:param hour: str
:param result: str
:return: str
"""
if hours > 0 and result != '':
result += f', {hour}'
elif hours > 0:
result += f'{hour}'
if hours > 0:
result += f', {hour}' if result else f'{hour}'

return result

Expand All @@ -104,12 +110,13 @@ def format_minutes(minutes: int,
minute: str,
result: str) -> str:
"""
Format minutes for the final string
:param minutes:
:param seconds:
:param minute:
:param result:
:return:
Format minutes for the final string.

:param minutes: int
:param seconds: int
:param minute: str
:param result: str
:return: str
"""
if minutes > 0 and result != '' and seconds == 0:
result += f' and {minute}'
Expand All @@ -123,26 +130,27 @@ def format_minutes(minutes: int,

def format_seconds(seconds: int, second: str, result: str) -> str:
"""
Format seconds for the final string
:param seconds:
:param second:
:param result:
:return:
Format seconds for the final string.

:param seconds: int
:param second: str
:param result: str
:return: str
"""
if seconds > 0 and result != '':
result += f' and {second}'
elif seconds > 0:
result += f'{second}'
if seconds > 0:
result += f' and {second}' if result else f'{second}'

return result


def get_string(number: int, string: str) -> str:
def get_string(number: int,
string: str) -> str:
"""
Concatenate string result
:param number:
:param string:
:return:
Concatenate string result.

:param number: int
:param string: str
:return: str
"""
result: str = ''
if number == 1:
Expand All @@ -155,59 +163,52 @@ def get_string(number: int, string: str) -> str:

def calc_seconds(seconds: int) -> int:
"""
Calculate seconds
:param seconds:
:return:
"""
if seconds < 60:
return seconds
Calculate seconds.

return seconds % 60
:param seconds: int
:return: int
"""
return seconds if seconds < 60 else seconds % 60


def calc_minutes(seconds: int) -> int:
"""
calculate minutes
:param seconds:
:return:
Calculate minutes.

:param seconds: int
:return: int
"""
minutes = seconds // 60
if minutes < 60:
return minutes

return minutes % 60
return minutes if minutes < 60 else minutes % 60


def calc_hours(seconds: int) -> int:
"""
Calculate hours
:param seconds:
:return:
Calculate hours.

:param seconds: int
:return: int
"""
hours = seconds // (60 * 60)
if hours < 24:
return hours

return hours % 24
return hours if hours < 24 else hours % 24


def calc_days(seconds: int) -> int:
"""
Calculate days
:param seconds:
:return:
Calculate days.

:param seconds: int
:return: int
"""
days = seconds // (60 * 60 * 24)
if days < 365:
return days

return days % 365
return days if days < 365 else days % 365


def calc_years(seconds: int) -> int:
"""
Calculate years
:param seconds:
:return:
Calculate years.

:param seconds: int
:return: int
"""
return seconds // (60 * 60 * 24 * 365)
9 changes: 5 additions & 4 deletions kyu_4/human_readable_duration_format/test_format_duration.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""
Test for 'Human readable duration format'
Test for 'Human readable duration format'.

Created by Egor Kostan.
GitHub: https://github.com/ikostan
"""
Expand Down Expand Up @@ -29,12 +30,12 @@
name='Source/Kata')
# pylint: enable-msg=R0801
class FormatDurationTestCase(unittest.TestCase):
"""
Testing format_duration
"""
"""Testing format_duration."""

def test_format_duration(self):
"""
Test format_duration function.

Test a function which formats a duration,
given as a number of seconds, in a human-friendly way.

Expand Down
1 change: 1 addition & 0 deletions kyu_4/most_frequently_used_words/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Most frequently used words in a text package."""
5 changes: 4 additions & 1 deletion kyu_4/most_frequently_used_words/solution.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
"""
Most frequently used words in a text
Most frequently used words in a text.

Created by Egor Kostan.
GitHub: https://github.com/ikostan
"""


def top_3_words(text: str) -> list:
"""
Top 3 words function.

Given a string of text (possibly with punctuation and line-breaks),
returns an array of the top-3 most occurring words, in descending
order of the number of occurrences.
Expand Down
11 changes: 6 additions & 5 deletions kyu_4/most_frequently_used_words/test_top_3_words.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""
Test for 'Most frequently used words in a text'
Test for 'Most frequently used words in a text'.

Created by Egor Kostan.
GitHub: https://github.com/ikostan
"""
Expand Down Expand Up @@ -28,13 +29,13 @@
url='https://www.codewars.com/kata/51e056fe544cf36c410000fb',
name='Source/Kata')
class Top3WordsTestCase(unittest.TestCase):
"""
Testing top_3_words
"""
"""Testing top_3_words."""

def test_top_3_words(self):
"""
Test top_3_words function
Test top_3_words function with various test data.

:return:
"""
# pylint: disable-msg=R0801
allure.dynamic.title("Testing top_3_words function")
Expand Down
1 change: 1 addition & 0 deletions kyu_4/next_bigger_number_with_the_same_digits/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Next bigger number with the same digits package."""
14 changes: 6 additions & 8 deletions kyu_4/next_bigger_number_with_the_same_digits/next_bigger.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""
Solution for -> Next bigger number with the same digits
Solution for -> Next bigger number with the same digits.

Created by Egor Kostan.
GitHub: https://github.com/ikostan
"""
Expand Down Expand Up @@ -62,11 +63,8 @@ def next_greater_digit_index(digits: list, i: int) -> int:
return i

for index, digit in enumerate(digits[i:]):
if digits[i - 1] < digit:
if current == '':
current = digit
j = i + index
elif current > digit:
current = digit
j = i + index
if digits[i - 1] < digit and (current == '' or current > digit):
current = digit
j = i + index

return j
1 change: 1 addition & 0 deletions kyu_4/next_smaller_number_with_the_same_digits/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Next smaller number with the same digits package."""
Loading
Loading