ttc-cases / pydevx-lindjacob

1 stars 0 forks source link

Deconstruct the system #7

Closed github-actions[bot] closed 2 months ago

github-actions[bot] commented 2 months ago

[!NOTE] ☝️ Learning goals in this issue

  • Organize your code into modules and classes

Start by setting up a development branch for this issue

Create a separate folder src/modules to hold our classes. be sure to add an empty file __init__.py.

Then add a file in that folder src/modules/hand.py and cut the Hand class out of the src/rps_game-py file and paste it into it:

class Hand:
    def __init__(self, name):
        self.name = name

    def play(self, opponent):
        outcomes = {
            "paper": ["rock"],
            "scissors": ["paper"],
            "rock": ["scissors"]
        }
        if opponent in outcomes[self.name]:
            return f"{self.name} wins against {opponent}!"
        elif self.name == opponent:
            return f"It's a tie Both {self.name} and {opponent} played."
        else:
            return f"{opponent} wins against {self.name}."

In the rps_game.py file, which no longer knows of the Hand class add an import statement:

from modules.hand import Hand

In the tests/test_hand.py unit test the import of Hand must point to it's new loaction:

from src.modules.hand import Hand

Run the tests again - and try to run the script again.

Is it working?

Wrap up the development branch and deliver it to main

lindjacob commented 2 months ago

I am not sure why, but I had issues importing the Hand class in the test_hand.py so added the following workaround:

import sys
import os

# Add the src directory to the Python path
sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'src'))

from modules.hand import Hand