I am new to OOP side of python and I am wondering if it is worth to refactor my non-OOP 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 non-OOP programming code to an OOP code?
2) If so, what should I pay extra care and attention to in the process? if not, what can I do to improve the existing code?
3) When should we convert a non-OOP code to OOP code and when shouldn'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. —— EDIT1: changed “functional peogramming” to non-OOP to better reflect the situation
If you need your program to do only one thing and do it well, just use functional programming. If your program need to do more than one set of different things to do, use OOP. For example, if you build an app to calculate some scientific things (for example, velocity, gravity, etc.) you make it as OOP, 1 object only for GUI, 1 object to set things up and call calculation object, and other to perform calculation.
Other example if you build a web app, use one object to send view to user, 1/more object to set things up and calling the object to take things from database (with different usage for each object, i.e. one for home, one for user control, one for calculating item etc.), and last one/more object to operate the database (CRUD), it could be different object accessing different databases.(this one is called MVC model).
If your code slowly becoming spaghetti code and isn't readable, consider using OOP.
I'm not really an expert but that's how I do things for 4 years now.