Project based on pure python with OOP

Related tags

MiscellaneousOOP
Overview

Object oriented programming review

Object oriented programming (OOP) is among the most used programming paradigms (if not the most common) in the industry. It's not usual however to begin learning programming with this paradigm. Entry courses generally introduce programming via the procedural paradigm. This guide serves to bridge the gap for people coming from a procedural background wanting a better comprehension of codebases focused on OOP.

In procedural programming the code is divided into data structures and procedures which transform them. Understanding how to write a solution in the procedural paradigm involves designing the data structures that most appropiately encode the problem and figuring out which transformations are needed. This defines a very clear line of separation between data and logic.

In contrast, in OOP we encapsulate both logic and code into things called objects. OOP-based solutions focus on understanding what objects can model the problem and what interactions are there between them. This way of designing solutions attempts to modularize responsibilities -- since each object is owner of its own data, instead of modifying another object's properties, we ask the object to execute an action. We delegate the implementation details to the object, in other words, knowing how to store and transform its data is an object's responsibility.

It's important to know there is no single paradigm that works best in all cases, and codebases that are purely within one paradigm only occur in classrooms.

In the following exercises we will attempt to transform a piece of code from a procedural design to an object-oriented one. At a first glance the end result might seem longer and more complicated than the starting point. That is true. Since procedural programming usually shares data structures between functions, this high code coupling lends itself well to small codebases. Trying to separate the different actors in such a short problem will undoubtedly lead to more apparent complexity. However! You must strive to see the bigger picture. These exercises are for testing and understanding OOP concepts. Try to imagine how the procedural code would look like if it had thousands of lines of logic, and do the same with the OOP code. It's not merely out of stubbornness that OOP became the de facto standard in the industry.

Introduction: the game

The following problem statement and code has been taken from Arianne Dee's OOP in Python Training. Refactoring steps have been ommitted, modified or added in order to more clearly explain the concepts behind them.

The problem statement is as follows:

Simulate a simple board game.

There are 2 players, each player takes turn rolling a die and moving that number of spaces.

The first person to space 100 wins.

Right off the bat we can see that the only thing we need to keep track of is each player's points (or space counts). A simple solution might look like the following:

= 100: print("Player 1 wins!") break player_2_roll = random.randint(1, 6) player_2_score += player_2_roll print(f"Player 2 score: {player_2_score} (rolled a {player_2_roll})") if player_2_score >= 100: print("Player 2 wins!") break ">
import random
player_1_score = 0
player_2_score = 0

while True:
    player_1_roll = random.randint(1, 6)
    player_1_score += player_1_roll
    print(f"Player 1 score: {player_1_score} (rolled a {player_1_roll})")
    if player_1_score >= 100:
        print("Player 1 wins!")
        break

    player_2_roll = random.randint(1, 6)
    player_2_score += player_2_roll
    print(f"Player 2 score: {player_2_score} (rolled a {player_2_roll})")
    if player_2_score >= 100:
        print("Player 2 wins!")
        break

A procedural solution

Our previous code works! But for it to truly be procedural we should avoid duplicating code and factor a player's movement into procedures that transform the underlying data structures -- in this case, the player scores:

= 100: print(f"Player {player_number+1} wins!") finished = True break ">
import random

def player_move(i, previous_score):
    player_roll = random.randint(1, 6)
    player_score = previous_score + player_roll
    print(f"Player {i+1} score: {player_score} (rolled a {player_roll})")
    return player_score

player_scores = [0, 0]
finished = False

while not finished:
    for player_number, player_score in enumerate(player_scores):
        player_scores[player_number] = player_move(player_number, player_score)
        if player_scores[player_number] >= 100:
            print(f"Player {player_number+1} wins!")
            finished = True
            break

now we can clearly see the main components of procedural programming:

  • figure out what data needs to be stored (the player scores)
  • decide what representation is best for that data (an array instead of many variables)
  • transform the data structures with procedures until the end result is achieved.

An object oriented approach

As we explained before, the main focus of object oriented programming is understanding which objects are there in the underlying problem, what data they have, how they operate on that data and how they talk to each other. Objects are usually modeled after real-life things and phenomena. In this case, the most direct example would be the players! Let's see the main points of this approach:

  • formulate the problem in terms of objects (two Players are rolling dice until one of them wins)
  • describe each object by what it knows and what it does:
    • each player knows their current score
    • in a given turn, they can increase their score by rolling a die
  • describe interactions between objects (none for now!)

First exercise: a Player class

Your first task is to write the player logic using a Player class. You should fill out your code in exercise_1/exercise_1.py. The game logic has already been rewritten and some of the class's functions (or methods) have been filled in. You'll need to complete the take_turn and has_won methods so that the following game logic works:

def play_game(num_players=2):
    players = [Player(i + 1) for i in range(num_players)]
    while True:
        for player in players:
            player.take_turn()
            if player.has_won():
                print(f"{player} wins")
                return

This method starts by creating an amount num_players of Players. For every turn, each player rolls their dice and updates their score count. If the player that has just rolled has surpassed 100, then they have won and the game ends.

Notice that much of the game logic has been encapsulated. We, as programmers of the play_game function, know that in each turn we must allow a player to do some actions and after check if they have won. The specifics of this, as well as where the data is saved and how, is up to the Player class. This is exactly the kind of delegation we want to do in object oriented programming.

Interlude: how objects are defined in Python

Python's object system is based on classes. To create an object we first must define its class via class X: and then instance it with x = X(). When first instantiating a class, the constructor method X.__init__ is called. This is often used to set up the object's properties and other procedures relating to its state (such as initiating a database connection or reading a configuration file in the disk).

You'll notice in exercise_1.py that most methods in a class have a first argument which is called self. This is the reference of the object. When you write a function call as x.fun(42), Python will call the fun function with first argument x and second argument 42. From the perspective of the function this is an explicit mapping, instead of the implicit this assignment that other languages use.

In the template for the first exercise you will also see a static method which is a function defined in a class using the @staticmethod decorator. A static method is a normal function with no access to the class instance (notice that it doesn't define self!). It could of course be defined outside of the class, but usually static methods are conceptually bound to the class we are defining. Since static methods aren't bound to an object, you can use it by calling X.static_method(). While defining other non-static functions in X, you can use self.static_method to refer to the function.

As you will see in these examples, Python doesn't have a syntax for defining methods as public or private. The naming convention in PEP8 describes properties or methods starting with a single underscore (_) as weakly private, and identifiers starting with a double underscore (__) as a stronger way of denoting a private property or method. With the latter one, Python also does some name mangling to avoid it being called (although it's still reachable if you really really need it). Identifiers starting and ending with a double underscore are labeled "magic" and shouldn't be defined since they help differentiate special Python functions (such as __init__ or __str__ as you'll see in the exercises) from user-defined ones.

Second exercise: factoring game logic

The idea for the second exercise is to create a class that encapsulates the game logic and its associated data. The code should make the following snippet work (found in exercise_2/main.py):

game1 = Game(num_players=2, target_score=20)
game2 = Game(num_players=3, target_score=50)

game2.play_game()
game1.play_game()

and it should produce an output like the following:

Game 2 start
Player 1: 3 (rolled a 3)
Player 2: 6 (rolled a 6)
Player 3: 6 (rolled a 6)
...
Game 2: Player 2 wins
Game 2 is over

Game 1 start
Player 1: 3 (rolled a 3)
Player 2: 2 (rolled a 2)
...
Game 1: Player 2 wins
Game 1 is over

We're now allowing for the games to have different target scores and we are creating many games at the same time. Each game should instantiate its own Players. You'll be reusing the code for the player from exercise 1, and you should modify it so that 100 is not hard-coded as the target score. A key question that you'll need to answer is who should know the target score. It's clear that to calculate Player.has_won() the player needs it, but should that information be stored in the player or the game?

There's something interesting going on in the expected output as well. We created two games and the constructor only received the number of players and the max score, but somehow game2 knows that it is game number 2 and game1 knows that it is game number 1? Somehow a Game instance is able to communicate with other Game instances.

We've talked about instance properties, which are variables that all the methods can use -- like the score in the Player class of exercise 1. If we have two different players, each instance has its own score property and there is no confusion since each method receives a self which is a reference to the class instance that it is acting on. However, for game2 to know that there was another game already created before it, we'll need a property that is shared across instances: a class property! These properties are not accessed via the self but through the class name. For example:

class X:
    class_property = 0
    def __init__(self):
        X.class_property = 42
        self.instance_property = X.class_property

Class properties (also called attributes) are defined outside of the methods and under the scope of the class. Any piece of code (both a method of the class and code outside of the class) can access it at any time using X.class_property.

For game instances to identify themselves without passing an index in the constructor we can use a counter as a class property. Each time a game is created the counter is incremented and the current number should be stored as an instance attribute (so that further game creations do not override it).

Third exercise: inheritance

We now want to introduce a new type of Player. This person is lucky and never rolls below a 3! How can we adapt our solution to include this new player?

Our first thought might be to create a new LuckyPlayer class that is pretty much the same as Player, but the LuckyPlayer._roll_die will do a randint starting on 3. It's a small change, we can just copy the original class, rename it and update that line. This however incurs in a lot of code duplication just to change one line! And it also means that if we want to add a new method for a player or change some behavior we'll need to do it twice, once for each class.

Another way might be through a parameter on the Player constructor to set a flag indicating whether the player is lucky or not. In the _roll_die method, if the flag is set the minimum roll will be 3. This avoids having duplicated code. This works well, but if the difference in behavior was bigger or more complex we might end up with a lot of branches. Having many different behaviors based on flags is not only unsightly and hard to mantain, it can also introduce bugs due to incomplete case handling.

In object oriented programming we can use the concept of inheritance to avoid having duplicated code and avoid having to program many different behaviors into the same class. The concept is simple: we can inherit the behavior we want from a class and modify or extend it in any way we want. Additionally, there are many interesting consequences that stem from this design pattern. Since LuckyPlayer is a subclass of Player, we can just expect some variable or reference to be a Player and if it ends up being a LuckyPlayer the code will still work! These are the benefits of subtyping and encapsulation: since the different behavior is factored in a subtype, we can operate with the usual interface (methods and attributes) and the outside code doesn't need to concern itself with which particular type of player it is dealing. This is a very powerful abstraction. Here is an example in Python:

class Greeting:
    def say(self):
        print(self.get_message())

    def get_message(self):
        return "Hi!"

class BusinessGreeting(Greeting):
    def get_message(self):
        return "How do you do?"

def say_hi(Greet):
    greet = Greet()
    greet.say()

say_hi(Greeting)
say_hi(BusinessGreeting)

Here we define a variant of Greeting that is oriented for formal use. In BusinessGreeting we don't have to redefine say since we inherit that from Greeting. Finally, the say_hi function can work with a regular Greeting or its subtype, BusinessGreeting.

Recognizing when to subtype to factorize different behaviors is key to mastering object oriented programming. Remember: in programming, there is no inheritance tax, so use it all you like!

In this exercise we will force the first player to be a lucky one. Use inheritance to modify the relevant method so that a Game with the following modified constructor works:

class Game:
    counter = 0
    def __init__(self, num_players, target_score=100):
        self.target_score = target_score
        self.players = [Player(i + 1) for i in range(num_players)]
        self.players[0] = LuckyPlayer(1)
        Game.counter += 1
        self.game_num = Game.counter
        print(self.players)

it might also be useful to change the __str__ in LuckyPlayer so we know which one is lucky!

Fourth exercise: polymorphism

Usually we get tired of playing the same game after a while. Let's spice it up with some variants!

An easy way of doing so would be by changing the type of dice we use:

from random import randint, random

def russian_roulette_die():
    # Standard 6-sided die, but with a 1/1000th chance of rolling -1000.
    if random() < 1/1000:
        return -1000
    return randint(1,6)

def d20():
    # Standard d20 die
    return randint(1,20)

def rigged_die(num_run):
    # Every eighth run the throw is lucky.
    if num_run % 8 == 0:
        return randint(3,6)
    return randint(1,6)

How can we implement these changes in the code? It would be madness to encode every type of dice in the Player class. Why would the players concern themselves with how a die throw is calculated? It should make sense at this point to factor this behavior into its own class.

We will define a base class that doesn't have any particular behavior but defines what methods we can call for any subclass. In other words, we are defining the interface:

class Die:
    def roll():
        raise NotImplementedError

    def __str__():
        raise NotImplementedError

When defining such classes that are not meant to be used directly, we usually raise errors to avoid calling the methods. We are meant to use subclasses that implement that interface. However, game logic should be written in terms of the interface and not depending on which implementation is used. This is related to the concept of polymorphism, where we define a single interface for many different data types.

In this exercise you'll have to define the classes RussianRouletteDie, D20Die and RiggedDie, inheriting from Die. The constructor of Game should also receive what type of die it is using by passing in the class, for example:

Game(2, RussianRouletteDie, target_score=20)

the die should be instantiated at the beginning of the game and in each turn a player has to be passed the die for them to be able to roll it. Check out the files in the exercise_4 folder to see what you have to implement!

Bonus points: add a method for lucky throw so that we can still have a LuckyPlayer. A LuckyPlayer will call die.lucky_roll() instead of die.roll().

Owner
Facundo Abrahan Cerimeli
I'm a computer science student and a physics student from Argentina
Facundo Abrahan Cerimeli
This speeds up PyCharm's package index processes and avoids CPU & memory overloading

This speeds up PyCharm's package index processes and avoids CPU & memory overloading

1 Feb 09, 2022
Understanding the field usage of any object in Salesforce

Understanding the field usage of any object in Salesforce One of the biggest problems that I have addressed while working with Salesforce is to unders

Sebastian Undurraga 1 Dec 14, 2021
Drop-down terminal for GNOME

Guake 3 README Introduction Guake is a python based dropdown terminal made for the GNOME desktop environment. Guake's style of window is based on an F

Guake 4.1k Dec 25, 2022
A general illumination correction method for optical microscopy.

CIDRE About CIDRE is a retrospective illumination correction method for optical microscopy. It is designed to correct collections of images by buildin

Kevin Smith 31 Sep 07, 2022
An example repository for how to generate results using PyBaMM

PyBaMM results This repository provides a template for generating results (for example, for a paper) using PyBaMM Installation Install PyBaMM using a

PyBaMM Team 7 Oct 09, 2022
A Python Perforce package that doesn't bring in any other packages to work.

P4CMD 🌴 A Python Perforce package that doesn't bring in any other packages to work. Relies on p4cli installed on the system. p4cmd The p4cmd module h

Niels Vaes 13 Dec 19, 2022
Kolibri: the offline app for universal education

Kolibri This repository is for software developers wishing to contribute to Kolibri. If you are looking for help installing, configuring and using Kol

Learning Equality 564 Jan 02, 2023
Provides guideline on how to configure pre-commit hooks in your own python project

Pre-commit Configuration Guide The main aim of this repository is to act as a guide on how to configure the pre-commit hooks in your existing python p

Faraz Ahmed Khan 2 Mar 31, 2022
This is a Python 3.10 port of mock, a library for manipulating human-readable message strings.

This is a Python 3.10 port of mock, a library for manipulating human-readable message strings.

Alexander Bartolomey 1 Dec 31, 2021
An alternative app for core Armoury Crate functions.

NoROG DISCLAIMER: Use at your own risk. This is alpha-quality software. It has not been extensively tested, though I personally run it daily on my lap

12 Nov 29, 2022
Speed up your typing by some exercises in the multi-platform(Windows/Ubuntu).

Introduction This project purpose is speed up your typing by some exercises in the multi-platform(Windows/Ubuntu). Build Environment Software Environm

lyfer233 1 Mar 24, 2022
a really simple bot that send you memes from reddit to whatsapp

a really simple bot that send you memes from reddit to whatsapp want to use use it? install the dependencies with pip3 install -r requirements.txt the

pai 10 Nov 28, 2021
Supply Chain will be a SAAS platfom to provide e-logistic facilites with most optimal

Shipp It Welcome To Supply Chain App [ Shipp It ] In "Shipp It" we are creating a full solution[web+app] for a entire supply chain from receiving orde

SAIKAT_CLAW 25 Dec 26, 2022
Backtest framework based on DAGs

MultitaskQueue It's a simple framework based on three composed concepts: Task: A task is the smaller unit of execution or simple a node in the DAG, ev

4 Dec 09, 2021
Simple Calculator Mobile Apps

Simple Calculator Mobile Apps Screenshoot If you want to try it please click the link below to download, this application is 100% safe no virus. link

0 Sep 24, 2022
Team Hash Brown Science4Cast Submission

Team Hash Brown Science4Cast Submission This code reproduces Team Hash Brown's (@princengoc, @Xieyangxinyu) best submission (ee5a) for the competition

3 Feb 02, 2022
[Cython] Vs [Python] Which one is Faster ?

[Cython] Vs [Python] ? Attractive Contrast :) Mission : Which one is Faster ? Comparing of Execution runtime for [Selection_sort] with Time Complexity

baqer marani 1 Dec 05, 2021
Some usefull scripts for the Nastran's 145 solution (Flutter Analysis) using the pyNastran package.

nastran-aero-flutter This project is intended to analyse the Supersonic Panel Flutter using the NASTRAN software. The project uses the pyNastran and t

zuckberj 11 Nov 16, 2022
Repo to demo translating colab/jupyter notebook to streamlit webapp

Repo to demo translating colab/jupyter notebook to streamlit webapp

Marisa Smith 2 Feb 02, 2022
Blender-miHoYo-Shaders - Shaders for Blender attempting to replicate the shading of games developed by miHoYo

Blender-miHoYo-Shaders - Shaders for Blender attempting to replicate the shading of games developed by miHoYo

Matsuri 449 Jan 07, 2023