Python 小型实时应用程序中的 OOP 与非 OOP 实现
OOP vs non-OOP implementation in Python on small scale real-time-ish application
我是 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' 一次时,代码终止。
我在这里寻求帮助,因为在良好的程序设计、技术债务或基本上任何关于 what/when 进行 OOP 以及如何正确进行的深入知识方面,我的知识几乎为 0 .
我的问题如下:
1) 将我的非 OOP 编程代码重构为 OOP 代码是否值得?
2) 如果有,过程中需要特别注意什么?如果没有,我可以做些什么来改进现有代码?
3) 我们什么时候应该将非 OOP 代码转换为 OOP 代码,什么时候不应该
4) 你介意在答案中发布一个示例,说明此代码的良好 OOP 版本是什么样的吗?(我的意思是,如果必须学习如何使用 OOP,不妨从一个很好的示例中学习)
提前谢谢大家。
——
编辑1:
将“函数式编程”更改为非 OOP 以更好地反映情况
如果你需要你的程序只做一件事并且把它做好,就用函数式编程吧。如果您的程序需要做不止一组不同的事情要做,请使用 OOP。例如,如果您构建一个应用程序来计算一些科学事物(例如,速度、重力等),您将其作为 OOP,1 个对象仅用于 GUI,1 个对象用于设置和调用计算对象,其他执行计算。
其他示例,如果您构建网络应用程序,使用一个对象向用户发送视图,使用 1 个/多个对象进行设置并调用对象从数据库中获取内容(每个对象有不同的用法,即一个对于家庭,一个用于用户控制,一个用于计算项目等),最后一个 one/more 操作数据库(CRUD)的对象,它可以是访问不同数据库的不同对象。(这种称为MVC模型)。
如果您的代码慢慢变成意大利面条式代码并且不可读,请考虑使用 OOP。
我不是真正的专家,但这就是我 4 年来做事的方式。
我是 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' 一次时,代码终止。
我在这里寻求帮助,因为在良好的程序设计、技术债务或基本上任何关于 what/when 进行 OOP 以及如何正确进行的深入知识方面,我的知识几乎为 0 .
我的问题如下:
1) 将我的非 OOP 编程代码重构为 OOP 代码是否值得?
2) 如果有,过程中需要特别注意什么?如果没有,我可以做些什么来改进现有代码?
3) 我们什么时候应该将非 OOP 代码转换为 OOP 代码,什么时候不应该
4) 你介意在答案中发布一个示例,说明此代码的良好 OOP 版本是什么样的吗?(我的意思是,如果必须学习如何使用 OOP,不妨从一个很好的示例中学习)
提前谢谢大家。 —— 编辑1: 将“函数式编程”更改为非 OOP 以更好地反映情况
如果你需要你的程序只做一件事并且把它做好,就用函数式编程吧。如果您的程序需要做不止一组不同的事情要做,请使用 OOP。例如,如果您构建一个应用程序来计算一些科学事物(例如,速度、重力等),您将其作为 OOP,1 个对象仅用于 GUI,1 个对象用于设置和调用计算对象,其他执行计算。
其他示例,如果您构建网络应用程序,使用一个对象向用户发送视图,使用 1 个/多个对象进行设置并调用对象从数据库中获取内容(每个对象有不同的用法,即一个对于家庭,一个用于用户控制,一个用于计算项目等),最后一个 one/more 操作数据库(CRUD)的对象,它可以是访问不同数据库的不同对象。(这种称为MVC模型)。
如果您的代码慢慢变成意大利面条式代码并且不可读,请考虑使用 OOP。
我不是真正的专家,但这就是我 4 年来做事的方式。