【发布时间】:2019-09-03 03:09:58
【问题描述】:
我是 python 的 OOP 方面的新手,我想知道是否值得将我的非 OOP 编程代码重构为 OOP 代码。我的代码如下:
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
这是我创建实时应用程序的尝试。它有状态变量。不同的功能在不同的状态下运行。接下来是否会访问某些状态取决于用户的输入。当状态“Fi”达到一次时,代码终止。
我在这里寻求帮助是因为我对良好的程序设计、技术债务或基本上任何关于什么/何时做 OOP 以及如何正确地做的深入了解的知识几乎为零。
我的问题如下:
1) 将我的非 OOP 编程代码重构为 OOP 代码是否值得?
2) 如果是这样,在这个过程中我应该特别注意什么?如果没有,我该如何改进现有代码?
3) 我们什么时候应该将非 OOP 代码转换为 OOP 代码,什么时候不应该
4)您介意在答案中发布一个示例,说明此代码的良好 OOP 版本是什么样的吗?(我的意思是,如果必须学习如何 OOP,不妨从一个很好的示例中学习)
提前谢谢你们。 —— 编辑1: 将“功能规划”改为非 OOP 以更好地反映情况
【问题讨论】:
-
你的代码绝对没有使用函数式编程,这是相当标准的命令式风格
-
@junapa.arrivillaga 将函数式编程更改为非 OOP。谢谢你告诉我
标签: python oop design-patterns real-time