Skip to content

Cleanup pluggable-libs & fix CI file output (#433, #447) #448

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
62 changes: 32 additions & 30 deletions .github/workflows/lint_pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ jobs:
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0 # To get all history for git diff commands
fetch-depth: 0 # To get all history for git diff commands

- name: Get changed Python files
id: changed-files
run: |
Expand All @@ -31,7 +31,7 @@ jobs:
CHANGED_FILES="$CHANGED_FILES $SETUP_PY_CHANGED"
fi
fi

# Check if any Python files were changed and set the output accordingly
if [ -z "$CHANGED_FILES" ]; then
echo "No Python files changed"
Expand All @@ -40,9 +40,11 @@ jobs:
else
echo "Changed Python files: $CHANGED_FILES"
echo "has_python_changes=true" >> $GITHUB_OUTPUT
echo "files=$CHANGED_FILES" >> $GITHUB_OUTPUT
# Use proper delimiter formatting for GitHub Actions
FILES_SINGLE_LINE=$(echo "$CHANGED_FILES" | tr '\n' ' ' | sed 's/[[:space:]]\+/ /g' | sed 's/^[[:space:]]*//' | sed 's/[[:space:]]*$//')
echo "files=$FILES_SINGLE_LINE" >> $GITHUB_OUTPUT
fi

- name: PR information
if: ${{ github.event_name == 'pull_request' }}
run: |
Expand All @@ -68,35 +70,35 @@ jobs:
echo "No Python files were changed. Skipping linting."
exit 0
fi

- uses: actions/checkout@v3
with:
fetch-depth: 0

- uses: actions/setup-python@v4
with:
python-version: 3.12

- uses: actions/cache@v3
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('requirements-dev.txt') }}
restore-keys: |
${{ runner.os }}-pip-

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements-dev.txt

# Flake8 linting
- name: Lint with flake8
if: ${{ matrix.tool == 'flake8' }}
id: flake8
run: |
echo "Linting files: ${{ needs.check_changes.outputs.files }}"
flake8 ${{ needs.check_changes.outputs.files }} --count --show-source --statistics

# Format checking with isort and black
- name: Format check
if: ${{ matrix.tool == 'format' }}
Expand All @@ -106,27 +108,27 @@ jobs:
isort --profile black --check ${{ needs.check_changes.outputs.files }}
echo "Checking format with black for: ${{ needs.check_changes.outputs.files }}"
black --check ${{ needs.check_changes.outputs.files }}

# Type checking with mypy
- name: Type check with mypy
if: ${{ matrix.tool == 'mypy' }}
id: mypy
run: |
echo "Type checking: ${{ needs.check_changes.outputs.files }}"
mypy --ignore-missing-imports ${{ needs.check_changes.outputs.files }}

# Run tests with pytest
- name: Run tests with pytest
if: ${{ matrix.tool == 'pytest' }}
id: pytest
run: |
echo "Running pytest discovery..."
python -m pytest --collect-only -v

# First run any test files that correspond to changed files
echo "Running tests for changed files..."
changed_files="${{ needs.check_changes.outputs.files }}"

# Extract module paths from changed files
modules=()
for file in $changed_files; do
Expand All @@ -137,13 +139,13 @@ jobs:
modules+=("$module_path")
fi
done

# Run tests for each module
for module in "${modules[@]}"; do
echo "Testing module: $module"
python -m pytest -xvs tests/ -k "$module" || true
done

# Then run doctests on the changed files
echo "Running doctests for changed files..."
for file in $changed_files; do
Expand All @@ -152,26 +154,26 @@ jobs:
python -m pytest --doctest-modules -v $file || true
fi
done

# Check Python version compatibility
- name: Check Python version compatibility
if: ${{ matrix.tool == 'pyupgrade' }}
id: pyupgrade
run: pyupgrade --py312-plus ${{ needs.check_changes.outputs.files }}

# Run tox
- name: Run tox
if: ${{ matrix.tool == 'tox' }}
id: tox
run: |
echo "Running tox integration for changed files..."
changed_files="${{ needs.check_changes.outputs.files }}"

# Create a temporary tox configuration that extends the original one
echo "[tox]" > tox_pr.ini
echo "envlist = py312" >> tox_pr.ini
echo "skip_missing_interpreters = true" >> tox_pr.ini

echo "[testenv]" >> tox_pr.ini
echo "setenv =" >> tox_pr.ini
echo " COVERAGE_FILE = .coverage.{envname}" >> tox_pr.ini
Expand All @@ -182,24 +184,24 @@ jobs:
echo " coverage" >> tox_pr.ini
echo " python" >> tox_pr.ini
echo "commands =" >> tox_pr.ini

# Check if we have any implementation files that changed
pattern_files=0
test_files=0

for file in $changed_files; do
if [[ $file == patterns/* ]]; then
pattern_files=1
elif [[ $file == tests/* ]]; then
test_files=1
fi
done

# Only run targeted tests, no baseline
echo " # Run specific tests for changed files" >> tox_pr.ini

has_tests=false

# Add coverage-focused test commands
for file in $changed_files; do
if [[ $file == *.py ]]; then
Expand Down Expand Up @@ -246,18 +248,18 @@ jobs:
fi
fi
done

# If we didn't find any specific tests to run, mention it
if [ "$has_tests" = false ]; then
echo " python -c \"print('No specific tests found for changed files. Consider adding tests.')\"" >> tox_pr.ini
# Add a minimal test to avoid failure, but ensure it generates coverage data
echo " coverage run -m pytest -xvs --cov=patterns --cov-append -k \"not integration\" --no-header" >> tox_pr.ini
fi

# Add coverage report command
echo " coverage combine" >> tox_pr.ini
echo " coverage report -m" >> tox_pr.ini

# Run tox with the custom configuration
echo "Running tox with custom PR configuration..."
echo "======================== TOX CONFIG ========================"
Expand All @@ -272,7 +274,7 @@ jobs:
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v3

- name: Summarize results
run: |
echo "## Pull Request Lint Results" >> $GITHUB_STEP_SUMMARY
Expand Down
3 changes: 3 additions & 0 deletions patterns/behavioral/memento.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,18 @@ def Transactional(method):

:param method: The function to be decorated.
"""

def transaction(obj, *args, **kwargs):
state = memento(obj)
try:
return method(obj, *args, **kwargs)
except Exception as e:
state()
raise e

return transaction


class NumObj:
def __init__(self, value):
self.value = value
Expand Down
6 changes: 5 additions & 1 deletion patterns/behavioral/servant.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,26 @@
References:
- https://en.wikipedia.org/wiki/Servant_(design_pattern)
"""

import math


class Position:
"""Representation of a 2D position with x and y coordinates."""

def __init__(self, x, y):
self.x = x
self.y = y


class Circle:
"""Representation of a circle defined by a radius and a position."""

def __init__(self, radius, position: Position):
self.radius = radius
self.position = position


class Rectangle:
"""Representation of a rectangle defined by width, height, and a position."""

Expand Down Expand Up @@ -65,7 +69,7 @@ def calculate_area(shape):
ValueError: If the shape type is unsupported.
"""
if isinstance(shape, Circle):
return math.pi * shape.radius ** 2
return math.pi * shape.radius**2
elif isinstance(shape, Rectangle):
return shape.width * shape.height
else:
Expand Down
5 changes: 5 additions & 0 deletions patterns/other/blackboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

class AbstractExpert(ABC):
"""Abstract class for experts in the blackboard system."""

@abstractmethod
def __init__(self, blackboard) -> None:
self.blackboard = blackboard
Expand All @@ -31,6 +32,7 @@ def contribute(self) -> None:

class Blackboard:
"""The blackboard system that holds the common state."""

def __init__(self) -> None:
self.experts: list = []
self.common_state = {
Expand All @@ -46,6 +48,7 @@ def add_expert(self, expert: AbstractExpert) -> None:

class Controller:
"""The controller that manages the blackboard system."""

def __init__(self, blackboard: Blackboard) -> None:
self.blackboard = blackboard

Expand All @@ -63,6 +66,7 @@ def run_loop(self):

class Student(AbstractExpert):
"""Concrete class for a student expert."""

def __init__(self, blackboard) -> None:
super().__init__(blackboard)

Expand All @@ -79,6 +83,7 @@ def contribute(self) -> None:

class Scientist(AbstractExpert):
"""Concrete class for a scientist expert."""

def __init__(self, blackboard) -> None:
super().__init__(blackboard)

Expand Down
Loading
Loading