lundi 2 septembre 2019

OOP vs functional Programing implementation in Python on small scale real-time-ish application

I am new to OOP side of python and I am wondering if it is worth to refactor my functional programming code to an OOP code. my code is as follows:

import time
import numpy as np
import pandas as pd


def choices_chooser(possibilities):
    final_choice = None

    while final_choice is None:
        temp_input = input(f'please enter one of the following: {possibilities}\n')

        if temp_input == 'True': temp_input = True
        elif temp_input == 'False':temp_input = False

        if temp_input in possibilities:
            final_choice= temp_input
        else:
            print('typo! plz enter again')

    return final_choice

def status_updater(dic, key, possibilities):
    if key in dic and type(possibilities) in {list,tuple}:
        print(f"the status of {key} before modification: {dic[key]}")
        print('now we are changing it')
        dic[key]= choices_chooser(possibilities)

        print(f"the status of {key} after modification: {dic[key]}")

def funcA(df):df['Be'] *= -1.5   
def funcB(df):df['Ch'] *= -2  
def funcC(df):df['De'] *= -3    
def funcD(df):df['Ep'] *= -4   
def funcE(df):df['Fi'] *= -5
def funcF(df):df['Al'] *= -6

func_dict = {'Al': funcA,
             'Be': funcB,
             'Ch': funcC,
             'De': funcD,
             'Ep': funcE,
             'Fi': funcF} 

options_lst = [True, False] 

status = ['Null']+['Al','Be','Ch','De','Ep','Fi']
status_dict = dict.fromkeys(status, False)
status_count = dict.fromkeys(status, 0)


status_index = 0
next_status_index = 1
previous_status_index = None

num_of_iteration = 0
num_of_complete_run = 0

df = pd.DataFrame(np.ones((4, len(status))), columns = status)

while num_of_complete_run < 1:
    time.sleep(0.2)
    num_of_iteration += 1

    current_state = status[status_index]
    next_state = status[next_status_index]

    if previous_status_index is None:
        print(f'current state: {current_state};next_state: {next_state}; iteration: {num_of_iteration}')
    else:
        previous_state = status[previous_status_index]
        print(f'previous state: {previous_state}; current state: {current_state}; next state: {next_state}; iteration: {num_of_iteration}')

    status_updater(status_dict,
                  next_state,
                  options_lst)

    if status_dict[next_state]:

        previous_status_index = status_index  
        status_index = next_status_index

        previous_state = status[previous_status_index]
        current_state = status[status_index]
        print(f'we have attained a new state\n' + '-----'*10)

        if current_state == 'Be':
            print('after state Beta we may directly skip to De, depending on RNG')
            next_state = choices_chooser(['Ch','De'])
            next_status_index = status.index(next_state)

        elif current_state == 'De':
            print('after state Delta we may directly skip to Fi, depending on RNG')
            next_state = choices_chooser(['Ep','Fi'])
            next_status_index = status.index(next_state)

        elif current_state == 'Fi':
            print('we are at state Fi, which means a full run has been completed')
            num_of_complete_run += 1

            next_status_index = 1
            next_state = status[next_status_index]

        else:
            next_status_index += 1  
            next_state = status[next_status_index]

        print(f'previous state: {previous_state}; current state: {current_state}; next state: {next_state}')
        #'do something unique at state {current_state}  
        func_dict[current_state](df)
        status_count[current_state] += 1


This is my attempt to create a real-time-ish application. It has status variables. Different functions are run at different states. Whether certain states will be visited next depends on the user's input. The code terminates when the status 'Fi' has been reached once.

I am asking for help here because I have almost 0 knowledge when it comes to good program designs, technical debts, or basically any in-depth knowledge about what/when to do OOP and how to do it correctly.


My questions are the following:

1) Is it worth to refactor my functional programming code to an OOP code?

2) If so, what should I pay extra care and attention to in the process?

3) When should we convert a functional code to OOP code and when shoudln't

4) Do you mind posting an example of what a good OOP version of this code is like in the answer?(I mean if one had to learn how to OOP, might as well learn from a great example)

Thank you guys in advance.

Aucun commentaire:

Enregistrer un commentaire